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

File GuiceCompatibilityTest.java

 

Code metrics

0
18
7
10
172
113
7
0.39
2.57
0.7
1

Classes

Class Line # Actions
GuiceCompatibilityTest 49 8 0% 1 0
1.0100%
GuiceCompatibilityTest.FieldRole 52 0 - 0 0
-1.0 -
GuiceCompatibilityTest.FieldRoleImpl1 59 0 - 0 0
-1.0 -
GuiceCompatibilityTest.FieldRoleImpl2 65 0 - 0 0
-1.0 -
GuiceCompatibilityTest.RoleClass 70 0 - 0 0
-1.0 -
GuiceCompatibilityTest.ProviderImpl 76 1 0% 1 0
1.0100%
GuiceCompatibilityTest.GenericFieldRole 86 0 - 0 0
-1.0 -
GuiceCompatibilityTest.GenericFieldRoleImpl 92 0 - 0 0
-1.0 -
GuiceCompatibilityTest.RoleImpl 99 4 0% 4 0
1.0100%
GuiceCompatibilityTest.TestModule 139 5 0% 1 0
1.0100%
 

Contributing tests

This file is covered by 1 test. .

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.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    * Verifies that XWiki components using JSR330 annotation can be used with Guice, thus demonstrating that we're kind of
44    * spec compliant (even if we do not implement the whole JSR330 specification).
45    *
46    * @version $Id: 7224e1fd076127c05aac8bdba9cd60a21c311577 $
47    * @since 3.1M1
48    */
 
49    public class GuiceCompatibilityTest
50    {
51    @Role
 
52    public interface FieldRole
53    {
54    }
55   
56    @Component(staticRegistration = false)
57    @Named("name")
58    @Singleton
 
59    public static class FieldRoleImpl1 implements FieldRole
60    {
61    }
62   
63    @Component(staticRegistration = false)
64    @Singleton
 
65    public static class FieldRoleImpl2 implements FieldRole
66    {
67    }
68   
69    @Role
 
70    public interface RoleClass
71    {
72    }
73   
74    @Component(staticRegistration = false)
75    @Singleton
 
76    public static class ProviderImpl implements Provider<FieldRole>
77    {
 
78  1 toggle @Override
79    public FieldRole get()
80    {
81  1 return new FieldRoleImpl2();
82    }
83    }
84   
85    @Role
 
86    public interface GenericFieldRole<T>
87    {
88    }
89   
90    @Component(staticRegistration = false)
91    @Singleton
 
92    public static class GenericFieldRoleImpl<String> implements GenericFieldRole<String>
93    {
94    }
95   
96    @Component(staticRegistration = false)
97    @Named("whatever")
98    @Singleton
 
99    public static class RoleImpl implements RoleClass
100    {
101    // Test a named component injection
102    @Inject
103    @Named("name")
104    private FieldRole fieldRole1;
105   
106    // Test a Provider injection
107    @Inject
108    private Provider<FieldRole> fieldRoleProvider;
109   
110    // Test a generics component injection
111    @Inject
112    private GenericFieldRole<String> genericFieldRole;
113   
114    // Test a Logger injection
115    @Inject
116    private Logger logger;
117   
 
118  3 toggle public FieldRole getFieldRole1()
119    {
120  3 return this.fieldRole1;
121    }
122   
 
123  1 toggle public FieldRole getFieldRole2()
124    {
125  1 return this.fieldRoleProvider.get();
126    }
127   
 
128  1 toggle public GenericFieldRole<String> getGenericFieldRole()
129    {
130  1 return this.genericFieldRole;
131    }
132   
 
133  1 toggle public Logger getLogger()
134    {
135  1 return this.logger;
136    }
137    }
138   
 
139    public class TestModule extends AbstractModule
140    {
 
141  1 toggle @Override
142    protected void configure()
143    {
144    // Special binding for SLJ4 Logger injection
145  1 bindListener(any(), new Slf4jInjectionTypeListener());
146   
147    // Since XWiki uses the @Inject annotation to inject Loggers too we need to resolve that interface to
148    // an implementation for Guice, even though it's going to be overwritten by the Slf4jInjectionTypeListener!
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   
 
157  1 toggle @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    // Test that FieldRole impl is a singleton
169  1 RoleImpl impl2 = injector.getInstance(RoleImpl.class);
170  1 Assert.assertSame(impl1.getFieldRole1(), impl2.getFieldRole1());
171    }
172    }