예제 #1
0
  @Override
  public List<ObjectDiff> getDiff(Object oldObject, XWikiContext context) {
    ArrayList<ObjectDiff> difflist = new ArrayList<ObjectDiff>();
    BaseClass oldClass = (BaseClass) oldObject;
    for (PropertyClass newProperty : (Collection<PropertyClass>) getFieldList()) {
      String propertyName = newProperty.getName();
      PropertyClass oldProperty = (PropertyClass) oldClass.get(propertyName);
      String propertyType = newProperty.getClassType();

      if (oldProperty == null) {
        difflist.add(
            new ObjectDiff(
                getXClassReference(),
                getNumber(),
                "",
                ObjectDiff.ACTION_PROPERTYADDED,
                propertyName,
                propertyType,
                "",
                ""));
      } else if (!oldProperty.equals(newProperty)) {
        difflist.add(
            new ObjectDiff(
                getXClassReference(),
                getNumber(),
                "",
                ObjectDiff.ACTION_PROPERTYCHANGED,
                propertyName,
                propertyType,
                "",
                ""));
      }
    }

    for (PropertyClass oldProperty : (Collection<PropertyClass>) oldClass.getFieldList()) {
      String propertyName = oldProperty.getName();
      PropertyClass newProperty = (PropertyClass) get(propertyName);
      String propertyType = oldProperty.getClassType();

      if (newProperty == null) {
        difflist.add(
            new ObjectDiff(
                getXClassReference(),
                getNumber(),
                "",
                ObjectDiff.ACTION_PROPERTYREMOVED,
                propertyName,
                propertyType,
                "",
                ""));
      }
    }

    return difflist;
  }
  /**
   * Set the default value of a boolean field of a XWiki class.
   *
   * @param baseClass the XWiki class.
   * @param fieldName the name of the field.
   * @param value the default value.
   * @return true if <code>baseClass</code> modified.
   */
  protected boolean updateBooleanClassDefaultValue(
      BaseClass baseClass, String fieldName, Boolean value) {
    boolean needsUpdate = false;

    BooleanClass bc = (BooleanClass) baseClass.get(fieldName);

    int old = bc.getDefaultValue();
    int intvalue = intFromBoolean(value);

    if (intvalue != old) {
      bc.setDefaultValue(intvalue);
      needsUpdate = true;
    }

    return needsUpdate;
  }
  @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());
  }