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 org.apache.maven.surefire.its.fixture.OutputValidator;
23  import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase;
24  import org.apache.maven.surefire.its.fixture.SurefireVerifierException;
25  import org.junit.BeforeClass;
26  import org.junit.Test;
27  
28  import java.io.File;
29  import java.io.FilenameFilter;
30  
31  import static org.fest.assertions.Assertions.assertThat;
32  
33  /**
34   * SUREFIRE-613 Asserts proper test counts when running in parallel
35   *
36   * @author Kristian Rosenvold
37   */
38  public class Surefire141PluggableProvidersIT
39      extends SurefireJUnit4IntegrationTestCase
40  {
41      @BeforeClass
42      public static void installProvider()
43      {
44          unpack( Surefire141PluggableProvidersIT.class, "surefire-141-pluggableproviders-provider", "prov" )
45              .executeInstall();
46      }
47  
48      @Test
49      public void pluggableProviderPresent()
50      {
51          unpack( "surefire-141-pluggableproviders" )
52              .setForkJvm()
53              .maven()
54              .showExceptionMessages()
55              .debugLogging()
56              .executeTest()
57              .verifyTextInLog( "Using configured provider org.apache.maven.surefire.testprovider.TestProvider" )
58              .verifyTextInLog( "Using configured provider org.apache.maven.surefire.junit.JUnit3Provider" )
59              .verifyErrorFreeLog();
60      }
61  
62      @Test
63      public void invokeRuntimeException()
64      {
65          final String errorText = "Let's fail with a runtimeException";
66  
67          OutputValidator validator = unpack( "surefire-141-pluggableproviders" )
68              .setForkJvm()
69              .sysProp( "invokeCrash", "runtimeException" )
70              .maven()
71              .withFailure()
72              .executeTest();
73  
74          assertErrorMessage( validator, errorText );
75  
76          boolean hasErrorInLog = verifiedErrorInLog( validator, "There was an error in the forked process" );
77          boolean verifiedInLog = verifiedErrorInLog( validator, errorText );
78          assertThat( hasErrorInLog && verifiedInLog )
79                  .describedAs( "'" + errorText + "' could not be verified in log.txt nor *.dump file. ("
80                                        + hasErrorInLog + ", " + verifiedInLog + ")" )
81                  .isTrue();
82      }
83  
84      @Test
85      public void invokeReporterException()
86      {
87          final String errorText = "Let's fail with a reporterexception";
88  
89          OutputValidator validator = unpack( "surefire-141-pluggableproviders" )
90              .setForkJvm()
91              .sysProp( "invokeCrash", "reporterException" )
92              .maven()
93              .withFailure()
94              .executeTest();
95  
96          assertErrorMessage( validator, errorText );
97  
98          boolean hasErrorInLog = verifiedErrorInLog( validator, "There was an error in the forked process" );
99          boolean verifiedInLog = verifiedErrorInLog( validator, errorText );
100         assertThat( hasErrorInLog && verifiedInLog )
101                 .describedAs( "'" + errorText + "' could not be verified in log.txt nor *.dump file. ("
102                                       + hasErrorInLog + ", " + verifiedInLog + ")" )
103                 .isTrue();
104     }
105 
106     @Test
107     public void constructorRuntimeException()
108     {
109         final String errorText = "Let's fail with a runtimeException";
110 
111         OutputValidator validator = unpack( "surefire-141-pluggableproviders" )
112                                             .setForkJvm()
113                                             .sysProp( "constructorCrash", "runtimeException" )
114                                             .maven()
115                                             .withFailure()
116                                             .executeTest();
117 
118         assertErrorMessage( validator, errorText );
119 
120         boolean hasErrorInLog = verifiedErrorInLog( validator, "There was an error in the forked process" );
121         boolean verifiedInLog = verifiedErrorInLog( validator, errorText );
122         assertThat( hasErrorInLog && verifiedInLog )
123                 .describedAs( "'" + errorText + "' could not be verified in log.txt nor *.dump file. ("
124                                       + hasErrorInLog + ", " + verifiedInLog + ")" )
125                 .isTrue();
126     }
127 
128     private static void assertErrorMessage( OutputValidator validator, String message )
129     {
130         File reportDir = validator.getSurefireReportsDirectory();
131         String[] dumpFiles = reportDir.list( new FilenameFilter()
132                                              {
133                                                  @Override
134                                                  public boolean accept( File dir, String name )
135                                                  {
136                                                      return name.endsWith( "-jvmRun1.dump" );
137                                                  }
138                                              });
139         assertThat( dumpFiles )
140                 .isNotNull()
141                 .isNotEmpty();
142         for ( String dump : dumpFiles )
143         {
144             validator.getSurefireReportsFile( dump )
145                     .assertContainsText( message );
146         }
147     }
148 
149     private static boolean verifiedErrorInLog( OutputValidator validator, String errorText )
150     {
151         try
152         {
153             validator.verifyTextInLog( errorText );
154             return true;
155         }
156         catch ( SurefireVerifierException e )
157         {
158             return false;
159         }
160     }
161 }