1 package com.google.inject.spi;
2 
3 import com.google.inject.AbstractModule;
4 import com.google.inject.Asserts;
5 import com.google.inject.CreationException;
6 import com.google.inject.Guice;
7 import com.google.inject.Inject;
8 import com.google.inject.Injector;
9 import com.google.inject.Key;
10 import com.google.inject.Provider;
11 import com.google.inject.Stage;
12 import com.google.inject.spi.Toolable;
13 
14 import junit.framework.TestCase;
15 
16 import java.util.Collection;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Set;
20 
21 public class ToolStageInjectorTest extends TestCase {
22 
23   @Override
setUp()24   protected void setUp() throws Exception {
25     Foo.s = null;
26     Foo.sm = null;
27   }
28 
testToolStageInjectorRestrictions()29   public void testToolStageInjectorRestrictions() {
30     Injector injector = Guice.createInjector(Stage.TOOL);
31     try {
32       injector.injectMembers(new Object());
33       fail("Non-SPI Injector methods must throw an exception in the TOOL stage.");
34     } catch (UnsupportedOperationException expected) {
35     }
36 
37     try {
38       injector.getInstance(Injector.class);
39       fail("Non-SPI Injector methods must throw an exception in the TOOL stage.");
40     } catch (UnsupportedOperationException expected) {
41     }
42 
43     try {
44       injector.getInstance(Key.get(Injector.class));
45       fail("Non-SPI Injector methods must throw an exception in the TOOL stage.");
46     } catch (UnsupportedOperationException expected) {
47     }
48 
49     try {
50       injector.getProvider(Injector.class);
51       fail("Non-SPI Injector methods must throw an exception in the TOOL stage.");
52     } catch (UnsupportedOperationException expected) {
53     }
54 
55     try {
56       injector.getProvider(Key.get(Injector.class));
57       fail("Non-SPI Injector methods must throw an exception in the TOOL stage.");
58     } catch (UnsupportedOperationException expected) {
59     }
60   }
61 
testToolStageDoesntInjectInstances()62   public void testToolStageDoesntInjectInstances() {
63     final Foo foo = new Foo();
64     Guice.createInjector(Stage.TOOL, new AbstractModule() {
65       @Override
66       protected void configure() {
67         requestStaticInjection(Foo.class);
68         requestInjection(foo);
69       }
70     });
71     assertNull(Foo.s);
72     assertNull(Foo.sm);
73     assertNull(foo.f);
74     assertNull(foo.m);
75   }
76 
testToolStageDoesntInjectProviders()77   public void testToolStageDoesntInjectProviders() {
78     final Foo foo = new Foo();
79     Guice.createInjector(Stage.TOOL, new AbstractModule() {
80       @Override
81       protected void configure() {
82         requestStaticInjection(Foo.class);
83         bind(Object.class).toProvider(foo);
84       }
85     });
86     assertNull(Foo.s);
87     assertNull(Foo.sm);
88     assertNull(foo.f);
89     assertNull(foo.m);
90   }
91 
testToolStageWarnsOfMissingObjectGraph()92   public void testToolStageWarnsOfMissingObjectGraph() {
93     final Bar bar = new Bar();
94     try {
95       Guice.createInjector(Stage.TOOL, new AbstractModule() {
96         @Override
97         protected void configure() {
98           requestStaticInjection(Bar.class);
99           requestInjection(bar);
100         }
101       });
102       fail("expected exception");
103     } catch(CreationException expected) {
104       Asserts.assertContains(expected.toString(), "No implementation for java.util.Collection was bound.",
105           "No implementation for java.util.Map was bound.",
106           "No implementation for java.util.List was bound.",
107           "No implementation for java.util.Set was bound.");
108     }
109   }
110 
testToolStageInjectsTooledMethods()111   public void testToolStageInjectsTooledMethods() {
112     final Tooled tooled = new Tooled();
113     Guice.createInjector(Stage.TOOL, new AbstractModule() {
114       @Override
115       protected void configure() {
116         requestStaticInjection(Tooled.class);
117         bind(Object.class).toProvider(tooled);
118       }
119     });
120     assertNull(Tooled.s);
121     assertNotNull(Tooled.sm);
122     assertNull(tooled.f);
123     assertNotNull(tooled.m);
124   }
125 
126   @SuppressWarnings("unchecked")
127   private static class Bar {
128     @SuppressWarnings("unused") @Inject private static List list;
129     @SuppressWarnings("unused") @Inject private Set set;
method(Collection c)130     @SuppressWarnings("unused") @Inject void method(Collection c) {}
staticMethod(Map map)131     @SuppressWarnings("unused") @Inject static void staticMethod(Map map) {}
132   }
133 
134   private static class Foo implements Provider<Object> {
135     @Inject private static S s;
136     @Inject private F f;
137     private M m;
method(M m)138     @SuppressWarnings("unused") @Inject void method(M m) { this.m = m; }
139     private static SM sm;
staticMethod(SM sm)140     @SuppressWarnings("unused") @Inject static void staticMethod(SM sm) { Tooled.sm = sm; }
141 
get()142     public Object get() {
143       return null;
144     }
145   }
146 
147   private static class Tooled implements Provider<Object> {
148     @Inject private static S s;
149     @Inject private F f;
150     private M m;
method(M m)151     @Toolable @SuppressWarnings("unused") @Inject void method(M m) { this.m = m; }
152     private static SM sm;
staticMethod(SM sm)153     @Toolable @SuppressWarnings("unused") @Inject static void staticMethod(SM sm) { Tooled.sm = sm; }
154 
get()155     public Object get() {
156       return null;
157     }
158   }
159 
160   private static class S {}
161   private static class F {}
162   private static class M {}
163   private static class SM {}
164 
165 }
166