1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.component.annotation; |
21 |
|
|
22 |
|
import java.lang.reflect.Type; |
23 |
|
import java.util.Set; |
24 |
|
|
25 |
|
import javax.inject.Named; |
26 |
|
import javax.inject.Provider; |
27 |
|
import javax.inject.Singleton; |
28 |
|
|
29 |
|
import org.jmock.Expectations; |
30 |
|
import org.junit.After; |
31 |
|
import org.junit.Assert; |
32 |
|
import org.junit.Before; |
33 |
|
import org.junit.Rule; |
34 |
|
import org.junit.Test; |
35 |
|
import org.slf4j.Logger; |
36 |
|
import org.xwiki.component.ProviderTest; |
37 |
|
import org.xwiki.component.descriptor.ComponentDescriptor; |
38 |
|
import org.xwiki.component.internal.ContextComponentManagerProvider; |
39 |
|
import org.xwiki.component.internal.RootComponentManager; |
40 |
|
import org.xwiki.component.internal.embed.EmbeddableComponentManagerFactory; |
41 |
|
import org.xwiki.component.internal.multi.DefaultComponentManagerManager; |
42 |
|
import org.xwiki.component.internal.namespace.DefaultNamespaceValidator; |
43 |
|
import org.xwiki.component.manager.ComponentManager; |
44 |
|
import org.xwiki.component.util.DefaultParameterizedType; |
45 |
|
import org.xwiki.test.jmock.JMockRule; |
46 |
|
|
47 |
|
|
48 |
|
@link |
49 |
|
|
50 |
|
@version |
51 |
|
@since |
52 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (64) |
Complexity: 12 |
Complexity Density: 0.23 |
|
53 |
|
public class ComponentAnnotationLoaderTest |
54 |
|
{ |
55 |
|
@Rule |
56 |
|
public final JMockRule mockery = new JMockRule(); |
57 |
|
|
58 |
|
@SuppressWarnings("deprecation") |
59 |
|
@ComponentRole |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
60 |
|
public interface NotGenericRole<T> |
61 |
|
{ |
62 |
|
} |
63 |
|
|
64 |
|
@SuppressWarnings("deprecation") |
65 |
|
@ComponentRole |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
66 |
|
public interface ExtendedRole extends NotGenericRole<String> |
67 |
|
{ |
68 |
|
} |
69 |
|
|
70 |
|
@Component(staticRegistration = false) |
71 |
|
@Singleton |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
72 |
|
public class RoleImpl implements ExtendedRole |
73 |
|
{ |
74 |
|
} |
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
@Component(staticRegistration = false) |
81 |
|
@Singleton |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
82 |
|
public class SuperRoleImpl extends RoleImpl implements NotGenericRole<String> |
83 |
|
{ |
84 |
|
} |
85 |
|
|
86 |
|
|
87 |
|
@Component(value = "test") |
88 |
|
@Singleton |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
89 |
|
public class SimpleRole implements NotGenericRole<String> |
90 |
|
{ |
91 |
|
} |
92 |
|
|
93 |
|
@Component(value = "test") |
94 |
|
@Singleton |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
95 |
|
public class OverrideRole implements NotGenericRole<String> |
96 |
|
{ |
97 |
|
} |
98 |
|
|
99 |
|
|
100 |
|
@Component(value = "deprecated") |
101 |
|
@Singleton |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
102 |
|
public class DeprecatedSimpleRole implements NotGenericRole<String> |
103 |
|
{ |
104 |
|
} |
105 |
|
|
106 |
|
@Component(value = "deprecated") |
107 |
|
@Singleton |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
108 |
|
public class DeprecatedOverrideRole implements NotGenericRole<String> |
109 |
|
{ |
110 |
|
} |
111 |
|
|
112 |
|
@Component(staticRegistration = false) |
113 |
|
@Named("customprovider") |
114 |
|
@Singleton |
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 1 |
|
115 |
|
public class ProviderImpl implements Provider<NotGenericRole<String>> |
116 |
|
{ |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
117 |
0 |
@Override... |
118 |
|
public NotGenericRole<String> get() |
119 |
|
{ |
120 |
0 |
return new RoleImpl(); |
121 |
|
} |
122 |
|
} |
123 |
|
|
124 |
|
@Role |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
125 |
|
public interface GenericRole<T> |
126 |
|
{ |
127 |
|
} |
128 |
|
|
129 |
|
@Component(staticRegistration = false) |
130 |
|
@Singleton |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
131 |
|
public class GenericComponent implements GenericRole<String> |
132 |
|
{ |
133 |
|
} |
134 |
|
|
135 |
|
@Component(staticRegistration = false) |
136 |
|
@Singleton |
137 |
|
@SuppressWarnings("rawtypes") |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
138 |
|
public class NonGenericComponent implements GenericRole |
139 |
|
{ |
140 |
|
} |
141 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
142 |
|
public abstract class AbstractGenericComponent<V> implements GenericRole<V> |
143 |
|
{ |
144 |
|
} |
145 |
|
|
146 |
|
@Component(staticRegistration = false) |
147 |
|
@Singleton |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
148 |
|
public class ExtendingGenericComponent extends AbstractGenericComponent<String> |
149 |
|
{ |
150 |
|
} |
151 |
|
|
152 |
|
@Component(staticRegistration = false) |
153 |
|
@Singleton |
154 |
|
@SuppressWarnings("rawtypes") |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
155 |
|
public class ExtendingNonGenericComponent extends AbstractGenericComponent |
156 |
|
{ |
157 |
|
} |
158 |
|
|
159 |
|
private ComponentAnnotationLoader loader; |
160 |
|
|
|
|
| 50% |
Uncovered Elements: 2 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
161 |
|
private class TestableComponentAnnotationLoader extends ComponentAnnotationLoader |
162 |
|
{ |
163 |
|
private Logger logger; |
164 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
165 |
8 |
TestableComponentAnnotationLoader(Logger logger)... |
166 |
|
{ |
167 |
8 |
this.logger = logger; |
168 |
|
} |
169 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
170 |
0 |
@Override... |
171 |
|
protected Logger getLogger() |
172 |
|
{ |
173 |
0 |
return this.logger; |
174 |
|
} |
175 |
|
} |
176 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
177 |
8 |
@Before... |
178 |
|
public void setupLogger() throws Exception |
179 |
|
{ |
180 |
|
|
181 |
|
|
182 |
8 |
this.loader = new TestableComponentAnnotationLoader(this.mockery.mock(Logger.class)); |
183 |
|
} |
184 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
185 |
8 |
@After... |
186 |
|
public void tearDown() throws Exception |
187 |
|
{ |
188 |
8 |
this.mockery.assertIsSatisfied(); |
189 |
|
} |
190 |
|
|
191 |
|
|
192 |
|
|
193 |
|
|
194 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 1 |
Complexity Density: 0.06 |
1PASS
|
|
195 |
1 |
@Test... |
196 |
|
@SuppressWarnings({ "rawtypes", "unchecked" }) |
197 |
|
public void testPriorities() throws Exception |
198 |
|
{ |
199 |
1 |
final ComponentManager mockManager = this.mockery.mock(ComponentManager.class); |
200 |
|
|
201 |
1 |
final ComponentDescriptor descriptor1 = |
202 |
|
this.loader.getComponentsDescriptors(DeprecatedOverrideRole.class).get(0); |
203 |
1 |
final ComponentDescriptor descriptor2 = |
204 |
|
this.loader.getComponentsDescriptors(RootComponentManager.class).get(0); |
205 |
1 |
final ComponentDescriptor descriptor3 = this.loader.getComponentsDescriptors(OverrideRole.class).get(0); |
206 |
1 |
final ComponentDescriptor descriptor4 = |
207 |
|
this.loader.getComponentsDescriptors(EmbeddableComponentManagerFactory.class).get(0); |
208 |
1 |
final ComponentDescriptor descriptor5 = |
209 |
|
this.loader.getComponentsDescriptors(DefaultComponentManagerManager.class).get(0); |
210 |
1 |
final ComponentDescriptor descriptor6 = |
211 |
|
this.loader.getComponentsDescriptors(ContextComponentManagerProvider.class).get(0); |
212 |
1 |
final ComponentDescriptor descriptor7 = |
213 |
|
this.loader.getComponentsDescriptors(DefaultNamespaceValidator.class).get(0); |
214 |
|
|
215 |
1 |
final ComponentDescriptor descriptor8 = |
216 |
|
this.loader.getComponentsDescriptors(ProviderTest.TestProvider1.class).get(0); |
217 |
1 |
final ComponentDescriptor descriptor9 = |
218 |
|
this.loader.getComponentsDescriptors(ProviderTest.TestProvider12.class).get(0); |
219 |
1 |
final ComponentDescriptor descriptor10 = |
220 |
|
this.loader.getComponentsDescriptors(ProviderTest.TestProvider2.class).get(0); |
221 |
1 |
final ComponentDescriptor descriptor11 = |
222 |
|
this.loader.getComponentsDescriptors(ProviderTest.TestComponentWithProviders.class).get(0); |
223 |
1 |
final ComponentDescriptor descriptor12 = |
224 |
|
this.loader.getComponentsDescriptors(ProviderTest.TestProviderWithExceptionInInitialize.class).get(0); |
225 |
1 |
final ComponentDescriptor descriptor13 = |
226 |
|
this.loader.getComponentsDescriptors(ProviderTest.TestComponentWithProviderInException.class).get(0); |
227 |
|
|
228 |
|
|
229 |
|
|
230 |
1 |
this.mockery.checking(new Expectations() |
231 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
|
232 |
1 |
{... |
233 |
1 |
oneOf(mockManager).registerComponent(descriptor1); |
234 |
1 |
oneOf(mockManager).registerComponent(descriptor2); |
235 |
1 |
oneOf(mockManager).registerComponent(descriptor3); |
236 |
1 |
oneOf(mockManager).registerComponent(descriptor4); |
237 |
1 |
oneOf(mockManager).registerComponent(descriptor5); |
238 |
1 |
oneOf(mockManager).registerComponent(descriptor6); |
239 |
1 |
oneOf(mockManager).registerComponent(descriptor7); |
240 |
1 |
oneOf(mockManager).registerComponent(descriptor8); |
241 |
1 |
oneOf(mockManager).registerComponent(descriptor9); |
242 |
1 |
oneOf(mockManager).registerComponent(descriptor10); |
243 |
1 |
oneOf(mockManager).registerComponent(descriptor11); |
244 |
1 |
oneOf(mockManager).registerComponent(descriptor12); |
245 |
1 |
oneOf(mockManager).registerComponent(descriptor13); |
246 |
|
} |
247 |
|
}); |
248 |
|
|
249 |
1 |
this.loader.initialize(mockManager, this.getClass().getClassLoader()); |
250 |
|
} |
251 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
252 |
1 |
@Test... |
253 |
|
public void testFindComponentRoleTypes() |
254 |
|
{ |
255 |
1 |
assertComponentRoleTypes(RoleImpl.class); |
256 |
|
} |
257 |
|
|
258 |
|
|
259 |
|
|
260 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
261 |
1 |
@Test... |
262 |
|
public void testFindComponentRoleTypesWhenClassExtension() |
263 |
|
{ |
264 |
1 |
assertComponentRoleTypes(SuperRoleImpl.class); |
265 |
|
} |
266 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
267 |
2 |
private void assertComponentRoleTypes(Class<?> componentClass)... |
268 |
|
{ |
269 |
2 |
Set<Type> type = this.loader.findComponentRoleTypes(componentClass); |
270 |
2 |
Assert.assertEquals(2, type.size()); |
271 |
2 |
Assert.assertTrue(type.contains(NotGenericRole.class)); |
272 |
2 |
Assert.assertTrue(type.contains(ExtendedRole.class)); |
273 |
|
} |
274 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
275 |
1 |
@Test... |
276 |
|
public void testFindComponentRoleTypesForProvider() |
277 |
|
{ |
278 |
1 |
Set<Type> types = this.loader.findComponentRoleTypes(ProviderImpl.class); |
279 |
|
|
280 |
1 |
Assert.assertEquals(1, types.size()); |
281 |
1 |
Assert.assertEquals(new DefaultParameterizedType(null, Provider.class, new DefaultParameterizedType( |
282 |
|
ComponentAnnotationLoaderTest.class, NotGenericRole.class, String.class)), types.iterator().next()); |
283 |
|
} |
284 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
285 |
1 |
@Test... |
286 |
|
public void testFindComponentRoleTypesWithGenericRole() |
287 |
|
{ |
288 |
1 |
Set<Type> types = this.loader.findComponentRoleTypes(GenericComponent.class); |
289 |
|
|
290 |
1 |
Assert.assertEquals(1, types.size()); |
291 |
1 |
Assert.assertEquals(new DefaultParameterizedType(ComponentAnnotationLoaderTest.class, GenericRole.class, |
292 |
|
String.class), types.iterator().next()); |
293 |
|
} |
294 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
295 |
1 |
@Test... |
296 |
|
public void testFindComponentRoleTypesWithGenericRoleAndNonGenericComponent() |
297 |
|
{ |
298 |
1 |
Set<Type> types = this.loader.findComponentRoleTypes(NonGenericComponent.class); |
299 |
|
|
300 |
1 |
Assert.assertEquals(1, types.size()); |
301 |
1 |
Assert.assertEquals(GenericRole.class, types.iterator().next()); |
302 |
|
} |
303 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
304 |
1 |
@Test... |
305 |
|
public void testFindComponentRoleTypesWithExtendingGenericRole() |
306 |
|
{ |
307 |
1 |
Set<Type> types = this.loader.findComponentRoleTypes(ExtendingGenericComponent.class); |
308 |
|
|
309 |
1 |
Assert.assertEquals(1, types.size()); |
310 |
1 |
Assert.assertEquals(new DefaultParameterizedType(ComponentAnnotationLoaderTest.class, GenericRole.class, |
311 |
|
String.class), types.iterator().next()); |
312 |
|
} |
313 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
314 |
1 |
@Test... |
315 |
|
public void testFindComponentRoleTypesWithExtendingNonGenericRole() |
316 |
|
{ |
317 |
1 |
Set<Type> types = this.loader.findComponentRoleTypes(ExtendingNonGenericComponent.class); |
318 |
|
|
319 |
1 |
Assert.assertEquals(1, types.size()); |
320 |
1 |
Assert.assertEquals(GenericRole.class, types.iterator().next()); |
321 |
|
} |
322 |
|
} |