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