Skip to content

Commit

Permalink
Improved Description class.
Browse files Browse the repository at this point in the history
A few modifications to improve the readability of the code:
* extracted METHOD_AND_CLASS_NAME_PATTERN constant
* extracted methodAndClassNamePatternGroupOrDefault(...)
* merged getMethodName() and parseMethod()
* moved validation of display name to constructor
* throw IllegalArgumentException if display name is null
  • Loading branch information
stefanbirkner committed Nov 28, 2011
1 parent a681b76 commit 7944e8f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
31 changes: 13 additions & 18 deletions src/main/java/org/junit/runner/Description.java
Expand Up @@ -28,6 +28,9 @@
public class Description implements Serializable {
private static final long serialVersionUID = 1L;

private static final Pattern METHOD_AND_CLASS_NAME_PATTERN= Pattern
.compile("(.*)\\((.*)\\)");

/**
* Create a <code>Description</code> named <code>name</code>.
* Generally, you will add children to this <code>Description</code>.
Expand All @@ -36,8 +39,6 @@ public class Description implements Serializable {
* @return a <code>Description</code> named <code>name</code>
*/
public static Description createSuiteDescription(String name, Annotation... annotations) {
if (name.length() == 0)
throw new IllegalArgumentException("name must have non-zero length");
return new Description(name, annotations);
}

Expand Down Expand Up @@ -88,10 +89,12 @@ public static Description createSuiteDescription(Class<?> testClass) {

private final ArrayList<Description> fChildren= new ArrayList<Description>();
private final String fDisplayName;

private final Annotation[] fAnnotations;

private Description(final String displayName, Annotation... annotations) {
private Description(String displayName, Annotation... annotations) {
if ((displayName == null) || (displayName.length() == 0))
throw new IllegalArgumentException(
"The display name must not be empty.");
fDisplayName= displayName;
fAnnotations= annotations;
}
Expand Down Expand Up @@ -215,28 +218,20 @@ public Class<?> getTestClass() {
* the name of the class of the test instance
*/
public String getClassName() {
Matcher matcher= methodStringMatcher();
return matcher.matches()
? matcher.group(2)
: toString();
return methodAndClassNamePatternGroupOrDefault(2, toString());
}

/**
* @return If this describes a method invocation,
* the name of the method (or null if not)
*/
public String getMethodName() {
return parseMethod();
}

private String parseMethod() {
Matcher matcher= methodStringMatcher();
if (matcher.matches())
return matcher.group(1);
return null;
return methodAndClassNamePatternGroupOrDefault(1, null);
}

private Matcher methodStringMatcher() {
return Pattern.compile("(.*)\\((.*)\\)").matcher(toString());
private String methodAndClassNamePatternGroupOrDefault(int group,
String defaultString) {
Matcher matcher= METHOD_AND_CLASS_NAME_PATTERN.matcher(toString());
return matcher.matches() ? matcher.group(group) : defaultString;
}
}
Expand Up @@ -20,5 +20,9 @@ public void parseMethod_whenCantParse() {
public void createSuiteDescription_whenZeroLength() {
Description.createSuiteDescription("");
}


@Test(expected= IllegalArgumentException.class)
public void createSuiteDescription_whenNull() {
Description.createSuiteDescription((String) null);
}
}

0 comments on commit 7944e8f

Please sign in to comment.