From bbf9b25189ca4a87db3cb2573b3715ed554557f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Horstmann?= Date: Sun, 21 Dec 2014 15:34:33 +0100 Subject: [PATCH] Match class names by first converting to a path pattern --- .../common/junit48/FilterFactory.java | 43 +++++++------------ .../common/junit48/FilterFactoryTest.java | 12 +----- 2 files changed, 17 insertions(+), 38 deletions(-) diff --git a/surefire-providers/common-junit48/src/main/java/org/apache/maven/surefire/common/junit48/FilterFactory.java b/surefire-providers/common-junit48/src/main/java/org/apache/maven/surefire/common/junit48/FilterFactory.java index dbab6ddc1b..3f81f87522 100644 --- a/surefire-providers/common-junit48/src/main/java/org/apache/maven/surefire/common/junit48/FilterFactory.java +++ b/surefire-providers/common-junit48/src/main/java/org/apache/maven/surefire/common/junit48/FilterFactory.java @@ -119,18 +119,19 @@ private static class MethodFilter private static class RequestedTestMethod { - final String className; - final String methodName; + final String classPattern; + final String methodPattern; private RequestedTestMethod( String className, String methodName ) { - this.className = className; - this.methodName = methodName; + // convert to path pattern so we can use the same matching logic as in includes + this.classPattern = className == null ? null : "**/" + convertToPath( className ); + this.methodPattern = methodName; } - private static boolean isSelectorPattern( String pattern ) + private static String convertToPath( String className ) { - return pattern.contains( "*" ) || pattern.contains( "?" ); + return className.replace( '.', '/' ); } public boolean isDescriptionmatch( Description description ) @@ -138,33 +139,18 @@ public boolean isDescriptionmatch( Description description ) String describedClassName = description.getClassName(); String describedMethodName = description.getMethodName(); - if ( methodName != null ) + if ( methodPattern != null ) { - if ( describedMethodName == null || !SelectorUtils.match( methodName, describedMethodName ) ) + if ( describedMethodName == null || !SelectorUtils.match( methodPattern, describedMethodName ) ) { return false; } } - if ( className != null && describedClassName != null ) + if ( classPattern != null && describedClassName != null ) { - if ( !isSelectorPattern( className ) ) - { - // existing implementation seems to be a simple contains check - if ( describedClassName.contains( className ) ) - { - return true; - } - } - else - { - if ( SelectorUtils.match( className, describedClassName ) ) - { - return true; - } - } - - return false; + String describedPath = convertToPath( describedClassName ); + return SelectorUtils.matchPath( classPattern, describedPath ); } return true; @@ -181,8 +167,9 @@ public MethodFilter( String requestString ) if ( !requestString.contains( "#" ) ) { - // old way before SUREFIRE-745, filter only by method name - // class name filtering is done separately + // a single method was specified, the leading hash sign was split off by + // {@link org.apache.maven.plugin.surefire.SurefirePlugin#getTestMethod()} + // filtering by class name is done via modified includes also in SurefirePlugin requestedTestMethods.add( new RequestedTestMethod( null, requestString ) ); } else diff --git a/surefire-providers/common-junit48/src/test/java/org/apache/maven/surefire/common/junit48/FilterFactoryTest.java b/surefire-providers/common-junit48/src/test/java/org/apache/maven/surefire/common/junit48/FilterFactoryTest.java index bf9dd98365..f44ee0a6fe 100644 --- a/surefire-providers/common-junit48/src/test/java/org/apache/maven/surefire/common/junit48/FilterFactoryTest.java +++ b/surefire-providers/common-junit48/src/test/java/org/apache/maven/surefire/common/junit48/FilterFactoryTest.java @@ -112,17 +112,9 @@ public void shouldMatchExactClassAndMethod() } @Test - public void shouldMatchEndOfClassNameWithMethod() + public void shouldMatchSimpleClassNameWithMethod() { - Filter exactFilter = createMethodFilter( "FirstClass#testMethod" ); - assertTrue( "exact match on name should run", exactFilter.shouldRun( testMethod ) ); - assertFalse( "other method should not match", exactFilter.shouldRun( otherMethod ) ); - } - - @Test - public void shouldMatchPartialClassNameWithMethod() - { - Filter exactFilter = createMethodFilter( "FirstClass#testMethod" ); + Filter exactFilter = createMethodFilter( "FilterFactoryTest$FirstClass#testMethod" ); assertTrue( "exact match on name should run", exactFilter.shouldRun( testMethod ) ); assertFalse( "other method should not match", exactFilter.shouldRun( otherMethod ) ); }