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.codehaus.plexus.logging.Logger;
31  import org.junit.Before;
32  import org.junit.Rule;
33  import org.junit.Test;
34  import org.junit.rules.TemporaryFolder;
35  
36  import static org.mockito.Mockito.mock;
37  import static org.mockito.Mockito.when;
38  
39  /**
40   */
41  public class VerifyMojoTest {
42      private VerifyMojo mojo;
43  
44      @Rule
45      public TemporaryFolder tempFolder = new TemporaryFolder();
46  
47      @Before
48      public void init() throws UnsupportedEncodingException {
49          mojo = new VerifyMojo();
50          mojo.setTestClassesDirectory(tempFolder.getRoot());
51          mojo.setReportsDirectory(getTestBaseDir());
52      }
53  
54      private void setupExecuteMocks() {
55          Logger logger = mock(Logger.class);
56          when(logger.isErrorEnabled()).thenReturn(true);
57          when(logger.isWarnEnabled()).thenReturn(true);
58          when(logger.isInfoEnabled()).thenReturn(true);
59          when(logger.isDebugEnabled()).thenReturn(false);
60          mojo.setLogger(logger);
61  
62          MavenSession session = mock(MavenSession.class);
63          MavenExecutionRequest request = mock(MavenExecutionRequest.class);
64          when(request.isShowErrors()).thenReturn(true);
65          when(request.getReactorFailureBehavior()).thenReturn(null);
66          when(session.getRequest()).thenReturn(request);
67          mojo.setSession(session);
68      }
69  
70      private File getTestBaseDir() throws UnsupportedEncodingException {
71          URL resource = getClass().getResource("/verify-mojo");
72          // URLDecoder.decode necessary for JDK 1.5+, where spaces are escaped to %20
73          return new File(URLDecoder.decode(resource.getPath(), "UTF-8")).getAbsoluteFile();
74      }
75  
76      @Test(expected = MojoExecutionException.class)
77      public void executeForForkError()
78              throws MojoExecutionException, MojoFailureException, UnsupportedEncodingException {
79          setupExecuteMocks();
80          mojo.setSummaryFile(new File(getTestBaseDir(), "failsafe-summary-booter-fork-error.xml"));
81          mojo.execute();
82      }
83  
84      @Test(expected = MojoExecutionException.class)
85      public void executeForForkErrorTestFailureIgnore()
86              throws MojoExecutionException, MojoFailureException, UnsupportedEncodingException {
87          setupExecuteMocks();
88          mojo.setSummaryFile(new File(getTestBaseDir(), "failsafe-summary-booter-fork-error.xml"));
89          mojo.setTestFailureIgnore(true);
90          mojo.execute();
91      }
92  
93      @Test
94      public void executeForPassingTests()
95              throws MojoExecutionException, MojoFailureException, UnsupportedEncodingException {
96          setupExecuteMocks();
97          mojo.setSummaryFile(new File(getTestBaseDir(), "failsafe-summary-success.xml"));
98          mojo.execute();
99      }
100 }