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 java.io.FileNotFoundException;
22  
23  import org.apache.maven.shared.utils.xml.Xpp3Dom;
24  import org.apache.maven.shared.utils.xml.Xpp3DomBuilder;
25  import org.apache.maven.surefire.its.fixture.OutputValidator;
26  import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase;
27  import org.junit.Assert;
28  import org.junit.Test;
29  
30  /**
31   *
32   */
33  public class Surefire943ReportContentIT extends SurefireJUnit4IntegrationTestCase {
34  
35      @Test
36      @SuppressWarnings("checkstyle:methodname")
37      public void test_noParallel() throws Exception {
38          doTest("none");
39      }
40  
41      @Test
42      @SuppressWarnings("checkstyle:methodname")
43      public void test_parallelBoth() throws Exception {
44          doTest("both");
45      }
46  
47      private void doTest(String parallelMode) throws Exception {
48          OutputValidator validator = unpack("surefire-943-report-content")
49                  .maven()
50                  .sysProp("parallel", parallelMode)
51                  .sysProp("threadCount", 4)
52                  .withFailure()
53                  .executeTest();
54  
55          validator.assertTestSuiteResults(10, 1, 3, 3);
56  
57          validate(validator, "org.sample.module.My1Test", 1);
58          validate(validator, "org.sample.module.My2Test", 1);
59          validate(validator, "org.sample.module.My3Test", 0);
60          validateSkipped(validator, "org.sample.module.My4Test");
61          validateFailInBeforeClass(validator, "org.sample.module.My5Test");
62      }
63  
64      private void validateFailInBeforeClass(OutputValidator validator, String className) throws FileNotFoundException {
65          Xpp3Dom[] children = readTests(validator, className);
66  
67          Assert.assertEquals(1, children.length);
68  
69          Xpp3Dom child = children[0];
70  
71          Assert.assertEquals(className, child.getAttribute("classname"));
72          Assert.assertEquals("", child.getAttribute("name"));
73  
74          Assert.assertEquals(
75                  "Expected error tag for failed BeforeClass method for " + className,
76                  1,
77                  child.getChildren("error").length);
78  
79          Assert.assertTrue(
80                  "time for test failure in BeforeClass is expected to be positive",
81                  Double.compare(Double.parseDouble(child.getAttribute("time")), 0.0d) >= 0);
82  
83          Assert.assertTrue(
84                  "time for test failure in BeforeClass is expected to be resonably low",
85                  Double.compare(Double.parseDouble(child.getAttribute("time")), 2.0d) <= 0);
86      }
87  
88      private void validateSkipped(OutputValidator validator, String className) throws FileNotFoundException {
89          Xpp3Dom[] children = readTests(validator, className);
90  
91          Assert.assertEquals(1, children.length);
92  
93          Xpp3Dom child = children[0];
94  
95          Assert.assertEquals(className, child.getAttribute("classname"));
96          Assert.assertEquals("", child.getAttribute("name"));
97  
98          Assert.assertEquals(
99                  "Expected skipped tag for ignored method for " + className, 1, child.getChildren("skipped").length);
100 
101         Assert.assertEquals(
102                 "time for ignored test is expected to be zero",
103                 0,
104                 Double.compare(Double.parseDouble(child.getAttribute("time")), 0.0d));
105     }
106 
107     private void validate(OutputValidator validator, String className, int ignored) throws FileNotFoundException {
108         Xpp3Dom[] children = readTests(validator, className);
109 
110         Assert.assertEquals(2 + ignored, children.length);
111 
112         for (Xpp3Dom child : children) {
113             Assert.assertEquals(className, child.getAttribute("classname"));
114 
115             if ("alwaysSuccessful".equals(child.getAttribute("name"))) {
116                 Assert.assertEquals(
117                         "Expected no failures for method alwaysSuccessful for " + className, 0, child.getChildCount());
118 
119                 Assert.assertTrue(
120                         "time for successful test is expected to be positive",
121                         Double.compare(Double.parseDouble(child.getAttribute("time")), 0.0d) > 0);
122             } else if (child.getAttribute("name").contains("Ignored")) {
123                 Assert.assertEquals(
124                         "Expected skipped-tag for ignored method for " + className,
125                         1,
126                         child.getChildren("skipped").length);
127 
128                 Assert.assertEquals(
129                         "time for ignored test is expected to be zero",
130                         0,
131                         Double.compare(Double.parseDouble(child.getAttribute("time")), 0.0d));
132 
133             } else {
134                 Assert.assertEquals(
135                         "Expected methods \"alwaysSuccessful\", \"*Ignored\" and \"fails\" in " + className,
136                         "fails",
137                         child.getAttribute("name"));
138                 Assert.assertEquals(
139                         "Expected failure description for method \"fails\" in " + className,
140                         1,
141                         child.getChildren("failure").length);
142                 Assert.assertTrue(
143                         "time for failed test is expected to be positive",
144                         Double.compare(Double.parseDouble(child.getAttribute("time")), 0.0d) > 0);
145             }
146         }
147     }
148 
149     private Xpp3Dom[] readTests(OutputValidator validator, String className) throws FileNotFoundException {
150         Xpp3Dom testResult = Xpp3DomBuilder.build(
151                 validator
152                         .getSurefireReportsXmlFile("TEST-" + className + ".xml")
153                         .getFileInputStream(),
154                 "UTF-8");
155         return testResult.getChildren("testcase");
156     }
157 }