Skip to content

Instantly share code, notes, and snippets.

@woshilapin
Created March 5, 2015 08:00
Show Gist options
  • Save woshilapin/efe489856c61f044870a to your computer and use it in GitHub Desktop.
Save woshilapin/efe489856c61f044870a to your computer and use it in GitHub Desktop.
Test of readObjectsFromFormUpdateOrCreate
/**
* Unit test for
* {@link XWikiDocument#readObjectsFromFormUpdateOrCreate(EditForm, XWikiContext)}
* .
*/
@Test
public void readObjectsFromFormUpdateOrCreate() throws Exception {
HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
Map<String, String[]> parameters = new HashMap<>();
// Testing update of values in existing object with existing properties
String[] string1 = { "string1" };
parameters.put("Space.Page_0_string", string1);
String[] int1 = { "7" };
parameters.put("Space.Page_1_int", int1);
// Testing creation of an object and set up of values in properties when
// object doesn't exist
String[] string2 = { "string2" };
String[] int2 = { "13" };
parameters.put("Space.Page_2_string", string2);
parameters.put("Space.Page_2_int", int2);
// Testing that objects with a number which is not following is not
// created
parameters.put("Space.Page_42_string", string1);
parameters.put("Space.Page_42_int", int1);
// Testing that invalid parameter are ignored
parameters.put("invalid", new String[] { "whatever" });
// Testing that invalid xclass page are ignored
parameters.put("InvalidSpace.InvalidPage_0_string",
new String[] { "whatever" });
// Testing that an invalid number is ignored
parameters.put("Space.Page_notANumber_string",
new String[] { "whatever" });
// XWikiDocument classDocument = new
// XWikiDocument(this.document.getDocumentReference());
MockitoOldcoreRule oldcore = new MockitoOldcoreRule();
DocumentReference documentReference = new DocumentReference("wiki",
"Space", "Page");
DocumentReferenceResolver<String> resolver = oldcore.getMocker()
.registerMockComponent(DocumentReferenceResolver.TYPE_STRING,
"current");
when(resolver.resolve("Space.Page")).thenReturn(documentReference);
when(request.getParameterMap()).thenReturn(parameters);
/**
* A fake class to populate the object being tested
*/
BaseClass baseClass;
baseClass = document.getXClass();
baseClass.addTextField("string", "String", 30);
baseClass.addTextAreaField("area", "Area", 10, 10);
baseClass.addTextAreaField("puretextarea", "Pure text area", 10, 10);
// set the text areas an non interpreted content
((TextAreaClass) baseClass.getField("puretextarea"))
.setContentType("puretext");
baseClass.addPasswordField("passwd", "Password", 30);
baseClass.addBooleanField("boolean", "Boolean", "yesno");
baseClass.addNumberField("int", "Int", 10, "integer");
baseClass.addStaticListField("stringlist", "StringList",
"value1, value2");
EditForm eform = new EditForm();
eform.setRequest(request);
this.document.readObjectsFromFormUpdateOrCreate(eform, context);
Assert.assertEquals(3,
this.document.getXObjectSize(baseClass.getDocumentReference()));
Assert.assertEquals("string1",
this.document.getXObject(baseClass.getDocumentReference(), 0)
.getStringValue("string"));
Assert.assertEquals(7,
this.document.getXObject(baseClass.getDocumentReference(), 1)
.getIntValue("int"));
Assert.assertNotNull(this.document.getXObject(
baseClass.getDocumentReference(), 2));
Assert.assertEquals("string2",
this.document.getXObject(baseClass.getDocumentReference(), 2)
.getStringValue("string"));
Assert.assertEquals(13,
this.document.getXObject(baseClass.getDocumentReference(), 2)
.getIntValue("int"));
Assert.assertNull(this.document.getXObject(
baseClass.getDocumentReference(), 42));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment