View Javadoc
1   package org.apache.maven.surefire.common.junit4;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * JUnit4 reflection helper
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              // may throw exception probably with broken JUnit 4.x
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 }