diff --git a/container/test-impl-base/src/main/java/org/jboss/arquillian/container/test/impl/client/container/ClientContainerController.java b/container/test-impl-base/src/main/java/org/jboss/arquillian/container/test/impl/client/container/ClientContainerController.java index f7f57a197..0e95654d0 100644 --- a/container/test-impl-base/src/main/java/org/jboss/arquillian/container/test/impl/client/container/ClientContainerController.java +++ b/container/test-impl-base/src/main/java/org/jboss/arquillian/container/test/impl/client/container/ClientContainerController.java @@ -36,6 +36,7 @@ import org.jboss.arquillian.core.api.Event; import org.jboss.arquillian.core.api.Instance; import org.jboss.arquillian.core.api.annotation.Inject; +import org.jboss.arquillian.test.spi.context.ClassContext; /** * ClientContainerController @@ -56,12 +57,12 @@ public class ClientContainerController implements ContainerController { @Inject private Instance deploymentScenario; + @Inject + private Instance classContextInstance; + @Override public void start(String containerQualifier) { - DeploymentScenario scenario = deploymentScenario.get(); - if (scenario == null) { - throw new IllegalArgumentException("No deployment scenario in context"); - } + DeploymentScenario scenario = obtainDeploymentScenario(); ContainerRegistry registry = containerRegistry.get(); if (registry == null) { @@ -103,10 +104,7 @@ public void start(String containerQualifier) { @Override public void start(String containerQualifier, Map config) { - DeploymentScenario scenario = deploymentScenario.get(); - if (scenario == null) { - throw new IllegalArgumentException("No deployment scenario in context"); - } + DeploymentScenario scenario = obtainDeploymentScenario(); ContainerRegistry registry = containerRegistry.get(); if (registry == null) { @@ -224,6 +222,30 @@ public boolean isStarted(String containerQualifier) { return container.getState() == Container.State.STARTED; } + private DeploymentScenario obtainDeploymentScenario() { + DeploymentScenario scenario = deploymentScenario.get(); + if (scenario == null) { + // A single edge case fix - where deployment is managed and is deployed in BeforeEach method + // In such case, the classContext is not active for the deployment and needs to be activated first + ClassContext classContext = classContextInstance.get(); + if (classContext != null && !classContext.isActive()) { + try { + // Activate the classContext for the class that called this deploy method + // That is the class with BeforeEach method + classContext.activate(Class.forName(Thread.currentThread().getStackTrace()[3].getClassName())); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + // Attempt to get the deployment for the second time + scenario = deploymentScenario.get(); + } + if (scenario == null) { + throw new IllegalArgumentException("No deployment scenario in context"); + } + } + return scenario; + } + protected boolean containerExists(List containers, String name) { for (Container container : containers) { if (container.getName().equals(name)) { diff --git a/container/test-impl-base/src/main/java/org/jboss/arquillian/container/test/impl/client/deployment/ClientDeployer.java b/container/test-impl-base/src/main/java/org/jboss/arquillian/container/test/impl/client/deployment/ClientDeployer.java index 49e91047f..0ca81f07a 100644 --- a/container/test-impl-base/src/main/java/org/jboss/arquillian/container/test/impl/client/deployment/ClientDeployer.java +++ b/container/test-impl-base/src/main/java/org/jboss/arquillian/container/test/impl/client/deployment/ClientDeployer.java @@ -33,6 +33,7 @@ import org.jboss.arquillian.core.api.Event; import org.jboss.arquillian.core.api.Instance; import org.jboss.arquillian.core.api.annotation.Inject; +import org.jboss.arquillian.test.spi.context.ClassContext; import org.jboss.shrinkwrap.api.Archive; import org.jboss.shrinkwrap.api.exporter.ZipExporter; @@ -52,6 +53,9 @@ public class ClientDeployer implements Deployer { @Inject private Instance deploymentScenario; + @Inject + private Instance classContextInstance; + /* (non-Javadoc) * @see org.jboss.arquillian.api.Deployer#deploy(java.lang.String) */ @@ -59,7 +63,23 @@ public class ClientDeployer implements Deployer { public void deploy(String name) { DeploymentScenario scenario = deploymentScenario.get(); if (scenario == null) { - throw new IllegalArgumentException("No deployment scenario in context"); + // A single edge case fix - where deployment is managed and is deployed in BeforeEach method + // In such case, the classContext is not active for the deployment and needs to be activated first + ClassContext classContext = classContextInstance.get(); + if (classContext != null && !classContext.isActive()) { + try { + // Activate the classContext for the class that called this deploy method + // That is the class with BeforeEach method + classContext.activate(Class.forName(Thread.currentThread().getStackTrace()[2].getClassName())); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + // Attempt to get the deployment for the second time + scenario = deploymentScenario.get(); + } + if (scenario == null) { + throw new IllegalArgumentException("No deployment scenario in context"); + } } ContainerRegistry registry = containerRegistry.get(); if (registry == null) {