Component state - tests for init and destroy

This commit is contained in:
Emmanuel BENOîT 2015-09-17 22:37:11 +02:00
parent da28d57f10
commit 7cfb53c60e
2 changed files with 208 additions and 2 deletions

2
TODO
View file

@ -1,5 +1,5 @@
To Do:
* Test for ComponentState.{init,destroy} and driver-related stuff
* Tests for driver-related stuff in ComponentState
* Registry tests
* Registry doc
* General usage documentation

View file

@ -305,6 +305,210 @@ public class TestComponentState
}
/** Test: {@link ComponentState#init()} with no lifecycle action runs without a hitch */
@Test
public void testInitNoAction( )
{
final NewComponentInfo< Object > ci = new NewComponentInfo< Object >( new Object( ) );
final ComponentState cs = makeState( ci , reg , false , false );
cs.init( );
Assert.assertTrue( cs.isInitialised( ) );
}
/** Test: {@link ComponentState#init()} runs the initialisation action */
@Test
public void testInitAction( )
{
final LCATest lcaTest = new LCATest( );
final NewComponentInfo< LCATest > ci = new NewComponentInfo< LCATest >( lcaTest ) //
.setLifecycleAction( LifecycleStage.INITIALISE , ( o ) -> {
o.stage = LifecycleStage.INITIALISE;
} );
final ComponentState cs = makeState( ci , reg , false , false );
cs.init( );
Assert.assertSame( LifecycleStage.INITIALISE , lcaTest.stage );
Assert.assertTrue( cs.isInitialised( ) );
}
/** Test: {@link ComponentState#init()} on an initialised component does nothing */
@Test
public void testInitAlreadyInitialised( )
{
final LCATest lcaTest = new LCATest( );
final NewComponentInfo< LCATest > ci = new NewComponentInfo< LCATest >( lcaTest ) //
.setLifecycleAction( LifecycleStage.INITIALISE , ( o ) -> {
o.stage = LifecycleStage.INITIALISE;
} );
final ComponentState cs = makeState( ci , reg , true , false );
cs.init( );
Assert.assertNull( lcaTest.stage );
}
/** Test: {@link ComponentState#init()} re-throws {@link ComponentInitialisationException} */
@Test
public void testInitActionExceptionPassthrough( )
{
final ComponentInitialisationException failure = new ComponentInitialisationException( );
final NewComponentInfo< Object > ci = new NewComponentInfo< Object >( new Object( ) ) //
.setLifecycleAction( LifecycleStage.INITIALISE , ( o ) -> {
throw failure;
} );
final ComponentState cs = TestComponentState.makeState( ci , this.reg , false , false );
try {
cs.init( );
} catch ( final ComponentInitialisationException e ) {
Assert.assertFalse( cs.isInitialised( ) );
Assert.assertSame( failure , e );
return;
}
Assert.fail( "no ComponentInitialisationException" );
}
/**
* Test: {@link ComponentState#init()} transmits other exceptions using a {@link ComponentInitialisationException}
*/
@Test
public void testInitActionExceptionTransmit( )
{
final Exception failure = new Exception( );
final NewComponentInfo< Object > ci = new NewComponentInfo< Object >( new Object( ) ) //
.setLifecycleAction( LifecycleStage.INITIALISE , ( o ) -> {
throw failure;
} );
final ComponentState cs = TestComponentState.makeState( ci , this.reg , false , false );
try {
cs.init( );
} catch ( final ComponentInitialisationException e ) {
Assert.assertFalse( cs.isInitialised( ) );
Assert.assertSame( failure , e.getCause( ) );
return;
}
Assert.fail( "no ComponentInitialisationException" );
}
/** Test: {@link ComponentState#init()} attempts to initialise a component's dependencies */
@Test
public void testInitDependencies( )
{
final LCATest lcaTest = new LCATest( );
final NewComponentInfo< LCATest > ci1 = new NewComponentInfo< LCATest >( lcaTest ) //
.setLifecycleAction( LifecycleStage.INITIALISE , ( o ) -> {
o.stage = LifecycleStage.INITIALISE;
} );
final ComponentState cs1 = TestComponentState.makeState( ci1 , this.reg , false , false );
final NewComponentInfo< Object > ci2 = new NewComponentInfo< Object >( new Object( ) );
final ComponentState cs2 = TestComponentState.makeState( ci2 , this.reg , false , false );
cs2.addDependency( cs1 );
cs2.init( );
Assert.assertSame( LifecycleStage.INITIALISE , lcaTest.stage );
}
/** Test: {@link ComponentState#destroy()} throws {@link IllegalStateException} if the component is active */
@Test( expected = IllegalStateException.class )
public void testDestroyActive( )
{
final NewComponentInfo< Object > ci = new NewComponentInfo< Object >( new Object( ) );
final ComponentState cs = TestComponentState.makeState( ci , this.reg , true , true );
cs.destroy( );
}
/** Test: {@link ComponentState#destroy()} with no configured action works */
@Test
public void testDestroyNoAction( )
{
final NewComponentInfo< Object > ci = new NewComponentInfo< Object >( new Object( ) );
final ComponentState cs = TestComponentState.makeState( ci , this.reg , true , false );
cs.destroy( );
Assert.assertFalse( cs.isInitialised( ) );
}
/** Test: {@link ComponentState#destroy()} executes the configured destruction action */
@Test
public void testDestroyAction( )
{
final LCATest lcaTest = new LCATest( );
final NewComponentInfo< LCATest > ci = new NewComponentInfo< LCATest >( lcaTest ) //
.setLifecycleAction( LifecycleStage.DESTROY , ( o ) -> {
o.stage = LifecycleStage.DESTROY;
} );
final ComponentState cs = TestComponentState.makeState( ci , this.reg , true , false );
cs.destroy( );
Assert.assertFalse( cs.isInitialised( ) );
Assert.assertSame( LifecycleStage.DESTROY , lcaTest.stage );
}
/** Test: {@link ComponentState#destroy()} re-throws {@link ComponentDestructionException} */
@Test
public void testDestroyActionExceptionPassthrough( )
{
final ComponentDestructionException failure = new ComponentDestructionException( );
final NewComponentInfo< Object > ci = new NewComponentInfo< Object >( new Object( ) ) //
.setLifecycleAction( LifecycleStage.DESTROY , ( o ) -> {
throw failure;
} );
final ComponentState cs = TestComponentState.makeState( ci , this.reg , true , false );
try {
cs.destroy( );
} catch ( final ComponentDestructionException e ) {
Assert.assertTrue( cs.isInitialised( ) );
Assert.assertSame( failure , e );
return;
}
Assert.fail( "no ComponentDestructionException" );
}
/**
* Test: {@link ComponentState#destroy()} transmits other exceptions using a {@link ComponentDestructionException}
*/
@Test
public void testDestroyActionExceptionTransmit( )
{
final Exception failure = new Exception( );
final NewComponentInfo< Object > ci = new NewComponentInfo< Object >( new Object( ) ) //
.setLifecycleAction( LifecycleStage.DESTROY , ( o ) -> {
throw failure;
} );
final ComponentState cs = TestComponentState.makeState( ci , this.reg , true , false );
try {
cs.destroy( );
} catch ( final ComponentDestructionException e ) {
Assert.assertTrue( cs.isInitialised( ) );
Assert.assertSame( failure , e.getCause( ) );
return;
}
Assert.fail( "no ComponentDestructionException" );
}
/** Test: {@link ComponentState#destroy()} destroys reverse dependencies */
@Test
public void testDestroyReverseDeps( )
{
final NewComponentInfo< Object > ci1 = new NewComponentInfo< Object >( new Object( ) );
final ComponentState cs1 = TestComponentState.makeState( ci1 , this.reg , true , false );
final NewComponentInfo< Object > ci2 = new NewComponentInfo< Object >( new Object( ) );
final ComponentState cs2 = TestComponentState.makeState( ci2 , this.reg , true , false );
cs2.addDependency( cs1 );
cs1.destroy( );
Assert.assertFalse( cs2.isInitialised( ) );
}
/** Test: {@link ComponentState#start()} on uninitialised components throws {@link IllegalStateException} */
@Test( expected = IllegalStateException.class )
public void testStartUninitialised( )
@ -396,6 +600,7 @@ public class TestComponentState
try {
cs.start( );
} catch ( final ComponentStartupException e ) {
Assert.assertFalse( cs.isActive( ) );
Assert.assertSame( failure , e );
return;
}
@ -417,6 +622,7 @@ public class TestComponentState
try {
cs.start( );
} catch ( final ComponentStartupException e ) {
Assert.assertFalse( cs.isActive( ) );
Assert.assertSame( failure , e.getCause( ) );
return;
}
@ -509,7 +715,7 @@ public class TestComponentState
/** Test: {@link ComponentState#stop()} stops reverse dependencies */
@Test
public void testStopActionStopReverseDeps( )
public void testStopReverseDeps( )
{
final NewComponentInfo< Object > ci1 = new NewComponentInfo< Object >( new Object( ) );
final ComponentState cs1 = TestComponentState.makeState( ci1 , this.reg , true , true );