| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.component.script; |
| 21 |
|
|
| 22 |
|
import javax.inject.Provider; |
| 23 |
|
|
| 24 |
|
import org.junit.Before; |
| 25 |
|
import org.junit.Rule; |
| 26 |
|
import org.junit.Test; |
| 27 |
|
import org.xwiki.bridge.DocumentAccessBridge; |
| 28 |
|
import org.xwiki.component.internal.ContextComponentManagerProvider; |
| 29 |
|
import org.xwiki.component.internal.multi.ComponentManagerManager; |
| 30 |
|
import org.xwiki.component.manager.ComponentLookupException; |
| 31 |
|
import org.xwiki.component.manager.ComponentManager; |
| 32 |
|
import org.xwiki.component.util.DefaultParameterizedType; |
| 33 |
|
import org.xwiki.context.Execution; |
| 34 |
|
import org.xwiki.context.ExecutionContext; |
| 35 |
|
import org.xwiki.test.annotation.ComponentList; |
| 36 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
| 37 |
|
|
| 38 |
|
import static org.junit.Assert.assertEquals; |
| 39 |
|
import static org.junit.Assert.assertNull; |
| 40 |
|
import static org.junit.Assert.assertSame; |
| 41 |
|
import static org.mockito.ArgumentMatchers.any; |
| 42 |
|
import static org.mockito.ArgumentMatchers.anyBoolean; |
| 43 |
|
import static org.mockito.Mockito.mock; |
| 44 |
|
import static org.mockito.Mockito.never; |
| 45 |
|
import static org.mockito.Mockito.verify; |
| 46 |
|
import static org.mockito.Mockito.when; |
| 47 |
|
|
| 48 |
|
|
| 49 |
|
@link |
| 50 |
|
|
| 51 |
|
@version |
| 52 |
|
@since |
| 53 |
|
|
| 54 |
|
@ComponentList(ContextComponentManagerProvider.class) |
| |
|
| 100% |
Uncovered Elements: 0 (72) |
Complexity: 15 |
Complexity Density: 0.26 |
|
| 55 |
|
public class ComponentScriptServiceTest |
| 56 |
|
{ |
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
| 60 |
|
private interface SomeRole |
| 61 |
|
{ |
| 62 |
|
} |
| 63 |
|
|
| 64 |
|
@Rule |
| 65 |
|
public MockitoComponentMockingRule<ComponentScriptService> mocker = new MockitoComponentMockingRule<>( |
| 66 |
|
ComponentScriptService.class); |
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
private DocumentAccessBridge dab; |
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
|
| 76 |
|
private ComponentManager contextComponentManager; |
| 77 |
|
|
| 78 |
|
private ComponentManager contextrootComponentManager; |
| 79 |
|
|
| 80 |
|
private ComponentManager rootComponentManager; |
| 81 |
|
|
| 82 |
|
private ComponentManagerManager componentManagerManager; |
| 83 |
|
|
| 84 |
|
private Execution execution; |
| 85 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
|
| 86 |
14 |
@Before... |
| 87 |
|
public void configure() throws Exception |
| 88 |
|
{ |
| 89 |
14 |
this.dab = this.mocker.getInstance(DocumentAccessBridge.class); |
| 90 |
|
|
| 91 |
14 |
this.contextComponentManager = this.mocker.registerMockComponent(ComponentManager.class, "context"); |
| 92 |
14 |
Provider<ComponentManager> contextComponentManagerProvider = this.mocker.registerMockComponent(new DefaultParameterizedType(null, Provider.class, ComponentManager.class), "context"); |
| 93 |
14 |
when(contextComponentManagerProvider.get()).thenReturn(this.contextComponentManager); |
| 94 |
|
|
| 95 |
14 |
this.contextrootComponentManager = this.mocker.registerMockComponent(ComponentManager.class, "context/root"); |
| 96 |
14 |
Provider<ComponentManager> contextrootComponentManagerProvider = this.mocker.registerMockComponent(new DefaultParameterizedType(null, Provider.class, ComponentManager.class), "context/root"); |
| 97 |
14 |
when(contextrootComponentManagerProvider.get()).thenReturn(this.contextrootComponentManager); |
| 98 |
|
|
| 99 |
14 |
this.rootComponentManager = this.mocker.registerMockComponent(ComponentManager.class, "root"); |
| 100 |
|
|
| 101 |
14 |
this.componentManagerManager = this.mocker.getInstance(ComponentManagerManager.class); |
| 102 |
|
|
| 103 |
14 |
when(this.componentManagerManager.getComponentManager(null, false)).thenReturn(this.rootComponentManager); |
| 104 |
|
|
| 105 |
14 |
this.execution = this.mocker.getInstance(Execution.class); |
| 106 |
|
} |
| 107 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 108 |
1 |
@Test... |
| 109 |
|
public void getComponentManagerWhenNoProgrammingRights() throws Exception |
| 110 |
|
{ |
| 111 |
1 |
when(this.dab.hasProgrammingRights()).thenReturn(false); |
| 112 |
|
|
| 113 |
1 |
assertNull(this.mocker.getComponentUnderTest().getComponentManager()); |
| 114 |
|
} |
| 115 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 116 |
1 |
@Test... |
| 117 |
|
public void getRootComponentManagerWhenNoProgrammingRights() throws Exception |
| 118 |
|
{ |
| 119 |
1 |
when(this.dab.hasProgrammingRights()).thenReturn(false); |
| 120 |
|
|
| 121 |
1 |
assertNull(this.mocker.getComponentUnderTest().getRootComponentManager()); |
| 122 |
|
} |
| 123 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 124 |
1 |
@Test... |
| 125 |
|
public void getContextComponentManagerWhenNoProgrammingRights() throws Exception |
| 126 |
|
{ |
| 127 |
1 |
when(this.dab.hasProgrammingRights()).thenReturn(false); |
| 128 |
|
|
| 129 |
1 |
assertNull(this.mocker.getComponentUnderTest().getContextComponentManager()); |
| 130 |
|
} |
| 131 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 132 |
1 |
@Test... |
| 133 |
|
public void getComponentManagerWhenProgrammingRights() throws Exception |
| 134 |
|
{ |
| 135 |
1 |
when(this.dab.hasProgrammingRights()).thenReturn(true); |
| 136 |
|
|
| 137 |
1 |
assertSame(this.contextrootComponentManager, this.mocker.getComponentUnderTest().getComponentManager()); |
| 138 |
|
} |
| 139 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 140 |
1 |
@Test... |
| 141 |
|
public void getRootComponentManagerWhenProgrammingRights() throws Exception |
| 142 |
|
{ |
| 143 |
1 |
when(this.dab.hasProgrammingRights()).thenReturn(true); |
| 144 |
|
|
| 145 |
1 |
assertSame(this.rootComponentManager, this.mocker.getComponentUnderTest().getRootComponentManager()); |
| 146 |
|
} |
| 147 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 148 |
1 |
@Test... |
| 149 |
|
public void getContextComponentManagerWhenProgrammingRights() throws Exception |
| 150 |
|
{ |
| 151 |
1 |
when(this.dab.hasProgrammingRights()).thenReturn(true); |
| 152 |
|
|
| 153 |
1 |
assertSame(this.contextComponentManager, this.mocker.getComponentUnderTest().getContextComponentManager()); |
| 154 |
|
} |
| 155 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
| 156 |
1 |
@Test... |
| 157 |
|
public void getComponentManagerForNamespaceWhenNoProgrammingRights() throws Exception |
| 158 |
|
{ |
| 159 |
1 |
when(this.dab.hasProgrammingRights()).thenReturn(false); |
| 160 |
|
|
| 161 |
1 |
assertNull(this.mocker.getComponentUnderTest().getComponentManager("wiki:xwiki")); |
| 162 |
|
|
| 163 |
1 |
ComponentManagerManager componentManagerManager = this.mocker.getInstance(ComponentManagerManager.class); |
| 164 |
1 |
verify(componentManagerManager, never()).getComponentManager(any(), anyBoolean()); |
| 165 |
|
} |
| 166 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
| 167 |
1 |
@Test... |
| 168 |
|
public void getComponentManagerForNamespaceWhenProgrammingRights() throws Exception |
| 169 |
|
{ |
| 170 |
1 |
when(this.dab.hasProgrammingRights()).thenReturn(true); |
| 171 |
|
|
| 172 |
1 |
ComponentManager wikiComponentManager = mock(ComponentManager.class); |
| 173 |
1 |
when(this.componentManagerManager.getComponentManager("wiki:chess", false)).thenReturn(wikiComponentManager); |
| 174 |
|
|
| 175 |
1 |
assertSame(wikiComponentManager, this.mocker.getComponentUnderTest().getComponentManager("wiki:chess")); |
| 176 |
|
} |
| 177 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 178 |
1 |
@Test... |
| 179 |
|
public void getComponentInstanceWithNoHintWhenNoProgrammingRights() throws Exception |
| 180 |
|
{ |
| 181 |
1 |
when(this.dab.hasProgrammingRights()).thenReturn(false); |
| 182 |
|
|
| 183 |
1 |
assertNull(this.mocker.getComponentUnderTest().getInstance(SomeRole.class)); |
| 184 |
|
|
| 185 |
1 |
verify(this.contextComponentManager, never()).getInstance(SomeRole.class); |
| 186 |
|
} |
| 187 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
| 188 |
1 |
@Test... |
| 189 |
|
public void getComponentInstanceWithNoHintWhenProgrammingRights() throws Exception |
| 190 |
|
{ |
| 191 |
1 |
when(this.dab.hasProgrammingRights()).thenReturn(true); |
| 192 |
|
|
| 193 |
1 |
SomeRole instance = mock(SomeRole.class); |
| 194 |
1 |
when(this.contextComponentManager.getInstance(SomeRole.class)).thenReturn(instance); |
| 195 |
|
|
| 196 |
1 |
assertSame(instance, this.mocker.getComponentUnderTest().getInstance(SomeRole.class)); |
| 197 |
|
} |
| 198 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 199 |
1 |
@Test... |
| 200 |
|
public void getComponentInstanceWithHintWhenNoProgrammingRights() throws Exception |
| 201 |
|
{ |
| 202 |
1 |
when(this.dab.hasProgrammingRights()).thenReturn(false); |
| 203 |
|
|
| 204 |
1 |
assertNull(this.mocker.getComponentUnderTest().getInstance(SomeRole.class, "hint")); |
| 205 |
|
|
| 206 |
1 |
verify(this.contextComponentManager, never()).getInstance(SomeRole.class, "hint"); |
| 207 |
|
} |
| 208 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
| 209 |
1 |
@Test... |
| 210 |
|
public void getComponentInstanceWithHintWhenProgrammingRights() throws Exception |
| 211 |
|
{ |
| 212 |
1 |
when(this.dab.hasProgrammingRights()).thenReturn(true); |
| 213 |
|
|
| 214 |
1 |
SomeRole instance = mock(SomeRole.class); |
| 215 |
1 |
when(this.contextComponentManager.getInstance(SomeRole.class, "hint")).thenReturn(instance); |
| 216 |
|
|
| 217 |
1 |
assertSame(instance, this.mocker.getComponentUnderTest().getInstance(SomeRole.class, "hint")); |
| 218 |
|
} |
| 219 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
| 220 |
1 |
@Test... |
| 221 |
|
public void getComponentInstanceWhenComponentDoesntExist() throws Exception |
| 222 |
|
{ |
| 223 |
1 |
when(this.dab.hasProgrammingRights()).thenReturn(true); |
| 224 |
1 |
when(this.contextComponentManager.getInstance(SomeRole.class)).thenThrow(new ComponentLookupException("error")); |
| 225 |
1 |
ExecutionContext context = new ExecutionContext(); |
| 226 |
1 |
when(this.execution.getContext()).thenReturn(context); |
| 227 |
|
|
| 228 |
1 |
assertNull(this.mocker.getComponentUnderTest().getInstance(SomeRole.class)); |
| 229 |
1 |
assertEquals("error", this.mocker.getComponentUnderTest().getLastError().getMessage()); |
| 230 |
|
} |
| 231 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
| 232 |
1 |
@Test... |
| 233 |
|
public void getComponentInstanceWithHintWhenComponentDoesntExist() throws Exception |
| 234 |
|
{ |
| 235 |
1 |
when(this.dab.hasProgrammingRights()).thenReturn(true); |
| 236 |
1 |
when(this.contextComponentManager.getInstance(SomeRole.class, "hint")).thenThrow( |
| 237 |
|
new ComponentLookupException("error")); |
| 238 |
1 |
ExecutionContext context = new ExecutionContext(); |
| 239 |
1 |
when(this.execution.getContext()).thenReturn(context); |
| 240 |
|
|
| 241 |
1 |
assertNull(this.mocker.getComponentUnderTest().getInstance(SomeRole.class, "hint")); |
| 242 |
1 |
assertEquals("error", this.mocker.getComponentUnderTest().getLastError().getMessage()); |
| 243 |
|
} |
| 244 |
|
} |