View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.surefire.junitcore;
20  
21  import java.lang.annotation.Annotation;
22  import java.lang.reflect.Method;
23  
24  import org.apache.maven.surefire.api.util.ReflectionUtils;
25  import org.apache.maven.surefire.common.junit4.JUnit4Reflector;
26  import org.junit.Ignore;
27  import org.junit.Test;
28  import org.junit.runner.Description;
29  
30  import static org.junit.Assert.assertEquals;
31  import static org.junit.Assert.assertNotNull;
32  import static org.junit.Assert.assertNull;
33  import static org.junit.Assert.assertSame;
34  
35  /**
36   * Reflector Test with junit 4.8.1
37   *
38   * @author Kristian Rosenvold
39   */
40  public class JUnit4Reflector481Test {
41      private static final Class[] EMPTY_CLASS_ARRAY = new Class[0];
42  
43      @Test
44      public void testGetAnnotatedIgnore() {
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(
56                  "testSomething2" + "(org.apache.maven.surefire.junitcore.JUnit4Reflector481Test$IgnoreWithDescription)",
57                  desc.toString());
58          assertEquals(
59                  "org.apache.maven.surefire.junitcore.JUnit4Reflector481Test$IgnoreWithDescription",
60                  desc.getClassName());
61          assertEquals("testSomething2", desc.getMethodName());
62          assertEquals(0, desc.getChildren().size());
63          assertEquals(2, desc.getAnnotations().size());
64          assertSame(annotatedIgnore, desc.getAnnotation(Ignore.class));
65          assertEquals(REASON, annotatedIgnore.value());
66      }
67  
68      @Test
69      public void testGetAnnotatedIgnoreWithoutClass() {
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      /**
89       *
90       */
91      public static class IgnoreWithDescription {
92  
93          @Test
94          @Ignore(REASON)
95          public void testSomething2() {}
96      }
97  
98      @Test
99      public void testCreatePureDescription() {
100         Description description = JUnit4Reflector.createDescription("exception");
101         assertEquals("exception", description.getDisplayName());
102         assertEquals("exception", description.toString());
103         assertEquals(0, description.getChildren().size());
104     }
105 
106     @Test
107     public void testCreateDescription() {
108         Ignore ignore = JUnit4Reflector.createIgnored("error");
109         Description description = JUnit4Reflector.createDescription("exception", ignore);
110         assertEquals("exception", description.getDisplayName());
111         assertEquals("exception", description.toString());
112         assertEquals("exception", description.getClassName());
113         assertEquals(0, description.getChildren().size());
114         Ignore annotatedIgnore = JUnit4Reflector.getAnnotatedIgnore(description);
115         assertNotNull(annotatedIgnore);
116         assertEquals("error", annotatedIgnore.value());
117     }
118 }