Skip to content

Commit

Permalink
cucumber-jvm issue junit-team#322, added a new Description factory me…
Browse files Browse the repository at this point in the history
…thod and temporarily changed from Serializable back to Object for the unique id; this needs a change to Gherkin
  • Loading branch information
Petter Måhlén committed Jun 4, 2012
1 parent c228881 commit 14f9868
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/main/java/org/junit/runner/Description.java
Expand Up @@ -35,7 +35,7 @@ public class Description implements Serializable {
* Create a <code>Description</code> named <code>name</code>.
* Generally, you will add children to this <code>Description</code>.
* @param name the name of the <code>Description</code>
* @param annotations
* @param annotations meta-data about the test, for downstream interpreters
* @return a <code>Description</code> named <code>name</code>
*/
public static Description createSuiteDescription(String name, Annotation... annotations) {
Expand All @@ -54,6 +54,18 @@ public static Description createSuiteDescription(String name, Serializable uniqu
return new Description(name, uniqueId, annotations);
}

/**
* Create a <code>Description</code> of a single test named <code>name</code> in the 'class' named <code>className</code>.
* Generally, this will be a leaf <code>Description</code>.
* @param className the class name of the test
* @param name the name of the test (a method name for test annotated with {@link org.junit.Test})
* @param annotations meta-data about the test, for downstream interpreters
* @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);
}

/**
* Create a <code>Description</code> of a single test named <code>name</code> in the class <code>clazz</code>.
* Generally, this will be a leaf <code>Description</code>.
Expand All @@ -63,19 +75,19 @@ public static Description createSuiteDescription(String name, Serializable uniqu
* @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()), annotations);
return createTestDescription(clazz.getName(), name, annotations);
}

/**
* Create a <code>Description</code> of a single test named <code>name</code> in the class <code>clazz</code>.
* Generally, this will be a leaf <code>Description</code>.
* Generally, this will be a leaf <code>Description</code>.
* (This remains for binary compatibility with clients of JUnit 4.3)
* @param clazz the class of the test
*
* @param name the name of the test (a method name for test annotated with {@link org.junit.Test})
* @return a <code>Description</code> named <code>name</code>
*/
public static Description createTestDescription(Class<?> clazz, String name) {
return createTestDescription(clazz, name, new Annotation[0]);
public static Description createTestDescription(String className, String name, Object uniqueId) {
return new Description(String.format("%s(%s)", name, className), uniqueId);
}

/**
Expand All @@ -101,14 +113,14 @@ public static Description createSuiteDescription(Class<?> testClass) {

private final ArrayList<Description> fChildren= new ArrayList<Description>();
private final String fDisplayName;
private final Serializable fUniqueId;
private final Object fUniqueId;
private final Annotation[] fAnnotations;

private Description(String displayName, Annotation... annotations) {
this(displayName, displayName, annotations);
}

private Description(String displayName, Serializable uniqueId, Annotation... annotations) {
private Description(String displayName, Object uniqueId, Annotation... annotations) {
if ((displayName == null) || (displayName.length() == 0))
throw new IllegalArgumentException(
"The display name must not be empty.");
Expand Down
@@ -1,9 +1,15 @@
package org.junit.tests.experimental.max;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;

import org.junit.Test;
import org.junit.runner.Description;

import java.lang.annotation.Annotation;

public class DescriptionTest {

@Test
Expand All @@ -25,4 +31,39 @@ public void createSuiteDescription_whenZeroLength() {
public void createSuiteDescription_whenNull() {
Description.createSuiteDescription((String) null);
}

@Test
public void parseClassAndMethodNoAnnotations() throws Exception {
Description description = Description.createTestDescription(Description.class, "aTestMethod");

assertThat(description.getClassName(), equalTo("org.junit.runner.Description"));
assertThat(description.getMethodName(), equalTo("aTestMethod"));
assertThat(description.getAnnotations().size(), equalTo(0));
}

@Test
public void parseClassAndMethodWithAnnotations() throws Exception {
Annotation[] annotations = DescriptionTest.class.getMethod("parseClassAndMethodWithAnnotations").getDeclaredAnnotations();

Description description = Description.createTestDescription(Description.class, "aTestMethod", annotations);

assertThat(description.getClassName(), equalTo("org.junit.runner.Description"));
assertThat(description.getMethodName(), equalTo("aTestMethod"));
assertThat(description.getAnnotations().size(), equalTo(1));
}

@Test
public void parseClassNameAndMethodUniqueId() throws Exception {
Description description = Description.createTestDescription("something that's not a class name", "aTestMethod", 123);

assertThat(description.getClassName(), equalTo("something that's not a class name"));
assertThat(description.getMethodName(), equalTo("aTestMethod"));
assertThat(description.getAnnotations().size(), equalTo(0));
}

@Test
public void sameNamesButDifferentUniqueIdAreNotEqual() throws Exception {
assertThat(Description.createTestDescription("something that's not a class name", "aTestMethod", 1),
not(equalTo(Description.createTestDescription("something that's not a class name", "aTestMethod", 2))));
}
}

0 comments on commit 14f9868

Please sign in to comment.