limpbizkit | 47151c2 | 2008-10-15 05:18:48 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2008 Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 17 | package com.google.inject; |
limpbizkit | 47151c2 | 2008-10-15 05:18:48 +0000 | [diff] [blame] | 18 | |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 19 | import static com.google.inject.Asserts.assertContains; |
sberlin | 45ca7f6 | 2011-02-20 00:05:28 +0000 | [diff] [blame] | 20 | |
| 21 | import java.util.ArrayList; |
| 22 | import java.util.Collection; |
| 23 | import java.util.List; |
| 24 | |
sberlin | d9c913a | 2011-06-26 21:02:54 +0000 | [diff] [blame^] | 25 | import com.google.common.collect.ImmutableSet; |
limpbizkit | 47151c2 | 2008-10-15 05:18:48 +0000 | [diff] [blame] | 26 | import com.google.inject.name.Named; |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 27 | import com.google.inject.name.Names; |
limpbizkit | 47151c2 | 2008-10-15 05:18:48 +0000 | [diff] [blame] | 28 | import static com.google.inject.name.Names.named; |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 29 | import com.google.inject.spi.Dependency; |
| 30 | import com.google.inject.spi.ExposedBinding; |
| 31 | import com.google.inject.spi.PrivateElements; |
sberlin | cc17f14 | 2011-02-27 00:02:03 +0000 | [diff] [blame] | 32 | import com.google.inject.util.Types; |
| 33 | |
limpbizkit | 47151c2 | 2008-10-15 05:18:48 +0000 | [diff] [blame] | 34 | import junit.framework.TestCase; |
| 35 | |
| 36 | /** |
| 37 | * @author jessewilson@google.com (Jesse Wilson) |
| 38 | */ |
| 39 | public class PrivateModuleTest extends TestCase { |
| 40 | |
| 41 | public void testBasicUsage() { |
| 42 | Injector injector = Guice.createInjector(new AbstractModule() { |
| 43 | protected void configure() { |
| 44 | bind(String.class).annotatedWith(named("a")).toInstance("public"); |
| 45 | |
| 46 | install(new PrivateModule() { |
crazyboblee | 9119370 | 2009-02-05 22:18:55 +0000 | [diff] [blame] | 47 | public void configure() { |
limpbizkit | 47151c2 | 2008-10-15 05:18:48 +0000 | [diff] [blame] | 48 | bind(String.class).annotatedWith(named("b")).toInstance("i"); |
| 49 | |
| 50 | bind(AB.class).annotatedWith(named("one")).to(AB.class); |
| 51 | expose(AB.class).annotatedWith(named("one")); |
| 52 | } |
| 53 | }); |
| 54 | |
| 55 | install(new PrivateModule() { |
crazyboblee | 9119370 | 2009-02-05 22:18:55 +0000 | [diff] [blame] | 56 | public void configure() { |
limpbizkit | 47151c2 | 2008-10-15 05:18:48 +0000 | [diff] [blame] | 57 | bind(String.class).annotatedWith(named("b")).toInstance("ii"); |
| 58 | |
| 59 | bind(AB.class).annotatedWith(named("two")).to(AB.class); |
| 60 | expose(AB.class).annotatedWith(named("two")); |
| 61 | } |
| 62 | }); |
| 63 | } |
| 64 | }); |
| 65 | |
| 66 | AB ab1 = injector.getInstance(Key.get(AB.class, named("one"))); |
| 67 | assertEquals("public", ab1.a); |
| 68 | assertEquals("i", ab1.b); |
| 69 | |
| 70 | AB ab2 = injector.getInstance(Key.get(AB.class, named("two"))); |
| 71 | assertEquals("public", ab2.a); |
| 72 | assertEquals("ii", ab2.b); |
| 73 | } |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 74 | |
| 75 | public void testWithoutPrivateModules() { |
| 76 | Injector injector = Guice.createInjector(new AbstractModule() { |
| 77 | protected void configure() { |
| 78 | PrivateBinder bindA = binder().newPrivateBinder(); |
| 79 | bindA.bind(String.class).annotatedWith(named("a")).toInstance("i"); |
| 80 | bindA.expose(String.class).annotatedWith(named("a")); |
| 81 | bindA.bind(String.class).annotatedWith(named("c")).toInstance("private to A"); |
| 82 | |
| 83 | PrivateBinder bindB = binder().newPrivateBinder(); |
| 84 | bindB.bind(String.class).annotatedWith(named("b")).toInstance("ii"); |
| 85 | bindB.expose(String.class).annotatedWith(named("b")); |
| 86 | bindB.bind(String.class).annotatedWith(named("c")).toInstance("private to B"); |
| 87 | } |
| 88 | }); |
| 89 | |
| 90 | assertEquals("i", injector.getInstance(Key.get(String.class, named("a")))); |
| 91 | assertEquals("ii", injector.getInstance(Key.get(String.class, named("b")))); |
| 92 | } |
limpbizkit | 47151c2 | 2008-10-15 05:18:48 +0000 | [diff] [blame] | 93 | |
limpbizkit | b1f42f5 | 2008-11-26 07:44:53 +0000 | [diff] [blame] | 94 | public void testMisplacedExposedAnnotation() { |
| 95 | try { |
| 96 | Guice.createInjector(new AbstractModule() { |
| 97 | protected void configure() {} |
| 98 | |
| 99 | @Provides @Exposed |
| 100 | String provideString() { |
| 101 | return "i"; |
| 102 | } |
| 103 | }); |
| 104 | fail(); |
| 105 | } catch (CreationException expected) { |
| 106 | assertContains(expected.getMessage(), "Cannot expose java.lang.String on a standard binder. ", |
| 107 | "Exposed bindings are only applicable to private binders.", |
| 108 | " at " + PrivateModuleTest.class.getName(), "provideString(PrivateModuleTest.java:"); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | public void testMisplacedExposeStatement() { |
| 113 | try { |
| 114 | Guice.createInjector(new AbstractModule() { |
| 115 | protected void configure() { |
| 116 | ((PrivateBinder) binder()).expose(String.class).annotatedWith(named("a")); |
| 117 | } |
| 118 | }); |
| 119 | fail(); |
| 120 | } catch (CreationException expected) { |
| 121 | assertContains(expected.getMessage(), "Cannot expose java.lang.String on a standard binder. ", |
| 122 | "Exposed bindings are only applicable to private binders.", |
| 123 | " at " + PrivateModuleTest.class.getName(), "configure(PrivateModuleTest.java:"); |
| 124 | } |
| 125 | } |
| 126 | |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 127 | public void testPrivateModulesAndProvidesMethods() { |
| 128 | Injector injector = Guice.createInjector(new AbstractModule() { |
| 129 | protected void configure() { |
| 130 | install(new PrivateModule() { |
crazyboblee | 9119370 | 2009-02-05 22:18:55 +0000 | [diff] [blame] | 131 | public void configure() { |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 132 | expose(String.class).annotatedWith(named("a")); |
| 133 | } |
| 134 | |
| 135 | @Provides @Named("a") String providePublicA() { |
| 136 | return "i"; |
| 137 | } |
| 138 | |
| 139 | @Provides @Named("b") String providePrivateB() { |
| 140 | return "private"; |
| 141 | } |
| 142 | }); |
| 143 | |
| 144 | install(new PrivateModule() { |
crazyboblee | 9119370 | 2009-02-05 22:18:55 +0000 | [diff] [blame] | 145 | public void configure() {} |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 146 | |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 147 | @Provides @Named("c") String providePrivateC() { |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 148 | return "private"; |
| 149 | } |
| 150 | |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 151 | @Provides @Exposed @Named("d") String providePublicD() { |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 152 | return "ii"; |
| 153 | } |
| 154 | }); |
| 155 | } |
| 156 | }); |
| 157 | |
| 158 | assertEquals("i", injector.getInstance(Key.get(String.class, named("a")))); |
limpbizkit | 0551b64 | 2008-11-26 22:25:05 +0000 | [diff] [blame] | 159 | |
| 160 | try { |
limpbizkit | 8dfd5cc | 2008-11-29 22:21:52 +0000 | [diff] [blame] | 161 | injector.getInstance(Key.get(String.class, named("b"))); |
limpbizkit | 0551b64 | 2008-11-26 22:25:05 +0000 | [diff] [blame] | 162 | fail(); |
| 163 | } catch(ConfigurationException expected) { |
| 164 | } |
| 165 | |
| 166 | try { |
limpbizkit | 8dfd5cc | 2008-11-29 22:21:52 +0000 | [diff] [blame] | 167 | injector.getInstance(Key.get(String.class, named("c"))); |
limpbizkit | 0551b64 | 2008-11-26 22:25:05 +0000 | [diff] [blame] | 168 | fail(); |
| 169 | } catch(ConfigurationException expected) { |
| 170 | } |
| 171 | |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 172 | assertEquals("ii", injector.getInstance(Key.get(String.class, named("d")))); |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | public void testCannotBindAKeyExportedByASibling() { |
| 176 | try { |
| 177 | Guice.createInjector(new AbstractModule() { |
| 178 | protected void configure() { |
| 179 | install(new PrivateModule() { |
crazyboblee | 9119370 | 2009-02-05 22:18:55 +0000 | [diff] [blame] | 180 | public void configure() { |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 181 | bind(String.class).toInstance("public"); |
| 182 | expose(String.class); |
| 183 | } |
| 184 | }); |
| 185 | |
| 186 | install(new PrivateModule() { |
crazyboblee | 9119370 | 2009-02-05 22:18:55 +0000 | [diff] [blame] | 187 | public void configure() { |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 188 | bind(String.class).toInstance("private"); |
| 189 | } |
| 190 | }); |
| 191 | } |
| 192 | }); |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 193 | fail(); |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 194 | } catch (CreationException expected) { |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 195 | assertContains(expected.getMessage(), |
| 196 | "A binding to java.lang.String was already configured at ", |
crazyboblee | 9119370 | 2009-02-05 22:18:55 +0000 | [diff] [blame] | 197 | getClass().getName(), ".configure(PrivateModuleTest.java:", |
| 198 | " at " + getClass().getName(), ".configure(PrivateModuleTest.java:"); |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
| 202 | public void testExposeButNoBind() { |
| 203 | try { |
| 204 | Guice.createInjector(new AbstractModule() { |
| 205 | protected void configure() { |
| 206 | bind(String.class).annotatedWith(named("a")).toInstance("a"); |
| 207 | bind(String.class).annotatedWith(named("b")).toInstance("b"); |
| 208 | |
| 209 | install(new PrivateModule() { |
crazyboblee | 9119370 | 2009-02-05 22:18:55 +0000 | [diff] [blame] | 210 | public void configure() { |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 211 | expose(AB.class); |
| 212 | } |
| 213 | }); |
| 214 | } |
| 215 | }); |
| 216 | fail("AB was exposed but not bound"); |
| 217 | } catch (CreationException expected) { |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 218 | assertContains(expected.getMessage(), |
| 219 | "Could not expose() " + AB.class.getName() + ", it must be explicitly bound", |
crazyboblee | 9119370 | 2009-02-05 22:18:55 +0000 | [diff] [blame] | 220 | ".configure(PrivateModuleTest.java:"); |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
limpbizkit | 72d11dd | 2008-11-02 07:59:13 +0000 | [diff] [blame] | 224 | /** |
| 225 | * Ensure that when we've got errors in different private modules, Guice presents all errors |
| 226 | * in a unified message. |
| 227 | */ |
| 228 | public void testMessagesFromPrivateModulesAreNicelyIntegrated() { |
| 229 | try { |
| 230 | Guice.createInjector( |
| 231 | new PrivateModule() { |
crazyboblee | 9119370 | 2009-02-05 22:18:55 +0000 | [diff] [blame] | 232 | public void configure() { |
limpbizkit | 72d11dd | 2008-11-02 07:59:13 +0000 | [diff] [blame] | 233 | bind(C.class); |
| 234 | } |
| 235 | }, |
| 236 | new PrivateModule() { |
crazyboblee | 9119370 | 2009-02-05 22:18:55 +0000 | [diff] [blame] | 237 | public void configure() { |
limpbizkit | 72d11dd | 2008-11-02 07:59:13 +0000 | [diff] [blame] | 238 | bind(AB.class); |
| 239 | } |
| 240 | } |
| 241 | ); |
| 242 | fail(); |
| 243 | } catch (CreationException expected) { |
| 244 | assertContains(expected.getMessage(), |
| 245 | "1) No implementation for " + C.class.getName() + " was bound.", |
crazyboblee | 9119370 | 2009-02-05 22:18:55 +0000 | [diff] [blame] | 246 | "at " + getClass().getName(), ".configure(PrivateModuleTest.java:", |
limpbizkit | 72d11dd | 2008-11-02 07:59:13 +0000 | [diff] [blame] | 247 | "2) No implementation for " + String.class.getName(), "Named(value=a) was bound.", |
| 248 | "for field at " + AB.class.getName() + ".a(PrivateModuleTest.java:", |
| 249 | "3) No implementation for " + String.class.getName(), "Named(value=b) was bound.", |
| 250 | "for field at " + AB.class.getName() + ".b(PrivateModuleTest.java:", |
| 251 | "3 errors"); |
| 252 | } |
| 253 | } |
| 254 | |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 255 | public void testNestedPrivateInjectors() { |
| 256 | Injector injector = Guice.createInjector(new PrivateModule() { |
crazyboblee | 9119370 | 2009-02-05 22:18:55 +0000 | [diff] [blame] | 257 | public void configure() { |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 258 | expose(String.class); |
| 259 | |
| 260 | install(new PrivateModule() { |
crazyboblee | 9119370 | 2009-02-05 22:18:55 +0000 | [diff] [blame] | 261 | public void configure() { |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 262 | bind(String.class).toInstance("nested"); |
| 263 | expose(String.class); |
| 264 | } |
| 265 | }); |
| 266 | } |
| 267 | }); |
| 268 | |
| 269 | assertEquals("nested", injector.getInstance(String.class)); |
| 270 | } |
| 271 | |
| 272 | public void testInstallingRegularModulesFromPrivateModules() { |
| 273 | Injector injector = Guice.createInjector(new PrivateModule() { |
crazyboblee | 9119370 | 2009-02-05 22:18:55 +0000 | [diff] [blame] | 274 | public void configure() { |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 275 | expose(String.class); |
| 276 | |
| 277 | install(new AbstractModule() { |
| 278 | protected void configure() { |
| 279 | bind(String.class).toInstance("nested"); |
| 280 | } |
| 281 | }); |
| 282 | } |
| 283 | }); |
| 284 | |
| 285 | assertEquals("nested", injector.getInstance(String.class)); |
| 286 | } |
| 287 | |
limpbizkit | a26a41e | 2008-10-17 17:49:49 +0000 | [diff] [blame] | 288 | public void testNestedPrivateModulesWithSomeKeysUnexposed() { |
| 289 | Injector injector = Guice.createInjector(new PrivateModule() { |
crazyboblee | 9119370 | 2009-02-05 22:18:55 +0000 | [diff] [blame] | 290 | public void configure() { |
limpbizkit | a26a41e | 2008-10-17 17:49:49 +0000 | [diff] [blame] | 291 | bind(String.class).annotatedWith(named("bound outer, exposed outer")).toInstance("boeo"); |
| 292 | expose(String.class).annotatedWith(named("bound outer, exposed outer")); |
| 293 | bind(String.class).annotatedWith(named("bound outer, exposed none")).toInstance("boen"); |
| 294 | expose(String.class).annotatedWith(named("bound inner, exposed both")); |
| 295 | |
| 296 | install(new PrivateModule() { |
crazyboblee | 9119370 | 2009-02-05 22:18:55 +0000 | [diff] [blame] | 297 | public void configure() { |
limpbizkit | a26a41e | 2008-10-17 17:49:49 +0000 | [diff] [blame] | 298 | bind(String.class).annotatedWith(named("bound inner, exposed both")).toInstance("bieb"); |
| 299 | expose(String.class).annotatedWith(named("bound inner, exposed both")); |
| 300 | bind(String.class).annotatedWith(named("bound inner, exposed none")).toInstance("bien"); |
| 301 | } |
| 302 | }); |
| 303 | } |
| 304 | }); |
| 305 | |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 306 | assertEquals("boeo", |
| 307 | injector.getInstance(Key.get(String.class, named("bound outer, exposed outer")))); |
| 308 | assertEquals("bieb", |
| 309 | injector.getInstance(Key.get(String.class, named("bound inner, exposed both")))); |
limpbizkit | a26a41e | 2008-10-17 17:49:49 +0000 | [diff] [blame] | 310 | |
| 311 | try { |
| 312 | injector.getInstance(Key.get(String.class, named("bound outer, exposed none"))); |
| 313 | fail(); |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 314 | } catch (ConfigurationException expected) { |
limpbizkit | a26a41e | 2008-10-17 17:49:49 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | try { |
| 318 | injector.getInstance(Key.get(String.class, named("bound inner, exposed none"))); |
| 319 | fail(); |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 320 | } catch (ConfigurationException expected) { |
limpbizkit | a26a41e | 2008-10-17 17:49:49 +0000 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | |
limpbizkit | e2db859 | 2008-10-15 23:49:00 +0000 | [diff] [blame] | 324 | public void testDependenciesBetweenPrivateAndPublic() { |
| 325 | Injector injector = Guice.createInjector( |
| 326 | new PrivateModule() { |
crazyboblee | 9119370 | 2009-02-05 22:18:55 +0000 | [diff] [blame] | 327 | protected void configure() {} |
limpbizkit | e2db859 | 2008-10-15 23:49:00 +0000 | [diff] [blame] | 328 | |
| 329 | @Provides @Exposed @Named("a") String provideA() { |
| 330 | return "A"; |
| 331 | } |
| 332 | |
limpbizkit | 2f51f2c | 2008-10-16 00:21:29 +0000 | [diff] [blame] | 333 | @Provides @Exposed @Named("abc") String provideAbc(@Named("ab") String ab) { |
limpbizkit | e2db859 | 2008-10-15 23:49:00 +0000 | [diff] [blame] | 334 | return ab + "C"; |
| 335 | } |
| 336 | }, |
| 337 | new AbstractModule() { |
| 338 | protected void configure() {} |
| 339 | |
limpbizkit | 2f51f2c | 2008-10-16 00:21:29 +0000 | [diff] [blame] | 340 | @Provides @Named("ab") String provideAb(@Named("a") String a) { |
limpbizkit | e2db859 | 2008-10-15 23:49:00 +0000 | [diff] [blame] | 341 | return a + "B"; |
| 342 | } |
| 343 | |
limpbizkit | 2f51f2c | 2008-10-16 00:21:29 +0000 | [diff] [blame] | 344 | @Provides @Named("abcd") String provideAbcd(@Named("abc") String abc) { |
limpbizkit | e2db859 | 2008-10-15 23:49:00 +0000 | [diff] [blame] | 345 | return abc + "D"; |
| 346 | } |
| 347 | } |
| 348 | ); |
| 349 | |
| 350 | assertEquals("ABCD", injector.getInstance(Key.get(String.class, named("abcd")))); |
| 351 | } |
| 352 | |
| 353 | public void testDependenciesBetweenPrivateAndPublicWithPublicEagerSingleton() { |
| 354 | Injector injector = Guice.createInjector( |
| 355 | new PrivateModule() { |
crazyboblee | 9119370 | 2009-02-05 22:18:55 +0000 | [diff] [blame] | 356 | protected void configure() {} |
limpbizkit | e2db859 | 2008-10-15 23:49:00 +0000 | [diff] [blame] | 357 | |
| 358 | @Provides @Exposed @Named("a") String provideA() { |
| 359 | return "A"; |
| 360 | } |
| 361 | |
limpbizkit | 2f51f2c | 2008-10-16 00:21:29 +0000 | [diff] [blame] | 362 | @Provides @Exposed @Named("abc") String provideAbc(@Named("ab") String ab) { |
limpbizkit | e2db859 | 2008-10-15 23:49:00 +0000 | [diff] [blame] | 363 | return ab + "C"; |
| 364 | } |
| 365 | }, |
| 366 | new AbstractModule() { |
| 367 | protected void configure() { |
| 368 | bind(String.class).annotatedWith(named("abcde")).toProvider(new Provider<String>() { |
| 369 | @Inject @Named("abcd") String abcd; |
| 370 | |
| 371 | public String get() { |
| 372 | return abcd + "E"; |
| 373 | } |
| 374 | }).asEagerSingleton(); |
| 375 | } |
| 376 | |
limpbizkit | 2f51f2c | 2008-10-16 00:21:29 +0000 | [diff] [blame] | 377 | @Provides @Named("ab") String provideAb(@Named("a") String a) { |
limpbizkit | e2db859 | 2008-10-15 23:49:00 +0000 | [diff] [blame] | 378 | return a + "B"; |
| 379 | } |
| 380 | |
limpbizkit | 2f51f2c | 2008-10-16 00:21:29 +0000 | [diff] [blame] | 381 | @Provides @Named("abcd") String provideAbcd(@Named("abc") String abc) { |
limpbizkit | e2db859 | 2008-10-15 23:49:00 +0000 | [diff] [blame] | 382 | return abc + "D"; |
| 383 | } |
| 384 | } |
| 385 | ); |
| 386 | |
| 387 | assertEquals("ABCDE", injector.getInstance(Key.get(String.class, named("abcde")))); |
| 388 | } |
| 389 | |
| 390 | public void testDependenciesBetweenPrivateAndPublicWithPrivateEagerSingleton() { |
| 391 | Injector injector = Guice.createInjector( |
| 392 | new AbstractModule() { |
| 393 | protected void configure() {} |
| 394 | |
limpbizkit | 2f51f2c | 2008-10-16 00:21:29 +0000 | [diff] [blame] | 395 | @Provides @Named("ab") String provideAb(@Named("a") String a) { |
limpbizkit | e2db859 | 2008-10-15 23:49:00 +0000 | [diff] [blame] | 396 | return a + "B"; |
| 397 | } |
| 398 | |
limpbizkit | 2f51f2c | 2008-10-16 00:21:29 +0000 | [diff] [blame] | 399 | @Provides @Named("abcd") String provideAbcd(@Named("abc") String abc) { |
limpbizkit | e2db859 | 2008-10-15 23:49:00 +0000 | [diff] [blame] | 400 | return abc + "D"; |
| 401 | } |
| 402 | }, |
| 403 | new PrivateModule() { |
crazyboblee | 9119370 | 2009-02-05 22:18:55 +0000 | [diff] [blame] | 404 | protected void configure() { |
limpbizkit | e2db859 | 2008-10-15 23:49:00 +0000 | [diff] [blame] | 405 | bind(String.class).annotatedWith(named("abcde")).toProvider(new Provider<String>() { |
| 406 | @Inject @Named("abcd") String abcd; |
| 407 | |
| 408 | public String get() { |
| 409 | return abcd + "E"; |
| 410 | } |
| 411 | }).asEagerSingleton(); |
| 412 | expose(String.class).annotatedWith(named("abcde")); |
| 413 | } |
| 414 | |
| 415 | @Provides @Exposed @Named("a") String provideA() { |
| 416 | return "A"; |
| 417 | } |
| 418 | |
limpbizkit | 2f51f2c | 2008-10-16 00:21:29 +0000 | [diff] [blame] | 419 | @Provides @Exposed @Named("abc") String provideAbc(@Named("ab") String ab) { |
limpbizkit | e2db859 | 2008-10-15 23:49:00 +0000 | [diff] [blame] | 420 | return ab + "C"; |
| 421 | } |
| 422 | } |
| 423 | ); |
| 424 | |
| 425 | assertEquals("ABCDE", injector.getInstance(Key.get(String.class, named("abcde")))); |
| 426 | } |
| 427 | |
limpbizkit | 47151c2 | 2008-10-15 05:18:48 +0000 | [diff] [blame] | 428 | static class AB { |
| 429 | @Inject @Named("a") String a; |
| 430 | @Inject @Named("b") String b; |
| 431 | } |
limpbizkit | e2db859 | 2008-10-15 23:49:00 +0000 | [diff] [blame] | 432 | |
| 433 | interface C {} |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 434 | |
| 435 | public void testSpiAccess() { |
| 436 | Injector injector = Guice.createInjector(new PrivateModule() { |
crazyboblee | 9119370 | 2009-02-05 22:18:55 +0000 | [diff] [blame] | 437 | public void configure() { |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 438 | bind(String.class).annotatedWith(named("a")).toInstance("private"); |
| 439 | bind(String.class).annotatedWith(named("b")).toInstance("exposed"); |
| 440 | expose(String.class).annotatedWith(named("b")); |
| 441 | } |
| 442 | }); |
| 443 | |
| 444 | ExposedBinding<?> binding |
| 445 | = (ExposedBinding<?>) injector.getBinding(Key.get(String.class, Names.named("b"))); |
| 446 | assertEquals(ImmutableSet.<Dependency<?>>of(Dependency.get(Key.get(Injector.class))), |
| 447 | binding.getDependencies()); |
| 448 | PrivateElements privateElements = binding.getPrivateElements(); |
| 449 | assertEquals(ImmutableSet.<Key<?>>of(Key.get(String.class, named("b"))), |
| 450 | privateElements.getExposedKeys()); |
limpbizkit | aa07ab0 | 2009-05-15 07:10:43 +0000 | [diff] [blame] | 451 | assertContains(privateElements.getExposedSource(Key.get(String.class, named("b"))).toString(), |
| 452 | PrivateModuleTest.class.getName(), ".configure(PrivateModuleTest.java:"); |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 453 | Injector privateInjector = privateElements.getInjector(); |
| 454 | assertEquals("private", privateInjector.getInstance(Key.get(String.class, Names.named("a")))); |
| 455 | } |
sberlin | 45ca7f6 | 2011-02-20 00:05:28 +0000 | [diff] [blame] | 456 | |
| 457 | public void testParentBindsSomethingInPrivate() { |
| 458 | try { |
| 459 | Guice.createInjector(new FailingModule()); |
| 460 | fail(); |
| 461 | } catch(CreationException expected) { |
| 462 | assertEquals(1, expected.getErrorMessages().size()); |
| 463 | assertContains(expected.toString(), |
sberlin | cc17f14 | 2011-02-27 00:02:03 +0000 | [diff] [blame] | 464 | "Unable to create binding for java.util.List.", |
| 465 | "It was already configured on one or more child injectors or private modules", |
| 466 | "bound at " + FailingPrivateModule.class.getName() + ".configure(", |
| 467 | "bound at " + SecondFailingPrivateModule.class.getName() + ".configure(", |
| 468 | "If it was in a PrivateModule, did you forget to expose the binding?", |
sberlin | 45ca7f6 | 2011-02-20 00:05:28 +0000 | [diff] [blame] | 469 | "at " + FailingModule.class.getName() + ".configure("); |
| 470 | } |
| 471 | } |
| 472 | |
sberlin | cc17f14 | 2011-02-27 00:02:03 +0000 | [diff] [blame] | 473 | public void testParentBindingToPrivateLinkedJitBinding() { |
| 474 | Injector injector = Guice.createInjector(new ManyPrivateModules()); |
| 475 | try { |
| 476 | injector.getBinding(Key.get(Types.providerOf(List.class))); |
| 477 | fail(); |
| 478 | } catch(ConfigurationException expected) { |
| 479 | assertEquals(1, expected.getErrorMessages().size()); |
| 480 | assertContains(expected.toString(), |
| 481 | "Unable to create binding for com.google.inject.Provider<java.util.List>.", |
| 482 | "It was already configured on one or more child injectors or private modules", |
| 483 | "bound at " + FailingPrivateModule.class.getName() + ".configure(", |
| 484 | "bound at " + SecondFailingPrivateModule.class.getName() + ".configure(", |
| 485 | "If it was in a PrivateModule, did you forget to expose the binding?", |
| 486 | "while locating com.google.inject.Provider<java.util.List>"); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | public void testParentBindingToPrivateJitBinding() { |
| 491 | Injector injector = Guice.createInjector(new ManyPrivateModules()); |
| 492 | try { |
| 493 | injector.getBinding(PrivateFoo.class); |
| 494 | fail(); |
| 495 | } catch(ConfigurationException expected) { |
| 496 | assertEquals(1, expected.getErrorMessages().size()); |
| 497 | assertContains(expected.toString(), |
| 498 | "Unable to create binding for " + PrivateFoo.class.getName(), |
| 499 | "It was already configured on one or more child injectors or private modules", |
| 500 | "(bound by a just-in-time binding)", |
| 501 | "If it was in a PrivateModule, did you forget to expose the binding?", |
| 502 | "while locating " + PrivateFoo.class.getName()); |
| 503 | } |
| 504 | } |
| 505 | |
sberlin | 45ca7f6 | 2011-02-20 00:05:28 +0000 | [diff] [blame] | 506 | private static class FailingModule extends AbstractModule { |
| 507 | @Override |
| 508 | protected void configure() { |
| 509 | bind(Collection.class).to(List.class); |
sberlin | cc17f14 | 2011-02-27 00:02:03 +0000 | [diff] [blame] | 510 | install(new ManyPrivateModules()); |
sberlin | 45ca7f6 | 2011-02-20 00:05:28 +0000 | [diff] [blame] | 511 | } |
| 512 | } |
sberlin | cc17f14 | 2011-02-27 00:02:03 +0000 | [diff] [blame] | 513 | |
| 514 | private static class ManyPrivateModules extends AbstractModule { |
| 515 | @Override |
| 516 | protected void configure() { |
| 517 | // make sure duplicate sources are collapsed |
| 518 | install(new FailingPrivateModule()); |
| 519 | install(new FailingPrivateModule()); |
| 520 | // but additional sources are listed |
| 521 | install(new SecondFailingPrivateModule()); |
| 522 | } |
| 523 | } |
| 524 | |
sberlin | 45ca7f6 | 2011-02-20 00:05:28 +0000 | [diff] [blame] | 525 | private static class FailingPrivateModule extends PrivateModule { |
| 526 | @Override |
| 527 | protected void configure() { |
| 528 | bind(List.class).toInstance(new ArrayList()); |
sberlin | cc17f14 | 2011-02-27 00:02:03 +0000 | [diff] [blame] | 529 | |
| 530 | // Add the Provider<List> binding, created just-in-time, |
| 531 | // to make sure our linked JIT bindings have the correct source. |
| 532 | getProvider(Key.get(Types.providerOf(List.class))); |
| 533 | |
| 534 | // Request a JIT binding for PrivateFoo, which can only |
| 535 | // be created in the private module because it depends |
| 536 | // on List. |
| 537 | getProvider(PrivateFoo.class); |
sberlin | 45ca7f6 | 2011-02-20 00:05:28 +0000 | [diff] [blame] | 538 | } |
| 539 | } |
sberlin | cc17f14 | 2011-02-27 00:02:03 +0000 | [diff] [blame] | 540 | |
| 541 | /** A second class, so we can see another name in the source list. */ |
| 542 | private static class SecondFailingPrivateModule extends PrivateModule { |
| 543 | @Override |
| 544 | protected void configure() { |
| 545 | bind(List.class).toInstance(new ArrayList()); |
| 546 | |
| 547 | // Add the Provider<List> binding, created just-in-time, |
| 548 | // to make sure our linked JIT bindings have the correct source. |
| 549 | getProvider(Key.get(Types.providerOf(List.class))); |
| 550 | |
| 551 | // Request a JIT binding for PrivateFoo, which can only |
| 552 | // be created in the private module because it depends |
| 553 | // on List. |
| 554 | getProvider(PrivateFoo.class); |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | private static class PrivateFoo { |
| 559 | @Inject List list; |
| 560 | } |
limpbizkit | 47151c2 | 2008-10-15 05:18:48 +0000 | [diff] [blame] | 561 | } |