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