1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.component.guice; |
21 |
|
|
22 |
|
import javax.inject.Inject; |
23 |
|
import javax.inject.Named; |
24 |
|
import javax.inject.Provider; |
25 |
|
import javax.inject.Singleton; |
26 |
|
|
27 |
|
import org.junit.Assert; |
28 |
|
import org.junit.Test; |
29 |
|
import org.slf4j.Logger; |
30 |
|
import org.slf4j.LoggerFactory; |
31 |
|
import org.xwiki.component.annotation.Component; |
32 |
|
import org.xwiki.component.annotation.Role; |
33 |
|
|
34 |
|
import com.google.inject.AbstractModule; |
35 |
|
import com.google.inject.Guice; |
36 |
|
import com.google.inject.Injector; |
37 |
|
import com.google.inject.TypeLiteral; |
38 |
|
import com.google.inject.name.Names; |
39 |
|
|
40 |
|
import static com.google.inject.matcher.Matchers.any; |
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
@version |
47 |
|
@since |
48 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.12 |
|
49 |
|
public class GuiceCompatibilityTest |
50 |
|
{ |
51 |
|
@Role |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
52 |
|
public interface FieldRole |
53 |
|
{ |
54 |
|
} |
55 |
|
|
56 |
|
@Component(staticRegistration = false) |
57 |
|
@Named("name") |
58 |
|
@Singleton |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
59 |
|
public static class FieldRoleImpl1 implements FieldRole |
60 |
|
{ |
61 |
|
} |
62 |
|
|
63 |
|
@Component(staticRegistration = false) |
64 |
|
@Singleton |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
65 |
|
public static class FieldRoleImpl2 implements FieldRole |
66 |
|
{ |
67 |
|
} |
68 |
|
|
69 |
|
@Role |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
70 |
|
public interface RoleClass |
71 |
|
{ |
72 |
|
} |
73 |
|
|
74 |
|
@Component(staticRegistration = false) |
75 |
|
@Singleton |
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 1 |
|
76 |
|
public static class ProviderImpl implements Provider<FieldRole> |
77 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
78 |
1 |
@Override... |
79 |
|
public FieldRole get() |
80 |
|
{ |
81 |
1 |
return new FieldRoleImpl2(); |
82 |
|
} |
83 |
|
} |
84 |
|
|
85 |
|
@Role |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
86 |
|
public interface GenericFieldRole<T> |
87 |
|
{ |
88 |
|
} |
89 |
|
|
90 |
|
@Component(staticRegistration = false) |
91 |
|
@Singleton |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
92 |
|
public static class GenericFieldRoleImpl<String> implements GenericFieldRole<String> |
93 |
|
{ |
94 |
|
} |
95 |
|
|
96 |
|
@Component(staticRegistration = false) |
97 |
|
@Named("whatever") |
98 |
|
@Singleton |
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 4 |
Complexity Density: 1 |
|
99 |
|
public static class RoleImpl implements RoleClass |
100 |
|
{ |
101 |
|
|
102 |
|
@Inject |
103 |
|
@Named("name") |
104 |
|
private FieldRole fieldRole1; |
105 |
|
|
106 |
|
|
107 |
|
@Inject |
108 |
|
private Provider<FieldRole> fieldRoleProvider; |
109 |
|
|
110 |
|
|
111 |
|
@Inject |
112 |
|
private GenericFieldRole<String> genericFieldRole; |
113 |
|
|
114 |
|
|
115 |
|
@Inject |
116 |
|
private Logger logger; |
117 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
118 |
3 |
public FieldRole getFieldRole1()... |
119 |
|
{ |
120 |
3 |
return this.fieldRole1; |
121 |
|
} |
122 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
123 |
1 |
public FieldRole getFieldRole2()... |
124 |
|
{ |
125 |
1 |
return this.fieldRoleProvider.get(); |
126 |
|
} |
127 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
128 |
1 |
public GenericFieldRole<String> getGenericFieldRole()... |
129 |
|
{ |
130 |
1 |
return this.genericFieldRole; |
131 |
|
} |
132 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
133 |
1 |
public Logger getLogger()... |
134 |
|
{ |
135 |
1 |
return this.logger; |
136 |
|
} |
137 |
|
} |
138 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.2 |
|
139 |
|
public class TestModule extends AbstractModule |
140 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
141 |
1 |
@Override... |
142 |
|
protected void configure() |
143 |
|
{ |
144 |
|
|
145 |
1 |
bindListener(any(), new Slf4jInjectionTypeListener()); |
146 |
|
|
147 |
|
|
148 |
|
|
149 |
1 |
bind(Logger.class).toInstance(LoggerFactory.getLogger("Not used!")); |
150 |
|
|
151 |
1 |
bind(FieldRole.class).annotatedWith(Names.named("name")).to(FieldRoleImpl1.class); |
152 |
1 |
bind(FieldRole.class).toProvider(ProviderImpl.class); |
153 |
1 |
bind(new TypeLiteral<GenericFieldRole<String>>(){}).to(new TypeLiteral<GenericFieldRoleImpl<String>>(){}); |
154 |
|
} |
155 |
|
} |
156 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
157 |
1 |
@Test... |
158 |
|
public void testGuice() |
159 |
|
{ |
160 |
1 |
Injector injector = Guice.createInjector(new TestModule()); |
161 |
|
|
162 |
1 |
RoleImpl impl1 = injector.getInstance(RoleImpl.class); |
163 |
1 |
Assert.assertEquals(FieldRoleImpl1.class.getName(), impl1.getFieldRole1().getClass().getName()); |
164 |
1 |
Assert.assertEquals(FieldRoleImpl2.class.getName(), impl1.getFieldRole2().getClass().getName()); |
165 |
1 |
Assert.assertEquals(GenericFieldRoleImpl.class.getName(), impl1.getGenericFieldRole().getClass().getName()); |
166 |
1 |
Assert.assertEquals(RoleImpl.class.getName(), impl1.getLogger().getName()); |
167 |
|
|
168 |
|
|
169 |
1 |
RoleImpl impl2 = injector.getInstance(RoleImpl.class); |
170 |
1 |
Assert.assertSame(impl1.getFieldRole1(), impl2.getFieldRole1()); |
171 |
|
} |
172 |
|
} |