Skip to content

Commit

Permalink
[ARQ-2231] The JUnit 5 container does not work with manual mode tests
Browse files Browse the repository at this point in the history
  • Loading branch information
petrberan committed Mar 22, 2024
1 parent e8f2aee commit b44241a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -56,12 +57,12 @@ public class ClientContainerController implements ContainerController {
@Inject
private Instance<DeploymentScenario> deploymentScenario;

@Inject
private Instance<ClassContext> 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) {
Expand Down Expand Up @@ -103,10 +104,7 @@ public void start(String containerQualifier) {

@Override
public void start(String containerQualifier, Map<String, String> 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) {
Expand Down Expand Up @@ -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<Container> containers, String name) {
for (Container container : containers) {
if (container.getName().equals(name)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -52,14 +53,33 @@ public class ClientDeployer implements Deployer {
@Inject
private Instance<DeploymentScenario> deploymentScenario;

@Inject
private Instance<ClassContext> classContextInstance;

/* (non-Javadoc)
* @see org.jboss.arquillian.api.Deployer#deploy(java.lang.String)
*/
@Override
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) {
Expand Down

0 comments on commit b44241a

Please sign in to comment.