Component state and registry - fixes and refactoring
* Errors during shutdown or destruction will be thrown as exceptions. Returning the collection of errors was clumsy, at best - if something goes wrong, the registry has failed anyway. * Recursive initialisation and destruction methods in ComponentState * Use ComponentState's methods while initialising, starting up, shutting down or destroying the registry.
This commit is contained in:
parent
7f919349e9
commit
81ea62047e
5 changed files with 174 additions and 237 deletions
src/test/java/info/ebenoit/ebul/cmp
|
@ -1,6 +1,8 @@
|
|||
package info.ebenoit.ebul.cmp;
|
||||
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
@ -116,8 +118,20 @@ public class TestComponentState
|
|||
final boolean initialised , final boolean active )
|
||||
{
|
||||
final ComponentState cs = new ComponentState( reg , ci );
|
||||
cs.setInitialised( initialised );
|
||||
cs.setActive( active );
|
||||
try {
|
||||
if ( initialised ) {
|
||||
Field f = ComponentState.class.getDeclaredField( "initialised" );
|
||||
f.setAccessible( true );
|
||||
f.setBoolean( cs , true );
|
||||
}
|
||||
if ( active ) {
|
||||
Field f = ComponentState.class.getDeclaredField( "active" );
|
||||
f.setAccessible( true );
|
||||
f.setBoolean( cs , true );
|
||||
}
|
||||
} catch ( NoSuchFieldException | IllegalAccessException e ) {
|
||||
throw new RuntimeException( e );
|
||||
}
|
||||
return cs;
|
||||
}
|
||||
|
||||
|
@ -250,27 +264,6 @@ public class TestComponentState
|
|||
}
|
||||
|
||||
|
||||
/** Test: {@link ComponentState} copies lifecycle actions and can execute them */
|
||||
@Test
|
||||
public void testInitialiseLCA( )
|
||||
throws Throwable
|
||||
{
|
||||
final LCATest lca = new LCATest( );
|
||||
final NewComponentInfo< LCATest > ci = new NewComponentInfo< LCATest >( lca );
|
||||
for ( final LifecycleStage stage : LifecycleStage.values( ) ) {
|
||||
ci.setLifecycleAction( stage , ( o ) -> {
|
||||
o.stage = stage;
|
||||
} );
|
||||
}
|
||||
final ComponentState cs = new ComponentState( this.reg , ci );
|
||||
|
||||
for ( final LifecycleStage stage : LifecycleStage.values( ) ) {
|
||||
cs.runLifecycleAction( stage );
|
||||
Assert.assertSame( lca.stage , stage );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** Test: {@link ComponentState#toString()} on anonymous components */
|
||||
@Test
|
||||
public void testToStringAnon( )
|
||||
|
@ -327,8 +320,7 @@ public class TestComponentState
|
|||
public void testStartFailedRegistry( )
|
||||
{
|
||||
final NewComponentInfo< Object > ci = new NewComponentInfo< Object >( new Object( ) );
|
||||
final ComponentState cs = new ComponentState( this.reg , ci );
|
||||
cs.setInitialised( true );
|
||||
final ComponentState cs = makeState( ci , reg , true , false );
|
||||
this.reg.failed = true;
|
||||
cs.start( );
|
||||
}
|
||||
|
@ -339,8 +331,7 @@ public class TestComponentState
|
|||
public void testStartInactiveRegistry( )
|
||||
{
|
||||
final NewComponentInfo< Object > ci = new NewComponentInfo< Object >( new Object( ) );
|
||||
final ComponentState cs = new ComponentState( this.reg , ci );
|
||||
cs.setInitialised( true );
|
||||
final ComponentState cs = makeState( ci , reg , true , false );
|
||||
cs.start( );
|
||||
}
|
||||
|
||||
|
@ -350,8 +341,7 @@ public class TestComponentState
|
|||
public void testStartNoAction( )
|
||||
{
|
||||
final NewComponentInfo< Object > ci = new NewComponentInfo< Object >( new Object( ) );
|
||||
final ComponentState cs = new ComponentState( this.reg , ci );
|
||||
cs.setInitialised( true );
|
||||
final ComponentState cs = makeState( ci , reg , true , false );
|
||||
this.reg.active = true;
|
||||
cs.start( );
|
||||
Assert.assertTrue( cs.isActive( ) );
|
||||
|
@ -367,8 +357,7 @@ public class TestComponentState
|
|||
.setLifecycleAction( LifecycleStage.START , ( o ) -> {
|
||||
o.stage = LifecycleStage.START;
|
||||
} );
|
||||
final ComponentState cs = new ComponentState( this.reg , ci );
|
||||
cs.setInitialised( true );
|
||||
final ComponentState cs = makeState( ci , reg , true , false );
|
||||
this.reg.active = true;
|
||||
cs.start( );
|
||||
Assert.assertSame( LifecycleStage.START , lcaTest.stage );
|
||||
|
@ -473,7 +462,7 @@ public class TestComponentState
|
|||
final NewComponentInfo< Object > ci = new NewComponentInfo< Object >( new Object( ) );
|
||||
final ComponentState cs = TestComponentState.makeState( ci , this.reg , true , true );
|
||||
this.reg.active = true;
|
||||
Assert.assertNull( cs.stop( ) );
|
||||
cs.stop( );
|
||||
Assert.assertFalse( cs.isActive( ) );
|
||||
}
|
||||
|
||||
|
@ -489,25 +478,31 @@ public class TestComponentState
|
|||
} );
|
||||
final ComponentState cs = TestComponentState.makeState( ci , this.reg , true , true );
|
||||
this.reg.active = true;
|
||||
Assert.assertNull( cs.stop( ) );
|
||||
cs.stop( );
|
||||
Assert.assertFalse( cs.isActive( ) );
|
||||
Assert.assertSame( LifecycleStage.STOP , lcaTest.stage );
|
||||
}
|
||||
|
||||
|
||||
/** Test: {@link ComponentState#stop()} returns an exception thrown by the configured stop action */
|
||||
/** Test: {@link ComponentState#stop()} transmits an exception thrown by the configured stop action */
|
||||
@Test
|
||||
public void testStopActionFailure( )
|
||||
{
|
||||
final Exception failure = new Exception( );
|
||||
final ComponentShutdownException failure = new ComponentShutdownException( );
|
||||
final NewComponentInfo< Object > ci = new NewComponentInfo< Object >( new Object( ) ) //
|
||||
.setLifecycleAction( LifecycleStage.STOP , ( o ) -> {
|
||||
throw failure;
|
||||
} );
|
||||
final ComponentState cs = TestComponentState.makeState( ci , this.reg , true , true );
|
||||
this.reg.active = true;
|
||||
Assert.assertSame( failure , cs.stop( ) );
|
||||
Assert.assertTrue( cs.isActive( ) );
|
||||
try {
|
||||
cs.stop( );
|
||||
} catch ( ComponentShutdownException e ) {
|
||||
Assert.assertSame( failure , e );
|
||||
Assert.assertTrue( cs.isActive( ) );
|
||||
return;
|
||||
}
|
||||
Assert.fail( "expected ComponentShutdownException" );
|
||||
}
|
||||
|
||||
|
||||
|
@ -523,7 +518,7 @@ public class TestComponentState
|
|||
cs2.addDependency( cs1 );
|
||||
|
||||
this.reg.active = true;
|
||||
Assert.assertNull( cs1.stop( ) );
|
||||
cs1.stop( );
|
||||
Assert.assertFalse( cs2.isActive( ) );
|
||||
}
|
||||
|
||||
|
@ -565,7 +560,7 @@ public class TestComponentState
|
|||
}
|
||||
|
||||
|
||||
/** Test: {@link ComponentState#restart()} throws a {@link ComponentRestartException} when stop fails */
|
||||
/** Test: {@link ComponentState#restart()} throws a {@link ComponentShutdownException} when stop fails */
|
||||
@Test
|
||||
public void testRestartWhenStopFails( )
|
||||
{
|
||||
|
@ -578,20 +573,20 @@ public class TestComponentState
|
|||
this.reg.active = true;
|
||||
try {
|
||||
cs.restart( );
|
||||
} catch ( final ComponentRestartException e ) {
|
||||
} catch ( final ComponentShutdownException e ) {
|
||||
Assert.assertSame( failure , e.getCause( ) );
|
||||
Assert.assertTrue( cs.isActive( ) );
|
||||
return;
|
||||
}
|
||||
Assert.fail( "no ComponentRestartException" );
|
||||
Assert.fail( "no ComponentShutdownException" );
|
||||
}
|
||||
|
||||
|
||||
/** Test: {@link ComponentState#restart()} throws a {@link ComponentRestartException} when start fails */
|
||||
/** Test: {@link ComponentState#restart()} throws a {@link ComponentStartupException} when start fails */
|
||||
@Test
|
||||
public void testRestartWhenStartFails( )
|
||||
{
|
||||
final ComponentStartupException failure = new ComponentStartupException( );
|
||||
final Exception failure = new Exception( );
|
||||
final NewComponentInfo< RestartTest > ci = RestartTest.getDef( );
|
||||
ci.setLifecycleAction( LifecycleStage.START , ( o ) -> {
|
||||
throw failure;
|
||||
|
@ -600,12 +595,12 @@ public class TestComponentState
|
|||
this.reg.active = true;
|
||||
try {
|
||||
cs.restart( );
|
||||
} catch ( final ComponentRestartException e ) {
|
||||
} catch ( final ComponentStartupException e ) {
|
||||
Assert.assertSame( failure , e.getCause( ) );
|
||||
Assert.assertFalse( cs.isActive( ) );
|
||||
return;
|
||||
}
|
||||
Assert.fail( "no ComponentRestartException" );
|
||||
Assert.fail( "no ComponentStartupException" );
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue