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.its.jiras;
20  
21  import org.apache.maven.it.VerificationException;
22  import org.apache.maven.surefire.its.fixture.OutputValidator;
23  import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase;
24  import org.apache.maven.surefire.its.fixture.SurefireLauncher;
25  import org.junit.Test;
26  
27  import static org.hamcrest.Matchers.containsString;
28  import static org.hamcrest.Matchers.is;
29  
30  /**
31   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
32   * @see <a href="https://issues.apache.org/jira/browse/SUREFIRE-995">SUREFIRE-995</a>
33   * @since 2.18.1
34   */
35  public class Surefire995CategoryInheritanceIT extends SurefireJUnit4IntegrationTestCase {
36  
37      @Test
38      public void negativeTestShouldRunAllCategories() {
39          unpack().setTestToRun("Special*Test").executeTest().verifyErrorFree(3);
40      }
41  
42      @Test
43      public void junit411ShouldRunExplicitCategory() {
44          final OutputValidator outputValidator = unpack().addGoal("-Ppositive-tests")
45                  .sysProp("version.junit", "4.11")
46                  .executeTest();
47  
48          outputValidator.verifyErrorFree(1).verifyTextInLog("CategorizedTest#a");
49      }
50  
51      @Test
52      public void junit411ShouldExcludeExplicitCategory() {
53          final OutputValidator outputValidator = unpack().addGoal("-Ppositive-tests-excluded-categories")
54                  .sysProp("version.junit", "4.11")
55                  .executeTest();
56          // SpecialCategorizedTest inherits the excluded annotation but should still run as
57          // until junit 4.11, the Category annotation is not inherited
58          outputValidator.verifyTextInLog("SpecialCategorizedTest#b").verifyErrorFree(16);
59      }
60  
61      @Test
62      public void junit412ShouldRunInheritedCategory() {
63          unpack().setTestToRun("Special*Test")
64                  .addGoal("-Ppositive-tests")
65                  .executeTest()
66                  .verifyErrorFree(2);
67      }
68  
69      @Test
70      public void junit412ShouldExcludeInheritedCategory() {
71          unpack().setTestToRun("Special*Test")
72                  .addGoal("-Ppositive-tests-excluded-categories")
73                  .executeTest()
74                  .verifyErrorFree(1)
75                  .verifyTextInLog("SpecialNonCategoryTest#test");
76      }
77  
78      @Test
79      public void junit411ShouldIgnoreInheritedCategories() throws VerificationException {
80          // GIVEN a project using junit 4.11
81          final OutputValidator outputValidator = unpack().addGoal("-Ppositive-tests-included-and-excluded-categories")
82                  .sysProp("version.junit", "4.11")
83                  // AND the tests marked with CategoryB are marked for execution
84                  .setGroups("jiras.surefire955.group.marker.CategoryB")
85                  // WHEN the tests are executed
86                  .executeTest();
87  
88          // THEN only the tests in classes directly annotated should be executed
89          outputValidator
90                  // Test runs when the category is present in the concrete class
91                  .verifyTextInLog("Running jiras.surefire955.group.BBCTest")
92                  .verifyTextInLog("BBCTest#bbc")
93                  .verifyTextInLog("AbstractBCTest#pb")
94                  .verifyTextInLog("AbstractCTest#pc")
95                  .verifyTextInLog("Running jiras.surefire955.group.BTest")
96                  .verifyTextInLog("BTest#b")
97                  // Test does not run when there is no category in the concrete class
98                  .assertThatLogLine(containsString("BCTest#bc"), is(0))
99                  .assertThatLogLine(containsString("ABCTest#abc"), is(0))
100                 .verifyErrorFree(4);
101     }
102 
103     @Test
104     public void junit412ShouldExecuteInheritedCategories() {
105         // GIVEN a project using junit 4.12
106         final OutputValidator outputValidator = unpack().addGoal("-Ppositive-tests-included-and-excluded-categories")
107                 .sysProp("version.junit", "4.12")
108                 // AND the tests marked with CategoryB are marked for execution
109                 .setGroups("jiras.surefire955.group.marker.CategoryB")
110                 // WHEN the tests are executed
111                 .executeTest();
112 
113         // THEN the tests in classes directly marked with the CategoryB
114         outputValidator
115                 .verifyErrorFree(10)
116                 .verifyTextInLog("Running jiras.surefire955.group.BBCTest")
117                 // AND Test runs when an already existing category is added in the concrete class
118                 .verifyTextInLog("BBCTest#bbc")
119                 .verifyTextInLog("AbstractBCTest#pb")
120                 .verifyTextInLog("AbstractCTest#pc")
121                 .verifyTextInLog("Running jiras.surefire955.group.BTest")
122                 .verifyTextInLog("BTest#b")
123                 // AND the tests in classes inheriting the CategoryB category should be executed
124                 .verifyTextInLog("Running jiras.surefire955.group.ABCTest")
125                 // AND Test runs when the concrete class has an additional (not excluded) category
126                 .verifyTextInLog("ABCTest#abc")
127                 .verifyTextInLog("AbstractBCTest#pb")
128                 .verifyTextInLog("AbstractCTest#pc")
129                 .verifyTextInLog("Running jiras.surefire955.group.BCTest")
130                 // AND Test runs when there is no category in the concrete class
131                 .verifyTextInLog("BCTest#bc")
132                 .verifyTextInLog("AbstractBCTest#pb")
133                 .verifyTextInLog("AbstractCTest#pc");
134     }
135 
136     private SurefireLauncher unpack() {
137         return unpack("surefire-995-categoryInheritance");
138     }
139 }