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.util.Collection; |
23 |
|
import java.util.Iterator; |
24 |
|
import java.util.List; |
25 |
|
import java.util.Map; |
26 |
|
|
27 |
|
import javax.inject.Inject; |
28 |
|
import javax.inject.Named; |
29 |
|
import javax.inject.Singleton; |
30 |
|
|
31 |
|
import org.junit.Assert; |
32 |
|
import org.junit.Test; |
33 |
|
import org.xwiki.component.descriptor.ComponentDependency; |
34 |
|
import org.xwiki.component.descriptor.ComponentDescriptor; |
35 |
|
import org.xwiki.component.descriptor.ComponentInstantiationStrategy; |
36 |
|
import org.xwiki.component.util.DefaultParameterizedType; |
37 |
|
|
38 |
|
|
39 |
|
@link |
40 |
|
|
41 |
|
@version |
42 |
|
@since |
43 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (67) |
Complexity: 6 |
Complexity Density: 0.1 |
|
44 |
|
public class ComponentDescriptorFactoryTest |
45 |
|
{ |
46 |
|
@ComponentRole |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
47 |
|
public interface NonGenericFieldRole<T> |
48 |
|
{ |
49 |
|
} |
50 |
|
|
51 |
|
@Role |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
52 |
|
public interface GenericFieldRole<T> |
53 |
|
{ |
54 |
|
} |
55 |
|
|
56 |
|
@Component(staticRegistration = false) |
57 |
|
@Singleton |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
58 |
|
public class FieldroleImpl implements NonGenericFieldRole<String> |
59 |
|
{ |
60 |
|
} |
61 |
|
|
62 |
|
@Component(value = "special", staticRegistration = false) |
63 |
|
@Singleton |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
64 |
|
public class SpecialFieldRoleImpl implements NonGenericFieldRole<String> |
65 |
|
{ |
66 |
|
} |
67 |
|
|
68 |
|
@ComponentRole |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
69 |
|
public interface NonGenericRole |
70 |
|
{ |
71 |
|
} |
72 |
|
|
73 |
|
@ComponentRole |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
74 |
|
public interface ExtendedRole extends NonGenericRole |
75 |
|
{ |
76 |
|
} |
77 |
|
|
78 |
|
@Component(staticRegistration = false) |
79 |
|
@Singleton |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
80 |
|
public class RoleImpl implements ExtendedRole |
81 |
|
{ |
82 |
|
@Inject |
83 |
|
private NonGenericFieldRole<String> fieldRole; |
84 |
|
|
85 |
|
@Inject |
86 |
|
@Named("special") |
87 |
|
private NonGenericFieldRole<String> specialFieldRole; |
88 |
|
|
89 |
|
@Inject |
90 |
|
private GenericFieldRole<String> genericFieldRole; |
91 |
|
|
92 |
|
@Inject |
93 |
|
private GenericFieldRole nonGenericFieldRole; |
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
@Inject |
99 |
|
private List<NonGenericFieldRole<String>> roles; |
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
@Inject |
105 |
|
private Map<String, NonGenericFieldRole<String>> mapRoles; |
106 |
|
} |
107 |
|
|
108 |
|
@Component(staticRegistration = false) |
109 |
|
@Singleton |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
110 |
|
public class SuperRoleImpl extends RoleImpl |
111 |
|
{ |
112 |
|
@Inject |
113 |
|
@Named("other") |
114 |
|
private NonGenericFieldRole<String> fieldRole; |
115 |
|
} |
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
@Component(hints = { "hint1", "hint2" }, staticRegistration = false) |
121 |
|
@Singleton |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
122 |
|
public class MultipleRolesImpl implements NonGenericRole |
123 |
|
{ |
124 |
|
} |
125 |
|
|
126 |
|
@Component(staticRegistration = false) |
127 |
|
@Singleton |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
128 |
|
public class SingletonImpl implements NonGenericRole |
129 |
|
{ |
130 |
|
} |
131 |
|
|
132 |
|
@Component(staticRegistration = false) |
133 |
|
@Named("special") |
134 |
|
@Singleton |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
135 |
|
public class SpecialImpl implements NonGenericRole |
136 |
|
{ |
137 |
|
} |
138 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
139 |
1 |
@Test... |
140 |
|
public void testCreateComponentDescriptor() |
141 |
|
{ |
142 |
1 |
assertComponentDescriptor(RoleImpl.class, "default"); |
143 |
|
} |
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
148 |
1 |
@Test... |
149 |
|
public void testCreateComponentDescriptorWhenClassExtension() |
150 |
|
{ |
151 |
1 |
assertComponentDescriptor(SuperRoleImpl.class, "other"); |
152 |
|
} |
153 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
154 |
1 |
@Test... |
155 |
|
public void testMultipleRolesForComponent() |
156 |
|
{ |
157 |
1 |
ComponentDescriptorFactory factory = new ComponentDescriptorFactory(); |
158 |
1 |
List<ComponentDescriptor> descriptors = |
159 |
|
factory.createComponentDescriptors(MultipleRolesImpl.class, NonGenericRole.class); |
160 |
|
|
161 |
1 |
Assert.assertEquals(2, descriptors.size()); |
162 |
1 |
Assert.assertEquals("hint1", descriptors.get(0).getRoleHint()); |
163 |
1 |
Assert.assertEquals("hint2", descriptors.get(1).getRoleHint()); |
164 |
|
} |
165 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
166 |
1 |
@Test... |
167 |
|
public void testSingletonAnnotationForComponent() |
168 |
|
{ |
169 |
1 |
ComponentDescriptorFactory factory = new ComponentDescriptorFactory(); |
170 |
1 |
List<ComponentDescriptor> descriptors = |
171 |
|
factory.createComponentDescriptors(SingletonImpl.class, NonGenericRole.class); |
172 |
|
|
173 |
1 |
Assert.assertEquals(1, descriptors.size()); |
174 |
1 |
Assert.assertEquals(ComponentInstantiationStrategy.SINGLETON, descriptors.get(0).getInstantiationStrategy()); |
175 |
|
} |
176 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
177 |
1 |
@Test... |
178 |
|
public void testNamedAnnotationForComponent() |
179 |
|
{ |
180 |
1 |
ComponentDescriptorFactory factory = new ComponentDescriptorFactory(); |
181 |
1 |
List<ComponentDescriptor> descriptors = |
182 |
|
factory.createComponentDescriptors(SpecialImpl.class, NonGenericRole.class); |
183 |
|
|
184 |
1 |
Assert.assertEquals(1, descriptors.size()); |
185 |
1 |
Assert.assertEquals("special", descriptors.get(0).getRoleHint()); |
186 |
|
} |
187 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (46) |
Complexity: 1 |
Complexity Density: 0.02 |
|
188 |
2 |
private void assertComponentDescriptor(Class<?> componentClass, String fieldRoleName)... |
189 |
|
{ |
190 |
2 |
ComponentDescriptorFactory factory = new ComponentDescriptorFactory(); |
191 |
2 |
List<ComponentDescriptor> descriptors = factory.createComponentDescriptors(componentClass, ExtendedRole.class); |
192 |
|
|
193 |
2 |
Assert.assertEquals(1, descriptors.size()); |
194 |
2 |
ComponentDescriptor descriptor = descriptors.get(0); |
195 |
|
|
196 |
2 |
Assert.assertSame(componentClass, descriptor.getImplementation()); |
197 |
2 |
Assert.assertSame(ExtendedRole.class, descriptor.getRole()); |
198 |
2 |
Assert.assertSame(ExtendedRole.class, descriptor.getRoleType()); |
199 |
2 |
Assert.assertEquals("default", descriptor.getRoleHint()); |
200 |
2 |
Assert.assertEquals(ComponentInstantiationStrategy.SINGLETON, descriptor.getInstantiationStrategy()); |
201 |
|
|
202 |
2 |
Collection<ComponentDependency<?>> deps = descriptor.getComponentDependencies(); |
203 |
2 |
Assert.assertEquals(6, deps.size()); |
204 |
2 |
Iterator<ComponentDependency<?>> it = deps.iterator(); |
205 |
|
|
206 |
|
|
207 |
|
|
208 |
|
|
209 |
2 |
ComponentDependency dep = it.next(); |
210 |
2 |
Assert.assertSame(NonGenericFieldRole.class, dep.getRole()); |
211 |
2 |
Assert.assertSame(NonGenericFieldRole.class, dep.getRoleType()); |
212 |
2 |
Assert.assertEquals(fieldRoleName, dep.getRoleHint()); |
213 |
2 |
Assert.assertSame(NonGenericFieldRole.class, dep.getMappingType()); |
214 |
2 |
Assert.assertEquals("fieldRole", dep.getName()); |
215 |
|
|
216 |
|
|
217 |
|
|
218 |
|
|
219 |
|
|
220 |
2 |
dep = it.next(); |
221 |
2 |
Assert.assertSame(NonGenericFieldRole.class, dep.getRole()); |
222 |
2 |
Assert.assertSame(NonGenericFieldRole.class, dep.getRoleType()); |
223 |
2 |
Assert.assertEquals("special", dep.getRoleHint()); |
224 |
2 |
Assert.assertSame(NonGenericFieldRole.class, dep.getMappingType()); |
225 |
2 |
Assert.assertEquals("specialFieldRole", dep.getName()); |
226 |
|
|
227 |
|
|
228 |
|
|
229 |
|
|
230 |
2 |
dep = it.next(); |
231 |
2 |
Assert.assertSame(GenericFieldRole.class, dep.getRole()); |
232 |
2 |
Assert.assertEquals(new DefaultParameterizedType(ComponentDescriptorFactoryTest.class, GenericFieldRole.class, |
233 |
|
String.class), dep.getRoleType()); |
234 |
2 |
Assert.assertEquals("default", dep.getRoleHint()); |
235 |
2 |
Assert.assertSame(GenericFieldRole.class, dep.getMappingType()); |
236 |
2 |
Assert.assertEquals("genericFieldRole", dep.getName()); |
237 |
|
|
238 |
|
|
239 |
|
|
240 |
|
|
241 |
2 |
dep = it.next(); |
242 |
2 |
Assert.assertSame(GenericFieldRole.class, dep.getRole()); |
243 |
2 |
Assert.assertEquals(GenericFieldRole.class, dep.getRoleType()); |
244 |
2 |
Assert.assertEquals("default", dep.getRoleHint()); |
245 |
2 |
Assert.assertSame(GenericFieldRole.class, dep.getMappingType()); |
246 |
2 |
Assert.assertEquals("nonGenericFieldRole", dep.getName()); |
247 |
|
|
248 |
|
|
249 |
|
|
250 |
|
|
251 |
2 |
dep = it.next(); |
252 |
2 |
Assert.assertSame(NonGenericFieldRole.class, dep.getRole()); |
253 |
2 |
Assert.assertEquals("default", dep.getRoleHint()); |
254 |
2 |
Assert.assertSame(List.class, dep.getMappingType()); |
255 |
2 |
Assert.assertEquals("roles", dep.getName()); |
256 |
|
|
257 |
|
|
258 |
|
|
259 |
|
|
260 |
2 |
dep = it.next(); |
261 |
2 |
Assert.assertSame(NonGenericFieldRole.class, dep.getRole()); |
262 |
2 |
Assert.assertEquals("default", dep.getRoleHint()); |
263 |
2 |
Assert.assertSame(Map.class, dep.getMappingType()); |
264 |
2 |
Assert.assertEquals("mapRoles", dep.getName()); |
265 |
|
} |
266 |
|
} |