{
registry.create(container1, serviceLoader).setState(State.STARTED);
registry.create(container2, serviceLoader).setState(State.STARTED);
// setup all deployment as deployed so that we can observe UnDeployDeployment events
scenario.deployment(new DeploymentTargetDescription(DEPLOYMENT_1_NAME)).deployed();
scenario.deployment(new DeploymentTargetDescription(DEPLOYMENT_2_NAME)).deployed();
scenario.deployment(new DeploymentTargetDescription(DEPLOYMENT_3_NAME)).deployed();
scenario.deployment(new DeploymentTargetDescription(DEPLOYMENT_4_NAME)).deployed();
fire(new UnDeployManagedDeployments());
assertEventFired(UnDeployDeployment.class, 4);
assertEventFiredInContext(UnDeployDeployment.class, ContainerContext.class);
assertEventFiredInContext(UnDeployDeployment.class, DeploymentContext.class);
assertEventFired(BeforeUnDeploy.class, 4);
assertEventFiredInContext(BeforeUnDeploy.class, ContainerContext.class);
assertEventFiredInContext(BeforeUnDeploy.class, DeploymentContext.class);
assertEventFired(AfterUnDeploy.class, 4);
assertEventFiredInContext(AfterUnDeploy.class, ContainerContext.class);
assertEventFiredInContext(AfterUnDeploy.class, DeploymentContext.class);
verify(deployableContainer1, times(1)).undeploy(isA(Archive.class));
verify(deployableContainer1, times(1)).undeploy(isA(Descriptor.class));
verify(deployableContainer2, times(2)).undeploy(isA(Archive.class));
InOrder ordered = inOrder(deployableContainer1, deployableContainer2);
ordered.verify(deployableContainer1, times(1)).undeploy(
scenario.deployment(new DeploymentTargetDescription(DEPLOYMENT_4_NAME)).getDescription().getDescriptor());
ordered.verify(deployableContainer2, times(1)).undeploy(
scenario.deployment(new DeploymentTargetDescription(DEPLOYMENT_3_NAME)).getDescription().getArchive());
ordered.verify(deployableContainer1, times(1)).undeploy(
scenario.deployment(new DeploymentTargetDescription(DEPLOYMENT_1_NAME)).getDescription().getArchive());
ordered.verify(deployableContainer2, times(1)).undeploy(
scenario.deployment(new DeploymentTargetDescription(DEPLOYMENT_2_NAME)).getDescription().getTestableArchive());
}