Permalink
Browse files

Description.getTestClass() did not work reliably.

Was assuming that the ClassLoader used for JUnit could also load test classes.
While this may be true in a simple test runner based on a flat classpath, it is not true in all test runners.
When we are given a Class object, keep it—do not try to reconstruct it from its name alone.
  • Loading branch information...
1 parent a01caef commit 8134371705a43fa2b2a8913678d4336d867f6647 @jglick committed Sep 6, 2012
@@ -40,7 +40,7 @@
* @return a <code>Description</code> named <code>name</code>
*/
public static Description createSuiteDescription(String name, Annotation... annotations) {
- return new Description(name, annotations);
+ return new Description(name, null, annotations);
}
/**
@@ -52,7 +52,7 @@ public static Description createSuiteDescription(String name, Annotation... anno
* @return a <code>Description</code> named <code>name</code>
*/
public static Description createSuiteDescription(String name, Serializable uniqueId, Annotation... annotations) {
- return new Description(name, uniqueId, annotations);
+ return new Description(name, null, uniqueId, annotations);
}
/**
@@ -66,7 +66,7 @@ public static Description createSuiteDescription(String name, Serializable uniqu
* @return a <code>Description</code> named <code>name</code>
*/
public static Description createTestDescription(String className, String name, Annotation... annotations) {
- return new Description(String.format("%s(%s)", name, className), annotations);
+ return new Description(String.format("%s(%s)", name, className), null, annotations);
}
/**
@@ -78,7 +78,7 @@ public static Description createTestDescription(String className, String name, A
* @return a <code>Description</code> named <code>name</code>
*/
public static Description createTestDescription(Class<?> clazz, String name, Annotation... annotations) {
- return createTestDescription(clazz.getName(), name, annotations);
+ return new Description(String.format("%s(%s)", name, clazz.getName()), clazz, annotations);
}
/**
@@ -90,7 +90,7 @@ public static Description createTestDescription(Class<?> clazz, String name, Ann
* @return a <code>Description</code> named <code>name</code>
*/
public static Description createTestDescription(Class<?> clazz, String name) {
- return createTestDescription(clazz.getName(), name);
+ return new Description(String.format("%s(%s)", name, clazz.getName()), clazz);
}
/**
@@ -101,7 +101,7 @@ public static Description createTestDescription(Class<?> clazz, String name) {
* @return a <code>Description</code> named <code>name</code>
*/
public static Description createTestDescription(String className, String name, Serializable uniqueId) {
- return new Description(String.format("%s(%s)", name, className), uniqueId);
+ return new Description(String.format("%s(%s)", name, className), null, uniqueId);
}
/**
@@ -110,38 +110,40 @@ public static Description createTestDescription(String className, String name, S
* @return a <code>Description</code> of <code>testClass</code>
*/
public static Description createSuiteDescription(Class<?> testClass) {
- return new Description(testClass.getName(), testClass.getAnnotations());
+ return new Description(testClass.getName(), testClass, testClass.getAnnotations());
}
/**
* Describes a Runner which runs no tests
*/
- public static final Description EMPTY= new Description("No Tests");
+ public static final Description EMPTY= new Description("No Tests", null);
/**
* Describes a step in the test-running mechanism that goes so wrong no
* other description can be used (for example, an exception thrown from a Runner's
* constructor
*/
- public static final Description TEST_MECHANISM= new Description("Test mechanism");
+ public static final Description TEST_MECHANISM= new Description("Test mechanism", null);
private final ArrayList<Description> fChildren= new ArrayList<Description>();
private final String fDisplayName;
private final Serializable fUniqueId;
private final Annotation[] fAnnotations;
+ private final Class<?> fClazz;
- private Description(String displayName, Annotation... annotations) {
- this(displayName, displayName, annotations);
+ private Description(String displayName, Class<?> clazz, Annotation... annotations) {
+ this(displayName, clazz, displayName, annotations);
}
- private Description(String displayName, Serializable uniqueId, Annotation... annotations) {
+ private Description(String displayName, Class<?> clazz, Serializable uniqueId, Annotation... annotations) {
if ((displayName == null) || (displayName.length() == 0))
throw new IllegalArgumentException(
"The display name must not be empty.");
if ((uniqueId == null))
throw new IllegalArgumentException(
"The unique id must not be null.");
fDisplayName= displayName;
+ fClazz = clazz;
fUniqueId= uniqueId;
fAnnotations= annotations;
}
@@ -224,7 +226,7 @@ public boolean isEmpty() {
* children will be added back)
*/
public Description childlessCopy() {
- return new Description(fDisplayName, fAnnotations);
+ return new Description(fDisplayName, fClazz, fAnnotations);
}
/**
@@ -250,6 +252,9 @@ public Description childlessCopy() {
* the class of the test instance.
*/
public Class<?> getTestClass() {
+ if (fClazz != null) {
+ return fClazz;
+ }
String name= getClassName();
if (name == null)
return null;
@@ -2,13 +2,17 @@
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.not;
+import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
+import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.Description;
import java.lang.annotation.Annotation;
+import java.net.URL;
+import java.net.URLClassLoader;
public class DescriptionTest {
@@ -67,4 +71,45 @@ public void sameNamesButDifferentUniqueIdAreNotEqual() throws Exception {
assertThat(Description.createTestDescription("not a class name", "aTestMethod", 1),
not(equalTo(Description.createTestDescription("not a class name", "aTestMethod", 2))));
}
+
+ @Test
+ public void roundTripsClassObject() throws Exception {
+ class URLClassLoader2 extends URLClassLoader {
+ URLClassLoader2(URL[] urls) {
+ super(urls);
+ }
+ @Override // just making public
+ public Class<?> findClass(String name) throws ClassNotFoundException {
+ return super.findClass(name);
+ }
+ }
+ URLClassLoader2 l = new URLClassLoader2(new URL[] {Sweet.class.getProtectionDomain().getCodeSource().getLocation()});
+ Class<?> clazz = l.findClass(Sweet.class.getName());
+ assertEquals(l, clazz.getClassLoader());
+ Description d = Description.createSuiteDescription(clazz);
+ assertEquals(clazz, d.getTestClass());
+ assertNull(d.getMethodName());
+ assertEquals(1, d.getAnnotations().size());
+ assertEquals(Ignore.class, d.getAnnotations().iterator().next().annotationType());
+ d = Description.createTestDescription(clazz, "tessed");
+ assertEquals(clazz, d.getTestClass());
+ assertEquals("tessed", d.getMethodName());
+ assertEquals(0, d.getAnnotations().size());
+ d = Description.createTestDescription(clazz, "tessed", clazz.getMethod("tessed").getAnnotations());
+ assertEquals(clazz, d.getTestClass());
+ assertEquals("tessed", d.getMethodName());
+ assertEquals(1, d.getAnnotations().size());
+ assertEquals(Test.class, d.getAnnotations().iterator().next().annotationType());
+ d = d.childlessCopy();
+ assertEquals(clazz, d.getTestClass());
+ assertEquals("tessed", d.getMethodName());
+ assertEquals(1, d.getAnnotations().size());
+ assertEquals(Test.class, d.getAnnotations().iterator().next().annotationType());
+ }
+ @Ignore
+ private static class Sweet {
+ @Test
+ public void tessed() {}
+ }
+
}

0 comments on commit 8134371

Please sign in to comment.