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.plugin.surefire;
20  
21  import java.io.File;
22  
23  import junit.framework.TestCase;
24  import org.apache.maven.plugin.MojoFailureException;
25  import org.apache.maven.surefire.api.suite.RunResult;
26  import org.junit.Rule;
27  import org.junit.rules.ExpectedException;
28  
29  import static org.assertj.core.api.Assertions.assertThat;
30  
31  /**
32   *
33   */
34  public class SurefirePluginTest extends TestCase {
35      @Rule
36      public final ExpectedException e = ExpectedException.none();
37  
38      public void testDefaultIncludes() {
39          assertThat(new SurefirePlugin().getDefaultIncludes())
40                  .containsOnly("**/Test*.java", "**/*Test.java", "**/*Tests.java", "**/*TestCase.java");
41      }
42  
43      public void testReportSchemaLocation() {
44          assertThat(new SurefirePlugin().getReportSchemaLocation())
45                  .isEqualTo("https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report-3.0.xsd");
46      }
47  
48      public void testFailIfNoTests() throws Exception {
49          RunResult runResult = new RunResult(0, 0, 0, 0);
50          try {
51              SurefirePlugin plugin = new SurefirePlugin();
52              plugin.setFailIfNoTests(true);
53              plugin.handleSummary(runResult, null);
54          } catch (MojoFailureException e) {
55              assertThat(e.getLocalizedMessage())
56                      .isEqualTo("No tests were executed!  (Set -DfailIfNoTests=false to ignore this error.)");
57              return;
58          }
59          fail("Expected MojoFailureException with message "
60                  + "'No tests were executed!  (Set -DfailIfNoTests=false to ignore this error.)'");
61      }
62  
63      public void testTestFailure() throws Exception {
64          RunResult runResult = new RunResult(1, 0, 1, 0);
65          try {
66              SurefirePlugin plugin = new SurefirePlugin();
67              plugin.handleSummary(runResult, null);
68          } catch (MojoFailureException e) {
69              assertThat(e.getLocalizedMessage())
70                      .isEqualTo("There are test failures.\n\nPlease refer to null "
71                              + "for the individual test results.\nPlease refer to dump files (if any exist) "
72                              + "[date].dump, [date]-jvmRun[N].dump and [date].dumpstream.");
73              return;
74          }
75          fail("Expected MojoFailureException with message "
76                  + "'There are test failures.\n\nPlease refer to null "
77                  + "for the individual test results.\nPlease refer to dump files (if any exist) "
78                  + "[date].dump, [date]-jvmRun[N].dump and [date].dumpstream.'");
79      }
80  
81      public void testPluginName() {
82          assertThat(new SurefirePlugin().getPluginName()).isEqualTo("surefire");
83      }
84  
85      public void testShouldGetNullEnv() {
86          SurefirePlugin plugin = new SurefirePlugin();
87          assertThat(plugin.getExcludedEnvironmentVariables()).hasSize(0);
88      }
89  
90      public void testShouldGetEnv() {
91          SurefirePlugin plugin = new SurefirePlugin();
92          plugin.setExcludedEnvironmentVariables(new String[] {"ABC", "KLM"});
93          assertThat(plugin.getExcludedEnvironmentVariables()).hasSize(2).contains("ABC", "KLM");
94      }
95  
96      public void testShouldGetPropertyFile() {
97          SurefirePlugin plugin = new SurefirePlugin();
98          plugin.setSystemPropertiesFile(new File("testShouldGetPropertyFile"));
99          assertThat(plugin.getSystemPropertiesFile()).isEqualTo(new File("testShouldGetPropertyFile"));
100     }
101 
102     public void testNegativeFailOnFlakeCount() {
103         SurefirePlugin plugin = new SurefirePlugin();
104         plugin.setFailOnFlakeCount(-1);
105         e.expect(MojoFailureException.class);
106         e.expectMessage("Parameter \"failOnFlakeCount\" should not be negative.");
107     }
108 
109     public void testFailOnFlakeCountWithoutRerun() {
110         SurefirePlugin plugin = new SurefirePlugin();
111         plugin.setFailOnFlakeCount(1);
112         e.expect(MojoFailureException.class);
113         e.expectMessage("\"failOnFlakeCount\" requires rerunFailingTestsCount to be at least 1.");
114     }
115 
116     public void testShouldHaveJUnit5EnginesFilter() {
117         SurefirePlugin plugin = new SurefirePlugin();
118 
119         plugin.setIncludeJUnit5Engines(new String[] {"e1", "e2"});
120         assertThat(plugin.getIncludeJUnit5Engines()).isEqualTo(new String[] {"e1", "e2"});
121 
122         plugin.setExcludeJUnit5Engines(new String[] {"e1", "e2"});
123         assertThat(plugin.getExcludeJUnit5Engines()).isEqualTo(new String[] {"e1", "e2"});
124     }
125 }