Skip to content
Browse files

https://github.com/KentBeck/junit/pull/498 review comments.

  • Loading branch information...
1 parent 8134371 commit 785c7859cd87dc5cb3872fef476ce2f86c9259dd @jglick committed Sep 6, 2012
View
41 src/main/java/org/junit/runner/Description.java
@@ -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, null, annotations);
+ return new Description(null, name, 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, null, uniqueId, annotations);
+ return new Description(null, name, 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), null, annotations);
+ return new Description(null, formatDisplayName(name, className), 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 new Description(String.format("%s(%s)", name, clazz.getName()), clazz, annotations);
+ return new Description(clazz, formatDisplayName(name, clazz.getName()), 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 new Description(String.format("%s(%s)", name, clazz.getName()), clazz);
+ return new Description(clazz, formatDisplayName(name, clazz.getName()));
}
/**
@@ -101,7 +101,11 @@ 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), null, uniqueId);
+ return new Description(null, formatDisplayName(name, className), uniqueId);
+ }
+
+ private static String formatDisplayName(String name, String className) {
+ return String.format("%s(%s)", name, className);
}
/**
@@ -110,40 +114,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, testClass.getAnnotations());
+ return new Description(testClass, testClass.getName(), testClass.getAnnotations());
}
/**
* Describes a Runner which runs no tests
*/
- public static final Description EMPTY= new Description("No Tests", null);
+ public static final Description EMPTY= new Description(null, "No Tests");
/**
* 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", null);
+ public static final Description TEST_MECHANISM= new Description(null, "Test mechanism");
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 /* write-once */ Class<?> fTestClass;
- private Description(String displayName, Class<?> clazz, Annotation... annotations) {
- this(displayName, clazz, displayName, annotations);
+ private Description(Class<?> clazz, String displayName, Annotation... annotations) {
+ this(clazz, displayName, displayName, annotations);
}
- private Description(String displayName, Class<?> clazz, Serializable uniqueId, Annotation... annotations) {
+ private Description(Class<?> clazz, String displayName, 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;
+ fTestClass= clazz;
fUniqueId= uniqueId;
fAnnotations= annotations;
}
@@ -226,7 +230,7 @@ public boolean isEmpty() {
* children will be added back)
*/
public Description childlessCopy() {
- return new Description(fDisplayName, fClazz, fAnnotations);
+ return new Description(fTestClass, fDisplayName, fAnnotations);
}
/**
@@ -252,14 +256,13 @@ public Description childlessCopy() {
* the class of the test instance.
*/
public Class<?> getTestClass() {
- if (fClazz != null) {
- return fClazz;
- }
+ if (fTestClass != null)
+ return fTestClass;
String name= getClassName();
if (name == null)
return null;
try {
- return Class.forName(name, false, getClass().getClassLoader());
+ return fTestClass= Class.forName(name, false, getClass().getClassLoader());
} catch (ClassNotFoundException e) {
return null;
}
View
84 src/test/java/org/junit/tests/experimental/max/DescriptionTest.java
@@ -72,44 +72,50 @@ public void sameNamesButDifferentUniqueIdAreNotEqual() throws Exception {
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() {}
- }
+ @Test
+ public void usesPassedInClassObject() 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);
+ }
+ }
+ URL classpath= Sweet.class.getProtectionDomain().getCodeSource().getLocation();
+ URLClassLoader2 loader= new URLClassLoader2(new URL[] {classpath});
+ Class<?> clazz= loader.findClass(Sweet.class.getName());
+ assertEquals(loader, 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 785c785

Please sign in to comment.
Something went wrong with that request. Please try again.