+ public void testEnvironmentApplicationRollbackOnException()
+ throws Exception {
+ IMocksControl control = EasyMock.createStrictControl();
+
+ Environment environment = new Environment(_tracker);
+ Application application = control.createMock(Application.class,
+ new ConstructorArgs(Application.class.getConstructor()),
+ Application.class.getDeclaredMethod("doStart"));
+
+ application.doStart();
+ EasyMock.expectLastCall().andThrow(new RuntimeException());
+ control.replay();
+
+ try {
+ Container container = new Container("root", new Component[] {
+ environment, application }, new ProvidedInterface[0],
+ new RequiredInterface[0]);
+
+ container.start();
+ } catch (RuntimeException e) {
+ AssertionUtils.assertEquals(new String[] { "start.environment",
+ "stop.environment" }, _tracker.getEvents(
+ Thread.currentThread()).toArray(new String[0]));
+ ProvidedInterface[] envServices = environment.getRunningInterfaces();
+ assertEquals(0, envServices.length);
+ return;
+ }
+ fail();
+ }
+
+ public void testEnvironmentApplicationRollbackOnExceptionWithExceptionOnStop()
+ throws Exception {
+ IMocksControl control = EasyMock.createControl();
+
+ Environment environment = new Environment(_tracker);
+ // Application 1 will throw an exception while stopping.
+ Application application1 = control.createMock(Application.class,
+ new ConstructorArgs(Application.class.getConstructor()),
+ Application.class.getDeclaredMethod("doStop"));
+
+ application1.doStop();
+ EasyMock.expectLastCall().andThrow(new RuntimeException());
+
+ // application 2 will throw an exception while starting
+ Application application2 = control.createMock(Application.class,
+ new ConstructorArgs(Application.class.getConstructor()),
+ Application.class.getDeclaredMethod("doStart"));
+
+ application2.doStart();
+ EasyMock.expectLastCall().andThrow(new RuntimeException());
+
+ control.replay();
+
+ try {
+ Container container = new Container("root", new Component[] {
+ environment, application1, application2 }, new ProvidedInterface[0],
+ new RequiredInterface[0]);
+
+ container.start();
+ } catch (RuntimeException e) {
+ AssertionUtils.assertEquals(new String[] { "start.environment",
+ "stop.environment" }, _tracker.getEvents(
+ Thread.currentThread()).toArray(new String[0]));
+ ProvidedInterface[] envServices = environment.getRunningInterfaces();
+ assertEquals(0, envServices.length);
+ return;
+ }
+ fail();
+ }