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;
20  
21  import java.util.HashSet;
22  import java.util.List;
23  import java.util.Set;
24  
25  import org.apache.maven.plugins.surefire.report.ReportTestSuite;
26  import org.apache.maven.surefire.its.fixture.HelperAssertions;
27  import org.apache.maven.surefire.its.fixture.IntegrationTestSuiteResults;
28  import org.apache.maven.surefire.its.fixture.OutputValidator;
29  import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase;
30  import org.junit.Test;
31  
32  import static org.junit.Assert.assertEquals;
33  import static org.junit.Assert.fail;
34  
35  /**
36   * Test running two test cases; confirms reporting works correctly
37   *
38   * @author <a href="mailto:dfabulich@apache.org">Dan Fabulich</a>
39   */
40  public class TwoTestCasesIT extends SurefireJUnit4IntegrationTestCase {
41      @Test
42      public void testTwoTestCases() {
43          unpack("junit-twoTestCases")
44                  .sysProp("testNgVersion", "5.7")
45                  .sysProp("testNgClassifier", "jdk15")
46                  .executeTest()
47                  .verifyErrorFreeLog()
48                  .assertTestSuiteResults(2, 0, 0, 0);
49      }
50  
51      /**
52       * Runs two tests encapsulated in a suite
53       */
54      @Test
55      public void testTwoTestCaseSuite() {
56          final OutputValidator outputValidator = unpack("junit-twoTestCaseSuite")
57                  .sysProp("testNgVersion", "5.7")
58                  .sysProp("testNgClassifier", "jdk15")
59                  .executeTest();
60          outputValidator.verifyErrorFreeLog().assertTestSuiteResults(2, 0, 0, 0);
61          List<ReportTestSuite> reports = HelperAssertions.extractReports(outputValidator.getBaseDir());
62          Set<String> classNames = extractClassNames(reports);
63          assertContains(classNames, "junit.twoTestCaseSuite.BasicTest");
64          assertContains(classNames, "junit.twoTestCaseSuite.TestTwo");
65          assertEquals("wrong number of classes", 2, classNames.size());
66          IntegrationTestSuiteResults results = HelperAssertions.parseReportList(reports);
67          HelperAssertions.assertTestSuiteResults(2, 0, 0, 0, results);
68      }
69  
70      private void assertContains(Set<String> set, String expected) {
71          if (set.contains(expected)) {
72              return;
73          }
74          fail("Set didn't contain " + expected);
75      }
76  
77      private Set<String> extractClassNames(List<ReportTestSuite> reports) {
78          HashSet<String> classNames = new HashSet<>();
79          for (ReportTestSuite suite : reports) {
80              classNames.add(suite.getFullClassName());
81          }
82          return classNames;
83      }
84  
85      @Test
86      public void testJunit4Suite() {
87          final OutputValidator outputValidator = unpack("junit4-twoTestCaseSuite")
88                  .sysProp("testNgVersion", "5.7")
89                  .sysProp("testNgClassifier", "jdk15")
90                  .executeTest();
91          outputValidator.verifyErrorFreeLog().assertTestSuiteResults(2, 0, 0, 0);
92  
93          List<ReportTestSuite> reports = HelperAssertions.extractReports(outputValidator.getBaseDir());
94          Set<String> classNames = extractClassNames(reports);
95          assertContains(classNames, "twoTestCaseSuite.BasicTest");
96          assertContains(classNames, "twoTestCaseSuite.Junit4TestTwo");
97          assertEquals("wrong number of classes", 2, classNames.size());
98          IntegrationTestSuiteResults results = HelperAssertions.parseReportList(reports);
99          HelperAssertions.assertTestSuiteResults(2, 0, 0, 0, results);
100     }
101 
102     @Test
103     public void testTestNGSuite() {
104         final OutputValidator outputValidator = unpack("testng-twoTestCaseSuite")
105                 .sysProp("testNgVersion", "5.7")
106                 .sysProp("testNgClassifier", "jdk15")
107                 .executeTest();
108         outputValidator.assertTestSuiteResults(2, 0, 0, 0);
109         List<ReportTestSuite> reports = HelperAssertions.extractReports(outputValidator.getBaseDir());
110         Set<String> classNames = extractClassNames(reports);
111         assertContains(classNames, "testng.two.TestNGTestTwo");
112         assertContains(classNames, "testng.two.TestNGSuiteTest");
113         assertEquals("wrong number of classes", 2, classNames.size());
114         IntegrationTestSuiteResults results = HelperAssertions.parseReportList(reports);
115         HelperAssertions.assertTestSuiteResults(2, 0, 0, 0, results);
116     }
117 }