diff options
author | gjoranv <gv@yahoo-inc.com> | 2017-05-18 14:35:52 +0200 |
---|---|---|
committer | gjoranv <gv@yahoo-inc.com> | 2017-05-18 16:19:14 +0200 |
commit | a20ede3c2016d6542020651fc51733d4fbc33c63 (patch) | |
tree | 188fd012545ff2ec20cb60e7cf9b82faa634e6d7 /container-di | |
parent | a3ef767b6961450299bbdf7bd813d5a4239917fa (diff) |
Add test for component constructor exceptions.
- Verify correct exception type.
Diffstat (limited to 'container-di')
-rw-r--r-- | container-di/src/test/scala/com/yahoo/container/di/ContainerTest.scala | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/container-di/src/test/scala/com/yahoo/container/di/ContainerTest.scala b/container-di/src/test/scala/com/yahoo/container/di/ContainerTest.scala index 6cccd5c06a4..31c0f787269 100644 --- a/container-di/src/test/scala/com/yahoo/container/di/ContainerTest.scala +++ b/container-di/src/test/scala/com/yahoo/container/di/ContainerTest.scala @@ -20,6 +20,7 @@ import scala.concurrent.ExecutionContext.Implicits.global import scala.util.Try import com.yahoo.container.di.config.RestApiContext import com.yahoo.container.bundle.MockBundle +import com.yahoo.container.di.componentgraph.core.ComponentNode.ComponentConstructorException import scala.language.postfixOps @@ -118,6 +119,38 @@ class ContainerTest { } @Test + def previous_graph_is_retained_when_new_graph_contains_component_that_throws_exception_in_ctor() { + val simpleComponentEntry = ComponentEntry("simpleComponent", classOf[SimpleComponent]) + + writeBootstrapConfigs(Array(simpleComponentEntry)) + val container = newContainer(dirConfigSource) + var currentGraph = container.runOnce() + + val simpleComponent = currentGraph.getInstance(classOf[SimpleComponent]) + + writeBootstrapConfigs("thrower", classOf[ComponentThrowingExceptionInConstructor]) + container.reloadConfig(2) + try { + currentGraph = container.runOnce(currentGraph) + fail("Expected exception") + } catch { + case _:ComponentConstructorException => // Expected, do nothing + case _: Throwable => fail("Expected ComponentConstructorException") + } + assertEquals(1, currentGraph.generation) + + val componentTakingConfigEntry = ComponentEntry("componentTakingConfig", classOf[ComponentTakingConfig]) + dirConfigSource.writeConfig("test", """stringVal "myString" """) + writeBootstrapConfigs(Array(simpleComponentEntry, componentTakingConfigEntry)) + container.reloadConfig(3) + currentGraph = container.runOnce(currentGraph) + + assertEquals(3, currentGraph.generation) + assertSame(simpleComponent, currentGraph.getInstance(classOf[SimpleComponent])) + assertNotNull(currentGraph.getInstance(classOf[ComponentTakingConfig])) + } + + @Test def previous_graph_is_retained_when_new_graph_throws_exception_for_missing_config() { val simpleComponentEntry = ComponentEntry("simpleComponent", classOf[SimpleComponent]) @@ -134,7 +167,8 @@ class ContainerTest { currentGraph = container.runOnce(currentGraph) fail("Expected exception") } catch { - case e: Exception => e.printStackTrace() + case _:IllegalArgumentException => // Expected, do nothing + case _: Throwable => fail("Expected IllegalArgumentException") } assertEquals(1, currentGraph.generation) @@ -322,6 +356,10 @@ object ContainerTest { require(config != null) } + class ComponentThrowingExceptionInConstructor() { + throw new RuntimeException("This component fails upon construction.") + } + class ComponentThrowingExceptionForMissingConfig(intConfig: IntConfig) extends AbstractComponent { fail("This component should never be created. Only used for tests where 'int' config is missing.") } |