@Test
  public void testAuthorWithDocument() throws Exception {
    EntityReferenceSerializer<String> compactWikiEntityReferenceSerializer =
        this.oldcore.getMocker().getInstance(EntityReferenceSerializer.TYPE_STRING, "compactwiki");
    DocumentReferenceResolver<EntityReference> explicitDocumentReferenceResolver =
        this.oldcore
            .getMocker()
            .registerMockComponent(DocumentReferenceResolver.TYPE_REFERENCE, "explicit");
    EntityReferenceResolver<String> xclassEntityReferenceResolver =
        this.oldcore
            .getMocker()
            .registerMockComponent(EntityReferenceResolver.TYPE_STRING, "xclass");

    XWikiDocument document = new XWikiDocument(new DocumentReference("wiki", "space", "page"));
    XWikiAttachment attachment = new XWikiAttachment(document, "filename");

    // getAuthor() based on getAuthorReference()
    DocumentReference userReference = new DocumentReference("userwiki", "userspace", "userpage");
    attachment.setAuthorReference(userReference);
    assertEquals(userReference, attachment.getAuthorReference());
    when(compactWikiEntityReferenceSerializer.serialize(userReference, attachment.getReference()))
        .thenReturn("stringUserReference");
    assertEquals("stringUserReference", attachment.getAuthor());

    // getAuthorReference() based on getAuthor()
    attachment.setAuthor("author");
    assertEquals("author", attachment.getAuthor());
    userReference = new DocumentReference("wiki", "XWiki", "author");
    EntityReference relativeUserReference =
        userReference.removeParent(userReference.getWikiReference());
    when(xclassEntityReferenceResolver.resolve("author", EntityType.DOCUMENT))
        .thenReturn(relativeUserReference);
    when(explicitDocumentReferenceResolver.resolve(
            relativeUserReference, attachment.getReference()))
        .thenReturn(userReference);
    assertEquals(userReference, attachment.getAuthorReference());

    // Guest author.
    attachment.setAuthor(XWikiRightService.GUEST_USER);
    userReference = new DocumentReference("wiki", "XWiki", XWikiRightService.GUEST_USER);
    relativeUserReference = userReference.removeParent(userReference.getWikiReference());
    when(xclassEntityReferenceResolver.resolve(any(String.class), eq(EntityType.DOCUMENT)))
        .thenReturn(relativeUserReference);
    when(explicitDocumentReferenceResolver.resolve(
            relativeUserReference, attachment.getReference()))
        .thenReturn(userReference);
    assertNull(attachment.getAuthorReference());
  }
  @Test
  public void testAuthorWithoutDocument() throws Exception {
    EntityReferenceSerializer<String> compactWikiEntityReferenceSerializer =
        this.oldcore.getMocker().getInstance(EntityReferenceSerializer.TYPE_STRING, "compactwiki");
    AttachmentReferenceResolver<String> currentAttachmentReferenceResolver =
        this.oldcore.getMocker().getInstance(AttachmentReferenceResolver.TYPE_STRING, "current");
    DocumentReferenceResolver<EntityReference> explicitDocumentReferenceResolver =
        this.oldcore
            .getMocker()
            .registerMockComponent(DocumentReferenceResolver.TYPE_REFERENCE, "explicit");
    EntityReferenceResolver<String> xclassEntityReferenceResolver =
        this.oldcore
            .getMocker()
            .registerMockComponent(EntityReferenceResolver.TYPE_STRING, "xclass");

    XWikiAttachment attachment = new XWikiAttachment(null, "filename");
    DocumentReference currentDocumentReference =
        new DocumentReference("currentWiki", "currentSpage", "currentPage");
    AttachmentReference attachmentReference =
        new AttachmentReference(attachment.getFilename(), currentDocumentReference);

    // getAuthor() based on getAuthorReference()
    DocumentReference userReference = new DocumentReference("userwiki", "userspace", "userpage");
    attachment.setAuthorReference(userReference);
    assertEquals(userReference, attachment.getAuthorReference());
    when(currentAttachmentReferenceResolver.resolve(attachment.getFilename()))
        .thenReturn(attachmentReference);
    when(compactWikiEntityReferenceSerializer.serialize(userReference, attachmentReference))
        .thenReturn("stringUserReference");
    assertEquals("stringUserReference", attachment.getAuthor());

    // getAuthorReference() based on getAuthor()
    attachment.setAuthor("author");
    assertEquals("author", attachment.getAuthor());
    userReference = new DocumentReference("wiki", "XWiki", "author");
    EntityReference relativeUserReference =
        userReference.removeParent(userReference.getWikiReference());
    when(xclassEntityReferenceResolver.resolve("author", EntityType.DOCUMENT))
        .thenReturn(relativeUserReference);
    when(explicitDocumentReferenceResolver.resolve(
            relativeUserReference, attachment.getReference()))
        .thenReturn(userReference);
    assertEquals(userReference, attachment.getAuthorReference());
  }