summaryrefslogtreecommitdiffstats
path: root/container-di
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@verizonmedia.com>2019-06-03 14:02:05 +0200
committerJon Bratseth <bratseth@verizonmedia.com>2019-06-03 14:02:05 +0200
commit424ed0598ed5347b73ae00e5e3f9b682591af2eb (patch)
tree3c5839a2ee2392329ca93451e2138cba13ccbeec /container-di
parent716c293fe3e766c6af8e3e62d65ea40f0bf8369d (diff)
Test lazy construction
Diffstat (limited to 'container-di')
-rw-r--r--container-di/src/test/java/com/yahoo/container/di/ContainerTest.java21
-rw-r--r--container-di/src/test/java/com/yahoo/container/di/componentgraph/core/ComponentGraphTest.java21
2 files changed, 36 insertions, 6 deletions
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<Integer> {
+
+ 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<ConfigKey<? extends ConfigInstance>, 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<Integer> {
+ public static class FailOnGetIntProvider implements Provider<Integer> {
+
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<Integer> {
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 {