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