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