View Javadoc
1   package org.apache.maven.surefire.junitcore;
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 java.lang.annotation.Annotation;
23  import java.lang.reflect.Method;
24  import org.apache.maven.surefire.common.junit4.JUnit4Reflector;
25  import org.apache.maven.surefire.util.ReflectionUtils;
26  
27  import org.junit.Ignore;
28  import org.junit.Test;
29  import org.junit.runner.Description;
30  
31  import static org.junit.Assert.*;
32  
33  /**
34   * Reflector Test with junit 4.8.1
35   *
36   * @author Kristian Rosenvold
37   */
38  public class JUnit4Reflector481Test
39  {
40      private static final Class[] EMPTY_CLASS_ARRAY = new Class[0];
41  
42      @Test
43      public void testGetAnnotatedIgnore()
44      {
45          final Method testSomething2 =
46              ReflectionUtils.getMethod( IgnoreWithDescription.class, "testSomething2", EMPTY_CLASS_ARRAY );
47          final Annotation[] annotations = testSomething2.getAnnotations();
48          Description desc =
49              Description.createTestDescription( IgnoreWithDescription.class, "testSomething2", annotations );
50          Ignore annotatedIgnore = JUnit4Reflector.getAnnotatedIgnore( desc );
51          assertNotNull( annotatedIgnore );
52          assertEquals(
53              "testSomething2" + "(org.apache.maven.surefire.junitcore.JUnit4Reflector481Test$IgnoreWithDescription)",
54              desc.getDisplayName() );
55          assertEquals( "testSomething2"
56                            + "(org.apache.maven.surefire.junitcore.JUnit4Reflector481Test$IgnoreWithDescription)",
57                        desc.toString() );
58          assertEquals( "org.apache.maven.surefire.junitcore.JUnit4Reflector481Test$IgnoreWithDescription",
59                        desc.getClassName() );
60          assertEquals( "testSomething2", desc.getMethodName() );
61          assertEquals( 0, desc.getChildren().size() );
62          assertEquals( 2, desc.getAnnotations().size() );
63          assertSame( annotatedIgnore, desc.getAnnotation( Ignore.class ) );
64          assertEquals( reason, annotatedIgnore.value() );
65      }
66  
67      @Test
68      public void testGetAnnotatedIgnoreWithoutClass()
69      {
70          final Method testSomething2 =
71              ReflectionUtils.getMethod( IgnoreWithDescription.class, "testSomething2", EMPTY_CLASS_ARRAY );
72          final Annotation[] annotations = testSomething2.getAnnotations();
73          Description desc = Description.createSuiteDescription( "testSomething2", annotations );
74          Ignore annotatedIgnore = JUnit4Reflector.getAnnotatedIgnore( desc );
75          assertNotNull( annotatedIgnore );
76          assertEquals( "testSomething2", desc.getDisplayName() );
77          assertEquals( "testSomething2", desc.toString() );
78          assertEquals( "testSomething2", desc.getClassName() );
79          assertNull( desc.getMethodName() );
80          assertEquals( 0, desc.getChildren().size() );
81          assertEquals( 2, desc.getAnnotations().size() );
82          assertSame( annotatedIgnore, desc.getAnnotation( Ignore.class ) );
83          assertEquals( reason, annotatedIgnore.value() );
84      }
85  
86      private static final String reason = "Ignorance is bliss";
87  
88      public static class IgnoreWithDescription
89      {
90  
91          @Test
92          @Ignore( reason )
93          public void testSomething2()
94          {
95          }
96      }
97  
98      @Test
99      public void testCreatePureDescription()
100     {
101         Description description = JUnit4Reflector.createDescription( "exception" );
102         assertEquals( "exception", description.getDisplayName() );
103         assertEquals( "exception", description.toString() );
104         assertEquals( 0, description.getChildren().size() );
105     }
106 
107     @Test
108     public void testCreateDescription()
109     {
110         Ignore ignore = JUnit4Reflector.createIgnored( "error" );
111         Description description = JUnit4Reflector.createDescription( "exception", ignore );
112         assertEquals( "exception", description.getDisplayName() );
113         assertEquals( "exception", description.toString() );
114         assertEquals( "exception", description.getClassName() );
115         assertEquals( 0, description.getChildren().size() );
116         Ignore annotatedIgnore = JUnit4Reflector.getAnnotatedIgnore( description );
117         assertNotNull( annotatedIgnore );
118         assertEquals( "error", annotatedIgnore.value() );
119     }
120 }