1 package com.google.inject.spi;
2 
3 import com.google.inject.AbstractModule;
4 import com.google.inject.Binding;
5 import com.google.inject.Guice;
6 import com.google.inject.Inject;
7 import com.google.inject.Injector;
8 import com.google.inject.Key;
9 import com.google.inject.Provider;
10 import com.google.inject.TypeLiteral;
11 
12 import junit.framework.TestCase;
13 
14 import java.util.Map;
15 
16 /**
17  * @author sberlin@gmail.com (Sam Berlin)
18  */
19 public class InjectorSpiTest extends TestCase {
20 
testExistingBinding()21   public void testExistingBinding() {
22     Injector injector = Guice.createInjector(new AbstractModule() {
23       @Override
24       protected void configure() {
25         bind(Foo.class);
26         bind(Baz.class);
27       }
28     });
29     // Sanity check -- ensure we return the proper binding for all existing bindings.
30     for(Map.Entry<Key<?>, Binding<?>> entry : injector.getAllBindings().entrySet()) {
31       assertSame(entry.getValue(), injector.getExistingBinding(entry.getKey()));
32     }
33 
34     // Now run through specifics...
35     Binding<?> binding;
36 
37     // 1) non-Provider Foo.class
38     binding = injector.getExistingBinding(Key.get(Foo.class));
39     assertNotNull(binding);
40     assertEquals(Foo.class, binding.getKey().getTypeLiteral().getRawType());
41 
42     // 2) Provider<Foo> class (should already exist, because Baz @Injects it).
43     // the assertTrue is a bit stricter than necessary, but makes sure this works for pre-existing Provider bindings
44     assertTrue(injector.getAllBindings().containsKey(Key.get(new TypeLiteral<Provider<Foo>>() {})));
45     binding = injector.getExistingBinding(Key.get(new TypeLiteral<Provider<Foo>>() {}));
46     assertNotNull(binding);
47     assertEquals(Provider.class, binding.getKey().getTypeLiteral().getRawType());
48     assertEquals(Foo.class, ((Provider)binding.getProvider().get()).get().getClass());
49 
50     // 3) non-Provider Baz.class
51     binding = injector.getExistingBinding(Key.get(Baz.class));
52     assertNotNull(binding);
53     assertEquals(Baz.class, binding.getKey().getTypeLiteral().getRawType());
54 
55     // 4) Provider<Baz> class (should not already exist, because nothing used it yet).
56     // the assertFalse is a bit stricter than necessary, but makes sure this works for non-pre-existing Provider bindings
57     assertFalse(injector.getAllBindings().containsKey(Key.get(new TypeLiteral<Provider<Baz>>() {})));
58     binding = injector.getExistingBinding(Key.get(new TypeLiteral<Provider<Baz>>() {}));
59     assertNotNull(binding);
60     assertEquals(Provider.class, binding.getKey().getTypeLiteral().getRawType());
61     assertEquals(Baz.class, ((Provider)binding.getProvider().get()).get().getClass());
62 
63     // 5) non-Provider Bar, doesn't exist.
64     assertNull(injector.getExistingBinding(Key.get(Bar.class)));
65 
66     // 6) Provider Bar, doesn't exist.
67     assertNull(injector.getExistingBinding(Key.get(new TypeLiteral<Provider<Bar>>() {})));
68   }
69 
70   private static class Foo {}
71   private static class Bar {}
72   private static class Baz { @SuppressWarnings("unused") @Inject Provider<Foo> fooP; }
73 
74 }
75