Permalink
Browse files

Merge pull request #365 from stefanbirkner/cleanjunit

Improved Description class
  • Loading branch information...
2 parents db9a456 + 7944e8f commit 59c686f5ca9d22288eca5cdbf2c75298fa1402ea @dsaff dsaff committed Feb 24, 2012
@@ -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>.
@@ -36,8 +39,6 @@
* @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);
}
@@ -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;
}
@@ -215,28 +218,20 @@ public Description childlessCopy() {
* 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;
}
}
@@ -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 59c686f

Please sign in to comment.