mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
junit: allow passing param set name to junit @Parameterized through program parameters
This commit is contained in:
@@ -61,6 +61,7 @@ import com.intellij.openapi.util.Getter;
|
||||
import com.intellij.openapi.util.Key;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.openapi.util.registry.Registry;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.openapi.vfs.CharsetToolkit;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.refactoring.listeners.RefactoringElementListener;
|
||||
@@ -192,7 +193,14 @@ public abstract class TestObject implements JavaCommandLine {
|
||||
}
|
||||
|
||||
protected void initialize() throws ExecutionException {
|
||||
JavaParametersUtil.configureConfiguration(myJavaParameters, myConfiguration);
|
||||
String parameters = myConfiguration.getProgramParameters();
|
||||
myConfiguration.getPersistentData().setProgramParameters(null);
|
||||
try {
|
||||
JavaParametersUtil.configureConfiguration(myJavaParameters, myConfiguration);
|
||||
}
|
||||
finally {
|
||||
myConfiguration.getPersistentData().setProgramParameters(parameters);
|
||||
}
|
||||
myJavaParameters.setMainClass(JUnitConfiguration.JUNIT_START_CLASS);
|
||||
final Module module = myConfiguration.getConfigurationModule().getModule();
|
||||
if (myJavaParameters.getJdk() == null){
|
||||
@@ -207,6 +215,9 @@ public abstract class TestObject implements JavaCommandLine {
|
||||
myJavaParameters.getClassPath().add(PathUtil.getJarPathForClass(ServiceMessageTypes.class));
|
||||
}
|
||||
myJavaParameters.getProgramParametersList().add(JUnitStarter.IDE_VERSION + JUnitStarter.VERSION);
|
||||
if (!StringUtil.isEmptyOrSpaces(parameters)) {
|
||||
myJavaParameters.getProgramParametersList().add("@name" + parameters);
|
||||
}
|
||||
for (RunConfigurationExtension ext : Extensions.getExtensions(RunConfigurationExtension.EP_NAME)) {
|
||||
ext.updateJavaParameters(myConfiguration, myJavaParameters, getRunnerSettings());
|
||||
}
|
||||
|
||||
+1
-1
@@ -214,7 +214,7 @@ public class JUnitConfigurable extends SettingsEditor<JUnitConfiguration> implem
|
||||
}
|
||||
});
|
||||
|
||||
myCommonJavaParameters.getProgramParametersComponent().setVisible(false);
|
||||
//myCommonJavaParameters.getProgramParametersComponent().setVisible(false);
|
||||
|
||||
setAnchor(mySearchForTestsLabel);
|
||||
myModule.setAnchor(myAlternativeJREPanel.getCbEnabled());
|
||||
|
||||
@@ -37,7 +37,7 @@ public class JUnit3IdeaTestRunner extends TestRunner implements IdeaTestRunner {
|
||||
super(DeafStream.DEAF_PRINT_STREAM);
|
||||
}
|
||||
|
||||
public int startRunnerWithArgs(String[] args, ArrayList listeners, boolean sendTree) {
|
||||
public int startRunnerWithArgs(String[] args, ArrayList listeners, String name, boolean sendTree) {
|
||||
myListeners = listeners;
|
||||
mySendTree = sendTree;
|
||||
if (sendTree) {
|
||||
@@ -74,7 +74,7 @@ public class JUnit3IdeaTestRunner extends TestRunner implements IdeaTestRunner {
|
||||
myTestsListener = new TestResultsSender(myRegistry);
|
||||
}
|
||||
|
||||
public Object getTestToStart(String[] args) {
|
||||
public Object getTestToStart(String[] args, String name) {
|
||||
return TestRunnerUtil.getTestSuite(this, args);
|
||||
}
|
||||
|
||||
|
||||
@@ -34,9 +34,9 @@ public class JUnit4IdeaTestRunner implements IdeaTestRunner {
|
||||
private RunListener myTestsListener;
|
||||
private OutputObjectRegistry myRegistry;
|
||||
|
||||
public int startRunnerWithArgs(String[] args, ArrayList listeners, boolean sendTree) {
|
||||
public int startRunnerWithArgs(String[] args, ArrayList listeners, String name, boolean sendTree) {
|
||||
|
||||
final Request request = JUnit4TestRunnerUtil.buildRequest(args, sendTree);
|
||||
final Request request = JUnit4TestRunnerUtil.buildRequest(args, name, sendTree);
|
||||
if (request == null) return -1;
|
||||
final Runner testRunner = request.getRunner();
|
||||
try {
|
||||
@@ -145,8 +145,8 @@ public class JUnit4IdeaTestRunner implements IdeaTestRunner {
|
||||
}
|
||||
}
|
||||
|
||||
public Object getTestToStart(String[] args) {
|
||||
final Request request = JUnit4TestRunnerUtil.buildRequest(args, false);
|
||||
public Object getTestToStart(String[] args, String name) {
|
||||
final Request request = JUnit4TestRunnerUtil.buildRequest(args, name, false);
|
||||
if (request == null) return null;
|
||||
final Runner testRunner = request.getRunner();
|
||||
Description description = null;
|
||||
|
||||
@@ -33,6 +33,7 @@ import org.junit.runners.model.FrameworkMethod;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.text.MessageFormat;
|
||||
import java.util.*;
|
||||
@@ -43,7 +44,7 @@ public class JUnit4TestRunnerUtil {
|
||||
*/
|
||||
private static final ResourceBundle ourBundle = ResourceBundle.getBundle("RuntimeBundle");
|
||||
|
||||
public static Request buildRequest(String[] suiteClassNames, boolean notForked) {
|
||||
public static Request buildRequest(String[] suiteClassNames, String name, boolean notForked) {
|
||||
if (suiteClassNames.length == 0) {
|
||||
return null;
|
||||
}
|
||||
@@ -143,15 +144,9 @@ public class JUnit4TestRunnerUtil {
|
||||
//return simple method runner
|
||||
}
|
||||
} else {
|
||||
final Class runnerClass = clazzAnnotation.value();
|
||||
if (Parameterized.class.isAssignableFrom(runnerClass)) {
|
||||
try {
|
||||
Class.forName("org.junit.runners.BlockJUnit4ClassRunner"); //ignore for junit4.4 and <
|
||||
return Request.runner(new ParameterizedMethodRunner(clazz, methodName));
|
||||
}
|
||||
catch (Throwable throwable) {
|
||||
//return simple method runner
|
||||
}
|
||||
final Request request = getParameterizedRequest(name, clazz, methodName, clazzAnnotation);
|
||||
if (request != null) {
|
||||
return request;
|
||||
}
|
||||
}
|
||||
try {
|
||||
@@ -163,6 +158,15 @@ public class JUnit4TestRunnerUtil {
|
||||
//ignore
|
||||
}
|
||||
return Request.method(clazz, methodName);
|
||||
} else if (name != null && suiteClassNames.length == 1) {
|
||||
final Class clazz = loadTestClass(suiteClassName);
|
||||
if (clazz != null) {
|
||||
final RunWith clazzAnnotation = (RunWith)clazz.getAnnotation(RunWith.class);
|
||||
final Request request = getParameterizedRequest(name, clazz, null, clazzAnnotation);
|
||||
if (request != null) {
|
||||
return request;
|
||||
}
|
||||
}
|
||||
}
|
||||
appendTestClass(result, suiteClassName);
|
||||
}
|
||||
@@ -183,6 +187,22 @@ public class JUnit4TestRunnerUtil {
|
||||
return Request.classes(getArrayOfClasses(result));
|
||||
}
|
||||
|
||||
private static Request getParameterizedRequest(String name, Class clazz, String methodName, RunWith clazzAnnotation) {
|
||||
if (clazzAnnotation == null) return null;
|
||||
|
||||
final Class runnerClass = clazzAnnotation.value();
|
||||
if (Parameterized.class.isAssignableFrom(runnerClass)) {
|
||||
try {
|
||||
Class.forName("org.junit.runners.BlockJUnit4ClassRunner"); //ignore for junit4.4 and <
|
||||
return Request.runner(new SelectedParameterizedRunner(clazz, name, methodName));
|
||||
}
|
||||
catch (Throwable throwable) {
|
||||
//return simple method runner
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static Request createIgnoreIgnoredClassRequest(final Class clazz, final boolean recursively) throws ClassNotFoundException {
|
||||
Class.forName("org.junit.runners.BlockJUnit4ClassRunner"); //ignore IgnoreIgnored for junit4.4 and <
|
||||
return new ClassRequest(clazz) {
|
||||
@@ -280,31 +300,64 @@ public class JUnit4TestRunnerUtil {
|
||||
}
|
||||
}
|
||||
|
||||
private static class ParameterizedMethodRunner extends Parameterized {
|
||||
private static class SelectedParameterizedRunner extends Parameterized {
|
||||
private final String myName;
|
||||
private final String myMethodName;
|
||||
|
||||
public ParameterizedMethodRunner(Class clazz, String methodName) throws Throwable {
|
||||
public SelectedParameterizedRunner(Class clazz, String name, String methodName) throws Throwable {
|
||||
super(clazz);
|
||||
myName = name;
|
||||
myMethodName = methodName;
|
||||
}
|
||||
|
||||
protected List getChildren() {
|
||||
final List children = super.getChildren();
|
||||
for (int i = 0; i < children.size(); i++) {
|
||||
try {
|
||||
final BlockJUnit4ClassRunner child = (BlockJUnit4ClassRunner)children.get(i);
|
||||
final Method getChildrenMethod = BlockJUnit4ClassRunner.class.getDeclaredMethod("getChildren", new Class[0]);
|
||||
getChildrenMethod.setAccessible(true);
|
||||
final List list = (List)getChildrenMethod.invoke(child, new Object[0]);
|
||||
for (Iterator iterator = list.iterator(); iterator.hasNext(); ) {
|
||||
final FrameworkMethod description = (FrameworkMethod)iterator.next();
|
||||
if (!description.getName().equals(myMethodName)) {
|
||||
//filter by params
|
||||
if (myName != null) {
|
||||
for (Iterator iterator = children.iterator(); iterator.hasNext(); ) {
|
||||
Object child = iterator.next();
|
||||
try {
|
||||
Field f;
|
||||
try {
|
||||
f = child.getClass().getDeclaredField("fName");
|
||||
}
|
||||
catch (NoSuchFieldException e) {
|
||||
continue;
|
||||
}
|
||||
f.setAccessible(true);
|
||||
String fName = (String)f.get(child);
|
||||
if (!myName.equals(fName)) {
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
if (children.isEmpty()) {
|
||||
System.err.println("No tests were found by passed name: " + myName);
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
//filter only selected method
|
||||
if (myMethodName != null) {
|
||||
for (int i = 0; i < children.size(); i++) {
|
||||
try {
|
||||
final BlockJUnit4ClassRunner child = (BlockJUnit4ClassRunner)children.get(i);
|
||||
final Method getChildrenMethod = BlockJUnit4ClassRunner.class.getDeclaredMethod("getChildren", new Class[0]);
|
||||
getChildrenMethod.setAccessible(true);
|
||||
final List list = (List)getChildrenMethod.invoke(child, new Object[0]);
|
||||
for (Iterator iterator = list.iterator(); iterator.hasNext(); ) {
|
||||
final FrameworkMethod description = (FrameworkMethod)iterator.next();
|
||||
if (!description.getName().equals(myMethodName)) {
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
return children;
|
||||
|
||||
@@ -28,10 +28,10 @@ import java.util.List;
|
||||
|
||||
public interface IdeaTestRunner {
|
||||
|
||||
int startRunnerWithArgs(String[] args, ArrayList listeners, boolean sendTree);
|
||||
int startRunnerWithArgs(String[] args, ArrayList listeners, String name, boolean sendTree);
|
||||
void setStreams(SegmentedOutputStream segmentedOut, SegmentedOutputStream segmentedErr, int lastIdx);
|
||||
|
||||
Object getTestToStart(String[] args);
|
||||
Object getTestToStart(String[] args, String name);
|
||||
List getChildTests(Object description);
|
||||
String getStartDescription(Object child);
|
||||
|
||||
|
||||
@@ -57,7 +57,7 @@ public class JUnitForkedStarter {
|
||||
IdeaTestRunner testRunner = (IdeaTestRunner)JUnitStarter.getAgentClass(isJUnit4).newInstance();
|
||||
//noinspection IOResourceOpenedButNotSafelyClosed
|
||||
testRunner.setStreams(new SegmentedOutputStream(out, true), new SegmentedOutputStream(err, true), lastIdx);
|
||||
System.exit(testRunner.startRunnerWithArgs(childTestDescription, listeners, false));
|
||||
System.exit(testRunner.startRunnerWithArgs(childTestDescription, listeners, null, false));
|
||||
}
|
||||
finally {
|
||||
System.setOut(oldOut);
|
||||
@@ -70,7 +70,7 @@ public class JUnitForkedStarter {
|
||||
String[] args,
|
||||
boolean isJUnit4,
|
||||
List listeners,
|
||||
SegmentedOutputStream out,
|
||||
String params, SegmentedOutputStream out,
|
||||
SegmentedOutputStream err,
|
||||
String forkMode,
|
||||
String path) throws Exception {
|
||||
@@ -88,7 +88,7 @@ public class JUnitForkedStarter {
|
||||
|
||||
IdeaTestRunner testRunner = (IdeaTestRunner)JUnitStarter.getAgentClass(isJUnit4).newInstance();
|
||||
testRunner.setStreams(out, err, 0);
|
||||
final Object description = testRunner.getTestToStart(args);
|
||||
final Object description = testRunner.getTestToStart(args, params);
|
||||
if (description == null) return -1;
|
||||
|
||||
TreeSender.sendTree(testRunner, description, true);
|
||||
|
||||
@@ -49,7 +49,9 @@ public class JUnitStarter {
|
||||
}
|
||||
|
||||
final ArrayList listeners = new ArrayList();
|
||||
boolean isJUnit4 = processParameters(argList, listeners);
|
||||
final String[] name = new String[1];
|
||||
|
||||
boolean isJUnit4 = processParameters(argList, listeners, name);
|
||||
|
||||
if (!canWorkWithJUnitVersion(err, isJUnit4)) {
|
||||
err.flush();
|
||||
@@ -62,11 +64,11 @@ public class JUnitStarter {
|
||||
|
||||
String[] array = new String[argList.size()];
|
||||
argList.copyInto(array);
|
||||
int exitCode = prepareStreamsAndStart(array, isJUnit4, listeners, out, err);
|
||||
int exitCode = prepareStreamsAndStart(array, isJUnit4, listeners, name[0], out, err);
|
||||
System.exit(exitCode);
|
||||
}
|
||||
|
||||
private static boolean processParameters(Vector args, final List listeners) {
|
||||
private static boolean processParameters(Vector args, final List listeners, String[] params) {
|
||||
boolean isJunit4 = true;
|
||||
Vector result = new Vector(args.size());
|
||||
for (int i = 0; i < args.size(); i++) {
|
||||
@@ -78,7 +80,10 @@ public class JUnitStarter {
|
||||
isJunit4 = false;
|
||||
}
|
||||
else {
|
||||
if (arg.startsWith("@w@")) {
|
||||
if (arg.startsWith("@name")) {
|
||||
params[0] = arg.substring("@name".length());
|
||||
continue;
|
||||
} else if (arg.startsWith("@w@")) {
|
||||
ourWorkingDirs = arg.substring(3);
|
||||
continue;
|
||||
} else if (arg.startsWith("@@@")) {
|
||||
@@ -185,7 +190,11 @@ public class JUnitStarter {
|
||||
new junit.textui.TestRunner().setPrinter(new com.intellij.junit3.JUnit3IdeaTestRunner.MockResultPrinter());
|
||||
}
|
||||
|
||||
private static int prepareStreamsAndStart(String[] args, final boolean isJUnit4, ArrayList listeners, SegmentedOutputStream out,
|
||||
private static int prepareStreamsAndStart(String[] args,
|
||||
final boolean isJUnit4,
|
||||
ArrayList listeners,
|
||||
String name,
|
||||
SegmentedOutputStream out,
|
||||
SegmentedOutputStream err) {
|
||||
PrintStream oldOut = System.out;
|
||||
PrintStream oldErr = System.err;
|
||||
@@ -194,12 +203,12 @@ public class JUnitStarter {
|
||||
System.setErr(new PrintStream(err));
|
||||
if (ourCommandFileName != null) {
|
||||
if (!"none".equals(ourForkMode) || ourWorkingDirs != null && new File(ourWorkingDirs).length() > 0) {
|
||||
return JUnitForkedStarter.startForkedVMs(ourWorkingDirs, args, isJUnit4, listeners, out, err, ourForkMode, ourCommandFileName);
|
||||
return JUnitForkedStarter.startForkedVMs(ourWorkingDirs, args, isJUnit4, listeners, name, out, err, ourForkMode, ourCommandFileName);
|
||||
}
|
||||
}
|
||||
IdeaTestRunner testRunner = (IdeaTestRunner)getAgentClass(isJUnit4).newInstance();
|
||||
testRunner.setStreams(out, err, 0);
|
||||
return testRunner.startRunnerWithArgs(args, listeners, !SM_RUNNER);
|
||||
return testRunner.startRunnerWithArgs(args, listeners, name, !SM_RUNNER);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace(System.err);
|
||||
|
||||
Reference in New Issue
Block a user