View Javadoc

1   /*
2    * Grapht, an open source dependency injector.
3    * Copyright 2014-2015 various contributors (see CONTRIBUTORS.txt)
4    * Copyright 2010-2014 Regents of the University of Minnesota
5    *
6    * This program is free software; you can redistribute it and/or modify
7    * it under the terms of the GNU Lesser General Public License as
8    * published by the Free Software Foundation; either version 2.1 of the
9    * License, or (at your option) any later version.
10   *
11   * This program is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13   * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14   * details.
15   *
16   * You should have received a copy of the GNU General Public License along with
17   * this program; if not, write to the Free Software Foundation, Inc., 51
18   * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19   */
20  package org.grouplens.grapht.reflect.internal;
21  
22  import com.google.common.collect.Maps;
23  import org.grouplens.grapht.Instantiator;
24  import org.grouplens.grapht.Instantiators;
25  import org.grouplens.grapht.reflect.Desire;
26  import org.grouplens.grapht.reflect.InjectionPoint;
27  import org.grouplens.grapht.reflect.Satisfaction;
28  import org.grouplens.grapht.reflect.internal.types.*;
29  import org.junit.Assert;
30  import org.junit.Before;
31  import org.junit.Ignore;
32  import org.junit.Test;
33  
34  import javax.annotation.Nullable;
35  import javax.inject.Provider;
36  import java.util.Collections;
37  import java.util.HashSet;
38  import java.util.Map;
39  import java.util.Set;
40  
41  public class SatisfactionTest {
42      private InjectionPoint ctorProviderCIP;
43      
44      private Set<InjectionPoint> typeCInjectPoints;
45      private Set<InjectionPoint> providerCInjectPoints;
46      
47      @Before
48      public void setup() throws Exception {
49          ctorProviderCIP = new ConstructorParameterInjectionPoint(ProviderC.class.getConstructor(int.class), 0);
50          
51          typeCInjectPoints = new HashSet<InjectionPoint>();
52          providerCInjectPoints = new HashSet<InjectionPoint>();
53          
54          typeCInjectPoints.add(TypeC.CONSTRUCTOR);
55          typeCInjectPoints.add(TypeC.INTERFACE_A);
56          typeCInjectPoints.add(TypeC.TYPE_A);
57          typeCInjectPoints.add(TypeC.INTERFACE_B);
58          typeCInjectPoints.add(TypeC.TYPE_B);
59          
60          providerCInjectPoints.add(ctorProviderCIP);
61      }
62      
63      private Set<InjectionPoint> getInjectionPoints(Satisfaction s) {
64          Set<InjectionPoint> detected = new HashSet<InjectionPoint>();
65          for (Desire d: s.getDependencies()) {
66              detected.add(d.getInjectionPoint());
67          }
68          return detected;
69      }
70      
71      @Test
72      public void testClassSatisfactionDesires() throws Exception {
73          ClassSatisfaction s = new ClassSatisfaction(TypeC.class);
74          Set<InjectionPoint> d = getInjectionPoints(s);
75          Assert.assertEquals(typeCInjectPoints, d);
76          Assert.assertEquals(TypeC.class, s.getErasedType());
77      }
78      
79      @Test
80      public void testClassSatisfactionProvider() throws Exception {
81          InterfaceA a1 = new TypeA();
82          TypeA a2 = new TypeA();
83          InterfaceB b1 = new TypeB();
84          TypeB b2 = new TypeB();
85  
86          Map<Desire,Instantiator> providers = Maps.newHashMap();
87          providers.put(new ReflectionDesire(TypeC.CONSTRUCTOR), Instantiators.ofInstance(10));
88          providers.put(new ReflectionDesire(TypeC.INTERFACE_A), Instantiators.ofInstance(a1));
89          providers.put(new ReflectionDesire(TypeC.TYPE_A), Instantiators.ofInstance(a2));
90          providers.put(new ReflectionDesire(TypeC.INTERFACE_B), Instantiators.ofInstance(b1));
91          providers.put(new ReflectionDesire(TypeC.TYPE_B), Instantiators.ofInstance(b2));
92          
93          Instantiator provider = new ClassSatisfaction(TypeC.class).makeInstantiator(providers, null);
94          Object o = provider.instantiate();
95          
96          Assert.assertTrue(o instanceof TypeC);
97          
98          TypeC c = (TypeC) o;
99          Assert.assertSame(a1, c.getInterfaceA());
100         Assert.assertSame(a2, c.getTypeA());
101         Assert.assertSame(b1, c.getInterfaceB());
102         Assert.assertSame(b2, c.getTypeB());
103         Assert.assertEquals(10, c.getIntValue());
104     }
105     
106     @Test
107     public void testInstanceSatisfactionDesires() throws Exception {
108         TypeC c = new TypeC(4);
109         InstanceSatisfaction s = new InstanceSatisfaction(c);
110         Set<InjectionPoint> d = getInjectionPoints(s);
111         Assert.assertTrue(d.isEmpty());
112         Assert.assertSame(c, s.getInstance());
113     }
114     
115     @Test
116     public void testInstanceSatisfactionProvider() throws Exception {
117         TypeC c = new TypeC(4);
118 
119         Instantiator p = new InstanceSatisfaction(c).makeInstantiator(Collections.EMPTY_MAP,
120                                                                       null);
121         Assert.assertSame(c, p.instantiate());
122     }
123     
124     @Test
125     public void testProviderClassSatisfactionDesires() throws Exception {
126         ProviderClassSatisfaction s = new ProviderClassSatisfaction(ProviderC.class);
127         Set<InjectionPoint> d = getInjectionPoints(s);
128         Assert.assertEquals(providerCInjectPoints, d);
129         Assert.assertEquals(ProviderC.class, s.getProviderType());
130     }
131     
132     @Test
133     public void testProviderClassSatisfactionProvider() throws Exception {
134         Map<Desire,Instantiator> providers = Maps.newHashMap();
135         providers.put(new ReflectionDesire(ctorProviderCIP), Instantiators.ofInstance(10));
136         Instantiator provider = new ProviderClassSatisfaction(ProviderC.class).makeInstantiator(providers, null);
137         // Assert.assertTrue(provider instanceof ProviderC);
138         
139         TypeC c = (TypeC) provider.instantiate();
140         Assert.assertEquals(10, c.getIntValue());
141         Assert.assertNull(c.getInterfaceA());
142         Assert.assertNull(c.getTypeA());
143         Assert.assertNull(c.getInterfaceB());
144         Assert.assertNull(c.getTypeB());
145     }
146     
147     @Test
148     public void testProviderInstanceSatisfactionDesires() throws Exception {
149         ProviderC c = new ProviderC(4);
150         ProviderInstanceSatisfaction s = new ProviderInstanceSatisfaction(c);
151         Set<InjectionPoint> d = getInjectionPoints(s);
152         Assert.assertTrue(d.isEmpty());
153         Assert.assertSame(c, s.getProvider());
154     }
155     
156     @Test
157     public void testClassSatisfactionEquals() throws Exception {
158         // two variations of the arguments
159         ClassSatisfaction s1 = new ClassSatisfaction(A.class);
160         ClassSatisfaction s2 = new ClassSatisfaction(B.class);
161         
162         Assert.assertEquals(s1, new ClassSatisfaction(A.class));
163         Assert.assertEquals(s2, new ClassSatisfaction(B.class));
164         Assert.assertEquals(s1, s1);
165         Assert.assertEquals(s2, s2);
166         
167         Assert.assertFalse(s1.equals(s2));
168         Assert.assertFalse(s2.equals(s1));
169         Assert.assertFalse(s1.equals(new Object()));
170         
171     }
172     
173     @Test
174     public void testNullSatisfactionEquals() throws Exception {
175         // two variations of the arguments
176         NullSatisfaction s1 = new NullSatisfaction(A.class);
177         NullSatisfaction s2 = new NullSatisfaction(B.class);
178         
179         Assert.assertEquals(s1, new NullSatisfaction(A.class));
180         Assert.assertEquals(s2, new NullSatisfaction(B.class));
181         Assert.assertEquals(s1, s1);
182         Assert.assertEquals(s2, s2);
183         
184         Assert.assertFalse(s1.equals(s2));
185         Assert.assertFalse(s2.equals(s1));
186         Assert.assertFalse(s1.equals(new Object()));
187     }
188     
189     @Test
190     public void testInstanceSEquals() throws Exception {
191         // three variations of the arguments
192         A a1 = new A();
193         A a2 = new A();
194         B b1 = new B();
195         
196         InstanceSatisfaction s1 = new InstanceSatisfaction(a1);
197         InstanceSatisfaction s2 = new InstanceSatisfaction(a2);
198         InstanceSatisfaction s3 = new InstanceSatisfaction(b1);
199         
200         Assert.assertEquals(s1, new InstanceSatisfaction(a1));
201         Assert.assertEquals(s2, new InstanceSatisfaction(a2));
202         Assert.assertEquals(s3, new InstanceSatisfaction(b1));
203         Assert.assertEquals(s1, s1);
204         Assert.assertEquals(s2, s2);
205         Assert.assertEquals(s3, s3);
206         
207         Assert.assertFalse(s1.equals(s2));
208         Assert.assertFalse(s2.equals(s3));
209         Assert.assertFalse(s3.equals(s1));
210         Assert.assertFalse(s1.equals(new Object()));
211     }
212     
213     @Test
214     public void testProviderClassSatisfactionEquals() throws Exception {
215         // two variations of the arguments
216         ProviderClassSatisfaction s1 = new ProviderClassSatisfaction(PA.class);
217         ProviderClassSatisfaction s2 = new ProviderClassSatisfaction(PB.class);
218         
219         Assert.assertEquals(s1, new ProviderClassSatisfaction(PA.class));
220         Assert.assertEquals(s2, new ProviderClassSatisfaction(PB.class));
221         Assert.assertEquals(s1, s1);
222         Assert.assertEquals(s2, s2);
223         
224         Assert.assertFalse(s1.equals(s2));
225         Assert.assertFalse(s2.equals(s1));
226         Assert.assertFalse(s1.equals(new Object()));
227     }
228     
229     @Test
230     public void testProviderInstanceSatisfactionEquals() throws Exception {
231      // three variations of the arguments
232         PA a1 = new PA();
233         PA a2 = new PA();
234         PB b1 = new PB();
235         
236         ProviderInstanceSatisfaction s1 = new ProviderInstanceSatisfaction(a1);
237         ProviderInstanceSatisfaction s2 = new ProviderInstanceSatisfaction(a2);
238         ProviderInstanceSatisfaction s3 = new ProviderInstanceSatisfaction(b1);
239         
240         Assert.assertEquals(s1, new ProviderInstanceSatisfaction(a1));
241         Assert.assertEquals(s2, new ProviderInstanceSatisfaction(a2));
242         Assert.assertEquals(s3, new ProviderInstanceSatisfaction(b1));
243         Assert.assertEquals(s1, s1);
244         Assert.assertEquals(s2, s2);
245         Assert.assertEquals(s3, s3);
246         
247         Assert.assertFalse(s1.equals(s2));
248         Assert.assertFalse(s2.equals(s3));
249         Assert.assertFalse(s3.equals(s1));
250         Assert.assertFalse(s1.equals(new Object()));
251     }
252     
253     private static class InstanceProvider<T> implements Provider<T> {
254         private T instance;
255         
256         public InstanceProvider(T t) {
257             instance = t;
258         }
259         
260         @Override
261         public T get() {
262             return instance;
263         }
264     }
265     
266     public static class A { }
267     
268     public static class B extends A { }
269     
270     public static abstract class C { }
271     
272     public static class D extends C { }
273     
274     public static class PA implements Provider<A> {
275         @Override
276         public A get() {
277             return new B();
278         }
279     }
280     
281     public static class PB implements Provider<B> {
282         @Override
283         public @Nullable B get() {
284             return null;
285         }
286     }
287 }