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

File ComponentAnnotationLoaderTest.java

 

Code metrics

0
55
15
17
322
237
15
0.27
3.67
0.88
1

Classes

Class Line # Actions
ComponentAnnotationLoaderTest 53 52 0% 12 0
1.0100%
ComponentAnnotationLoaderTest.NotGenericRole 60 0 - 0 0
-1.0 -
ComponentAnnotationLoaderTest.ExtendedRole 66 0 - 0 0
-1.0 -
ComponentAnnotationLoaderTest.RoleImpl 72 0 - 0 0
-1.0 -
ComponentAnnotationLoaderTest.SuperRoleImpl 82 0 - 0 0
-1.0 -
ComponentAnnotationLoaderTest.SimpleRole 89 0 - 0 0
-1.0 -
ComponentAnnotationLoaderTest.OverrideRole 95 0 - 0 0
-1.0 -
ComponentAnnotationLoaderTest.DeprecatedSimpleRole 102 0 - 0 0
-1.0 -
ComponentAnnotationLoaderTest.DeprecatedOverrideRole 108 0 - 0 0
-1.0 -
ComponentAnnotationLoaderTest.ProviderImpl 115 1 0% 1 2
0.00%
ComponentAnnotationLoaderTest.GenericRole 125 0 - 0 0
-1.0 -
ComponentAnnotationLoaderTest.GenericComponent 131 0 - 0 0
-1.0 -
ComponentAnnotationLoaderTest.NonGenericComponent 138 0 - 0 0
-1.0 -
ComponentAnnotationLoaderTest.AbstractGenericComponent 142 0 - 0 0
-1.0 -
ComponentAnnotationLoaderTest.ExtendingGenericComponent 148 0 - 0 0
-1.0 -
ComponentAnnotationLoaderTest.ExtendingNonGenericComponent 155 0 - 0 0
-1.0 -
ComponentAnnotationLoaderTest.TestableComponentAnnotationLoader 161 2 0% 2 2
0.550%
 

Contributing tests

This file is covered by 8 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.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    * Unit tests for {@link ComponentAnnotationLoader}.
49    *
50    * @version $Id: fae0d4d135efdd77fee65469610ff40958a2a9ae $
51    * @since 1.8.1
52    */
 
53    public class ComponentAnnotationLoaderTest
54    {
55    @Rule
56    public final JMockRule mockery = new JMockRule();
57   
58    @SuppressWarnings("deprecation")
59    @ComponentRole
 
60    public interface NotGenericRole<T>
61    {
62    }
63   
64    @SuppressWarnings("deprecation")
65    @ComponentRole
 
66    public interface ExtendedRole extends NotGenericRole<String>
67    {
68    }
69   
70    @Component(staticRegistration = false)
71    @Singleton
 
72    public class RoleImpl implements ExtendedRole
73    {
74    }
75   
76    /**
77    * Note that even though it's not necessary we implement Role to ensure that the SuperRoleImpl component will only
78    * be registered once.
79    */
80    @Component(staticRegistration = false)
81    @Singleton
 
82    public class SuperRoleImpl extends RoleImpl implements NotGenericRole<String>
83    {
84    }
85   
86    // Test overrides with priorities (see components.txt file)
87    @Component(value = "test")
88    @Singleton
 
89    public class SimpleRole implements NotGenericRole<String>
90    {
91    }
92   
93    @Component(value = "test")
94    @Singleton
 
95    public class OverrideRole implements NotGenericRole<String>
96    {
97    }
98   
99    // Verify backward compatibility for deprecated component-overrides.txt file
100    @Component(value = "deprecated")
101    @Singleton
 
102    public class DeprecatedSimpleRole implements NotGenericRole<String>
103    {
104    }
105   
106    @Component(value = "deprecated")
107    @Singleton
 
108    public class DeprecatedOverrideRole implements NotGenericRole<String>
109    {
110    }
111   
112    @Component(staticRegistration = false)
113    @Named("customprovider")
114    @Singleton
 
115    public class ProviderImpl implements Provider<NotGenericRole<String>>
116    {
 
117  0 toggle @Override
118    public NotGenericRole<String> get()
119    {
120  0 return new RoleImpl();
121    }
122    }
123   
124    @Role
 
125    public interface GenericRole<T>
126    {
127    }
128   
129    @Component(staticRegistration = false)
130    @Singleton
 
131    public class GenericComponent implements GenericRole<String>
132    {
133    }
134   
135    @Component(staticRegistration = false)
136    @Singleton
137    @SuppressWarnings("rawtypes")
 
138    public class NonGenericComponent implements GenericRole
139    {
140    }
141   
 
142    public abstract class AbstractGenericComponent<V> implements GenericRole<V>
143    {
144    }
145   
146    @Component(staticRegistration = false)
147    @Singleton
 
148    public class ExtendingGenericComponent extends AbstractGenericComponent<String>
149    {
150    }
151   
152    @Component(staticRegistration = false)
153    @Singleton
154    @SuppressWarnings("rawtypes")
 
155    public class ExtendingNonGenericComponent extends AbstractGenericComponent
156    {
157    }
158   
159    private ComponentAnnotationLoader loader;
160   
 
161    private class TestableComponentAnnotationLoader extends ComponentAnnotationLoader
162    {
163    private Logger logger;
164   
 
165  8 toggle TestableComponentAnnotationLoader(Logger logger)
166    {
167  8 this.logger = logger;
168    }
169   
 
170  0 toggle @Override
171    protected Logger getLogger()
172    {
173  0 return this.logger;
174    }
175    }
176   
 
177  8 toggle @Before
178    public void setupLogger() throws Exception
179    {
180    // Note: we don't define any expectation on the Logger since we want to be sure that the tests below don't
181    // generate any logging at all.
182  8 this.loader = new TestableComponentAnnotationLoader(this.mockery.mock(Logger.class));
183    }
184   
 
185  8 toggle @After
186    public void tearDown() throws Exception
187    {
188  8 this.mockery.assertIsSatisfied();
189    }
190   
191    /**
192    * Verify that when there are several component implementations for the same role/hint then the one with the highest
193    * priority wins (ie the smallest integer value).
194    */
 
195  1 toggle @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    // This is the test, we verify that registerComponent() is called for each of the descriptor we're expecting
229    // to be discovered through annotations by the call to initialize() below.
230  1 this.mockery.checking(new Expectations()
231    {
 
232  1 toggle {
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   
 
252  1 toggle @Test
253    public void testFindComponentRoleTypes()
254    {
255  1 assertComponentRoleTypes(RoleImpl.class);
256    }
257   
258    /**
259    * Verify that we get the same result when we use a class that extends another class (i.e. inheritance works).
260    */
 
261  1 toggle @Test
262    public void testFindComponentRoleTypesWhenClassExtension()
263    {
264  1 assertComponentRoleTypes(SuperRoleImpl.class);
265    }
266   
 
267  2 toggle 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   
 
275  1 toggle @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   
 
285  1 toggle @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   
 
295  1 toggle @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   
 
304  1 toggle @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   
 
314  1 toggle @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    }