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.failsafe;
20  
21  import java.io.File;
22  import java.io.UnsupportedEncodingException;
23  import java.net.URL;
24  import java.net.URLDecoder;
25  
26  import org.apache.maven.execution.MavenExecutionRequest;
27  import org.apache.maven.execution.MavenSession;
28  import org.apache.maven.plugin.MojoExecutionException;
29  import org.apache.maven.plugin.MojoFailureException;
30  import org.junit.jupiter.api.BeforeEach;
31  import org.junit.jupiter.api.Test;
32  import org.junit.jupiter.api.io.TempDir;
33  import org.slf4j.Logger;
34  
35  import static org.assertj.core.api.Assertions.assertThatCode;
36  import static org.mockito.Mockito.mock;
37  import static org.mockito.Mockito.when;
38  
39  class VerifyMojoTest {
40      private VerifyMojo mojo;
41  
42      @TempDir
43      private File tempFolder;
44  
45      private Logger logger = mock(Logger.class);
46  
47      @BeforeEach
48      void init() throws UnsupportedEncodingException {
49          mojo = new VerifyMojo(logger);
50          mojo.setTestClassesDirectory(tempFolder);
51          mojo.setReportsDirectory(getTestBaseDir());
52      }
53  
54      private void setupExecuteMocks() {
55          when(logger.isErrorEnabled()).thenReturn(true);
56          when(logger.isWarnEnabled()).thenReturn(true);
57          when(logger.isInfoEnabled()).thenReturn(true);
58          when(logger.isDebugEnabled()).thenReturn(false);
59  
60          MavenSession session = mock(MavenSession.class);
61          MavenExecutionRequest request = mock(MavenExecutionRequest.class);
62          when(request.isShowErrors()).thenReturn(true);
63          when(request.getReactorFailureBehavior()).thenReturn(null);
64          when(session.getRequest()).thenReturn(request);
65          mojo.setSession(session);
66      }
67  
68      private File getTestBaseDir() throws UnsupportedEncodingException {
69          URL resource = getClass().getResource("/verify-mojo");
70          // URLDecoder.decode necessary for JDK 1.5+, where spaces are escaped to %20
71          return new File(URLDecoder.decode(resource.getPath(), "UTF-8")).getAbsoluteFile();
72      }
73  
74      @Test
75      void executeForForkError() throws UnsupportedEncodingException {
76          setupExecuteMocks();
77          mojo.setSummaryFile(new File(getTestBaseDir(), "failsafe-summary-booter-fork-error.xml"));
78  
79          assertThatCode(mojo::execute).isExactlyInstanceOf(MojoExecutionException.class);
80      }
81  
82      @Test
83      void executeForForkErrorTestFailureIgnore() throws UnsupportedEncodingException {
84          setupExecuteMocks();
85          mojo.setSummaryFile(new File(getTestBaseDir(), "failsafe-summary-booter-fork-error.xml"));
86          mojo.setTestFailureIgnore(true);
87  
88          assertThatCode(mojo::execute).isExactlyInstanceOf(MojoExecutionException.class);
89      }
90  
91      @Test
92      void executeForPassingTests() throws MojoExecutionException, MojoFailureException, UnsupportedEncodingException {
93          setupExecuteMocks();
94          mojo.setSummaryFile(new File(getTestBaseDir(), "failsafe-summary-success.xml"));
95          mojo.execute();
96      }
97  }