From 96d4eef46826f8db860401c84012c6d8009e3963 Mon Sep 17 00:00:00 2001 From: tibordigana Date: Mon, 14 Mar 2022 21:58:40 +0100 Subject: [PATCH 1/2] Revert "[SUREFIRE-1964] Support for method filtering on excludesFile and includesFile" This reverts commit 342ff2b10ad7b9ff29748c7b44a042652fae1eb6. --- .../plugin/surefire/AbstractSurefireMojo.java | 120 ++++------- .../surefire/AbstractSurefireMojoTest.java | 198 +----------------- .../plugin/surefire/MojoMocklessTest.java | 6 - .../maven/plugin/surefire/SurefirePlugin.java | 12 -- .../AbstractTestMultipleMethodPatterns.java | 3 +- .../surefire/its/jiras/Surefire1964IT.java | 55 ----- .../resources/surefire-1964/exclusions.txt | 1 - .../resources/surefire-1964/inclusions.txt | 1 - .../src/test/resources/surefire-1964/pom.xml | 58 ----- .../src/test/java/pkg/ExcludedTest.java | 12 -- .../src/test/java/pkg/FilterTest.java | 24 --- 11 files changed, 58 insertions(+), 432 deletions(-) delete mode 100644 surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1964IT.java delete mode 100644 surefire-its/src/test/resources/surefire-1964/exclusions.txt delete mode 100644 surefire-its/src/test/resources/surefire-1964/inclusions.txt delete mode 100644 surefire-its/src/test/resources/surefire-1964/pom.xml delete mode 100644 surefire-its/src/test/resources/surefire-1964/src/test/java/pkg/ExcludedTest.java delete mode 100644 surefire-its/src/test/resources/surefire-1964/src/test/java/pkg/FilterTest.java 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 40ed1996cc..c9f44b53c3 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 @@ -2194,110 +2194,78 @@ private boolean isSpecificTestSpecified() } } - @Nonnull - private List getExcludedScanList() - throws MojoFailureException + private void maybeAppendList( List base, List list ) { - return getExcludeList( true ); - } - - @Nonnull - private List getExcludeList() - throws MojoFailureException - { - return getExcludeList( false ); + if ( list != null ) + { + base.addAll( list ); + } } - /** - * Computes a merge list of test exclusions. - * Used only in {@link #getExcludeList()} and {@link #getExcludedScanList()}. - * @param asScanList true if dependency or directory scanner - * @return list of patterns - * @throws MojoFailureException if the excludes breaks a pattern format - */ - @Nonnull - private List getExcludeList( boolean asScanList ) + @Nonnull private List getExcludeList() throws MojoFailureException { - List excludes; + List actualExcludes = null; if ( isSpecificTestSpecified() ) { - excludes = Collections.emptyList(); + actualExcludes = Collections.emptyList(); } else { - excludes = new ArrayList<>(); - if ( asScanList ) + if ( getExcludesFile() != null ) { - if ( getExcludes() != null ) - { - excludes.addAll( getExcludes() ); - } - checkMethodFilterInIncludesExcludes( excludes ); + actualExcludes = readListFromFile( getExcludesFile() ); } - if ( getExcludesFile() != null ) + if ( actualExcludes == null ) { - excludes.addAll( readListFromFile( getExcludesFile() ) ); + actualExcludes = getExcludes(); } + else + { + maybeAppendList( actualExcludes, getExcludes() ); + } + + checkMethodFilterInIncludesExcludes( actualExcludes ); - if ( asScanList && excludes.isEmpty() ) + if ( actualExcludes == null || actualExcludes.isEmpty() ) { - excludes = Collections.singletonList( getDefaultExcludes() ); + actualExcludes = Collections.singletonList( getDefaultExcludes() ); } } - return filterNulls( excludes ); - } - - @Nonnull - private List getIncludedScanList() - throws MojoFailureException - { - return getIncludeList( true ); + return filterNulls( actualExcludes ); } - @Nonnull private List getIncludeList() throws MojoFailureException { - return getIncludeList( false ); - } - - /** - * Computes a merge list of test inclusions. - * Used only in {@link #getIncludeList()} and {@link #getIncludedScanList()}. - * @param asScanList true if dependency or directory scanner - * @return list of patterns - * @throws MojoFailureException if the includes breaks a pattern format - */ - @Nonnull - private List getIncludeList( boolean asScanList ) - throws MojoFailureException - { - final List includes = new ArrayList<>(); + List includes = null; if ( isSpecificTestSpecified() ) { + includes = new ArrayList<>(); addAll( includes, split( getTest(), "," ) ); } else { - if ( asScanList ) + if ( getIncludesFile() != null ) { - if ( getIncludes() != null ) - { - includes.addAll( getIncludes() ); - } - checkMethodFilterInIncludesExcludes( includes ); + includes = readListFromFile( getIncludesFile() ); } - if ( getIncludesFile() != null ) + if ( includes == null ) { - includes.addAll( readListFromFile( getIncludesFile() ) ); + includes = getIncludes(); } + else + { + maybeAppendList( includes, getIncludes() ); + } + + checkMethodFilterInIncludesExcludes( includes ); - if ( asScanList && includes.isEmpty() ) + if ( includes == null || includes.isEmpty() ) { - addAll( includes, getDefaultIncludes() ); + includes = asList( getDefaultIncludes() ); } } @@ -2307,12 +2275,16 @@ private List getIncludeList( boolean asScanList ) private void checkMethodFilterInIncludesExcludes( Iterable patterns ) throws MojoFailureException { - for ( String pattern : patterns ) + if ( patterns != null ) { - if ( pattern != null && pattern.contains( "#" ) ) + for ( String pattern : patterns ) { - throw new MojoFailureException( "Method filter prohibited in includes|excludes parameter: " - + pattern ); + if ( pattern != null && pattern.contains( "#" ) ) + { + throw new MojoFailureException( "Method filter prohibited in " + + "includes|excludes|includesFile|excludesFile parameter: " + + pattern ); + } } } } @@ -2322,18 +2294,16 @@ private TestListResolver getIncludedAndExcludedTests() { if ( includedExcludedTests == null ) { - includedExcludedTests = new TestListResolver( getIncludedScanList(), getExcludedScanList() ); - getConsoleLogger().debug( "Resolved included and excluded patterns: " + includedExcludedTests ); + includedExcludedTests = new TestListResolver( getIncludeList(), getExcludeList() ); } return includedExcludedTests; } public TestListResolver getSpecificTests() - throws MojoFailureException { if ( specificTests == null ) { - specificTests = new TestListResolver( getIncludeList(), getExcludeList() ); + specificTests = new TestListResolver( getTest() ); } return specificTests; } diff --git a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/AbstractSurefireMojoTest.java b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/AbstractSurefireMojoTest.java index 9d89447aff..7d7d15f6d1 100644 --- a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/AbstractSurefireMojoTest.java +++ b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/AbstractSurefireMojoTest.java @@ -30,17 +30,14 @@ import java.util.HashSet; import java.util.LinkedHashMap; import java.util.LinkedHashSet; -import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; -import org.apache.commons.io.FileUtils; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.DefaultArtifact; import org.apache.maven.artifact.handler.ArtifactHandler; -import org.apache.maven.artifact.handler.DefaultArtifactHandler; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.resolver.ArtifactResolutionRequest; import org.apache.maven.artifact.resolver.ArtifactResolutionResult; @@ -87,7 +84,6 @@ import org.powermock.modules.junit4.PowerMockRunner; import static java.io.File.separatorChar; -import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.file.Files.write; import static java.util.Arrays.asList; import static java.util.Collections.emptyList; @@ -99,8 +95,8 @@ import static org.apache.maven.surefire.shared.lang3.JavaVersion.JAVA_9; import static org.apache.maven.surefire.shared.lang3.JavaVersion.JAVA_RECENT; import static org.apache.maven.surefire.shared.lang3.SystemUtils.IS_OS_WINDOWS; -import static org.assertj.core.api.Assertions.assertThat; import static org.codehaus.plexus.languages.java.jpms.ModuleNameSource.MODULEDESCRIPTOR; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; @@ -1994,12 +1990,6 @@ public static class Mojo private int failOnFlakeCount; private String[] includeJUnit5Engines; private String[] excludeJUnit5Engines; - private List projectTestArtifacts; - private File includesFile; - private File excludesFile; - private List includes; - private List excludes; - private String test; private JUnitPlatformProviderInfo createJUnitPlatformProviderInfo( Artifact junitPlatformArtifact, TestClassPath testClasspathWrapper ) @@ -2007,17 +1997,6 @@ private JUnitPlatformProviderInfo createJUnitPlatformProviderInfo( Artifact juni return new JUnitPlatformProviderInfo( null, junitPlatformArtifact, testClasspathWrapper ); } - void setProjectTestArtifacts( List projectTestArtifacts ) - { - this.projectTestArtifacts = projectTestArtifacts; - } - - @Override - List getProjectTestArtifacts() - { - return projectTestArtifacts; - } - @Override protected void logDebugOrCliShowErrors( String s ) { @@ -2135,48 +2114,43 @@ public void setReportsDirectory( File reportsDirectory ) @Override public String getTest() { - return test; + return null; } @Override public void setTest( String test ) { - this.test = test; + } @Override public List getIncludes() { - return includes; + return null; } @Override - public void setIncludes( List includes ) - { - this.includes = includes; - } - - void setIncludesFile( File includesFile ) + public File getIncludesFile() { - this.includesFile = includesFile; + return null; } @Override - public File getIncludesFile() + public void setIncludes( List includes ) { - return includesFile; + } @Override public List getExcludes() { - return excludes; + return null; } @Override public void setExcludes( List excludes ) { - this.excludes = excludes; + } @Override @@ -2335,15 +2309,10 @@ public String getShutdown() return null; } - void setExcludesFile( File excludesFile ) - { - this.excludesFile = excludesFile; - } - @Override public File getExcludesFile() { - return excludesFile; + return null; } @Override @@ -2514,151 +2483,6 @@ public void setExcludeJUnit5Engines( String[] excludeJUnit5Engines ) } } - @Test - public void shouldNotPerformMethodFilteringOnIncludes() throws Exception - { - Mojo plugin = new Mojo(); - - File includesExcludes = File.createTempFile( "surefire", "-includes" ); - FileUtils.write( includesExcludes, "AnotherTest#method" , UTF_8 ); - plugin.setIncludesFile( includesExcludes ); - - List includes = new LinkedList<>(); - includes.add( "AnotherTest#method " ); - plugin.setIncludes( includes ); - - VersionRange version = VersionRange.createFromVersion( "1.0" ); - ArtifactHandler handler = new DefaultArtifactHandler(); - Artifact testDeps = new DefaultArtifact( "g", "a", version, "compile", "jar", null, handler ); - File artifactFile = File.createTempFile( "surefire", ".jar" ); - artifactFile.deleteOnExit(); - testDeps.setFile( artifactFile ); - plugin.setProjectTestArtifacts( singletonList( testDeps ) ); - plugin.setDependenciesToScan( new String[] { "g:a" } ); - - e.expectMessage( "Method filter prohibited in includes|excludes parameter: AnotherTest#method " ); - plugin.scanDependencies(); - } - - @Test - public void shouldFilterTestsOnIncludesFile() throws Exception - { - Mojo plugin = new Mojo(); - - plugin.setLogger( mock( Logger.class ) ); - - File includes = File.createTempFile( "surefire", "-includes" ); - FileUtils.write( includes, "AnotherTest#method" , UTF_8 ); - plugin.setIncludesFile( includes ); - - VersionRange version = VersionRange.createFromVersion( "1.0" ); - ArtifactHandler handler = new DefaultArtifactHandler(); - Artifact testDeps = new DefaultArtifact( "g", "a", version, "compile", "test-jar", null, handler ); - File artifactFile = File.createTempFile( "surefire", "-classes" ); - String classDir = artifactFile.getCanonicalPath(); - assertThat( artifactFile.delete() ).isTrue(); - File classes = new File( classDir ); - assertThat( classes.mkdir() ).isTrue(); - testDeps.setFile( classes ); - assertThat( new File( classes, "AnotherTest.class" ).createNewFile() ) - .isTrue(); - plugin.setProjectTestArtifacts( singletonList( testDeps ) ); - plugin.setDependenciesToScan( new String[] { "g:a" } ); - - DefaultScanResult result = plugin.scanDependencies(); - assertThat ( result.getClasses() ).hasSize( 1 ); - assertThat ( result.getClasses().iterator().next() ).isEqualTo( "AnotherTest" ); - } - - @Test - public void shouldFilterTestsOnExcludesFile() throws Exception - { - Mojo plugin = new Mojo(); - - plugin.setLogger( mock( Logger.class ) ); - - File excludes = File.createTempFile( "surefire", "-excludes" ); - FileUtils.write( excludes, "AnotherTest" , UTF_8 ); - plugin.setExcludesFile( excludes ); - - VersionRange version = VersionRange.createFromVersion( "1.0" ); - ArtifactHandler handler = new DefaultArtifactHandler(); - Artifact testDeps = new DefaultArtifact( "g", "a", version, "compile", "test-jar", null, handler ); - File artifactFile = File.createTempFile( "surefire", "-classes" ); - String classDir = artifactFile.getCanonicalPath(); - assertThat( artifactFile.delete() ).isTrue(); - File classes = new File( classDir ); - assertThat( classes.mkdir() ).isTrue(); - testDeps.setFile( classes ); - assertThat( new File( classes, "AnotherTest.class" ).createNewFile() ) - .isTrue(); - plugin.setProjectTestArtifacts( singletonList( testDeps ) ); - plugin.setDependenciesToScan( new String[] { "g:a" } ); - - DefaultScanResult result = plugin.scanDependencies(); - assertThat ( result.getClasses() ) - .isEmpty(); - } - - @Test - public void shouldFilterTestsOnExcludes() throws Exception - { - Mojo plugin = new Mojo(); - - plugin.setLogger( mock( Logger.class ) ); - - plugin.setExcludes( singletonList( "AnotherTest" ) ); - - VersionRange version = VersionRange.createFromVersion( "1.0" ); - ArtifactHandler handler = new DefaultArtifactHandler(); - Artifact testDeps = new DefaultArtifact( "g", "a", version, "compile", "jar", null, handler ); - File artifactFile = File.createTempFile( "surefire", "-classes" ); - String classDir = artifactFile.getCanonicalPath(); - assertThat( artifactFile.delete() ).isTrue(); - File classes = new File( classDir ); - assertThat( classes.mkdir() ).isTrue(); - testDeps.setFile( classes ); - assertThat( new File( classes, "AnotherTest.class" ).createNewFile() ) - .isTrue(); - plugin.setProjectTestArtifacts( singletonList( testDeps ) ); - plugin.setDependenciesToScan( new String[] { "g:a" } ); - - DefaultScanResult result = plugin.scanDependencies(); - assertThat ( result.getClasses() ) - .isEmpty(); - } - - @Test - public void shouldUseOnlySpecificTests() throws Exception - { - Mojo plugin = new Mojo(); - - plugin.setLogger( mock( Logger.class ) ); - - File includes = File.createTempFile( "surefire", "-includes" ); - FileUtils.write( includes, "AnotherTest" , UTF_8 ); - plugin.setIncludesFile( includes ); - plugin.setTest( "DifferentTest" ); - - VersionRange version = VersionRange.createFromVersion( "1.0" ); - ArtifactHandler handler = new DefaultArtifactHandler(); - Artifact testDeps = new DefaultArtifact( "g", "a", version, "compile", "test-jar", null, handler ); - File artifactFile = File.createTempFile( "surefire", "-classes" ); - String classDir = artifactFile.getCanonicalPath(); - assertThat( artifactFile.delete() ).isTrue(); - File classes = new File( classDir ); - assertThat( classes.mkdir() ).isTrue(); - testDeps.setFile( classes ); - assertThat( new File( classes, "AnotherTest.class" ).createNewFile() ) - .isTrue(); - plugin.setProjectTestArtifacts( singletonList( testDeps ) ); - plugin.setDependenciesToScan( new String[] { "g:a" } ); - - DefaultScanResult result = plugin.scanDependencies(); - assertThat ( result.getClasses() ) - .isEmpty(); - } - private static File mockFile( String absolutePath ) { File f = mock( File.class ); diff --git a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/MojoMocklessTest.java b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/MojoMocklessTest.java index 6ad04160cc..1548dfdbe9 100644 --- a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/MojoMocklessTest.java +++ b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/MojoMocklessTest.java @@ -32,7 +32,6 @@ import org.apache.maven.surefire.api.suite.RunResult; import org.apache.maven.surefire.api.util.DefaultScanResult; import org.apache.maven.toolchain.Toolchain; -import org.codehaus.plexus.logging.Logger; import org.junit.Test; import java.io.File; @@ -45,7 +44,6 @@ import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; -import static org.mockito.Mockito.mock; import static org.powermock.reflect.Whitebox.invokeMethod; import static org.powermock.reflect.Whitebox.setInternalState; @@ -245,7 +243,6 @@ public void scanDependenciesShouldReturnClassWithExistingTestJAR() List projectTestArtifacts = singletonList( testDeps ); String[] dependenciesToScan = { "g:a" }; Mojo mojo = new Mojo( projectTestArtifacts, dependenciesToScan ); - mojo.setLogger( mock( Logger.class ) ); DefaultScanResult result = mojo.scanDependencies(); assertThat( result ) @@ -278,7 +275,6 @@ public void scanDependenciesShouldReturnNullWithEmptyTestJAR() List projectTestArtifacts = singletonList( testDeps ); String[] dependenciesToScan = { "g:a" }; Mojo mojo = new Mojo( projectTestArtifacts, dependenciesToScan ); - mojo.setLogger( mock( Logger.class ) ); DefaultScanResult result = mojo.scanDependencies(); assertThat( result ) @@ -310,7 +306,6 @@ public void scanDependenciesShouldReturnClassWithDirectory() List projectTestArtifacts = singletonList( testDeps ); String[] dependenciesToScan = { "g:a" }; Mojo mojo = new Mojo( projectTestArtifacts, dependenciesToScan ); - mojo.setLogger( mock( Logger.class ) ); DefaultScanResult result = mojo.scanDependencies(); assertThat( result ) @@ -357,7 +352,6 @@ public void scanMultipleDependencies() List projectTestArtifacts = asList( testDep1, testDep2 ); String[] dependenciesToScan = { "g:a" }; Mojo mojo = new Mojo( projectTestArtifacts, dependenciesToScan ); - mojo.setLogger( mock( Logger.class ) ); DefaultScanResult result = mojo.scanDependencies(); assertThat( result ) 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 2ce8caaacb..240d2a5423 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 @@ -355,12 +355,6 @@ public class SurefirePlugin * **{@literal /}NotIncludedByDefault.java * %regex[.*Test.*|.*Not.*] * - * - * Since 3.0.0-M6, method filtering support is provided in includes file. Example: - *

-     * foo.bar.SomeTest#test
-     * com.test.Test#testMethod
-     * 
* * @since 2.13 */ @@ -376,12 +370,6 @@ public class SurefirePlugin * %regex[.*Test.*|.*Not.*] * * - * Since 3.0.0-M6, method filtering support is provided in excludes file. Example: - *

-     * foo.bar.SomeTest#test
-     * com.test.Test#testMethod
-     * 
- * * @since 2.13 */ @Parameter( property = "surefire.excludesFile" ) diff --git a/surefire-its/src/test/java/org/apache/maven/surefire/its/AbstractTestMultipleMethodPatterns.java b/surefire-its/src/test/java/org/apache/maven/surefire/its/AbstractTestMultipleMethodPatterns.java index 055c407110..6c458f501b 100644 --- a/surefire-its/src/test/java/org/apache/maven/surefire/its/AbstractTestMultipleMethodPatterns.java +++ b/surefire-its/src/test/java/org/apache/maven/surefire/its/AbstractTestMultipleMethodPatterns.java @@ -483,6 +483,7 @@ public void negativeTest() prepare( pattern ) .failNever() .executeTest() - .verifyTextInLog( "Method filter prohibited in includes|excludes parameter: " + pattern ); + .verifyTextInLog( "Method filter prohibited in includes|excludes|includesFile|excludesFile parameter: " + + pattern ); } } diff --git a/surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1964IT.java b/surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1964IT.java deleted file mode 100644 index 62f60d25ca..0000000000 --- a/surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1964IT.java +++ /dev/null @@ -1,55 +0,0 @@ -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; - -import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.is; - -/** - * - */ -public class Surefire1964IT extends SurefireJUnit4IntegrationTestCase -{ - @Test - public void shouldFilterTests() throws Exception - { - unpack( "surefire-1964" ) - .executeTest() - .assertTestSuiteResults( 1 ) - .assertThatLogLine( containsString( "executed testXYZ" ), is( 1 ) ) - .assertThatLogLine( containsString( "executed testABC" ), is( 0 ) ) - .assertThatLogLine( containsString( "executed dontRun" ), is( 0 ) ); - } - - @Test - public void shouldFilterTestsInPluginProcess() throws Exception - { - unpack( "surefire-1964" ) - .forkCount( 0 ) - .executeTest() - .assertTestSuiteResults( 1 ) - .assertThatLogLine( containsString( "executed testXYZ" ), is( 1 ) ) - .assertThatLogLine( containsString( "executed testABC" ), is( 0 ) ) - .assertThatLogLine( containsString( "executed dontRun" ), is( 0 ) ); - } -} diff --git a/surefire-its/src/test/resources/surefire-1964/exclusions.txt b/surefire-its/src/test/resources/surefire-1964/exclusions.txt deleted file mode 100644 index 5bacad2cab..0000000000 --- a/surefire-its/src/test/resources/surefire-1964/exclusions.txt +++ /dev/null @@ -1 +0,0 @@ -FilterTest#testABC \ No newline at end of file diff --git a/surefire-its/src/test/resources/surefire-1964/inclusions.txt b/surefire-its/src/test/resources/surefire-1964/inclusions.txt deleted file mode 100644 index 50077e4c4d..0000000000 --- a/surefire-its/src/test/resources/surefire-1964/inclusions.txt +++ /dev/null @@ -1 +0,0 @@ -FilterTest#test* \ No newline at end of file diff --git a/surefire-its/src/test/resources/surefire-1964/pom.xml b/surefire-its/src/test/resources/surefire-1964/pom.xml deleted file mode 100644 index 8f3ba29389..0000000000 --- a/surefire-its/src/test/resources/surefire-1964/pom.xml +++ /dev/null @@ -1,58 +0,0 @@ - - - - - 4.0.0 - - org.example - maven-surefire-excludefiles - 1.0-SNAPSHOT - - - ${java.specification.version} - ${java.specification.version} - - - - - junit - junit - 4.13.2 - test - - - - - - - org.apache.maven.plugins - maven-surefire-plugin - ${surefire.version} - - inclusions.txt - exclusions.txt - - - - - - diff --git a/surefire-its/src/test/resources/surefire-1964/src/test/java/pkg/ExcludedTest.java b/surefire-its/src/test/resources/surefire-1964/src/test/java/pkg/ExcludedTest.java deleted file mode 100644 index b871231ef2..0000000000 --- a/surefire-its/src/test/resources/surefire-1964/src/test/java/pkg/ExcludedTest.java +++ /dev/null @@ -1,12 +0,0 @@ -package pkg; - -import org.junit.Test; - -public class ExcludedTest -{ - @Test - public void dontRun() - { - System.out.println( "executed dontRun" ); - } -} diff --git a/surefire-its/src/test/resources/surefire-1964/src/test/java/pkg/FilterTest.java b/surefire-its/src/test/resources/surefire-1964/src/test/java/pkg/FilterTest.java deleted file mode 100644 index d93eca9291..0000000000 --- a/surefire-its/src/test/resources/surefire-1964/src/test/java/pkg/FilterTest.java +++ /dev/null @@ -1,24 +0,0 @@ -package pkg; - -import org.junit.Test; - -public class FilterTest -{ - @Test - public void testABC() - { - System.out.println( "executed testABC" ); - } - - @Test - public void dontRun() - { - System.out.println( "executed dontRun" ); - } - - @Test - public void testXYZ() - { - System.out.println( "executed testXYZ" ); - } -} From f4ee9aa809fc5b41ac24d468b400f9e26605be2a Mon Sep 17 00:00:00 2001 From: imonteroperez Date: Tue, 8 Feb 2022 18:14:04 +0100 Subject: [PATCH 2/2] =?UTF-8?q?[SUREFIRE-1964]=20Support=20for=20method=20?= =?UTF-8?q?filtering=20on=20excludesFile=20and=20includesFile=20committers?= =?UTF-8?q?:=20Ildefonso=20Montero,=20Tibor=20Diga=C5=88a=20Add=20the=20im?= =?UTF-8?q?plementation=20and=20integration=20tests=20Add=20some=20unit=20?= =?UTF-8?q?tests=20Add=20some=20javadoc=20to=20includesFile=20and=20exclud?= =?UTF-8?q?esFile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugin/failsafe/IntegrationTestMojo.java | 10 + .../plugin/surefire/AbstractSurefireMojo.java | 120 +++++++---- .../surefire/AbstractSurefireMojoTest.java | 198 +++++++++++++++++- .../plugin/surefire/MojoMocklessTest.java | 6 + .../maven/plugin/surefire/SurefirePlugin.java | 10 + .../AbstractTestMultipleMethodPatterns.java | 3 +- .../surefire/its/jiras/Surefire1964IT.java | 55 +++++ .../resources/surefire-1964/exclusions.txt | 1 + .../resources/surefire-1964/inclusions.txt | 1 + .../src/test/resources/surefire-1964/pom.xml | 58 +++++ .../src/test/java/pkg/ExcludedTest.java | 12 ++ .../src/test/java/pkg/FilterTest.java | 24 +++ 12 files changed, 440 insertions(+), 58 deletions(-) create mode 100644 surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1964IT.java create mode 100644 surefire-its/src/test/resources/surefire-1964/exclusions.txt create mode 100644 surefire-its/src/test/resources/surefire-1964/inclusions.txt create mode 100644 surefire-its/src/test/resources/surefire-1964/pom.xml create mode 100644 surefire-its/src/test/resources/surefire-1964/src/test/java/pkg/ExcludedTest.java create mode 100644 surefire-its/src/test/resources/surefire-1964/src/test/java/pkg/FilterTest.java 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 11c3e5828a..c3466916b1 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 @@ -364,6 +364,11 @@ public class IntegrationTestMojo * **{@literal /}NotIncludedByDefault.java * %regex[.*IT.*|.*Not.*] * + *
+ * Since 3.0.0-M6, method filtering support is provided in the inclusions file as well, example: + *

