+ 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);
+ assertNull(environment.getProvidedInterfaces()[0].getImplementation());
+ assertNull(environment.getProvidedInterfaces()[0].getImplementation());
+ 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);
+ assertNull(environment.getProvidedInterfaces()[0].getImplementation());
+ assertNull(environment.getProvidedInterfaces()[0].getImplementation());
+ return;
+ }
+ fail();
+ }
+
+ public void testOptionalRequiredInterfaceProvidedOptionalInternal() {
+ Application application = new Application(true);
+ Container container = new Container("top", new Component[] { application },
+ new ProvidedInterface[0], Application.required(true));
+ Environment env = new Environment();
+ container.getRequiredInterfaces()[0].setProvider(
+ env.getProvidedInterfaces()[0]);
+ container.getRequiredInterfaces()[1].setProvider(
+ env.getProvidedInterfaces()[1]);
+ container.start();
+ assertSame(env.getProvidedInterfaces()[0], container.getRequiredInterfaces()[0].getProvider());
+ assertSame(env.getProvidedInterfaces()[1], container.getRequiredInterfaces()[1].getProvider());
+ assertSame(env.getProvidedInterfaces()[0], application.getRequiredInterfaces()[0].getProvider());
+ assertSame(env.getProvidedInterfaces()[1], application.getRequiredInterfaces()[1].getProvider());
+ }
+
+ public void testOptionalRequiredInterfaceNotProvidedOptionalInternal() {
+ Application application = new Application(true);
+ Container container = new Container("top", new Component[] { application },
+ new ProvidedInterface[0], Application.required(true));
+ Environment env = new Environment();
+ container.getRequiredInterfaces()[0].setProvider(
+ env.getProvidedInterfaces()[0]);
+ container.start();
+ assertSame(env.getProvidedInterfaces()[0], container.getRequiredInterfaces()[0].getProvider());
+ assertNull(container.getRequiredInterfaces()[1].getProvider());
+ assertSame(env.getProvidedInterfaces()[0], application.getRequiredInterfaces()[0].getProvider());
+ assertNull(application.getRequiredInterfaces()[1].getProvider());
+ }
+
+ public void testOptionalRequiredInterfaceProvidedMandatoryInternal() {
+ Application application = new Application();
+ Container container = new Container("top", new Component[] { application },
+ new ProvidedInterface[0], Application.required(true));
+ Environment env = new Environment();
+ container.getRequiredInterfaces()[0].setProvider(
+ env.getProvidedInterfaces()[0]);
+ container.getRequiredInterfaces()[1].setProvider(
+ env.getProvidedInterfaces()[1]);
+ container.start();
+ assertSame(env.getProvidedInterfaces()[0], container.getRequiredInterfaces()[0].getProvider());
+ assertSame(env.getProvidedInterfaces()[1], container.getRequiredInterfaces()[1].getProvider());
+ assertSame(env.getProvidedInterfaces()[0], application.getRequiredInterfaces()[0].getProvider());
+ assertSame(env.getProvidedInterfaces()[1], application.getRequiredInterfaces()[1].getProvider());
+ }