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 static org.apache.maven.shared.utils.xml.Xpp3DomBuilder.build;
23  import static org.hamcrest.Matchers.*;
24  import static org.junit.Assert.assertThat;
25  
26  import java.io.FileNotFoundException;
27  
28  import org.apache.maven.shared.utils.xml.Xpp3Dom;
29  import org.apache.maven.surefire.its.fixture.OutputValidator;
30  import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase;
31  import org.apache.maven.surefire.its.fixture.SurefireLauncher;
32  import org.junit.Test;
33  
34  /**
35   * Test surefire-report on TestNG test
36   *
37   * @author <a href="mailto:michal.bocek@gmail.com">Michal Bocek</a>
38   */
39  public class Surefire1135ImproveIgnoreMessageForTestNGIT
40          extends SurefireJUnit4IntegrationTestCase
41  {
42  
43      private enum ResultType
44      {
45          SKIPPED( "skipped" ), FAILURE( "failure" );
46  
47          private final String type;
48  
49          ResultType(String type)
50          {
51              this.type = type;
52          }
53  
54          public String getType() {
55              return type;
56          }
57      }
58  
59      @Test
60      public void testNgReport688() throws Exception {
61          testNgReport( "6.8.8", null, ResultType.SKIPPED,
62                              "Skip test",
63                                     /*"org.testng.SkipException"*/ null,
64                                     /*"SkipExceptionReportTest.java:30"*/ null );
65      }
66  
67      @Test
68      public void testNgReport57() throws Exception {
69          testNgReport( "5.7", "jdk15", ResultType.SKIPPED,
70                              "Skip test",
71                                     /*"org.testng.SkipException"*/ null,
72                                     /*"SkipExceptionReportTest.java:30"*/ null );
73      }
74  
75      private void testNgReport( String version, String classifier, ResultType resultType, String message, String type,
76                                 String stackTrace )
77              throws Exception
78      {
79          OutputValidator outputValidator =
80                  runTest( version, classifier, resultType, "/surefire-1135-improve-ignore-message-for-testng" );
81  
82          Xpp3Dom[] children = readTests( outputValidator, "testng.SkipExceptionReportTest" );
83          assertThat( "Report should contains only one test case", children.length, is( 1 ) );
84  
85          Xpp3Dom test = children[0];
86          assertThat( "Not expected classname", test.getAttribute( "classname" ),
87                            is( "testng.SkipExceptionReportTest" ) );
88  
89          assertThat( "Not expected test name", test.getAttribute( "name" ), is( "testSkipException" ) );
90  
91          children = test.getChildren( resultType.getType() );
92          assertThat( "Test should contains only one " + resultType.getType() + " element", children,
93                            is( arrayWithSize( 1 ) ) );
94  
95          Xpp3Dom result = children[0];
96          if ( message == null )
97          {
98              assertThat( "Subelement message attribute must be null", result.getAttribute( "message" ),
99                                is( nullValue() ) );
100         }
101         else
102         {
103             assertThat( "Subelement should contains message attribute", result.getAttribute( "message" ),
104                               is( message ) );
105         }
106 
107         if ( type == null )
108         {
109             assertThat( "Subelement type attribute must be null", result.getAttribute( "type" ), is( nullValue() ) );
110         }
111         else
112         {
113             assertThat( "Subelement should contains type attribute", result.getAttribute( "type" ), is( type ) );
114         }
115 
116         if ( stackTrace == null )
117         {
118             assertThat( "Element body must be null", result.getValue() , isEmptyOrNullString() );
119         }
120         else
121         {
122             assertThat( "Element body must contains", result.getValue(), containsString( stackTrace ) );
123         }
124     }
125 
126     private OutputValidator runTest( String version, String classifier, ResultType resultType, String resource )
127     {
128         int skipped = ResultType.SKIPPED.equals( resultType ) ? 1 : 0;
129         int failure = ResultType.FAILURE.equals( resultType ) ? 1 : 0;
130 
131         SurefireLauncher launcher = unpack( resource ).sysProp( "testNgVersion", version );
132 
133         if ( classifier != null )
134         {
135             launcher.sysProp( "testNgClassifier", classifier );
136         }
137 
138         return launcher.addSurefireReportGoal()
139                 .executeCurrentGoals()
140                 .assertTestSuiteResults( 1, 0, failure, skipped );
141     }
142 
143     private static Xpp3Dom[] readTests( OutputValidator validator, String className )
144             throws FileNotFoundException
145     {
146         Xpp3Dom testResult =
147                 build( validator.getSurefireReportsXmlFile( "TEST-" + className + ".xml" ).getFileInputStream(),
148                              "UTF-8"
149                 );
150         return testResult.getChildren( "testcase" );
151     }
152 }