Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
fabrizzio-dotCMS committed Oct 25, 2024
1 parent 13170d1 commit ee13ed5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import org.jboss.weld.environment.se.WeldContainer;
import org.junit.runners.model.InitializationError;

/**
* Annotate your JUnit4 test using {@code @DataProviderRunner} class with {@code @RunWith(DataProviderWeldRunner.class)} to run it with Weld container.
*/
public class DataProviderWeldRunner extends DataProviderRunner {

private static final Weld WELD;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.InitializationError;

/**
* Annotate your JUnit4 test class with {@code @RunWith(JUnit4WeldRunner.class)} to run it with Weld container.
*/
public class JUnit4WeldRunner extends BlockJUnit4ClassRunner {

private static final Weld WELD;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,55 @@
package com.dotcms.junit;

import com.dotcms.DataProviderWeldRunner;
import com.dotcms.JUnit4WeldRunner;
import com.dotmarketing.util.Logger;
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import com.tngtech.java.junit.dataprovider.internal.DataConverter;
import com.tngtech.java.junit.dataprovider.internal.TestGenerator;
import com.tngtech.java.junit.dataprovider.internal.TestValidator;
import java.util.Optional;
import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;
import org.junit.Ignore;
import org.junit.rules.RunRules;
import org.junit.runner.Description;
import org.junit.runner.RunWith;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;

import java.util.List;

public class CustomDataProviderRunner extends DataProviderRunner {

// We assume that any test annotated with any of the following runners is meant to be run with Weld
static final List<Class<?>> weldRunners = List.of(JUnit4WeldRunner.class, DataProviderWeldRunner.class);

/**
* Check if the given class is annotated with any of the Weld runners
* @param clazz the class to check
* @return true if the class is annotated with any of the Weld runners
*/
static boolean isWeldRunnerPresent(Class<?> clazz) {
return Optional.ofNullable(clazz.getAnnotation(RunWith.class))
.map(RunWith::value)
.map(runnerClass -> weldRunners.stream()
.anyMatch(weldRunner -> weldRunner.equals(runnerClass)))
.orElse(false);
}

private static final Weld WELD;
private static final WeldContainer CONTAINER;

static {
WELD = new Weld("JUnit4WeldRunner");
WELD = new Weld("CustomDataProviderRunner");
CONTAINER = WELD.initialize();
}

private final boolean instantiateWithWeld;

public CustomDataProviderRunner(Class<?> clazz) throws InitializationError {
super(clazz);
instantiateWithWeld = isWeldRunnerPresent(clazz);
}

@Override
Expand Down Expand Up @@ -73,7 +95,11 @@ public Object invokeExplosively(Object target, Object... params) throws Throwabl

@Override
protected Object createTest() throws Exception {
return CONTAINER.instance().select(getTestClass().getJavaClass()).get();
if (instantiateWithWeld) {
final Class<?> javaClass = getTestClass().getJavaClass();
Logger.debug(this, String.format("Instantiating [%s] with Weld", javaClass));
return CONTAINER.instance().select(javaClass).get();
}
return super.createTest();
}

}

0 comments on commit ee13ed5

Please sign in to comment.