@Override public Long call() { latch.countDown(); return graph.get(Long.class); } }));
public static void main(String[] args) { ObjectGraph objectGraph = ObjectGraph.create(new DripCoffeeModule()); CoffeeApp coffeeApp = objectGraph.get(CoffeeApp.class); coffeeApp.run(); } }
private void assertFailInjectNotRegistered(ObjectGraph graph, Class<?> clazz) { try { assertThat(graph.get(clazz)).isNull(); } catch (IllegalArgumentException e) { assertThat(e.getMessage()).contains("No inject"); } } }
@Test public void basicInjection() { ObjectGraph root = ObjectGraph.createWith(new TestingLoader(), new RootModule()); assertThat(root.get(A.class)).isNotNull(); assertThat(root.get(A.class)).isSameAs(root.get(A.class)); // Present and Singleton. assertThat(root.get(B.class)).isNotSameAs(root.get(B.class)); // Not singleton. assertFailInjectNotRegistered(root, C.class); // Not declared in RootModule. assertFailInjectNotRegistered(root, D.class); // Not declared in RootModule. // Extension graph behaves as the root graph would for root-ish things. ObjectGraph extension = root.plus(new ExtensionModule()); assertThat(root.get(A.class)).isSameAs(extension.get(A.class)); assertThat(root.get(B.class)).isNotSameAs(extension.get(B.class)); assertThat(root.get(B.class).a).isSameAs(extension.get(B.class).a); assertThat(extension.get(C.class).a).isNotNull(); assertThat(extension.get(D.class).c).isNotNull(); }
@Test public void getInstance() { final AtomicInteger next = new AtomicInteger(0); @Module(injects = Integer.class) class TestModule { @Provides Integer provideInteger() { return next.getAndIncrement(); } } ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); assertThat((int) graph.get(Integer.class)).isEqualTo(0); assertThat((int) graph.get(Integer.class)).isEqualTo(1); }
@Test public void autoInstantiationOfIncludedModules() { // Have to make these non-method-scoped or instantiation errors occur. ObjectGraph objectGraph = ObjectGraph.createWith(new TestingLoader(), new TestModuleB()); // TestModuleA auto-created. assertThat(objectGraph.get(A.class)).isNotNull(); assertThat(objectGraph.get(B.class).a).isNotNull(); }
@Test public void basicInjectionWithExtension() { A a = new A(); ObjectGraph root = ObjectGraph.createWith(new TestingLoader(), new RootModule(a)); assertThat(root.get(A.class)).isSameAs(a); // Extension graph behaves as the root graph would for root-ish things. ObjectGraph extension = root.plus(new ExtensionModule()); assertThat(extension.get(A.class)).isSameAs(a); assertThat(extension.get(B.class).a).isSameAs(a); }
@Test public void basicInjectionWithExtension() { ObjectGraph root = ObjectGraph.createWith(new TestingLoader(), new RootModule()); RealSingleton rs = root.get(RealSingleton.class); assertThat(rs.ints).containsExactly(0, 1); ObjectGraph extension = root.plus(new ExtensionModule()); Main main = extension.get(Main.class); assertThat(main.ints).containsExactly(0, 1, 2, 3); // Second time around. ObjectGraph extension2 = root.plus(new ExtensionModule()); Main main2 = extension2.get(Main.class); assertThat(main2.ints).containsExactly(0, 1, 4, 5); }
@Test public void runtimeConstructorExceptionsAreNotWrapped() { @Module(injects = ThrowsOnConstruction.class) class TestModule { } try { ObjectGraph.createWith(new TestingLoader(), new TestModule()).get(ThrowsOnConstruction.class); fail(); } catch (ClassCastException e) { assertThat(e.getMessage()).isEqualTo("foo"); } }
@Test public void testSingletonLinkingThroughExtensionGraph() { ObjectGraph root = ObjectGraph.createWith(new TestingLoader(), new RootModule()); // DO NOT CALL root.get(C.class)) HERE to get forced-linking behaviour from plus(); ObjectGraph extension = root.plus(new ExtensionModule()); assertThat(extension.get(SingletonLinkedFromExtension.class).c).isSameAs(root.get(C.class)); }
@Test public void autoInstantiationOfModules() { // Have to make these non-method-scoped or instantiation errors occur. ObjectGraph objectGraph = ObjectGraph.createWith(new TestingLoader(), TestModuleA.class); assertThat(objectGraph.get(A.class)).isNotNull(); }
@Test public void childModuleWithEntryPoint() { @Module(includes = ModuleWithEntryPoint.class) class TestModule { @Provides String provideString() { return "injected"; } } ObjectGraph objectGraph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); TestEntryPoint entryPoint = objectGraph.get(TestEntryPoint.class); assertThat(entryPoint.s).isEqualTo("injected"); }
@Test public void getInstanceOfArray() { @Module(injects = int[].class) class TestModule { @Provides int[] provideIntArray() { return new int[] { 1, 2, 3 }; } } ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); assertEquals("[1, 2, 3]", Arrays.toString(graph.get(int[].class))); }
@Test public void privateConstructorsFail() { class Test { @Inject private Test() { } } @Module(injects = Test.class) class TestModule { } try { ObjectGraph.createWith(new TestingLoader(), new TestModule()).get(Test.class); fail(); } catch (IllegalStateException e) { assertThat(e.getMessage()).contains("Can't inject private constructor: "); } }
@Test public void getInstanceOfPrimitive() { @Module(injects = int.class) class TestModule { @Provides int provideInt() { return 1; } } ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); assertEquals(1, (int) graph.get(int.class)); }
@Test public void rejectUnconstructableSingleton() { class TestEntryPoint { @Inject MembersInjector<UnconstructableSingleton> membersInjector; } @Module(injects = TestEntryPoint.class) class TestModule { } ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); try { graph.get(TestEntryPoint.class); fail(); } catch (IllegalStateException expected) { } }
@Test public void instanceInjectionOfNonStaticInnerFailsEarly() { class TestEntryPoint { @Inject NonStaticInner nonStaticInner; } @Module(injects = TestEntryPoint.class) class TestModule { } ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); try { graph.get(TestEntryPoint.class); fail(); } catch (IllegalStateException expected) { } }
@Test public void getInstanceRequiresEntryPoint() { @Module class TestModule { @Provides Integer provideInteger() { throw new AssertionError(); } } ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); try { graph.get(Integer.class); fail(); } catch (IllegalArgumentException expected) { } }
@Test public void injectionOfUnconstructableFails() { class TestEntryPoint { @Inject Unconstructable unconstructable; } @Module(injects = TestEntryPoint.class) class TestModule { } ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); try { graph.get(TestEntryPoint.class); fail(); } catch (IllegalStateException expected) { } }
@Test public void instanceInjectionOfMembersOnlyType() { class TestEntryPoint { @Inject Provider<Unconstructable> provider; } @Module(injects = TestEntryPoint.class) class TestModule { } ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); try { graph.get(TestEntryPoint.class); fail(); } catch (IllegalStateException expected) { } }