Пример #1
0
 @Override
 public void safeput(String name, PropertyInterface property) {
   addField(PROPERTY_NAME_PREFIX + name, property);
   if (property instanceof PropertyClass) {
     ((PropertyClass) property).setObject(this);
     ((PropertyClass) property).setName(name);
   }
 }
  @Test
  public void getDocumentWithObjects() throws Exception {
    DocumentReference commentsClassReference =
        new DocumentReference("wiki", "space", "commentsClass");
    String commentContent = "This is a comment";
    String commentAuthor = "wiki:space.commentAuthor";
    Date commentDate = new Date();
    // Adding a fake password field to the comments class just to test the branch in the code.
    String commentPassword = "******";
    List<String> commentList = Arrays.asList("a", "list");

    List<BaseProperty<EntityReference>> commentFields =
        new ArrayList<BaseProperty<EntityReference>>();

    // Mock

    BaseProperty<EntityReference> mockCommentField = mock(BaseProperty.class);
    when(mockCommentField.getName()).thenReturn("comment");
    when(mockCommentField.getValue()).thenReturn(commentContent);
    commentFields.add(mockCommentField);

    BaseProperty<EntityReference> mockAuthorField = mock(BaseProperty.class);
    when(mockAuthorField.getName()).thenReturn("author");
    when(mockAuthorField.getValue()).thenReturn(commentAuthor);
    commentFields.add(mockAuthorField);

    BaseProperty<EntityReference> mockDateField = mock(BaseProperty.class);
    when(mockDateField.getName()).thenReturn("date");
    when(mockDateField.getValue()).thenReturn(commentDate);
    commentFields.add(mockDateField);

    BaseProperty<EntityReference> mockPasswordField = mock(BaseProperty.class);
    when(mockPasswordField.getName()).thenReturn("password");
    when(mockPasswordField.getValue()).thenReturn(commentPassword);
    commentFields.add(mockPasswordField);

    BaseProperty<EntityReference> mockListField = mock(BaseProperty.class);
    when(mockListField.getName()).thenReturn("list");
    when(mockListField.getValue()).thenReturn(commentList);
    commentFields.add(mockListField);

    BaseClass mockXClass = mock(BaseClass.class);

    BaseObject mockComment = mock(BaseObject.class);

    // When handled as a comment
    Vector<BaseObject> comments = new Vector<BaseObject>();
    comments.add(mockComment);
    when(mockDocument.getComments()).thenReturn(comments);

    when(mockComment.getStringValue("comment")).thenReturn(commentContent);
    when(mockComment.getStringValue("author")).thenReturn(commentAuthor);
    when(mockComment.getDateValue("date")).thenReturn(commentDate);

    // When handled as a general object
    HashMap<DocumentReference, List<BaseObject>> xObjects =
        new HashMap<DocumentReference, List<BaseObject>>();
    xObjects.put(commentsClassReference, Arrays.asList(mockComment));
    when(mockDocument.getXObjects()).thenReturn(xObjects);

    when(mockComment.getXClass(mockContext)).thenReturn(mockXClass);
    when(mockComment.getFieldList()).thenReturn(commentFields);

    PropertyClass passwordClass = mock(PasswordClass.class);
    when(mockXClass.get("password")).thenReturn(passwordClass);
    when(passwordClass.getClassType()).thenReturn("Password");

    // Call

    DocumentSolrMetadataExtractor extractor =
        (DocumentSolrMetadataExtractor) mocker.getComponentUnderTest();
    SolrInputDocument solrDocument = extractor.getSolrDocument(documentReference);

    // Assert and verify

    Assert.assertEquals(
        String.format("%s by %s on %s", commentContent, commentAuthor, commentDate),
        solrDocument.getFieldValue(
            String.format(Fields.MULTILIGNUAL_FORMAT, Fields.COMMENT, language)));

    Collection<Object> objectProperties =
        solrDocument.getFieldValues(
            String.format(Fields.MULTILIGNUAL_FORMAT, Fields.OBJECT_CONTENT, language));
    MatcherAssert.assertThat(
        objectProperties,
        Matchers.containsInAnyOrder(
            (Object) ("comment:" + commentContent),
            (Object) ("author:" + commentAuthor),
            (Object) ("date:" + commentDate.toString()),
            (Object) ("list:" + commentList.get(0)),
            (Object) ("list:" + commentList.get(1))));
    Assert.assertEquals(5, objectProperties.size());
  }
  public static Object createObject(
      ObjectFactory objectFactory,
      URI baseUri,
      XWikiContext xwikiContext,
      Document doc,
      BaseObject xwikiObject,
      boolean useVersion,
      XWiki xwikiApi,
      Boolean withPrettyNames)
      throws XWikiException {
    Object object = objectFactory.createObject();
    fillObjectSummary(object, objectFactory, baseUri, doc, xwikiObject, xwikiApi, withPrettyNames);

    BaseClass xwikiClass = xwikiObject.getXClass(xwikiContext);

    for (java.lang.Object propertyClassObject : xwikiClass.getProperties()) {
      com.xpn.xwiki.objects.classes.PropertyClass propertyClass =
          (com.xpn.xwiki.objects.classes.PropertyClass) propertyClassObject;

      Property property = objectFactory.createProperty();

      for (java.lang.Object o : propertyClass.getProperties()) {
        BaseProperty baseProperty = (BaseProperty) o;
        Attribute attribute = objectFactory.createAttribute();
        attribute.setName(baseProperty.getName());

        /* Check for null values in order to prevent NPEs */
        if (baseProperty.getValue() != null) {
          attribute.setValue(baseProperty.getValue().toString());
        } else {
          attribute.setValue("");
        }

        property.getAttributes().add(attribute);
      }

      if (propertyClass instanceof ListClass) {
        ListClass listClass = (ListClass) propertyClass;

        List allowedValueList = listClass.getList(xwikiContext);

        if (!allowedValueList.isEmpty()) {
          Formatter f = new Formatter();
          for (int i = 0; i < allowedValueList.size(); i++) {
            if (i != allowedValueList.size() - 1) {
              f.format("%s,", allowedValueList.get(i).toString());
            } else {
              f.format("%s", allowedValueList.get(i).toString());
            }
          }

          Attribute attribute = objectFactory.createAttribute();
          attribute.setName(Constants.ALLOWED_VALUES_ATTRIBUTE_NAME);
          attribute.setValue(f.toString());
          property.getAttributes().add(attribute);
        }
      }

      property.setName(propertyClass.getName());
      property.setType(propertyClass.getClassType());
      if (xwikiObject.get(propertyClass.getName()) != null) {
        property.setValue(xwikiObject.get(propertyClass.getName()).toFormString());
      } else {
        property.setValue("");
      }

      String propertyUri;

      if (useVersion) {
        propertyUri =
            uri(
                baseUri,
                ObjectPropertyAtPageVersionResource.class,
                doc.getWiki(),
                doc.getSpace(),
                doc.getName(),
                doc.getVersion(),
                xwikiObject.getClassName(),
                xwikiObject.getNumber(),
                propertyClass.getName());
      } else {
        propertyUri =
            uri(
                baseUri,
                ObjectPropertyResource.class,
                doc.getWiki(),
                doc.getSpace(),
                doc.getName(),
                xwikiObject.getClassName(),
                xwikiObject.getNumber(),
                propertyClass.getName());
      }
      Link propertyLink = objectFactory.createLink();
      propertyLink.setHref(propertyUri);
      propertyLink.setRel(Relations.SELF);
      property.getLinks().add(propertyLink);

      object.getProperties().add(property);
    }

    Link objectLink =
        getObjectLink(objectFactory, baseUri, doc, xwikiObject, useVersion, Relations.SELF);
    object.getLinks().add(objectLink);

    return object;
  }