use of com.xpn.xwiki.web.EditForm in project xwiki-platform by xwiki.
the class XWikiDocumentMockitoTest method readObjectsFromFormUpdateOrCreate.
/**
* Unit test for {@link XWikiDocument#readObjectsFromFormUpdateOrCreate(EditForm, XWikiContext)} .
*/
@Test
public void readObjectsFromFormUpdateOrCreate() throws Exception {
this.document = new XWikiDocument(new DocumentReference(DOCWIKI, DOCSPACE, DOCNAME));
this.oldcore.getSpyXWiki().saveDocument(this.document, "", true, this.oldcore.getXWikiContext());
HttpServletRequest request = mock(HttpServletRequest.class);
MockitoComponentManager mocker = this.oldcore.getMocker();
XWikiContext context = this.oldcore.getXWikiContext();
DocumentReferenceResolver<String> documentReferenceResolverString = mocker.registerMockComponent(DocumentReferenceResolver.TYPE_STRING, "current");
// Entity Reference resolver is used in <BaseObject>.getXClass()
DocumentReferenceResolver<EntityReference> documentReferenceResolverEntity = mocker.registerMockComponent(DocumentReferenceResolver.TYPE_REFERENCE, "current");
Map<String, String[]> parameters = generateFakeRequestMap();
BaseClass baseClass = generateFakeClass();
generateFakeObjects();
EditForm eform = new EditForm();
when(request.getParameterMap()).thenReturn(parameters);
when(documentReferenceResolverString.resolve("space.page")).thenReturn(this.document.getDocumentReference());
when(documentReferenceResolverString.resolve("InvalidSpace.InvalidPage")).thenReturn(new DocumentReference("wiki", "InvalidSpace", "InvalidPage"));
// This entity resolver with this 'resolve' method is used in
// <BaseCollection>.getXClassReference()
when(documentReferenceResolverEntity.resolve(any(EntityReference.class), any(DocumentReference.class))).thenReturn(this.document.getDocumentReference());
doReturn(this.document).when(this.oldcore.getSpyXWiki()).getDocument(this.document.getDocumentReference(), context);
eform.setRequest(request);
this.document.readObjectsFromFormUpdateOrCreate(eform, context);
assertEquals(43, this.document.getXObjectSize(baseClass.getDocumentReference()));
assertEquals("bloublou", this.document.getXObject(baseClass.getDocumentReference(), 0).getStringValue("string"));
assertEquals(42, this.document.getXObject(baseClass.getDocumentReference(), 0).getIntValue("int"));
assertEquals("string2", this.document.getXObject(baseClass.getDocumentReference(), 1).getStringValue("string"));
assertEquals(7, this.document.getXObject(baseClass.getDocumentReference(), 1).getIntValue("int"));
assertEquals("string3", this.document.getXObject(baseClass.getDocumentReference(), 2).getStringValue("string"));
assertEquals(42, this.document.getXObject(baseClass.getDocumentReference(), 2).getIntValue("int"));
assertNotNull(this.document.getXObject(baseClass.getDocumentReference(), 3));
assertEquals("blabla", this.document.getXObject(baseClass.getDocumentReference(), 3).getStringValue("string"));
assertEquals(13, this.document.getXObject(baseClass.getDocumentReference(), 3).getIntValue("int"));
assertNotNull(this.document.getXObject(baseClass.getDocumentReference(), 42));
assertEquals("bloublou", this.document.getXObject(baseClass.getDocumentReference(), 42).getStringValue("string"));
assertEquals(7, this.document.getXObject(baseClass.getDocumentReference(), 42).getIntValue("int"));
}
use of com.xpn.xwiki.web.EditForm in project xwiki-platform by xwiki.
the class XWikiDocumentMockitoTest method readObjectsFromForm.
/**
* Unit test for {@link XWikiDocument#readObjectsFromForm(EditForm, XWikiContext)}.
*/
@Test
public void readObjectsFromForm() throws Exception {
this.document = new XWikiDocument(new DocumentReference(DOCWIKI, DOCSPACE, DOCNAME));
this.oldcore.getSpyXWiki().saveDocument(this.document, "", true, this.oldcore.getXWikiContext());
HttpServletRequest request = mock(HttpServletRequest.class);
MockitoComponentManager mocker = this.oldcore.getMocker();
XWikiContext context = this.oldcore.getXWikiContext();
DocumentReferenceResolver<String> documentReferenceResolverString = mocker.registerMockComponent(DocumentReferenceResolver.TYPE_STRING, "current");
// Entity Reference resolver is used in <BaseObject>.getXClass()
DocumentReferenceResolver<EntityReference> documentReferenceResolverEntity = mocker.registerMockComponent(DocumentReferenceResolver.TYPE_REFERENCE, "current");
EntityReferenceSerializer<String> entityReferenceResolver = mocker.registerMockComponent(EntityReferenceSerializer.TYPE_STRING, "local");
Map<String, String[]> parameters = generateFakeRequestMap();
BaseClass baseClass = generateFakeClass();
generateFakeObjects();
when(request.getParameterMap()).thenReturn(parameters);
DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
// This entity resolver with this 'resolve' method is used in
// <BaseCollection>.getXClassReference()
when(documentReferenceResolverEntity.resolve(any(EntityReference.class), any(DocumentReference.class))).thenReturn(this.document.getDocumentReference());
when(documentReferenceResolverString.resolve("space.page")).thenReturn(documentReference);
when(entityReferenceResolver.serialize(any(EntityReference.class))).thenReturn("space.page");
EditForm eform = new EditForm();
eform.setRequest(request);
document.readObjectsFromForm(eform, context);
assertEquals(3, this.document.getXObjectSize(baseClass.getDocumentReference()));
assertEquals("string", this.document.getXObject(baseClass.getDocumentReference(), 0).getStringValue("string"));
assertEquals(42, this.document.getXObject(baseClass.getDocumentReference(), 0).getIntValue("int"));
assertEquals("string2", this.document.getXObject(baseClass.getDocumentReference(), 1).getStringValue("string"));
assertEquals(42, this.document.getXObject(baseClass.getDocumentReference(), 1).getIntValue("int"));
assertEquals("string3", this.document.getXObject(baseClass.getDocumentReference(), 2).getStringValue("string"));
assertEquals(42, this.document.getXObject(baseClass.getDocumentReference(), 2).getIntValue("int"));
assertNull(this.document.getXObject(baseClass.getDocumentReference(), 3));
assertNull(this.document.getXObject(baseClass.getDocumentReference(), 42));
}
Aggregations