1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
|
46 |
|
|
47 |
|
@version |
48 |
|
|
|
|
| 96.4% |
Uncovered Elements: 1 (28) |
Complexity: 3 |
Complexity Density: 0.12 |
|
49 |
|
public class ProviderTest |
50 |
|
{ |
51 |
|
@ComponentRole |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
52 |
|
public static interface TestComponentRole |
53 |
|
{ |
54 |
|
|
55 |
|
} |
56 |
|
|
57 |
|
@Component |
58 |
|
@Singleton |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 1 |
|
78 |
|
public static class TestProvider1 implements Provider<String> |
79 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
80 |
1 |
@Override... |
81 |
|
public String get() |
82 |
|
{ |
83 |
1 |
return "value"; |
84 |
|
} |
85 |
|
} |
86 |
|
|
87 |
|
@Named("another") |
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 1 |
|
88 |
|
public static class TestProvider12 implements Provider<String> |
89 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
90 |
1 |
@Override... |
91 |
|
public String get() |
92 |
|
{ |
93 |
1 |
return "another value"; |
94 |
|
} |
95 |
|
} |
96 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 1 |
|
97 |
|
public static class TestProvider2 implements Provider<Integer> |
98 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
99 |
1 |
@Override... |
100 |
|
public Integer get() |
101 |
|
{ |
102 |
1 |
return 1; |
103 |
|
} |
104 |
|
} |
105 |
|
|
106 |
|
@Component |
107 |
|
@Named("exception") |
108 |
|
@Singleton |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
109 |
|
public static class TestComponentWithProviderInException implements TestComponentRole |
110 |
|
{ |
111 |
|
@Inject |
112 |
|
@Named("exception") |
113 |
|
public Provider<String> providerWithExceptionInInitialize; |
114 |
|
} |
115 |
|
|
116 |
|
@Named("exception") |
|
|
| 50% |
Uncovered Elements: 2 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
117 |
|
public static class TestProviderWithExceptionInInitialize implements Provider<String>, Initializable |
118 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
119 |
1 |
@Override... |
120 |
|
public void initialize() throws InitializationException |
121 |
|
{ |
122 |
1 |
throw new InitializationException("Some error in init"); |
123 |
|
} |
124 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
125 |
0 |
@Override... |
126 |
|
public String get() |
127 |
|
{ |
128 |
0 |
throw new RuntimeException("should not be called!"); |
129 |
|
} |
130 |
|
} |
131 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (18) |
Complexity: 1 |
Complexity Density: 0.06 |
1PASS
|
|
132 |
1 |
@Test... |
133 |
|
public void loadAndInjectProviders() throws ComponentLookupException, ComponentRepositoryException |
134 |
|
{ |
135 |
1 |
EmbeddableComponentManager cm = new EmbeddableComponentManager(); |
136 |
|
|
137 |
|
|
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 |
|
|
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 |
|
@link |
164 |
|
|
|
|
| 87.5% |
Uncovered Elements: 1 (8) |
Complexity: 2 |
Complexity Density: 0.25 |
1PASS
|
|
165 |
1 |
@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 |
|
} |