Search in sources :

Example 51 with BaseClass

use of com.xpn.xwiki.objects.classes.BaseClass in project xwiki-platform by xwiki.

the class XWikiDocumentMockitoTest method testObjectNumbersAfterXMLRoundrip.

@Test
public void testObjectNumbersAfterXMLRoundrip() throws XWikiException {
    String wiki = oldcore.getXWikiContext().getWikiId();
    XWikiDocument tagDocument = new XWikiDocument(new DocumentReference(wiki, "XWiki", "TagClass"));
    BaseClass tagClass = tagDocument.getXClass();
    tagClass.addStaticListField(XWikiConstant.TAG_CLASS_PROP_TAGS, "Tags", 30, true, "", "checkbox");
    this.oldcore.getSpyXWiki().saveDocument(tagDocument, this.oldcore.getXWikiContext());
    XWikiDocument doc = new XWikiDocument(new DocumentReference(wiki, "test", "document"));
    doReturn("iso-8859-1").when(this.oldcore.getSpyXWiki()).getEncoding();
    BaseObject object1 = doc.newXObject(tagDocument.getDocumentReference(), this.oldcore.getXWikiContext());
    BaseObject object2 = doc.newXObject(tagDocument.getDocumentReference(), this.oldcore.getXWikiContext());
    BaseObject object3 = doc.newXObject(tagDocument.getDocumentReference(), this.oldcore.getXWikiContext());
    // Remove first object
    doc.removeXObject(object1);
    String docXML = doc.toXML(this.oldcore.getXWikiContext());
    XWikiDocument docFromXML = new XWikiDocument(doc.getDocumentReference());
    docFromXML.fromXML(docXML);
    List<BaseObject> objects = doc.getXObjects(tagDocument.getDocumentReference());
    List<BaseObject> objectsFromXML = docFromXML.getXObjects(tagDocument.getDocumentReference());
    assertNotNull(objects);
    assertNotNull(objectsFromXML);
    assertTrue(objects.size() == objectsFromXML.size());
    for (int i = 0; i < objects.size(); i++) {
        if (objects.get(i) == null) {
            assertNull(objectsFromXML.get(i));
        } else {
            assertTrue(objects.get(i).getNumber() == objectsFromXML.get(i).getNumber());
        }
    }
}
Also used : BaseClass(com.xpn.xwiki.objects.classes.BaseClass) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject) Test(org.junit.Test)

Example 52 with BaseClass

use of com.xpn.xwiki.objects.classes.BaseClass 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 53 with BaseClass

use of com.xpn.xwiki.objects.classes.BaseClass 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)

Example 54 with BaseClass

use of com.xpn.xwiki.objects.classes.BaseClass in project xwiki-platform by xwiki.

the class ExplicitlyAllowedValuesDBListQueryBuilderTest method configure.

@Before
public void configure() throws Exception {
    this.authorizationManager = this.mocker.getInstance(AuthorizationManager.class);
    this.secureQueryManager = this.mocker.getInstance(QueryManager.class, "secure");
    XWikiDocument ownerDocument = mock(XWikiDocument.class);
    when(ownerDocument.getDocumentReference()).thenReturn(new DocumentReference("math", "Some", "Page"));
    when(ownerDocument.getAuthorReference()).thenReturn(new DocumentReference("wiki", "Users", "alice"));
    BaseClass xclass = new BaseClass();
    xclass.setDocumentReference(ownerDocument.getDocumentReference());
    this.dbListClass.setOwnerDocument(ownerDocument);
    this.dbListClass.setObject(xclass);
    this.dbListClass.setName("category");
    this.dbListClass.setSql("select ...");
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) QueryManager(org.xwiki.query.QueryManager) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) AuthorizationManager(org.xwiki.security.authorization.AuthorizationManager) DocumentReference(org.xwiki.model.reference.DocumentReference) Before(org.junit.Before)

Example 55 with BaseClass

use of com.xpn.xwiki.objects.classes.BaseClass in project xwiki-platform by xwiki.

the class DocumentSolrMetadataExtractorTest method setStaticListPropertyValue.

/**
 * @see "XWIKI-9417: Search does not return any results for Static List values"
 */
@Test
public void setStaticListPropertyValue() throws Exception {
    BaseObject xobject = mock(BaseObject.class);
    @SuppressWarnings("unchecked") BaseProperty<EntityReference> listProperty = mock(BaseProperty.class);
    when(listProperty.getName()).thenReturn("color");
    when(listProperty.getValue()).thenReturn(Arrays.asList("red", "green"));
    when(listProperty.getObject()).thenReturn(xobject);
    DocumentReference classReference = new DocumentReference("wiki", "Space", "MyClass");
    when(this.document.getXObjects()).thenReturn(Collections.singletonMap(classReference, Arrays.asList(xobject)));
    BaseClass xclass = mock(BaseClass.class);
    when(xobject.getXClass(this.xcontext)).thenReturn(xclass);
    when(xobject.getFieldList()).thenReturn(Arrays.<Object>asList(listProperty));
    when(xobject.getRelativeXClassReference()).thenReturn(classReference.removeParent(classReference.getWikiReference()));
    StaticListClass staticListClass = mock(StaticListClass.class);
    when(xclass.get("color")).thenReturn(staticListClass);
    when(staticListClass.getMap(xcontext)).thenReturn(Collections.singletonMap("red", new ListItem("red", "Dark Red")));
    SolrInputDocument solrDocument = this.mocker.getComponentUnderTest().getSolrDocument(this.documentReference);
    // Make sure both the raw value (which is saved in the database) and the display value (specified in the XClass)
    // are indexed. The raw values are indexed as strings in order to be able to perform exact matches.
    assertEquals(Arrays.asList("red", "green"), solrDocument.getFieldValues("property.Space.MyClass.color_string"));
    assertEquals(Collections.singletonList("Dark Red"), solrDocument.getFieldValues(FieldUtils.getFieldName("property.Space.MyClass.color", Locale.US)));
    // Check the sort field. Only the last value we set is used for sorting because we cannot sort on fields that
    // have multiple values.
    assertEquals(Collections.singletonList("green"), solrDocument.getFieldValues("property.Space.MyClass.color_sortString"));
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) StaticListClass(com.xpn.xwiki.objects.classes.StaticListClass) EntityReference(org.xwiki.model.reference.EntityReference) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) ListItem(com.xpn.xwiki.objects.classes.ListItem) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject) Test(org.junit.Test)

Aggregations

BaseClass (com.xpn.xwiki.objects.classes.BaseClass)100 DocumentReference (org.xwiki.model.reference.DocumentReference)42 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)41 BaseObject (com.xpn.xwiki.objects.BaseObject)40 XWikiException (com.xpn.xwiki.XWikiException)26 XWikiContext (com.xpn.xwiki.XWikiContext)24 ArrayList (java.util.ArrayList)18 PropertyClass (com.xpn.xwiki.objects.classes.PropertyClass)16 XWiki (com.xpn.xwiki.XWiki)15 Test (org.junit.Test)15 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)15 EntityReference (org.xwiki.model.reference.EntityReference)10 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)9 BaseProperty (com.xpn.xwiki.objects.BaseProperty)9 List (java.util.List)9 ToString (org.suigeneris.jrcs.util.ToString)9 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)7 Before (org.junit.Before)6 TextAreaClass (com.xpn.xwiki.objects.classes.TextAreaClass)5 MigrationRequiredException (com.xpn.xwiki.store.migration.MigrationRequiredException)5