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

File ProviderTest.java

 

Code metrics

0
31
7
8
186
133
8
0.26
4.43
0.88
1.14

Classes

Class Line # Actions
ProviderTest 49 26 0% 3 1
0.9642857396.4%
ProviderTest.TestComponentRole 52 0 - 0 0
-1.0 -
ProviderTest.TestComponentWithProviders 59 0 - 0 0
-1.0 -
ProviderTest.TestProvider1 78 1 0% 1 0
1.0100%
ProviderTest.TestProvider12 88 1 0% 1 0
1.0100%
ProviderTest.TestProvider2 97 1 0% 1 0
1.0100%
ProviderTest.TestComponentWithProviderInException 109 0 - 0 0
-1.0 -
ProviderTest.TestProviderWithExceptionInInitialize 117 2 0% 2 2
0.550%
 

Contributing tests

This file is covered by 2 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;
21   
22    import java.util.List;
23    import java.util.Map;
24   
25    import javax.inject.Inject;
26    import javax.inject.Named;
27    import javax.inject.Provider;
28    import javax.inject.Singleton;
29   
30    import org.junit.Assert;
31    import org.junit.Test;
32    import org.xwiki.component.annotation.Component;
33    import org.xwiki.component.annotation.ComponentRole;
34    import org.xwiki.component.descriptor.DefaultComponentDescriptor;
35    import org.xwiki.component.embed.EmbeddableComponentManager;
36    import org.xwiki.component.embed.EmbeddableComponentManagerTest;
37    import org.xwiki.component.embed.EmbeddableComponentManagerTest.Role;
38    import org.xwiki.component.embed.EmbeddableComponentManagerTest.RoleImpl;
39    import org.xwiki.component.manager.ComponentLookupException;
40    import org.xwiki.component.manager.ComponentRepositoryException;
41    import org.xwiki.component.phase.Initializable;
42    import org.xwiki.component.phase.InitializationException;
43   
44    /**
45    * Validate loading and injection of Providers in a real use case.
46    *
47    * @version $Id: 92fafeee4bb49d9cf35eb3f5b52b3d8e493940dc $
48    */
 
49    public class ProviderTest
50    {
51    @ComponentRole
 
52    public static interface TestComponentRole
53    {
54   
55    }
56   
57    @Component
58    @Singleton
 
59    public static class TestComponentWithProviders implements TestComponentRole
60    {
61    @Inject
62    public Provider<String> provider1;
63   
64    @Inject
65    @Named("another")
66    public Provider<String> provider12;
67   
68    @Inject
69    public Provider<Integer> provider2;
70   
71    @Inject
72    public Provider<List<EmbeddableComponentManagerTest.Role>> providerList;
73   
74    @Inject
75    public Provider<Map<String, EmbeddableComponentManagerTest.Role>> providerMap;
76    }
77   
 
78    public static class TestProvider1 implements Provider<String>
79    {
 
80  1 toggle @Override
81    public String get()
82    {
83  1 return "value";
84    }
85    }
86   
87    @Named("another")
 
88    public static class TestProvider12 implements Provider<String>
89    {
 
90  1 toggle @Override
91    public String get()
92    {
93  1 return "another value";
94    }
95    }
96   
 
97    public static class TestProvider2 implements Provider<Integer>
98    {
 
99  1 toggle @Override
100    public Integer get()
101    {
102  1 return 1;
103    }
104    }
105   
106    @Component
107    @Named("exception")
108    @Singleton
 
109    public static class TestComponentWithProviderInException implements TestComponentRole
110    {
111    @Inject
112    @Named("exception")
113    public Provider<String> providerWithExceptionInInitialize;
114    }
115   
116    @Named("exception")
 
117    public static class TestProviderWithExceptionInInitialize implements Provider<String>, Initializable
118    {
 
119  1 toggle @Override
120    public void initialize() throws InitializationException
121    {
122  1 throw new InitializationException("Some error in init");
123    }
124   
 
125  0 toggle @Override
126    public String get()
127    {
128  0 throw new RuntimeException("should not be called!");
129    }
130    }
131   
 
132  1 toggle @Test
133    public void loadAndInjectProviders() throws ComponentLookupException, ComponentRepositoryException
134    {
135  1 EmbeddableComponentManager cm = new EmbeddableComponentManager();
136   
137    // Register components for the list and map
138  1 DefaultComponentDescriptor<Role> cd1 = new DefaultComponentDescriptor<Role>();
139  1 cd1.setRole(Role.class);
140  1 cd1.setRoleHint("hint1");
141  1 cd1.setImplementation(RoleImpl.class);
142  1 cm.registerComponent(cd1);
143  1 DefaultComponentDescriptor<Role> cd2 = new DefaultComponentDescriptor<Role>();
144  1 cd2.setRole(Role.class);
145  1 cd2.setRoleHint("hint2");
146  1 cd2.setImplementation(RoleImpl.class);
147  1 cm.registerComponent(cd2);
148   
149    // Initialize
150  1 cm.initialize(getClass().getClassLoader());
151   
152  1 TestComponentWithProviders component = cm.getInstance(TestComponentRole.class);
153   
154  1 Assert.assertEquals("value", component.provider1.get());
155  1 Assert.assertEquals("another value", component.provider12.get());
156  1 Assert.assertEquals(Integer.valueOf(1), component.provider2.get());
157   
158  1 Assert.assertEquals(2, component.providerList.get().size());
159  1 Assert.assertEquals(2, component.providerMap.get().size());
160    }
161   
162    /**
163    * Verify that an exception is raised when a Provider implementing {@link Initializable} fails to initialize.
164    */
 
165  1 toggle @Test
166    public void loadAndInjectProviderWhenExceptionInInitialize() throws Exception
167    {
168  1 EmbeddableComponentManager cm = new EmbeddableComponentManager();
169  1 cm.initialize(getClass().getClassLoader());
170   
171  1 try {
172  1 cm.getInstance(TestComponentRole.class, "exception");
173  0 Assert.fail("Should have thrown an exception");
174    } catch (ComponentLookupException expected) {
175  1 Assert.assertEquals("Failed to lookup component "
176    + "[org.xwiki.component.ProviderTest$TestComponentWithProviderInException] identified by "
177    + "[role = [interface org.xwiki.component.ProviderTest$TestComponentRole] hint = [exception]]",
178    expected.getMessage());
179  1 Assert.assertEquals("Failed to lookup component "
180    + "[org.xwiki.component.ProviderTest$TestProviderWithExceptionInInitialize] identified by "
181    + "[role = [javax.inject.Provider<java.lang.String>] hint = [exception]]",
182    expected.getCause().getMessage());
183  1 Assert.assertEquals("Some error in init", expected.getCause().getCause().getMessage());
184    }
185    }
186    }