Integrate VogarTest more closely with JUnit
Adds a getDescription() method to VogarTest to allow JUnit
Filters to be applied.
Bug: 27940141
Change-Id: I84e4b4f9adfaf5b3bb09bdfed830a66e56fcd747
diff --git a/src/vogar/target/junit/ConfigurationError.java b/src/vogar/target/junit/ConfigurationError.java
index d9883b1..d325787 100644
--- a/src/vogar/target/junit/ConfigurationError.java
+++ b/src/vogar/target/junit/ConfigurationError.java
@@ -16,12 +16,18 @@
package vogar.target.junit;
+import javax.annotation.Nullable;
+import org.junit.runner.Description;
+
class ConfigurationError implements VogarTest {
- private final String name;
+ private final String className;
+ @Nullable
+ private final String methodName;
private final Throwable cause;
- ConfigurationError(String name, Throwable cause) {
- this.name = name;
+ ConfigurationError(String className, @Nullable String methodName, Throwable cause) {
+ this.className = className;
+ this.methodName = methodName;
this.cause = cause;
}
@@ -31,7 +37,17 @@
}
@Override
+ public Description getDescription() {
+ if (methodName == null) {
+ return Description.createSuiteDescription(className);
+ } else {
+ String testName = String.format("%s(%s)", methodName, className);
+ return Description.createSuiteDescription(testName);
+ }
+ }
+
+ @Override
public String toString() {
- return name;
+ return methodName == null ? className : className + "#" + methodName;
}
}
diff --git a/src/vogar/target/junit/JUnitUtils.java b/src/vogar/target/junit/JUnitUtils.java
new file mode 100644
index 0000000..6068584
--- /dev/null
+++ b/src/vogar/target/junit/JUnitUtils.java
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package vogar.target.junit;
+
+/**
+ * Utilities for bridging between JUnit and Vogar.
+ */
+public class JUnitUtils {
+ private JUnitUtils() {}
+
+ /**
+ * Get the name of the test in {@code <class>(#<method>)?} format.
+ */
+ public static String getTestName(String className, String methodName) {
+ if (methodName == null) {
+ return className;
+ } else {
+ return className + "#" + methodName;
+ }
+ }
+}
diff --git a/src/vogar/target/junit/Junit3.java b/src/vogar/target/junit/Junit3.java
index 00ca0e3..f078cee 100644
--- a/src/vogar/target/junit/Junit3.java
+++ b/src/vogar/target/junit/Junit3.java
@@ -30,6 +30,7 @@
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
+import org.junit.runner.Description;
import vogar.ClassAnalyzer;
/**
@@ -104,7 +105,7 @@
AssertionFailedError cause = new AssertionFailedError(
"Method \"" + methodName + "\" not found");
ConfigurationError error = new ConfigurationError(
- testClass.getName() + "#" + methodName,
+ testClass.getName(), methodName,
cause);
out.add(error);
}
@@ -123,10 +124,10 @@
try {
test = (junit.framework.Test) suiteMethod.invoke(null);
} catch (InvocationTargetException e) {
- out.add(new ConfigurationError(testClass.getName() + "#suite", e.getCause()));
+ out.add(new ConfigurationError(testClass.getName(), "suite", e.getCause()));
return;
} catch (Throwable e) {
- out.add(new ConfigurationError(testClass.getName() + "#suite", e));
+ out.add(new ConfigurationError(testClass.getName(), "suite", e));
return;
}
@@ -135,14 +136,14 @@
} else if (test instanceof TestSuite) {
getTestSuiteTests(out, (TestSuite) test, methodNames);
} else {
- out.add(new ConfigurationError(testClass.getName() + "#suite",
+ out.add(new ConfigurationError(testClass.getName(), "suite",
new IllegalStateException("Unknown suite() result: " + test)));
}
return;
} catch (NoSuchMethodException ignored) {
}
- out.add(new ConfigurationError(testClass.getName() + "#suite",
+ out.add(new ConfigurationError(testClass.getName(), "suite",
new IllegalStateException("Not a test case: " + testClass)));
}
@@ -154,7 +155,7 @@
} else if (testsOrSuite instanceof TestSuite) {
getTestSuiteTests(out, (TestSuite) testsOrSuite, methodNames);
} else if (testsOrSuite != null) {
- out.add(new ConfigurationError(testsOrSuite.getClass().getName() + "#getClass",
+ out.add(new ConfigurationError(testsOrSuite.getClass().getName(), "getClass",
new IllegalStateException("Unknown test: " + testsOrSuite)));
}
}
@@ -242,7 +243,7 @@
} catch (NoSuchMethodException ignored) {
}
String testClassName = testClass.getName();
- return new ConfigurationError(testClassName + "#" + method.getName(),
+ return new ConfigurationError(testClassName, method.getName(),
new AssertionFailedError("Class " + testClassName
+ " has no public constructor TestCase(String name) or TestCase()"));
}
@@ -265,6 +266,11 @@
@Override public String toString() {
return testClass.getName() + "#" + method.getName();
}
+
+ @Override
+ public Description getDescription() {
+ return Description.createTestDescription(testClass, method.getName());
+ }
}
/**
@@ -285,5 +291,10 @@
@Override public String toString() {
return testCase.getClass().getName() + "#" + testCase.getName();
}
+
+ @Override
+ public Description getDescription() {
+ return Description.createTestDescription(testClass, testCase.getName());
+ }
}
}
diff --git a/src/vogar/target/junit/Junit4.java b/src/vogar/target/junit/Junit4.java
index 1f0526f..345c11f 100644
--- a/src/vogar/target/junit/Junit4.java
+++ b/src/vogar/target/junit/Junit4.java
@@ -31,6 +31,7 @@
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
+import org.junit.runner.Description;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@@ -98,8 +99,7 @@
cause = new AssertionFailedError("Method \"" + methodName + "\" not found");
}
ConfigurationError error = new ConfigurationError(
- testClass.getName() + "#" + methodName,
- cause);
+ testClass.getName(), methodName, cause);
out.add(error);
// An error happened so just treat this as a JUnit4 test otherwise it will end
@@ -110,7 +110,7 @@
}
if (!isJunit4TestClass) {
- out.add(new ConfigurationError(testClass.getName(),
+ out.add(new ConfigurationError(testClass.getName(), null,
new IllegalStateException("Not a test case: " + testClass)));
}
}
@@ -122,7 +122,7 @@
addAllParameterizedTests(out, testClass, m, argCollection);
} else {
String name = m.getName();
- out.add(new ConfigurationError(testClass.getName() + "#" + name,
+ out.add(new ConfigurationError(testClass.getName(), name,
new Exception("Method " + name + " should have no parameters")));
}
}
@@ -323,6 +323,15 @@
}
protected abstract Object getTestCase() throws Throwable;
+
+ @Override
+ public Description getDescription() {
+ return Description.createTestDescription(testClass, method.getName());
+ }
+
+ @Override public String toString() {
+ return testClass.getName() + "#" + method.getName();
+ }
}
/**
@@ -348,7 +357,7 @@
}
}
- return new ConfigurationError(testClass.getName() + "#" + method.getName(),
+ return new ConfigurationError(testClass.getName(), method.getName(),
new Exception("Parameterized test cases must have "
+ constructorArgs.length + " arg constructor"));
}
@@ -363,7 +372,7 @@
} catch (NoSuchMethodException ignored) {
}
- return new ConfigurationError(testClass.getName() + "#" + method.getName(),
+ return new ConfigurationError(testClass.getName(), method.getName(),
new Exception("Test cases must have a no-arg or string constructor."));
}
@@ -374,27 +383,5 @@
throw e.getCause();
}
}
-
- @Override public String toString() {
- return testClass.getName() + "#" + method.getName();
- }
- }
-
- private static class IgnoredTest extends VogarJUnitTest {
- private IgnoredTest(Class<?> testClass, Method method) {
- super(testClass, method);
- }
-
- @Override public void run() throws Throwable {
- System.out.println("@Ignored.");
- }
-
- @Override protected Object getTestCase() {
- throw new UnsupportedOperationException();
- }
-
- @Override public String toString() {
- return testClass.getName() + "#" + method.getName();
- }
}
}
diff --git a/src/vogar/target/junit/VogarTest.java b/src/vogar/target/junit/VogarTest.java
index 36a359d..970add5 100644
--- a/src/vogar/target/junit/VogarTest.java
+++ b/src/vogar/target/junit/VogarTest.java
@@ -16,6 +16,10 @@
package vogar.target.junit;
+import org.junit.runner.Description;
+
public interface VogarTest {
void run() throws Throwable;
+
+ Description getDescription();
}
diff --git a/test/vogar/target/AbstractTestRunnerTest.java b/test/vogar/target/AbstractTestRunnerTest.java
index 93b3bb5..76466d9 100644
--- a/test/vogar/target/AbstractTestRunnerTest.java
+++ b/test/vogar/target/AbstractTestRunnerTest.java
@@ -25,6 +25,7 @@
import org.junit.Before;
import org.junit.Rule;
import vogar.Result;
+import vogar.target.junit.JUnitUtils;
import vogar.testing.InterceptOutputStreams;
import vogar.testing.InterceptOutputStreams.Stream;
@@ -191,7 +192,7 @@
private static String outcome(
String testClassName, String methodName, String message, Result result) {
- String testName = testName(testClassName, methodName);
+ String testName = JUnitUtils.getTestName(testClassName, methodName);
return String.format("//00xx{\"outcome\":\"%s\"}\n"
+ "%s"
@@ -199,16 +200,6 @@
testName, message == null ? "" : message, result);
}
- private static String testName(String testClassName, String methodName) {
- String format;
- if (methodName == null) {
- format = "%1$s";
- } else {
- format = "%1$s#%2$s";
- }
- return String.format(format, testClassName, methodName);
- }
-
private void checkFilteredOutput(String expected) {
checkCount.decrementAndGet();
String output = ios.contents(Stream.OUT);