From 0eb85f7a2f971968a9293307616818a0d85f2ee8 Mon Sep 17 00:00:00 2001 From: tibordigana Date: Sat, 3 Jan 2015 20:09:01 +0100 Subject: [PATCH] [SUREFIRE-855] Allow failsafe to use actual jar file instead of target/classes --- .../plugin/failsafe/IntegrationTestMojo.java | 21 ++- .../plugin/surefire/AbstractSurefireMojo.java | 16 +- .../maven/plugin/surefire/SurefirePlugin.java | 7 + .../apt/examples/configuring-classpath.apt.vm | 8 +- surefire-integration-tests/pom.xml | 2 +- .../surefire/its/fixture/OutputValidator.java | 14 +- ...fire855AllowFailsafeUseArtifactFileIT.java | 58 +++++++ .../surefire-855-failsafe-use-bundle/pom.xml | 129 ++++++++++++++ .../src/main/java/pkg/AClassInOSGiBundle.java | 24 +++ .../resources/main/surefire855.properties | 1 + .../java/jiras/surefre855/bundle/FooIT.java | 146 ++++++++++++++++ .../bundle/properties/surefire855.properties | 1 + .../surefire-855-failsafe-use-jar/pom.xml | 123 +++++++++++++ .../src/main/java/pkg/ToRunJavadoc.java | 8 + .../resources/main/surefire855.properties | 1 + .../java/jiras/surefire855/jar/FooIT.java | 162 ++++++++++++++++++ .../jar/properties/surefire855.properties | 1 + .../surefire-855-failsafe-use-war/pom.xml | 126 ++++++++++++++ .../java/pkg/ToCreateClassesDirectory.java | 24 +++ .../resources/main/surefire855.properties | 1 + .../java/jiras/surefire855/war/FooIT.java | 142 +++++++++++++++ .../war/properties/surefire855.properties | 1 + 22 files changed, 988 insertions(+), 28 deletions(-) create mode 100644 surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire855AllowFailsafeUseArtifactFileIT.java create mode 100644 surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-bundle/pom.xml create mode 100644 surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-bundle/src/main/java/pkg/AClassInOSGiBundle.java create mode 100644 surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-bundle/src/main/resources/main/surefire855.properties create mode 100644 surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-bundle/src/test/java/jiras/surefre855/bundle/FooIT.java create mode 100644 surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-bundle/src/test/resources/jiras/surefire855/bundle/properties/surefire855.properties create mode 100644 surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-jar/pom.xml create mode 100644 surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-jar/src/main/java/pkg/ToRunJavadoc.java create mode 100644 surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-jar/src/main/resources/main/surefire855.properties create mode 100644 surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-jar/src/test/java/jiras/surefire855/jar/FooIT.java create mode 100644 surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-jar/src/test/resources/jiras/surefire855/jar/properties/surefire855.properties create mode 100644 surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-war/pom.xml create mode 100644 surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-war/src/main/java/pkg/ToCreateClassesDirectory.java create mode 100644 surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-war/src/main/resources/main/surefire855.properties create mode 100644 surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-war/src/test/java/jiras/surefire855/war/FooIT.java create mode 100644 surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-war/src/test/resources/jiras/surefire855/war/properties/surefire855.properties diff --git a/maven-failsafe-plugin/src/main/java/org/apache/maven/plugin/failsafe/IntegrationTestMojo.java b/maven-failsafe-plugin/src/main/java/org/apache/maven/plugin/failsafe/IntegrationTestMojo.java index a4a69d7fe5..a7d108963d 100644 --- a/maven-failsafe-plugin/src/main/java/org/apache/maven/plugin/failsafe/IntegrationTestMojo.java +++ b/maven-failsafe-plugin/src/main/java/org/apache/maven/plugin/failsafe/IntegrationTestMojo.java @@ -25,6 +25,7 @@ import java.io.IOException; import java.util.List; +import org.apache.maven.artifact.Artifact; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugin.surefire.AbstractSurefireMojo; @@ -54,6 +55,13 @@ public class IntegrationTestMojo private static final String FAILSAFE_IN_PROGRESS_CONTEXT_KEY = "failsafe-in-progress"; + /** + * The path representing project jar archive, if exists; Otherwise the directory containing generated + * classes of the project being tested. This will be included after the test classes in the test classpath. + */ + @Parameter( defaultValue = "${project.build.outputDirectory}" ) + private File classesDirectory; + /** * Set this to "true" to skip running integration tests, but still compile them. Its use is NOT RECOMMENDED, but * quite convenient on occasion. @@ -422,9 +430,20 @@ public void setTestClassesDirectory( File testClassesDirectory ) this.testClassesDirectory = testClassesDirectory; } + /** + * @return Output directory, or artifact file if artifact type is "jar". If not forking the JVM, parameter + * {@link #useSystemClassLoader} is ignored and the {@link org.apache.maven.surefire.booter.IsolatedClassLoader} is + * used instead. See the resolution of {@link #getClassLoaderConfiguration() ClassLoaderConfiguration}. + */ public File getClassesDirectory() { - return classesDirectory; + Artifact artifact = getProject().getArtifact(); + File artifactFile = artifact.getFile(); + + boolean useArtifactFile = artifactFile != null && artifactFile.isFile() + && artifactFile.getName().toLowerCase().endsWith( ".jar" ); + + return useArtifactFile ? artifactFile : classesDirectory; } public void setClassesDirectory( File classesDirectory ) diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java index f59576d3a9..2999a8ebfc 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java @@ -160,13 +160,6 @@ public abstract class AbstractSurefireMojo @Parameter( defaultValue = "${project.build.testOutputDirectory}" ) protected File testClassesDirectory; - /** - * The directory containing generated classes of the project being tested. This will be included after the test - * classes in the test classpath. - */ - @Parameter( defaultValue = "${project.build.outputDirectory}" ) - protected File classesDirectory; - /** * List of dependencies to exclude from the test classpath. Each dependency string must follow the format * groupId:artifactId. For example: org.acme:project-a @@ -947,7 +940,7 @@ private RunResult executeProvider( ProviderInfo provider, DefaultScanResult scan TestSetFailedException { SurefireProperties effectiveProperties = setupProperties(); - ClassLoaderConfiguration classLoaderConfiguration = getClassLoaderConfiguration( isForking() ); + ClassLoaderConfiguration classLoaderConfiguration = getClassLoaderConfiguration(); provider.addProviderProperties(); RunOrderParameters runOrderParameters = new RunOrderParameters( getRunOrder(), getStatisticsFileName( getConfigChecksum() ) ); @@ -1330,7 +1323,7 @@ static boolean isForkModeNever( String forkMode ) return ForkConfiguration.FORK_NEVER.equals( forkMode ); } - boolean isForking() + protected boolean isForking() { return 0 < getEffectiveForkCount(); } @@ -1493,7 +1486,6 @@ StartupConfiguration createStartupConfiguration( ProviderInfo provider, { throw new MojoExecutionException( "Unable to generate classpath: " + e, e ); } - } private Artifact getCommonArtifact() @@ -1986,9 +1978,9 @@ protected boolean hasExecutedBefore() return false; } - protected ClassLoaderConfiguration getClassLoaderConfiguration( boolean isForking ) + protected ClassLoaderConfiguration getClassLoaderConfiguration() { - return isForking + return isForking() ? new ClassLoaderConfiguration( isUseSystemClassLoader(), isUseManifestOnlyJar() ) : new ClassLoaderConfiguration( false, false ); } diff --git a/maven-surefire-plugin/src/main/java/org/apache/maven/plugin/surefire/SurefirePlugin.java b/maven-surefire-plugin/src/main/java/org/apache/maven/plugin/surefire/SurefirePlugin.java index 6356c41a14..e3c2e8d82d 100644 --- a/maven-surefire-plugin/src/main/java/org/apache/maven/plugin/surefire/SurefirePlugin.java +++ b/maven-surefire-plugin/src/main/java/org/apache/maven/plugin/surefire/SurefirePlugin.java @@ -43,6 +43,13 @@ public class SurefirePlugin implements SurefireReportParameters { + /** + * The directory containing generated classes of the project being tested. This will be included after the test + * classes in the test classpath. + */ + @Parameter( defaultValue = "${project.build.outputDirectory}" ) + private File classesDirectory; + /** * Set this to "true" to ignore a failure during testing. Its use is NOT RECOMMENDED, but quite convenient on * occasion. diff --git a/maven-surefire-plugin/src/site/apt/examples/configuring-classpath.apt.vm b/maven-surefire-plugin/src/site/apt/examples/configuring-classpath.apt.vm index bd4b631d8b..baeae01736 100644 --- a/maven-surefire-plugin/src/site/apt/examples/configuring-classpath.apt.vm +++ b/maven-surefire-plugin/src/site/apt/examples/configuring-classpath.apt.vm @@ -38,13 +38,19 @@ The Default Classpath #{else} [[1]] The {{{../integration-test-mojo.html#testClassesDirectory}test-classes}} directory - [[2]] The {{{../integration-test-mojo.html#classesDirectory}classes}} directory + [[2]] The {{{../integration-test-mojo.html#classesDirectory}classes}} jar archive or directory #{end} [[3]] The project dependencies [[4]] Additional classpath elements + +#{if}(${project.artifactId}=="maven-failsafe-plugin") + Notice that loading jar archive is preferable over the output classes directory in the maven-failsafe-plugin. +#{end} + + Additional Classpath Elements If you need to put more stuff in your classpath when ${thisPlugin} executes (e.g some funky resources or a container specific JAR), diff --git a/surefire-integration-tests/pom.xml b/surefire-integration-tests/pom.xml index 2800a13778..d1171d5de5 100644 --- a/surefire-integration-tests/pom.xml +++ b/surefire-integration-tests/pom.xml @@ -61,7 +61,7 @@ junit junit - 4.10 + 4.11 test diff --git a/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/fixture/OutputValidator.java b/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/fixture/OutputValidator.java index 970189839e..6ef06b4d63 100644 --- a/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/fixture/OutputValidator.java +++ b/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/fixture/OutputValidator.java @@ -44,7 +44,6 @@ public OutputValidator( Verifier verifier ) { this.verifier = verifier; this.baseDir = new File( verifier.getBasedir() ); - } public OutputValidator verifyTextInLog( String text ) @@ -107,8 +106,6 @@ public List loadFile( File file, Charset charset ) } } - - public String getBasedir() { return verifier.getBasedir(); @@ -167,7 +164,6 @@ public TestFile getTargetFile( String fileName ) return new TestFile( new File( targetDir, fileName ), this ); } - public TestFile getSurefireReportsFile( String fileName ) { File targetDir = getSubFile( "target/surefire-reports" ); @@ -186,24 +182,16 @@ public TestFile getSiteFile( String fileName ) return new TestFile( new File( targetDir, fileName ), this ); } - public File getBaseDir() { return baseDir; } - @SuppressWarnings( "unchecked" ) - private List getLog() - throws VerificationException - { - return verifier.loadFile( verifier.getBasedir(), verifier.getLogFileName(), false ); - } - public boolean stringsAppearInSpecificOrderInLog( String[] strings ) throws VerificationException { int i = 0; - for ( String line : getLog() ) + for ( String line : loadLogLines() ) { if ( line.startsWith( strings[i] ) ) { diff --git a/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire855AllowFailsafeUseArtifactFileIT.java b/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire855AllowFailsafeUseArtifactFileIT.java new file mode 100644 index 0000000000..9ee7a41c69 --- /dev/null +++ b/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire855AllowFailsafeUseArtifactFileIT.java @@ -0,0 +1,58 @@ +package org.apache.maven.surefire.its.jiras; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase; +import org.junit.Test; + +/** + * @author Tibor Digana (tibor17) + * @see {@linkplain https://jira.codehaus.org/browse/SUREFIRE-855} + * @since 2.19 + */ +public class Surefire855AllowFailsafeUseArtifactFileIT + extends SurefireJUnit4IntegrationTestCase +{ + @Test + public void warShouldUseClasses() + { + unpack( "surefire-855-failsafe-use-war" ).maven().executeVerify().verifyErrorFree( 2 ); + } + + @Test + public void jarShouldUseFile() + { + unpack( "surefire-855-failsafe-use-jar" ) + .maven().sysProp( "forkMode", "once" ).executeVerify().assertIntegrationTestSuiteResults( 3, 0, 0, 1 ); + } + + @Test + public void jarNotForkingShouldUseFile() + { + unpack( "surefire-855-failsafe-use-jar" ) + .maven().sysProp( "forkMode", "never" ).executeVerify().assertIntegrationTestSuiteResults( 3, 0, 0, 1 ); + } + + @Test + public void osgiBundleShouldUseFile() + { + unpack( "surefire-855-failsafe-use-bundle" ).maven().executeVerify().verifyErrorFree( 2 ); + } +} \ No newline at end of file diff --git a/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-bundle/pom.xml b/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-bundle/pom.xml new file mode 100644 index 0000000000..db53997a0d --- /dev/null +++ b/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-bundle/pom.xml @@ -0,0 +1,129 @@ + + + + 4.0.0 + + org.apache.maven.surefire + it-parent + 1.0 + ../pom.xml + + org.apache.maven.plugins.surefire + jiras-surefire-855-bundle + 1.0 + bundle + http://maven.apache.org + + + tibordigana + Tibor Digaňa (tibor17) + tibordigana@apache.org + + Committer + + Europe/Bratislava + + + + + junit + junit + 4.11 + test + + + org.easytesting + fest-assert-core + 2.0M9 + test + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.5 + 1.5 + + + + org.apache.maven.plugins + maven-source-plugin + 2.3 + + + attach-sources + + jar-no-fork + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.9.1 + + + attach-javadoc + + jar + + + + + + org.apache.felix + maven-bundle-plugin + 2.3.7 + true + + + org.surefire.its.${project.artifactId} + + + + + org.apache.maven.plugins + maven-failsafe-plugin + + + integration-test + + integration-test + + + + verify + + verify + + + + + always + + + + + diff --git a/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-bundle/src/main/java/pkg/AClassInOSGiBundle.java b/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-bundle/src/main/java/pkg/AClassInOSGiBundle.java new file mode 100644 index 0000000000..48141e615b --- /dev/null +++ b/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-bundle/src/main/java/pkg/AClassInOSGiBundle.java @@ -0,0 +1,24 @@ +package pkg; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +public class AClassInOSGiBundle +{ +} diff --git a/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-bundle/src/main/resources/main/surefire855.properties b/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-bundle/src/main/resources/main/surefire855.properties new file mode 100644 index 0000000000..690842d518 --- /dev/null +++ b/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-bundle/src/main/resources/main/surefire855.properties @@ -0,0 +1 @@ +issue=SUREFIRE-855 diff --git a/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-bundle/src/test/java/jiras/surefre855/bundle/FooIT.java b/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-bundle/src/test/java/jiras/surefre855/bundle/FooIT.java new file mode 100644 index 0000000000..6787b91020 --- /dev/null +++ b/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-bundle/src/test/java/jiras/surefre855/bundle/FooIT.java @@ -0,0 +1,146 @@ +package jiras.surefire855.bundle; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.junit.Test; + +import java.io.File; +import java.io.FileFilter; +import java.io.IOException; +import java.io.InputStream; +import java.io.StringReader; +import java.util.Properties; +import java.util.jar.Manifest; + +import static org.fest.assertions.api.Assertions.assertThat; +import static org.fest.assertions.api.Assertions.contentOf; +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.MatcherAssert.assertThat; + +public final class FooIT +{ + private static final String ARTIFACT_FILE_NAME = "jiras-surefire-855-bundle-1.0.jar"; + + private static final String MAIN_RESOURCE = "/main/surefire855.properties"; + + private static final String TEST_RESOURCE = "/jiras/surefire855/bundle/properties/surefire855.properties"; + + private static File[] surefireProviderProperties() + throws IOException + { + final File surefireDir = new File( "target/surefire" ).getCanonicalFile(); + return surefireDir.listFiles( new FileFilter() + { + public boolean accept( File pathname ) + { + try + { + return isSurefireProviderProperties( pathname ); + } + catch ( IOException e ) + { + return false; + } + } + } ); + } + + /** + * See BooterSerializer#serialize(). + */ + private static boolean isSurefireProviderProperties( File pathname ) + throws IOException + { + pathname = pathname.getCanonicalFile(); + String fileName = pathname.getName(); + return pathname.isFile() && fileName.startsWith( "surefire" ) && !fileName.startsWith( "surefire_" ) + && fileName.endsWith( "tmp" ); + } + + private static String manifestClassPath( Class clazz ) + throws IOException + { + Manifest manifest = new Manifest( clazz.getResourceAsStream( "/META-INF/MANIFEST.MF" ) ); + return manifest.getMainAttributes().getValue( "Class-Path" ); + } + + private static Properties loadProperties( Class clazz, String resourcePath ) + throws IOException + { + InputStream is = clazz.getResourceAsStream( resourcePath ); + Properties prop = new Properties(); + prop.load( is ); + is.close(); + return prop; + } + + private static Properties loadMainProperties( Class clazz ) + throws IOException + { + return loadProperties( clazz, MAIN_RESOURCE ); + } + + private static Properties loadTestProperties( Class clazz ) + throws IOException + { + return loadProperties( clazz, TEST_RESOURCE ); + } + + @Test + public void test() + throws IOException + { + String classPath = manifestClassPath( getClass() ); + System.out.println( "CLASS PATH:" ); + System.out.println( classPath ); + + assertThat( classPath, not( containsString( "/target/classes" ) ) ); + assertThat( classPath, containsString( "/target/" + ARTIFACT_FILE_NAME ) ); + + File surefireDir = new File( "target/surefire" ).getCanonicalFile(); + System.out.println( "SUREFIRE DIR:" ); + System.out.println( surefireDir ); + + File[] descriptors = surefireProviderProperties(); + assertThat( descriptors ).hasSize( 1 ); + assertThat( descriptors ).doesNotContainNull(); + assertThat( descriptors[0] ).isFile(); + + String surefireProperties = contentOf( descriptors[0] ); + Properties properties = new Properties(); + properties.load( new StringReader( surefireProperties ) ); + System.out.println( properties.toString() ); + File actualArtifact = new File( properties.getProperty( "classPathUrl.1" ) ).getCanonicalFile(); + File expectedArtifact = new File( "target/" + ARTIFACT_FILE_NAME ).getCanonicalFile(); + assertThat( actualArtifact ).isFile(); + assertThat( expectedArtifact ).isFile(); + assertThat( actualArtifact ).isEqualTo( expectedArtifact ); + } + + @Test + public void shouldAlwaysHaveResources() + throws IOException + { + assertThat( loadTestProperties( getClass() ).getProperty( "issue" ), is( "SUREFIRE-855" ) ); + assertThat( loadMainProperties( getClass() ).getProperty( "issue" ), is( "SUREFIRE-855" ) ); + } +} diff --git a/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-bundle/src/test/resources/jiras/surefire855/bundle/properties/surefire855.properties b/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-bundle/src/test/resources/jiras/surefire855/bundle/properties/surefire855.properties new file mode 100644 index 0000000000..690842d518 --- /dev/null +++ b/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-bundle/src/test/resources/jiras/surefire855/bundle/properties/surefire855.properties @@ -0,0 +1 @@ +issue=SUREFIRE-855 diff --git a/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-jar/pom.xml b/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-jar/pom.xml new file mode 100644 index 0000000000..1635a678d0 --- /dev/null +++ b/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-jar/pom.xml @@ -0,0 +1,123 @@ + + + + 4.0.0 + + org.apache.maven.surefire + it-parent + 1.0 + ../pom.xml + + org.apache.maven.plugins.surefire + jiras-surefire-855-jar + 1.0 + http://maven.apache.org + + + tibordigana + Tibor Digaňa (tibor17) + tibordigana@apache.org + + Committer + + Europe/Bratislava + + + + + junit + junit + 4.11 + test + + + org.easytesting + fest-assert-core + 2.0M9 + test + + + org.hamcrest + hamcrest-library + 1.3 + test + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.5 + 1.5 + + + + org.apache.maven.plugins + maven-source-plugin + 2.3 + + + attach-sources + + jar-no-fork + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.9.1 + + + attach-javadoc + + jar + + + + + + org.apache.maven.plugins + maven-failsafe-plugin + + + integration-test + + integration-test + + + + verify + + verify + + + + + ${forkMode} + + + + + diff --git a/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-jar/src/main/java/pkg/ToRunJavadoc.java b/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-jar/src/main/java/pkg/ToRunJavadoc.java new file mode 100644 index 0000000000..5998fa6b1b --- /dev/null +++ b/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-jar/src/main/java/pkg/ToRunJavadoc.java @@ -0,0 +1,8 @@ +package pkg; + +public class ToRunJavadoc +{ + public void x() + { + } +} diff --git a/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-jar/src/main/resources/main/surefire855.properties b/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-jar/src/main/resources/main/surefire855.properties new file mode 100644 index 0000000000..690842d518 --- /dev/null +++ b/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-jar/src/main/resources/main/surefire855.properties @@ -0,0 +1 @@ +issue=SUREFIRE-855 diff --git a/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-jar/src/test/java/jiras/surefire855/jar/FooIT.java b/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-jar/src/test/java/jiras/surefire855/jar/FooIT.java new file mode 100644 index 0000000000..cb1526d706 --- /dev/null +++ b/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-jar/src/test/java/jiras/surefire855/jar/FooIT.java @@ -0,0 +1,162 @@ +package jiras.surefire855.jar; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.junit.Test; + +import java.io.File; +import java.io.FileFilter; +import java.io.IOException; +import java.io.InputStream; +import java.io.StringReader; +import java.util.Properties; +import java.util.jar.Manifest; + +import static org.fest.assertions.api.Assertions.assertThat; +import static org.fest.assertions.api.Assertions.contentOf; +import static org.hamcrest.CoreMatchers.*; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assume.assumeThat; + +public final class FooIT +{ + private static final String ARTIFACT_FILE_NAME = "jiras-surefire-855-jar-1.0.jar"; + + private static final String MAIN_RESOURCE = "/main/surefire855.properties"; + + private static final String TEST_RESOURCE = "/jiras/surefire855/jar/properties/surefire855.properties"; + + private static File surefireDir() + throws IOException + { + return new File( "target/surefire" ).getCanonicalFile(); + } + + private static File[] surefireProviderProperties() + throws IOException + { + return surefireDir().listFiles( new FileFilter() + { + public boolean accept( File pathname ) + { + try + { + return isSurefireProviderProperties( pathname ); + } + catch ( IOException e ) + { + return false; + } + } + } ); + } + + /** + * See BooterSerializer#serialize(). + */ + private static boolean isSurefireProviderProperties( File pathname ) + throws IOException + { + pathname = pathname.getCanonicalFile(); + String fileName = pathname.getName(); + return pathname.isFile() && fileName.startsWith( "surefire" ) && !fileName.startsWith( "surefire_" ) + && fileName.endsWith( "tmp" ); + } + + private static String manifestClassPath( Class clazz ) + throws IOException + { + Manifest manifest = new Manifest( clazz.getResourceAsStream( "/META-INF/MANIFEST.MF" ) ); + return manifest.getMainAttributes().getValue( "Class-Path" ); + } + + private static Properties loadProperties( Class clazz, String resourcePath ) + throws IOException + { + InputStream is = clazz.getResourceAsStream( resourcePath ); + Properties prop = new Properties(); + prop.load( is ); + is.close(); + return prop; + } + + private static Properties loadMainProperties( Class clazz ) + throws IOException + { + return loadProperties( clazz, MAIN_RESOURCE ); + } + + private static Properties loadTestProperties( Class clazz ) + throws IOException + { + return loadProperties( clazz, TEST_RESOURCE ); + } + + @Test + public void shouldBeJarWithForking() + throws IOException + { + assumeThat( System.getProperty( "forkMode" ), is( not( "never" ) ) ); + + String classPath = manifestClassPath( getClass() ); + System.out.println( "CLASS PATH:" ); + System.out.println( classPath ); + + assertThat( classPath, not( containsString( "/target/classes" ) ) ); + assertThat( classPath, containsString( "/target/" + ARTIFACT_FILE_NAME ) ); + + File surefireDir = new File( "target/surefire" ).getCanonicalFile(); + System.out.println( "SUREFIRE DIR:" ); + System.out.println( surefireDir ); + + File[] descriptors = surefireProviderProperties(); + assertThat( descriptors ).hasSize( 1 ); + assertThat( descriptors ).doesNotContainNull(); + assertThat( descriptors[0] ).isFile(); + + String surefireProperties = contentOf( descriptors[0] ); + Properties properties = new Properties(); + properties.load( new StringReader( surefireProperties ) ); + System.out.println( properties.toString() ); + File actualArtifact = new File( properties.getProperty( "classPathUrl.1" ) ).getCanonicalFile(); + File expectedArtifact = new File( "target/" + ARTIFACT_FILE_NAME ).getCanonicalFile(); + assertThat( actualArtifact ).isFile(); + assertThat( expectedArtifact ).isFile(); + assertThat( actualArtifact ).isEqualTo( expectedArtifact ); + } + + @Test + public void jarShouldExistWhenNotForking() + throws Exception + { + assumeThat( System.getProperty( "forkMode" ), is( "never" ) ); + + assertThat( surefireDir() ).doesNotExist(); + assertThat( new File( "target/" + ARTIFACT_FILE_NAME ).getCanonicalFile() ).isFile(); + } + + @Test + public void shouldAlwaysHaveResources() + throws IOException + { + assertThat( loadTestProperties( getClass() ).getProperty( "issue" ), is( "SUREFIRE-855" ) ); + assertThat( loadMainProperties( getClass() ).getProperty( "issue" ), is( "SUREFIRE-855" ) ); + } +} diff --git a/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-jar/src/test/resources/jiras/surefire855/jar/properties/surefire855.properties b/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-jar/src/test/resources/jiras/surefire855/jar/properties/surefire855.properties new file mode 100644 index 0000000000..690842d518 --- /dev/null +++ b/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-jar/src/test/resources/jiras/surefire855/jar/properties/surefire855.properties @@ -0,0 +1 @@ +issue=SUREFIRE-855 diff --git a/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-war/pom.xml b/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-war/pom.xml new file mode 100644 index 0000000000..4b0fb51337 --- /dev/null +++ b/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-war/pom.xml @@ -0,0 +1,126 @@ + + + + 4.0.0 + + org.apache.maven.surefire + it-parent + 1.0 + ../pom.xml + + org.apache.maven.plugins.surefire + jiras-surefire-855-war + 1.0 + war + http://maven.apache.org + + + tibordigana + Tibor Digaňa (tibor17) + tibordigana@apache.org + + Committer + + Europe/Bratislava + + + + + junit + junit + 4.11 + test + + + org.easytesting + fest-assert-core + 2.0M9 + test + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.5 + 1.5 + + + + org.apache.maven.plugins + maven-source-plugin + 2.3 + + + attach-sources + + jar-no-fork + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.9.1 + + + attach-javadoc + + jar + + + + + + org.apache.maven.plugins + maven-war-plugin + 2.4 + + false + + + + org.apache.maven.plugins + maven-failsafe-plugin + + + integration-test + + integration-test + + + + verify + + verify + + + + + always + + + + + diff --git a/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-war/src/main/java/pkg/ToCreateClassesDirectory.java b/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-war/src/main/java/pkg/ToCreateClassesDirectory.java new file mode 100644 index 0000000000..ffe6be993c --- /dev/null +++ b/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-war/src/main/java/pkg/ToCreateClassesDirectory.java @@ -0,0 +1,24 @@ +package pkg; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +public class ToCreateClassesDirectory +{ +} diff --git a/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-war/src/main/resources/main/surefire855.properties b/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-war/src/main/resources/main/surefire855.properties new file mode 100644 index 0000000000..690842d518 --- /dev/null +++ b/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-war/src/main/resources/main/surefire855.properties @@ -0,0 +1 @@ +issue=SUREFIRE-855 diff --git a/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-war/src/test/java/jiras/surefire855/war/FooIT.java b/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-war/src/test/java/jiras/surefire855/war/FooIT.java new file mode 100644 index 0000000000..7905079b71 --- /dev/null +++ b/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-war/src/test/java/jiras/surefire855/war/FooIT.java @@ -0,0 +1,142 @@ +package jiras.surefire855.war; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.junit.Test; + +import java.io.File; +import java.io.FileFilter; +import java.io.IOException; +import java.io.InputStream; +import java.io.StringReader; +import java.util.Properties; +import java.util.jar.Manifest; + +import static org.fest.assertions.api.Assertions.assertThat; +import static org.fest.assertions.api.Assertions.contentOf; +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public final class FooIT +{ + private static final String MAIN_RESOURCE = "/main/surefire855.properties"; + + private static final String TEST_RESOURCE = "/jiras/surefire855/war/properties/surefire855.properties"; + + private static File[] surefireProviderProperties() + throws IOException + { + final File surefireDir = new File( "target/surefire" ).getCanonicalFile(); + return surefireDir.listFiles( new FileFilter() + { + public boolean accept( File pathname ) + { + try + { + return isSurefireProviderProperties( pathname ); + } + catch ( IOException e ) + { + return false; + } + } + } ); + } + + /** + * See BooterSerializer#serialize(). + */ + private static boolean isSurefireProviderProperties( File pathname ) + throws IOException + { + pathname = pathname.getCanonicalFile(); + String fileName = pathname.getName(); + return pathname.isFile() && fileName.startsWith( "surefire" ) && !fileName.startsWith( "surefire_" ) + && fileName.endsWith( "tmp" ); + } + + private static String manifestClassPath( Class clazz ) + throws IOException + { + Manifest manifest = new Manifest( clazz.getResourceAsStream( "/META-INF/MANIFEST.MF" ) ); + return manifest.getMainAttributes().getValue( "Class-Path" ); + } + + private static Properties loadProperties( Class clazz, String resourcePath ) + throws IOException + { + InputStream is = clazz.getResourceAsStream( resourcePath ); + Properties prop = new Properties(); + prop.load( is ); + is.close(); + return prop; + } + + private static Properties loadMainProperties( Class clazz ) + throws IOException + { + return loadProperties( clazz, MAIN_RESOURCE ); + } + + private static Properties loadTestProperties( Class clazz ) + throws IOException + { + return loadProperties( clazz, TEST_RESOURCE ); + } + + @Test + public void test() + throws IOException + { + String classPath = manifestClassPath( getClass() ); + System.out.println( "CLASS PATH:" ); + System.out.println( classPath ); + + assertThat( classPath, containsString( "/target/classes" ) ); + + File surefireDir = new File( "target/surefire" ).getCanonicalFile(); + System.out.println( "SUREFIRE DIR:" ); + System.out.println( surefireDir ); + + File[] descriptors = surefireProviderProperties(); + assertThat( descriptors ).hasSize( 1 ); + assertThat( descriptors ).doesNotContainNull(); + assertThat( descriptors[0] ).isFile(); + + String surefireProperties = contentOf( descriptors[0] ); + Properties properties = new Properties(); + properties.load( new StringReader( surefireProperties ) ); + System.out.println( properties.toString() ); + File actualArtifact = new File( properties.getProperty( "classPathUrl.1" ) ).getCanonicalFile(); + File expectedArtifact = new File( "target/classes" ).getCanonicalFile(); + assertThat( actualArtifact ).isDirectory(); + assertThat( expectedArtifact ).isDirectory(); + assertThat( actualArtifact ).isEqualTo( expectedArtifact ); + } + + @Test + public void shouldAlwaysHaveResources() + throws IOException + { + assertThat( loadTestProperties( getClass() ).getProperty( "issue" ), is( "SUREFIRE-855" ) ); + assertThat( loadMainProperties( getClass() ).getProperty( "issue" ), is( "SUREFIRE-855" ) ); + } +} diff --git a/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-war/src/test/resources/jiras/surefire855/war/properties/surefire855.properties b/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-war/src/test/resources/jiras/surefire855/war/properties/surefire855.properties new file mode 100644 index 0000000000..690842d518 --- /dev/null +++ b/surefire-integration-tests/src/test/resources/surefire-855-failsafe-use-war/src/test/resources/jiras/surefire855/war/properties/surefire855.properties @@ -0,0 +1 @@ +issue=SUREFIRE-855