@Test
  public void onEventWithExceptions() throws ComponentLookupException, XWikiException {
    Utils.setComponentManager(this.mocker);

    DocumentReference docReference = new DocumentReference("xwiki", "Groups", "Group1");
    XWikiContext context = mock(XWikiContext.class);
    XWiki xwiki = mock(XWiki.class);
    when(context.getWiki()).thenReturn(xwiki);

    XWikiDocument doc = mock(XWikiDocument.class);
    when(doc.getDocumentReference()).thenReturn(docReference);
    when(doc.getXObject(Group.CLASS_REFERENCE)).thenReturn(mock(BaseObject.class));

    DocumentReference adminsDocReference =
        new DocumentReference("xwiki", "Groups", "Group1 Administrators");
    when(xwiki.getDocument(eq(adminsDocReference), eq(context)))
        .thenThrow(new XWikiException(0, 0, "DB Error"));

    DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);

    DocumentReference userReference = new DocumentReference("xwiki", "XWiki", "User");
    when(dab.getCurrentUserReference()).thenReturn(userReference);

    this.mocker
        .getComponentUnderTest()
        .onEvent(new DocumentCreatingEvent(docReference), doc, context);

    Mockito.verify(xwiki, Mockito.never())
        .saveDocument(eq(doc), any(String.class), any(Boolean.class), eq(context));

    Mockito.verify(doc).getXObject(Group.CLASS_REFERENCE);
    Mockito.verify(doc).getDocumentReference();
    Mockito.verifyNoMoreInteractions(doc);
  }
  @Test
  public void onEvent() throws ComponentLookupException, XWikiException {
    Utils.setComponentManager(this.mocker);

    DocumentReference docReference = new DocumentReference("xwiki", "Groups", "Group1");
    XWikiContext context = mock(XWikiContext.class);
    XWiki xwiki = mock(XWiki.class);
    when(context.getWiki()).thenReturn(xwiki);

    XWikiDocument doc = mock(XWikiDocument.class);
    when(doc.getDocumentReference()).thenReturn(docReference);
    BaseObject groupObject = new BaseObject();
    BaseObject rightsObject = new BaseObject();
    when(doc.newXObject(eq(this.groupsClassReference), eq(context))).thenReturn(groupObject);
    when(doc.newXObject(eq(this.rightsClassReference), eq(context))).thenReturn(rightsObject);
    when(doc.getXObject(Group.CLASS_REFERENCE)).thenReturn(mock(BaseObject.class));

    DocumentReference adminsDocReference =
        new DocumentReference("xwiki", "Groups", "Group1 Administrators");
    XWikiDocument adminsDoc = mock(XWikiDocument.class);
    BaseObject adminsGroupObject = new BaseObject();
    BaseObject adminsRightsObject = new BaseObject();
    when(adminsDoc.newXObject(eq(this.groupsClassReference), eq(context)))
        .thenReturn(adminsGroupObject);
    when(adminsDoc.newXObject(eq(this.rightsClassReference), eq(context)))
        .thenReturn(adminsRightsObject);
    when(xwiki.getDocument(eq(adminsDocReference), eq(context))).thenReturn(adminsDoc);

    DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);

    DocumentReference userReference = new DocumentReference("xwiki", "XWiki", "User");
    when(dab.getCurrentUserReference()).thenReturn(userReference);

    this.mocker
        .getComponentUnderTest()
        .onEvent(new DocumentCreatingEvent(docReference), doc, context);

    Mockito.verify(xwiki).saveDocument(eq(adminsDoc), any(String.class), eq(true), eq(context));
    Mockito.verify(xwiki, Mockito.never())
        .saveDocument(eq(doc), any(String.class), any(Boolean.class), eq(context));

    Assert.assertEquals(1, rightsObject.getIntValue("allow"));
    Assert.assertEquals("edit", rightsObject.getStringValue("levels"));
    Assert.assertEquals(
        "xwiki:Groups.Group1 Administrators", rightsObject.getLargeStringValue("groups"));
    Assert.assertEquals("", rightsObject.getLargeStringValue("users"));

    Assert.assertEquals(1, adminsRightsObject.getIntValue("allow"));
    Assert.assertEquals("edit", adminsRightsObject.getStringValue("levels"));
    Assert.assertEquals(
        "xwiki:Groups.Group1 Administrators", adminsRightsObject.getLargeStringValue("groups"));
    Assert.assertEquals("", adminsRightsObject.getLargeStringValue("users"));

    Assert.assertEquals("xwiki:Groups.Group1 Administrators", groupObject.getStringValue("member"));
    Assert.assertEquals("xwiki:XWiki.User", adminsGroupObject.getStringValue("member"));
  }
  @Before
  public void setUp() throws Exception {
    EntityReferenceSerializer<String> localSerializer =
        mocker.getInstance(EntityReferenceSerializer.TYPE_STRING, "local");
    EntityReferenceSerializer<String> serializer =
        mocker.getInstance(EntityReferenceSerializer.TYPE_STRING, "default");

    // No locale provided.
    documentReference = new DocumentReference("wiki", "space", "name");
    documentReferenceString = serializer.serialize(documentReference);
    documentReferenceLocalString = localSerializer.serialize(documentReference);

    language = "en";
    renderedContent = "content";
    title = "title";
    version = "1.1";
    hidden = false;
    date = new Date();
    creationDate = new Date();

    authorReference = new DocumentReference("wiki", "space", "author");
    authorString = serializer.serialize(authorReference);
    authorDisplay = "Au Thor";

    creatorReference = new DocumentReference("wiki", "space", "Creator");
    creatorString = serializer.serialize(creatorReference);
    creatorDisplay = "Crea Tor";

    // Mock

    mockContext = mock(XWikiContext.class);
    Execution mockExecution = mocker.getInstance(Execution.class);
    ExecutionContext mockExecutionContext = new ExecutionContext();
    mockExecutionContext.setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, mockContext);

    when(mockExecution.getContext()).thenReturn(mockExecutionContext);

    mockXWiki = mock(XWiki.class);
    mockDocument = mock(XWikiDocument.class);

    when(mockContext.getWiki()).thenReturn(mockXWiki);
    when(mockXWiki.getDocument(documentReference, mockContext)).thenReturn(mockDocument);

    when(mockDocument.getRealLanguage()).thenReturn(language);
    when(mockDocument.getTranslatedDocument(any(String.class), eq(mockContext)))
        .thenReturn(mockDocument);

    mockDab = mocker.getInstance(DocumentAccessBridge.class);
    when(mockDab.getDocument(documentReference)).thenReturn(mockDocument);

    BlockRenderer mockPlainRenderer = mocker.getInstance(BlockRenderer.class, "plain/1.0");
    doAnswer(
            new Answer<Object>() {
              public Object answer(InvocationOnMock invocation) {
                Object[] args = invocation.getArguments();

                WikiPrinter printer = (WikiPrinter) args[1];
                printer.print(renderedContent);

                return null;
              }
            })
        .when(mockPlainRenderer)
        .render(any(Block.class), any(WikiPrinter.class));

    when(mockDocument.getRenderedTitle(any(Syntax.class), eq(mockContext))).thenReturn(title);

    when(mockDocument.getVersion()).thenReturn(version);

    when(mockDocument.getAuthorReference()).thenReturn(authorReference);
    when(mockXWiki.getUserName(authorString, null, false, mockContext)).thenReturn(authorDisplay);

    when(mockDocument.getCreatorReference()).thenReturn(creatorReference);
    when(mockXWiki.getUserName(creatorString, null, false, mockContext)).thenReturn(creatorDisplay);

    when(mockDocument.getCreationDate()).thenReturn(creationDate);
    when(mockDocument.getContentUpdateDate()).thenReturn(date);

    when(mockDocument.isHidden()).thenReturn(hidden);
  }