1 package org.apache.maven.surefire.common.junit4;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.junit.Ignore;
23 import org.junit.runner.Description;
24
25 import java.lang.annotation.Annotation;
26 import java.lang.reflect.Method;
27
28 import static org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray;
29 import static org.apache.maven.surefire.util.ReflectionUtils.getMethod;
30 import static org.apache.maven.surefire.util.ReflectionUtils.tryGetMethod;
31
32
33
34
35
36 public final class JUnit4Reflector
37 {
38 private static final Class[] PARAMS = { Class.class };
39
40 private static final Object[] IGNORE_PARAMS = { Ignore.class };
41
42 private static final Class[] PARAMS_WITH_ANNOTATIONS = { String.class, Annotation[].class };
43
44 private JUnit4Reflector()
45 {
46 throw new IllegalStateException( "not instantiable constructor" );
47 }
48
49 public static Ignore getAnnotatedIgnore( Description d )
50 {
51 Method getAnnotation = tryGetMethod( d.getClass(), "getAnnotation", PARAMS );
52 return getAnnotation == null ? null : (Ignore) invokeMethodWithArray( d, getAnnotation, IGNORE_PARAMS );
53 }
54
55 static String getAnnotatedIgnoreValue( Description description )
56 {
57 final Ignore ignore = getAnnotatedIgnore( description );
58 return ignore != null ? ignore.value() : null;
59 }
60
61 public static Description createDescription( String description )
62 {
63 try
64 {
65 return Description.createSuiteDescription( description );
66 }
67 catch ( NoSuchMethodError e )
68 {
69 Method method = getMethod( Description.class, "createSuiteDescription", PARAMS_WITH_ANNOTATIONS );
70
71 return (Description) invokeMethodWithArray( null, method, description, new Annotation[0] );
72 }
73 }
74
75 public static Description createDescription( String description, Annotation... annotations )
76 {
77 Method method = tryGetMethod( Description.class, "createSuiteDescription", PARAMS_WITH_ANNOTATIONS );
78 return method == null
79 ? Description.createSuiteDescription( description )
80 : (Description) invokeMethodWithArray( null, method, description, annotations );
81 }
82
83 public static Ignore createIgnored( String value )
84 {
85 return new IgnoredWithUserError( value );
86 }
87
88 @SuppressWarnings( "ClassExplicitlyAnnotation" )
89 private static class IgnoredWithUserError
90 implements Annotation, Ignore
91 {
92 private final String value;
93
94 IgnoredWithUserError( String value )
95 {
96 this.value = value;
97 }
98
99 @Override
100 public String value()
101 {
102 return value;
103 }
104
105 @Override
106 public Class<? extends Annotation> annotationType()
107 {
108 return Ignore.class;
109 }
110
111 @Override
112 public int hashCode()
113 {
114 return value == null ? 0 : value.hashCode();
115 }
116
117 @Override
118 public boolean equals( Object obj )
119 {
120 return obj instanceof Ignore && equalValue( ( Ignore ) obj );
121 }
122
123 @Override
124 public String toString()
125 {
126 return String.format( "%s(%s)", Ignore.class, value );
127 }
128
129 private boolean equalValue( Ignore ignore )
130 {
131 return ignore != null && ignore.value().equals( value );
132 }
133 }
134 }