From 424ed0598ed5347b73ae00e5e3f9b682591af2eb Mon Sep 17 00:00:00 2001 From: Jon Bratseth Date: Mon, 3 Jun 2019 14:02:05 +0200 Subject: Test lazy construction --- .../java/com/yahoo/container/di/ContainerTest.java | 21 +++++++++++++++++++++ .../di/componentgraph/core/ComponentGraphTest.java | 21 +++++++++++++++------ 2 files changed, 36 insertions(+), 6 deletions(-) (limited to 'container-di/src/test/java') diff --git a/container-di/src/test/java/com/yahoo/container/di/ContainerTest.java b/container-di/src/test/java/com/yahoo/container/di/ContainerTest.java index 7e01505dc03..3996dff2811 100644 --- a/container-di/src/test/java/com/yahoo/container/di/ContainerTest.java +++ b/container-di/src/test/java/com/yahoo/container/di/ContainerTest.java @@ -306,6 +306,15 @@ public class ContainerTest extends ContainerTestBase { assertTrue(destructableEntity.deconstructed); } + @Test + public void providers_are_invoked_only_when_needed() { + writeBootstrapConfigs("id1", FailOnGetProvider.class); + + Container container = newContainer(dirConfigSource); + + ComponentGraph oldGraph = container.getNewComponentGraph(); + } + static class DestructableEntity { private boolean deconstructed = false; } @@ -323,6 +332,18 @@ public class ContainerTest extends ContainerTestBase { } } + public static class FailOnGetProvider implements Provider { + + public Integer get() { + fail("Should never be called."); + return null; + } + + public void deconstruct() { + } + + } + public static class ComponentTakingConfig extends AbstractComponent { private final TestConfig config; diff --git a/container-di/src/test/java/com/yahoo/container/di/componentgraph/core/ComponentGraphTest.java b/container-di/src/test/java/com/yahoo/container/di/componentgraph/core/ComponentGraphTest.java index 337c875b429..a5934bcf098 100644 --- a/container-di/src/test/java/com/yahoo/container/di/componentgraph/core/ComponentGraphTest.java +++ b/container-di/src/test/java/com/yahoo/container/di/componentgraph/core/ComponentGraphTest.java @@ -39,6 +39,7 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.CoreMatchers.sameInstance; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; @@ -51,6 +52,7 @@ import static org.junit.Assert.fail; * @author ollivir */ public class ComponentGraphTest { + public static class ConfigMap extends HashMap, ConfigInstance> { public ConfigMap() { super(); @@ -240,7 +242,8 @@ public class ComponentGraphTest { Integer instance1 = componentGraph.getInstance(Integer.class); Integer instance2 = componentGraph.getInstance(Integer.class); - assertThat(instance1, not(equalTo(instance2))); + assertEquals(1, instance1.intValue()); + assertEquals(2, instance2.intValue()); } @Test @@ -266,7 +269,7 @@ public class ComponentGraphTest { componentGraph.add(mockComponentNode(ComponentTakingExecutor.class)); componentGraph.add(mockComponentNode(ExecutorProvider.class)); - componentGraph.add(mockComponentNode(IntProvider.class)); + componentGraph.add(mockComponentNode(FailOnGetIntProvider.class)); componentGraph.complete(); assertNotNull(componentGraph.getInstance(ComponentTakingExecutor.class)); @@ -559,21 +562,23 @@ public class ComponentGraphTest { public static class DerivedExecutorProvider extends ExecutorProvider { } - public static class IntProvider implements Provider { + public static class FailOnGetIntProvider implements Provider { + public Integer get() { - throw new AssertionError("Should never be called."); + fail("Should never be called."); + return null; } public void deconstruct() { } + } public static class NewIntProvider implements Provider { int i = 0; public Integer get() { - i++; - return i; + return ++i; } public void deconstruct() { @@ -590,6 +595,7 @@ public class ComponentGraphTest { } public static class ComponentWithInjectConstructor { + public ComponentWithInjectConstructor(TestConfig c, Test2Config c2) { throw new RuntimeException("Should not be called"); } @@ -597,9 +603,11 @@ public class ComponentGraphTest { @Inject public ComponentWithInjectConstructor(Test2Config c) { } + } public static class ComponentWithMultipleConstructors { + private ComponentWithMultipleConstructors(int dummy) { } @@ -615,6 +623,7 @@ public class ComponentGraphTest { public ComponentWithMultipleConstructors(Test2Config c) { this(); } + } public static class ComponentTakingComponentId { -- cgit v1.2.3