+     * pkg.SomeIT#testMethod
+     * 
* * @since 2.13 */ @@ -379,6 +384,11 @@ public class IntegrationTestMojo * **{@literal /}DontRunIT.* * %regex[.*IT.*|.*Not.*] * + *
+ * Since 3.0.0-M6, method filtering support is provided in the exclusions file as well, example: + *

+     * pkg.SomeIT#testMethod
+     * 
* * @since 2.13 */ 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 c9f44b53c3..40ed1996cc 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 @@ -2194,78 +2194,110 @@ private boolean isSpecificTestSpecified() } } - private void maybeAppendList( List base, List list ) + @Nonnull + private List getExcludedScanList() + throws MojoFailureException { - if ( list != null ) - { - base.addAll( list ); - } + return getExcludeList( true ); } - @Nonnull private List getExcludeList() + @Nonnull + private List getExcludeList() + throws MojoFailureException + { + return getExcludeList( false ); + } + + /** + * Computes a merge list of test exclusions. + * Used only in {@link #getExcludeList()} and {@link #getExcludedScanList()}. + * @param asScanList true if dependency or directory scanner + * @return list of patterns + * @throws MojoFailureException if the excludes breaks a pattern format + */ + @Nonnull + private List getExcludeList( boolean asScanList ) throws MojoFailureException { - List actualExcludes = null; + List excludes; if ( isSpecificTestSpecified() ) { - actualExcludes = Collections.emptyList(); + excludes = Collections.emptyList(); } else { - if ( getExcludesFile() != null ) + excludes = new ArrayList<>(); + if ( asScanList ) { - actualExcludes = readListFromFile( getExcludesFile() ); + if ( getExcludes() != null ) + { + excludes.addAll( getExcludes() ); + } + checkMethodFilterInIncludesExcludes( excludes ); } - if ( actualExcludes == null ) - { - actualExcludes = getExcludes(); - } - else + if ( getExcludesFile() != null ) { - maybeAppendList( actualExcludes, getExcludes() ); + excludes.addAll( readListFromFile( getExcludesFile() ) ); } - checkMethodFilterInIncludesExcludes( actualExcludes ); - - if ( actualExcludes == null || actualExcludes.isEmpty() ) + if ( asScanList && excludes.isEmpty() ) { - actualExcludes = Collections.singletonList( getDefaultExcludes() ); + excludes = Collections.singletonList( getDefaultExcludes() ); } } - return filterNulls( actualExcludes ); + return filterNulls( excludes ); + } + + @Nonnull + private List getIncludedScanList() + throws MojoFailureException + { + return getIncludeList( true ); } + @Nonnull private List getIncludeList() throws MojoFailureException { - List includes = null; + return getIncludeList( false ); + } + + /** + * Computes a merge list of test inclusions. + * Used only in {@link #getIncludeList()} and {@link #getIncludedScanList()}. + * @param asScanList true if dependency or directory scanner + * @return list of patterns + * @throws MojoFailureException if the includes breaks a pattern format + */ + @Nonnull + private List getIncludeList( boolean asScanList ) + throws MojoFailureException + { + final List includes = new ArrayList<>(); if ( isSpecificTestSpecified() ) { - includes = new ArrayList<>(); addAll( includes, split( getTest(), "," ) ); } else { - if ( getIncludesFile() != null ) + if ( asScanList ) { - includes = readListFromFile( getIncludesFile() ); + if ( getIncludes() != null ) + { + includes.addAll( getIncludes() ); + } + checkMethodFilterInIncludesExcludes( includes ); } - if ( includes == null ) - { - includes = getIncludes(); - } - else + if ( getIncludesFile() != null ) { - maybeAppendList( includes, getIncludes() ); + includes.addAll( readListFromFile( getIncludesFile() ) ); } - checkMethodFilterInIncludesExcludes( includes ); - - if ( includes == null || includes.isEmpty() ) + if ( asScanList && includes.isEmpty() ) { - includes = asList( getDefaultIncludes() ); + addAll( includes, getDefaultIncludes() ); } } @@ -2275,16 +2307,12 @@ private List getIncludeList() private void checkMethodFilterInIncludesExcludes( Iterable patterns ) throws MojoFailureException { - if ( patterns != null ) + for ( String pattern : patterns ) { - for ( String pattern : patterns ) + if ( pattern != null && pattern.contains( "#" ) ) { - if ( pattern != null && pattern.contains( "#" ) ) - { - throw new MojoFailureException( "Method filter prohibited in " - + "includes|excludes|includesFile|excludesFile parameter: " - + pattern ); - } + throw new MojoFailureException( "Method filter prohibited in includes|excludes parameter: " + + pattern ); } } } @@ -2294,16 +2322,18 @@ private TestListResolver getIncludedAndExcludedTests() { if ( includedExcludedTests == null ) { - includedExcludedTests = new TestListResolver( getIncludeList(), getExcludeList() ); + includedExcludedTests = new TestListResolver( getIncludedScanList(), getExcludedScanList() ); + getConsoleLogger().debug( "Resolved included and excluded patterns: " + includedExcludedTests ); } return includedExcludedTests; } public TestListResolver getSpecificTests() + throws MojoFailureException { if ( specificTests == null ) { - specificTests = new TestListResolver( getTest() ); + specificTests = new TestListResolver( getIncludeList(), getExcludeList() ); } return specificTests; } diff --git a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/AbstractSurefireMojoTest.java b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/AbstractSurefireMojoTest.java index 7d7d15f6d1..9d89447aff 100644 --- a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/AbstractSurefireMojoTest.java +++ b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/AbstractSurefireMojoTest.java @@ -30,14 +30,17 @@ import java.util.HashSet; import java.util.LinkedHashMap; import java.util.LinkedHashSet; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; +import org.apache.commons.io.FileUtils; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.DefaultArtifact; import org.apache.maven.artifact.handler.ArtifactHandler; +import org.apache.maven.artifact.handler.DefaultArtifactHandler; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.resolver.ArtifactResolutionRequest; import org.apache.maven.artifact.resolver.ArtifactResolutionResult; @@ -84,6 +87,7 @@ import org.powermock.modules.junit4.PowerMockRunner; import static java.io.File.separatorChar; +import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.file.Files.write; import static java.util.Arrays.asList; import static java.util.Collections.emptyList; @@ -95,8 +99,8 @@ import static org.apache.maven.surefire.shared.lang3.JavaVersion.JAVA_9; import static org.apache.maven.surefire.shared.lang3.JavaVersion.JAVA_RECENT; import static org.apache.maven.surefire.shared.lang3.SystemUtils.IS_OS_WINDOWS; -import static org.codehaus.plexus.languages.java.jpms.ModuleNameSource.MODULEDESCRIPTOR; import static org.assertj.core.api.Assertions.assertThat; +import static org.codehaus.plexus.languages.java.jpms.ModuleNameSource.MODULEDESCRIPTOR; import static org.junit.Assert.fail; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; @@ -1990,6 +1994,12 @@ public static class Mojo private int failOnFlakeCount; private String[] includeJUnit5Engines; private String[] excludeJUnit5Engines; + private List projectTestArtifacts; + private File includesFile; + private File excludesFile; + private List includes; + private List excludes; + private String test; private JUnitPlatformProviderInfo createJUnitPlatformProviderInfo( Artifact junitPlatformArtifact, TestClassPath testClasspathWrapper ) @@ -1997,6 +2007,17 @@ private JUnitPlatformProviderInfo createJUnitPlatformProviderInfo( Artifact juni return new JUnitPlatformProviderInfo( null, junitPlatformArtifact, testClasspathWrapper ); } + void setProjectTestArtifacts( List projectTestArtifacts ) + { + this.projectTestArtifacts = projectTestArtifacts; + } + + @Override + List getProjectTestArtifacts() + { + return projectTestArtifacts; + } + @Override protected void logDebugOrCliShowErrors( String s ) { @@ -2114,43 +2135,48 @@ public void setReportsDirectory( File reportsDirectory ) @Override public String getTest() { - return null; + return test; } @Override public void setTest( String test ) { - + this.test = test; } @Override public List getIncludes() { - return null; + return includes; } @Override - public File getIncludesFile() + public void setIncludes( List includes ) { - return null; + this.includes = includes; } - @Override - public void setIncludes( List includes ) + void setIncludesFile( File includesFile ) { + this.includesFile = includesFile; + } + @Override + public File getIncludesFile() + { + return includesFile; } @Override public List getExcludes() { - return null; + return excludes; } @Override public void setExcludes( List excludes ) { - + this.excludes = excludes; } @Override @@ -2309,10 +2335,15 @@ public String getShutdown() return null; } + void setExcludesFile( File excludesFile ) + { + this.excludesFile = excludesFile; + } + @Override public File getExcludesFile() { - return null; + return excludesFile; } @Override @@ -2483,6 +2514,151 @@ public void setExcludeJUnit5Engines( String[] excludeJUnit5Engines ) } } + @Test + public void shouldNotPerformMethodFilteringOnIncludes() throws Exception + { + Mojo plugin = new Mojo(); + + File includesExcludes = File.createTempFile( "surefire", "-includes" ); + FileUtils.write( includesExcludes, "AnotherTest#method" , UTF_8 ); + plugin.setIncludesFile( includesExcludes ); + + List includes = new LinkedList<>(); + includes.add( "AnotherTest#method " ); + plugin.setIncludes( includes ); + + VersionRange version = VersionRange.createFromVersion( "1.0" ); + ArtifactHandler handler = new DefaultArtifactHandler(); + Artifact testDeps = new DefaultArtifact( "g", "a", version, "compile", "jar", null, handler ); + File artifactFile = File.createTempFile( "surefire", ".jar" ); + artifactFile.deleteOnExit(); + testDeps.setFile( artifactFile ); + plugin.setProjectTestArtifacts( singletonList( testDeps ) ); + plugin.setDependenciesToScan( new String[] { "g:a" } ); + + e.expectMessage( "Method filter prohibited in includes|excludes parameter: AnotherTest#method " ); + plugin.scanDependencies(); + } + + @Test + public void shouldFilterTestsOnIncludesFile() throws Exception + { + Mojo plugin = new Mojo(); + + plugin.setLogger( mock( Logger.class ) ); + + File includes = File.createTempFile( "surefire", "-includes" ); + FileUtils.write( includes, "AnotherTest#method" , UTF_8 ); + plugin.setIncludesFile( includes ); + + VersionRange version = VersionRange.createFromVersion( "1.0" ); + ArtifactHandler handler = new DefaultArtifactHandler(); + Artifact testDeps = new DefaultArtifact( "g", "a", version, "compile", "test-jar", null, handler ); + File artifactFile = File.createTempFile( "surefire", "-classes" ); + String classDir = artifactFile.getCanonicalPath(); + assertThat( artifactFile.delete() ).isTrue(); + File classes = new File( classDir ); + assertThat( classes.mkdir() ).isTrue(); + testDeps.setFile( classes ); + assertThat( new File( classes, "AnotherTest.class" ).createNewFile() ) + .isTrue(); + plugin.setProjectTestArtifacts( singletonList( testDeps ) ); + plugin.setDependenciesToScan( new String[] { "g:a" } ); + + DefaultScanResult result = plugin.scanDependencies(); + assertThat ( result.getClasses() ).hasSize( 1 ); + assertThat ( result.getClasses().iterator().next() ).isEqualTo( "AnotherTest" ); + } + + @Test + public void shouldFilterTestsOnExcludesFile() throws Exception + { + Mojo plugin = new Mojo(); + + plugin.setLogger( mock( Logger.class ) ); + + File excludes = File.createTempFile( "surefire", "-excludes" ); + FileUtils.write( excludes, "AnotherTest" , UTF_8 ); + plugin.setExcludesFile( excludes ); + + VersionRange version = VersionRange.createFromVersion( "1.0" ); + ArtifactHandler handler = new DefaultArtifactHandler(); + Artifact testDeps = new DefaultArtifact( "g", "a", version, "compile", "test-jar", null, handler ); + File artifactFile = File.createTempFile( "surefire", "-classes" ); + String classDir = artifactFile.getCanonicalPath(); + assertThat( artifactFile.delete() ).isTrue(); + File classes = new File( classDir ); + assertThat( classes.mkdir() ).isTrue(); + testDeps.setFile( classes ); + assertThat( new File( classes, "AnotherTest.class" ).createNewFile() ) + .isTrue(); + plugin.setProjectTestArtifacts( singletonList( testDeps ) ); + plugin.setDependenciesToScan( new String[] { "g:a" } ); + + DefaultScanResult result = plugin.scanDependencies(); + assertThat ( result.getClasses() ) + .isEmpty(); + } + + @Test + public void shouldFilterTestsOnExcludes() throws Exception + { + Mojo plugin = new Mojo(); + + plugin.setLogger( mock( Logger.class ) ); + + plugin.setExcludes( singletonList( "AnotherTest" ) ); + + VersionRange version = VersionRange.createFromVersion( "1.0" ); + ArtifactHandler handler = new DefaultArtifactHandler(); + Artifact testDeps = new DefaultArtifact( "g", "a", version, "compile", "jar", null, handler ); + File artifactFile = File.createTempFile( "surefire", "-classes" ); + String classDir = artifactFile.getCanonicalPath(); + assertThat( artifactFile.delete() ).isTrue(); + File classes = new File( classDir ); + assertThat( classes.mkdir() ).isTrue(); + testDeps.setFile( classes ); + assertThat( new File( classes, "AnotherTest.class" ).createNewFile() ) + .isTrue(); + plugin.setProjectTestArtifacts( singletonList( testDeps ) ); + plugin.setDependenciesToScan( new String[] { "g:a" } ); + + DefaultScanResult result = plugin.scanDependencies(); + assertThat ( result.getClasses() ) + .isEmpty(); + } + + @Test + public void shouldUseOnlySpecificTests() throws Exception + { + Mojo plugin = new Mojo(); + + plugin.setLogger( mock( Logger.class ) ); + + File includes = File.createTempFile( "surefire", "-includes" ); + FileUtils.write( includes, "AnotherTest" , UTF_8 ); + plugin.setIncludesFile( includes ); + plugin.setTest( "DifferentTest" ); + + VersionRange version = VersionRange.createFromVersion( "1.0" ); + ArtifactHandler handler = new DefaultArtifactHandler(); + Artifact testDeps = new DefaultArtifact( "g", "a", version, "compile", "test-jar", null, handler ); + File artifactFile = File.createTempFile( "surefire", "-classes" ); + String classDir = artifactFile.getCanonicalPath(); + assertThat( artifactFile.delete() ).isTrue(); + File classes = new File( classDir ); + assertThat( classes.mkdir() ).isTrue(); + testDeps.setFile( classes ); + assertThat( new File( classes, "AnotherTest.class" ).createNewFile() ) + .isTrue(); + plugin.setProjectTestArtifacts( singletonList( testDeps ) ); + plugin.setDependenciesToScan( new String[] { "g:a" } ); + + DefaultScanResult result = plugin.scanDependencies(); + assertThat ( result.getClasses() ) + .isEmpty(); + } + private static File mockFile( String absolutePath ) { File f = mock( File.class ); diff --git a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/MojoMocklessTest.java b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/MojoMocklessTest.java index 1548dfdbe9..6ad04160cc 100644 --- a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/MojoMocklessTest.java +++ b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/MojoMocklessTest.java @@ -32,6 +32,7 @@ import org.apache.maven.surefire.api.suite.RunResult; import org.apache.maven.surefire.api.util.DefaultScanResult; import org.apache.maven.toolchain.Toolchain; +import org.codehaus.plexus.logging.Logger; import org.junit.Test; import java.io.File; @@ -44,6 +45,7 @@ import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; import static org.powermock.reflect.Whitebox.invokeMethod; import static org.powermock.reflect.Whitebox.setInternalState; @@ -243,6 +245,7 @@ public void scanDependenciesShouldReturnClassWithExistingTestJAR() List projectTestArtifacts = singletonList( testDeps ); String[] dependenciesToScan = { "g:a" }; Mojo mojo = new Mojo( projectTestArtifacts, dependenciesToScan ); + mojo.setLogger( mock( Logger.class ) ); DefaultScanResult result = mojo.scanDependencies(); assertThat( result ) @@ -275,6 +278,7 @@ public void scanDependenciesShouldReturnNullWithEmptyTestJAR() List projectTestArtifacts = singletonList( testDeps ); String[] dependenciesToScan = { "g:a" }; Mojo mojo = new Mojo( projectTestArtifacts, dependenciesToScan ); + mojo.setLogger( mock( Logger.class ) ); DefaultScanResult result = mojo.scanDependencies(); assertThat( result ) @@ -306,6 +310,7 @@ public void scanDependenciesShouldReturnClassWithDirectory() List projectTestArtifacts = singletonList( testDeps ); String[] dependenciesToScan = { "g:a" }; Mojo mojo = new Mojo( projectTestArtifacts, dependenciesToScan ); + mojo.setLogger( mock( Logger.class ) ); DefaultScanResult result = mojo.scanDependencies(); assertThat( result ) @@ -352,6 +357,7 @@ public void scanMultipleDependencies() List projectTestArtifacts = asList( testDep1, testDep2 ); String[] dependenciesToScan = { "g:a" }; Mojo mojo = new Mojo( projectTestArtifacts, dependenciesToScan ); + mojo.setLogger( mock( Logger.class ) ); DefaultScanResult result = mojo.scanDependencies(); assertThat( result ) 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 240d2a5423..c40696d7a5 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 @@ -355,6 +355,11 @@ public class SurefirePlugin * **{@literal /}NotIncludedByDefault.java * %regex[.*Test.*|.*Not.*] * + *
+ * Since 3.0.0-M6, method filtering support is provided in the inclusions file as well, example: + *

+     * pkg.SomeTest#testMethod
+     * 
* * @since 2.13 */ @@ -370,6 +375,11 @@ public class SurefirePlugin * %regex[.*Test.*|.*Not.*] * * + * Since 3.0.0-M6, method filtering support is provided in the exclusions file as well, example: + *

+     * pkg.SomeTest#testMethod
+     * 
+ * * @since 2.13 */ @Parameter( property = "surefire.excludesFile" ) diff --git a/surefire-its/src/test/java/org/apache/maven/surefire/its/AbstractTestMultipleMethodPatterns.java b/surefire-its/src/test/java/org/apache/maven/surefire/its/AbstractTestMultipleMethodPatterns.java index 6c458f501b..055c407110 100644 --- a/surefire-its/src/test/java/org/apache/maven/surefire/its/AbstractTestMultipleMethodPatterns.java +++ b/surefire-its/src/test/java/org/apache/maven/surefire/its/AbstractTestMultipleMethodPatterns.java @@ -483,7 +483,6 @@ public void negativeTest() prepare( pattern ) .failNever() .executeTest() - .verifyTextInLog( "Method filter prohibited in includes|excludes|includesFile|excludesFile parameter: " - + pattern ); + .verifyTextInLog( "Method filter prohibited in includes|excludes parameter: " + pattern ); } } diff --git a/surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1964IT.java b/surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1964IT.java new file mode 100644 index 0000000000..62f60d25ca --- /dev/null +++ b/surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1964IT.java @@ -0,0 +1,55 @@ +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; + +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.is; + +/** + * + */ +public class Surefire1964IT extends SurefireJUnit4IntegrationTestCase +{ + @Test + public void shouldFilterTests() throws Exception + { + unpack( "surefire-1964" ) + .executeTest() + .assertTestSuiteResults( 1 ) + .assertThatLogLine( containsString( "executed testXYZ" ), is( 1 ) ) + .assertThatLogLine( containsString( "executed testABC" ), is( 0 ) ) + .assertThatLogLine( containsString( "executed dontRun" ), is( 0 ) ); + } + + @Test + public void shouldFilterTestsInPluginProcess() throws Exception + { + unpack( "surefire-1964" ) + .forkCount( 0 ) + .executeTest() + .assertTestSuiteResults( 1 ) + .assertThatLogLine( containsString( "executed testXYZ" ), is( 1 ) ) + .assertThatLogLine( containsString( "executed testABC" ), is( 0 ) ) + .assertThatLogLine( containsString( "executed dontRun" ), is( 0 ) ); + } +} diff --git a/surefire-its/src/test/resources/surefire-1964/exclusions.txt b/surefire-its/src/test/resources/surefire-1964/exclusions.txt new file mode 100644 index 0000000000..5bacad2cab --- /dev/null +++ b/surefire-its/src/test/resources/surefire-1964/exclusions.txt @@ -0,0 +1 @@ +FilterTest#testABC \ No newline at end of file diff --git a/surefire-its/src/test/resources/surefire-1964/inclusions.txt b/surefire-its/src/test/resources/surefire-1964/inclusions.txt new file mode 100644 index 0000000000..50077e4c4d --- /dev/null +++ b/surefire-its/src/test/resources/surefire-1964/inclusions.txt @@ -0,0 +1 @@ +FilterTest#test* \ No newline at end of file diff --git a/surefire-its/src/test/resources/surefire-1964/pom.xml b/surefire-its/src/test/resources/surefire-1964/pom.xml new file mode 100644 index 0000000000..8f3ba29389 --- /dev/null +++ b/surefire-its/src/test/resources/surefire-1964/pom.xml @@ -0,0 +1,58 @@ + + + + + 4.0.0 + + org.example + maven-surefire-excludefiles + 1.0-SNAPSHOT + + + ${java.specification.version} + ${java.specification.version} + + + + + junit + junit + 4.13.2 + test + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + ${surefire.version} + + inclusions.txt + exclusions.txt + + + + + + diff --git a/surefire-its/src/test/resources/surefire-1964/src/test/java/pkg/ExcludedTest.java b/surefire-its/src/test/resources/surefire-1964/src/test/java/pkg/ExcludedTest.java new file mode 100644 index 0000000000..b871231ef2 --- /dev/null +++ b/surefire-its/src/test/resources/surefire-1964/src/test/java/pkg/ExcludedTest.java @@ -0,0 +1,12 @@ +package pkg; + +import org.junit.Test; + +public class ExcludedTest +{ + @Test + public void dontRun() + { + System.out.println( "executed dontRun" ); + } +} diff --git a/surefire-its/src/test/resources/surefire-1964/src/test/java/pkg/FilterTest.java b/surefire-its/src/test/resources/surefire-1964/src/test/java/pkg/FilterTest.java new file mode 100644 index 0000000000..d93eca9291 --- /dev/null +++ b/surefire-its/src/test/resources/surefire-1964/src/test/java/pkg/FilterTest.java @@ -0,0 +1,24 @@ +package pkg; + +import org.junit.Test; + +public class FilterTest +{ + @Test + public void testABC() + { + System.out.println( "executed testABC" ); + } + + @Test + public void dontRun() + { + System.out.println( "executed dontRun" ); + } + + @Test + public void testXYZ() + { + System.out.println( "executed testXYZ" ); + } +}