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