| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.component.wiki; |
| 21 |
|
|
| 22 |
|
import java.util.Arrays; |
| 23 |
|
|
| 24 |
|
import org.junit.Before; |
| 25 |
|
import org.junit.Rule; |
| 26 |
|
import org.junit.Test; |
| 27 |
|
import org.xwiki.bridge.DocumentModelBridge; |
| 28 |
|
import org.xwiki.bridge.event.ApplicationReadyEvent; |
| 29 |
|
import org.xwiki.bridge.event.DocumentCreatedEvent; |
| 30 |
|
import org.xwiki.bridge.event.DocumentDeletedEvent; |
| 31 |
|
import org.xwiki.bridge.event.DocumentUpdatedEvent; |
| 32 |
|
import org.xwiki.component.wiki.internal.DefaultWikiComponent; |
| 33 |
|
import org.xwiki.component.wiki.internal.DefaultWikiComponentManagerEventListener; |
| 34 |
|
import org.xwiki.component.wiki.internal.WikiComponentConstants; |
| 35 |
|
import org.xwiki.model.reference.DocumentReference; |
| 36 |
|
import org.xwiki.observation.EventListener; |
| 37 |
|
import org.xwiki.observation.event.Event; |
| 38 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
| 39 |
|
|
| 40 |
|
import static org.mockito.ArgumentMatchers.*; |
| 41 |
|
import static org.mockito.Mockito.*; |
| 42 |
|
|
| 43 |
|
|
| 44 |
|
@link |
| 45 |
|
|
| 46 |
|
@version |
| 47 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (48) |
Complexity: 9 |
Complexity Density: 0.23 |
|
| 48 |
|
public class DefaultWikiComponentManagerEventListenerTest implements WikiComponentConstants |
| 49 |
|
{ |
| 50 |
|
private static final String ROLE_HINT = "roleHint"; |
| 51 |
|
|
| 52 |
|
private static final DocumentReference DOC_REFERENCE = new DocumentReference("xwiki", "XWiki", "MyComponent"); |
| 53 |
|
|
| 54 |
|
private static final DocumentReference AUTHOR_REFERENCE = new DocumentReference("xwiki", "XWiki", "Admin"); |
| 55 |
|
|
| 56 |
|
@Rule |
| 57 |
|
public MockitoComponentMockingRule<EventListener> mocker = new MockitoComponentMockingRule<EventListener>( |
| 58 |
|
DefaultWikiComponentManagerEventListener.class); |
| 59 |
|
|
| 60 |
|
private WikiComponentBuilder wikiComponentBuilder; |
| 61 |
|
|
| 62 |
|
private WikiComponentManager wikiComponentManager; |
| 63 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 64 |
7 |
@Before... |
| 65 |
|
public void setUp() throws Exception |
| 66 |
|
{ |
| 67 |
7 |
wikiComponentBuilder = mocker.registerMockComponent(WikiComponentBuilder.class); |
| 68 |
7 |
wikiComponentManager = mocker.getInstance(WikiComponentManager.class); |
| 69 |
|
} |
| 70 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 71 |
1 |
@Test... |
| 72 |
|
public void onEventWhenSourceIsNotAXWikiDocumentAndNoMatchingEvent() throws Exception |
| 73 |
|
{ |
| 74 |
1 |
mocker.getComponentUnderTest().onEvent(null, null, null); |
| 75 |
|
|
| 76 |
1 |
verify(wikiComponentManager, never()).registerWikiComponent(any(WikiComponent.class)); |
| 77 |
1 |
verify(wikiComponentManager, never()).unregisterWikiComponents(any(DocumentReference.class)); |
| 78 |
|
} |
| 79 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
| 80 |
1 |
@Test... |
| 81 |
|
public void onEventWhenSourceDocumentButNoMatchingEvent() throws Exception |
| 82 |
|
{ |
| 83 |
1 |
DocumentModelBridge componentDocument = mock(DocumentModelBridge.class); |
| 84 |
1 |
when(componentDocument.getDocumentReference()).thenReturn(DOC_REFERENCE); |
| 85 |
|
|
| 86 |
1 |
mocker.getComponentUnderTest().onEvent(null, componentDocument, null); |
| 87 |
|
|
| 88 |
1 |
verify(wikiComponentManager, never()).registerWikiComponent(any(WikiComponent.class)); |
| 89 |
1 |
verify(wikiComponentManager, never()).unregisterWikiComponents(any(DocumentReference.class)); |
| 90 |
|
} |
| 91 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
| 92 |
1 |
@Test... |
| 93 |
|
public void onDocumentCreated() throws Exception |
| 94 |
|
{ |
| 95 |
1 |
onDocumentCreatedOrUpdated(new DocumentCreatedEvent(DOC_REFERENCE)); |
| 96 |
|
} |
| 97 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
| 98 |
1 |
@Test... |
| 99 |
|
public void onDocumentUpdated() throws Exception |
| 100 |
|
{ |
| 101 |
1 |
onDocumentCreatedOrUpdated(new DocumentUpdatedEvent(DOC_REFERENCE)); |
| 102 |
|
} |
| 103 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
| 104 |
1 |
@Test... |
| 105 |
|
public void onDocumentUpdatedWhenTwoComponents() throws Exception |
| 106 |
|
{ |
| 107 |
1 |
DocumentModelBridge componentDocument = mock(DocumentModelBridge.class); |
| 108 |
1 |
when(componentDocument.getDocumentReference()).thenReturn(DOC_REFERENCE); |
| 109 |
1 |
when(wikiComponentBuilder.getDocumentReferences()).thenReturn(Arrays.asList(DOC_REFERENCE)); |
| 110 |
|
|
| 111 |
1 |
WikiComponent component = |
| 112 |
|
new DefaultWikiComponent(DOC_REFERENCE, AUTHOR_REFERENCE, TestRole.class, ROLE_HINT, |
| 113 |
|
WikiComponentScope.WIKI); |
| 114 |
1 |
when(wikiComponentBuilder.buildComponents(DOC_REFERENCE)).thenReturn(Arrays.asList(component, component)); |
| 115 |
|
|
| 116 |
1 |
mocker.getComponentUnderTest().onEvent(new DocumentUpdatedEvent(DOC_REFERENCE), componentDocument, null); |
| 117 |
|
|
| 118 |
1 |
verify(wikiComponentManager, times(1)).unregisterWikiComponents(DOC_REFERENCE); |
| 119 |
1 |
verify(wikiComponentManager, times(2)).registerWikiComponent(component); |
| 120 |
|
} |
| 121 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
| 122 |
1 |
@Test... |
| 123 |
|
public void onDocumentDeleted() throws Exception |
| 124 |
|
{ |
| 125 |
1 |
DocumentModelBridge componentDocument = mock(DocumentModelBridge.class); |
| 126 |
1 |
when(componentDocument.getDocumentReference()).thenReturn(DOC_REFERENCE); |
| 127 |
|
|
| 128 |
1 |
mocker.getComponentUnderTest().onEvent(new DocumentDeletedEvent(DOC_REFERENCE), componentDocument, null); |
| 129 |
|
|
| 130 |
1 |
verify(wikiComponentManager, times(1)).unregisterWikiComponents(DOC_REFERENCE); |
| 131 |
|
} |
| 132 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
| 133 |
1 |
@Test... |
| 134 |
|
public void onApplicationReady() throws Exception |
| 135 |
|
{ |
| 136 |
1 |
DocumentModelBridge componentDocument = mock(DocumentModelBridge.class); |
| 137 |
1 |
when(componentDocument.getDocumentReference()).thenReturn(DOC_REFERENCE); |
| 138 |
1 |
when(wikiComponentBuilder.getDocumentReferences()).thenReturn(Arrays.asList(DOC_REFERENCE)); |
| 139 |
|
|
| 140 |
1 |
WikiComponent component = |
| 141 |
|
new DefaultWikiComponent(DOC_REFERENCE, AUTHOR_REFERENCE, TestRole.class, ROLE_HINT, |
| 142 |
|
WikiComponentScope.WIKI); |
| 143 |
1 |
when(wikiComponentBuilder.buildComponents(DOC_REFERENCE)).thenReturn(Arrays.asList(component)); |
| 144 |
|
|
| 145 |
1 |
mocker.getComponentUnderTest().onEvent(new ApplicationReadyEvent(), null, null); |
| 146 |
|
|
| 147 |
1 |
verify(wikiComponentManager, times(1)).registerWikiComponent(component); |
| 148 |
|
} |
| 149 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
|
| 150 |
2 |
private void onDocumentCreatedOrUpdated(Event event) throws Exception... |
| 151 |
|
{ |
| 152 |
2 |
DocumentModelBridge componentDocument = mock(DocumentModelBridge.class); |
| 153 |
2 |
when(componentDocument.getDocumentReference()).thenReturn(DOC_REFERENCE); |
| 154 |
2 |
when(wikiComponentBuilder.getDocumentReferences()).thenReturn(Arrays.asList(DOC_REFERENCE)); |
| 155 |
|
|
| 156 |
2 |
WikiComponent component = |
| 157 |
|
new DefaultWikiComponent(DOC_REFERENCE, AUTHOR_REFERENCE, TestRole.class, ROLE_HINT, |
| 158 |
|
WikiComponentScope.WIKI); |
| 159 |
2 |
when(wikiComponentBuilder.buildComponents(DOC_REFERENCE)).thenReturn(Arrays.asList(component)); |
| 160 |
|
|
| 161 |
2 |
mocker.getComponentUnderTest().onEvent(event, componentDocument, null); |
| 162 |
|
|
| 163 |
2 |
verify(wikiComponentManager, times(1)).unregisterWikiComponents(DOC_REFERENCE); |
| 164 |
2 |
verify(wikiComponentManager, times(1)).registerWikiComponent(component); |
| 165 |
|
} |
| 166 |
|
|
| 167 |
|
} |