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