1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.component.annotation

File ComponentDescriptorFactoryTest.java

 

Code metrics

0
61
6
12
266
173
6
0.1
10.17
0.5
1

Classes

Class Line # Actions
ComponentDescriptorFactoryTest 44 61 0% 6 0
1.0100%
ComponentDescriptorFactoryTest.NonGenericFieldRole 47 0 - 0 0
-1.0 -
ComponentDescriptorFactoryTest.GenericFieldRole 52 0 - 0 0
-1.0 -
ComponentDescriptorFactoryTest.FieldroleImpl 58 0 - 0 0
-1.0 -
ComponentDescriptorFactoryTest.SpecialFieldRoleImpl 64 0 - 0 0
-1.0 -
ComponentDescriptorFactoryTest.NonGenericRole 69 0 - 0 0
-1.0 -
ComponentDescriptorFactoryTest.ExtendedRole 74 0 - 0 0
-1.0 -
ComponentDescriptorFactoryTest.RoleImpl 80 0 - 0 0
-1.0 -
ComponentDescriptorFactoryTest.SuperRoleImpl 110 0 - 0 0
-1.0 -
ComponentDescriptorFactoryTest.MultipleRolesImpl 122 0 - 0 0
-1.0 -
ComponentDescriptorFactoryTest.SingletonImpl 128 0 - 0 0
-1.0 -
ComponentDescriptorFactoryTest.SpecialImpl 135 0 - 0 0
-1.0 -
 

Contributing tests

This file is covered by 5 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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    * Unit tests for {@link ComponentDescriptorFactory}.
40    *
41    * @version $Id: 405a024f6671357d7a2db657cb10736f8d678ffb $
42    * @since 1.8.1
43    */
 
44    public class ComponentDescriptorFactoryTest
45    {
46    @ComponentRole
 
47    public interface NonGenericFieldRole<T>
48    {
49    }
50   
51    @Role
 
52    public interface GenericFieldRole<T>
53    {
54    }
55   
56    @Component(staticRegistration = false)
57    @Singleton
 
58    public class FieldroleImpl implements NonGenericFieldRole<String>
59    {
60    }
61   
62    @Component(value = "special", staticRegistration = false)
63    @Singleton
 
64    public class SpecialFieldRoleImpl implements NonGenericFieldRole<String>
65    {
66    }
67   
68    @ComponentRole
 
69    public interface NonGenericRole
70    {
71    }
72   
73    @ComponentRole
 
74    public interface ExtendedRole extends NonGenericRole
75    {
76    }
77   
78    @Component(staticRegistration = false)
79    @Singleton
 
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    * Inject all implementation of the FieldRole role.
97    */
98    @Inject
99    private List<NonGenericFieldRole<String>> roles;
100   
101    /**
102    * Inject all implementation of the FieldRole role.
103    */
104    @Inject
105    private Map<String, NonGenericFieldRole<String>> mapRoles;
106    }
107   
108    @Component(staticRegistration = false)
109    @Singleton
 
110    public class SuperRoleImpl extends RoleImpl
111    {
112    @Inject
113    @Named("other")
114    private NonGenericFieldRole<String> fieldRole;
115    }
116   
117    /**
118    * Test that we can have a component implementing several roles.
119    */
120    @Component(hints = { "hint1", "hint2" }, staticRegistration = false)
121    @Singleton
 
122    public class MultipleRolesImpl implements NonGenericRole
123    {
124    }
125   
126    @Component(staticRegistration = false)
127    @Singleton
 
128    public class SingletonImpl implements NonGenericRole
129    {
130    }
131   
132    @Component(staticRegistration = false)
133    @Named("special")
134    @Singleton
 
135    public class SpecialImpl implements NonGenericRole
136    {
137    }
138   
 
139  1 toggle @Test
140    public void testCreateComponentDescriptor()
141    {
142  1 assertComponentDescriptor(RoleImpl.class, "default");
143    }
144   
145    /**
146    * Verify that we get the same result when we use a class that extends another class (i.e. inheritance works).
147    */
 
148  1 toggle @Test
149    public void testCreateComponentDescriptorWhenClassExtension()
150    {
151  1 assertComponentDescriptor(SuperRoleImpl.class, "other");
152    }
153   
 
154  1 toggle @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   
 
166  1 toggle @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   
 
177  1 toggle @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   
 
188  2 toggle 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    // Test the following injection:
207    // @Inject
208    // private NonGenericFieldRole<String> fieldRole;
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    // Test the following injection:
217    // @Inject
218    // @Named("special")
219    // private NonGenericFieldRole<String> specialFieldRole;
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    // Test the following injection:
228    // @Inject
229    // private GenericFieldRole<String> genericFieldRole;
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    // Test the following injection:
239    // @Inject
240    // private GenericFieldRole nonGenericFieldRole;
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    // Test the following injection:
249    // @Inject
250    // private List<NonGenericFieldRole<String>> roles;
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    // Test the following injection:
258    // @Inject
259    // private Map<String, NonGenericFieldRole<String>> mapRoles;
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    }