Skip to content

Commit

Permalink
ConsoleRunner can now take any name (package, class or method) as arg…
Browse files Browse the repository at this point in the history
…ument
  • Loading branch information
jlink committed Nov 17, 2015
1 parent e8af9d2 commit 123e49d
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 44 deletions.
Expand Up @@ -11,7 +11,6 @@
package org.junit.gen5.console; package org.junit.gen5.console;


import static io.airlift.airline.SingleCommand.singleCommand; import static io.airlift.airline.SingleCommand.singleCommand;
import static java.util.Arrays.stream;


import java.util.List; import java.util.List;


Expand Down Expand Up @@ -48,12 +47,7 @@ public class ConsoleRunner {
description = "Disable colored output (not supported by all terminals)") description = "Disable colored output (not supported by all terminals)")
private boolean disableAnsiColors; private boolean disableAnsiColors;


@Option(name = { "-m", "--argument-mode" }, @Arguments(description = "Test classes, methods or packages to execute")
arity = 1,
description = "How to treat arguments. Possible values: classes, packages")
private String argumentMode = "classes";

@Arguments(description = "Test classes or packages to execute (depending on --argument-mode/-m)")
private List<String> arguments; private List<String> arguments;


// @formatter:on // @formatter:on
Expand Down Expand Up @@ -110,38 +104,11 @@ private void run() {


private List<TestPlanSpecificationElement> testPlanSpecificationElementsFromArguments() { private List<TestPlanSpecificationElement> testPlanSpecificationElementsFromArguments() {
Preconditions.notNull(arguments, "No arguments given"); Preconditions.notNull(arguments, "No arguments given");
ArgumentMode mode = ArgumentMode.parse(argumentMode); return toTestPlanSpecificationElements(arguments);
return mode.toTestPlanSpecificationElements(arguments);
} }


private enum ArgumentMode { List<TestPlanSpecificationElement> toTestPlanSpecificationElements(List<String> arguments) {

return TestPlanSpecification.forNames(arguments);
CLASSES {

@Override
List<TestPlanSpecificationElement> toTestPlanSpecificationElements(List<String> arguments) {
return TestPlanSpecification.forClassNames(arguments);
}
},

PACKAGES {

@Override
List<TestPlanSpecificationElement> toTestPlanSpecificationElements(List<String> arguments) {
return TestPlanSpecification.forPackages(arguments);
}
};

abstract List<TestPlanSpecificationElement> toTestPlanSpecificationElements(List<String> arguments);

static ArgumentMode parse(String value) {
// @formatter:off
return stream(values())
.filter(mode -> mode.name().equalsIgnoreCase(value))
.findAny()
.orElseThrow(() -> new IllegalArgumentException("Illegal argument mode: " + value));
// @formatter:on
}
} }


} }
Expand Up @@ -41,10 +41,6 @@ public static List<TestPlanSpecificationElement> forPackages(Collection<String>
return packageNames.stream().map(packageName -> forPackage(packageName)).collect(toList()); return packageNames.stream().map(packageName -> forPackage(packageName)).collect(toList());
} }


public static TestPlanSpecificationElement forMethod(Method testMethod) {
return forMethod(testMethod.getDeclaringClass(), testMethod);
}

public static TestPlanSpecificationElement forMethod(Class<?> testClass, Method testMethod) { public static TestPlanSpecificationElement forMethod(Class<?> testClass, Method testMethod) {
return new MethodSpecification(testClass, testMethod); return new MethodSpecification(testClass, testMethod);
} }
Expand All @@ -70,7 +66,7 @@ public static TestPlanSpecificationElement forName(String anyName) {
String.format("'%s' specifies neither a class, nor a method, nor a package.", anyName)); String.format("'%s' specifies neither a class, nor a method, nor a package.", anyName));
} }


public static List<TestPlanSpecificationElement> forClassNames(Collection<String> classNames) { public static List<TestPlanSpecificationElement> forNames(Collection<String> classNames) {
return forNames(classNames.stream()); return forNames(classNames.stream());
} }


Expand Down
2 changes: 1 addition & 1 deletion sample-project/build.gradle
Expand Up @@ -13,7 +13,7 @@ task runSampleTestCases(type: JavaExec) {
classpath = sourceSets.test.runtimeClasspath classpath = sourceSets.test.runtimeClasspath
classpath += project(':junit-console').sourceSets.main.runtimeClasspath classpath += project(':junit-console').sourceSets.main.runtimeClasspath
main = 'org.junit.gen5.console.ConsoleRunner' main = 'org.junit.gen5.console.ConsoleRunner'
args 'com.example.SampleTestCase', 'com.example.SucceedingTestCase', 'com.example.JUnit4TestCase', 'com.example.HierarchyTestCase' args 'com.example'
} }


test { test {
Expand Down
Expand Up @@ -30,6 +30,6 @@ public class JUnit4SamplesSuite {
// When you have the following method, it overrides all annotations // When you have the following method, it overrides all annotations
// public static TestPlanSpecification createSpecification() { // public static TestPlanSpecification createSpecification() {
// return TestPlanSpecification.build( // return TestPlanSpecification.build(
// TestPlanSpecification.forClassNames(SucceedingTestCase.class.getName())); // TestPlanSpecification.forNames(SucceedingTestCase.class.getName()));
// } // }
} }

0 comments on commit 123e49d

Please sign in to comment.