Search in sources :

Example 1 with MockitoComponentManager

use of org.xwiki.test.mockito.MockitoComponentManager 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"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockitoComponentManager(org.xwiki.test.mockito.MockitoComponentManager) EntityReference(org.xwiki.model.reference.EntityReference) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) XWikiContext(com.xpn.xwiki.XWikiContext) DocumentReference(org.xwiki.model.reference.DocumentReference) EditForm(com.xpn.xwiki.web.EditForm) Test(org.junit.Test)

Example 2 with MockitoComponentManager

use of org.xwiki.test.mockito.MockitoComponentManager 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));
}
Also used : XWikiContext(com.xpn.xwiki.XWikiContext) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockitoComponentManager(org.xwiki.test.mockito.MockitoComponentManager) EntityReference(org.xwiki.model.reference.EntityReference) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) DocumentReference(org.xwiki.model.reference.DocumentReference) EditForm(com.xpn.xwiki.web.EditForm) Test(org.junit.Test)

Aggregations

XWikiContext (com.xpn.xwiki.XWikiContext)2 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)2 EditForm (com.xpn.xwiki.web.EditForm)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 Test (org.junit.Test)2 DocumentReference (org.xwiki.model.reference.DocumentReference)2 EntityReference (org.xwiki.model.reference.EntityReference)2 MockitoComponentManager (org.xwiki.test.mockito.MockitoComponentManager)2