Unit tests of method filtering and according fixes

Project: http://git-wip-us.apache.org/repos/asf/maven-surefire/repo
Commit: http://git-wip-us.apache.org/repos/asf/maven-surefire/commit/a439d07c
Tree: http://git-wip-us.apache.org/repos/asf/maven-surefire/tree/a439d07c
Diff: http://git-wip-us.apache.org/repos/asf/maven-surefire/diff/a439d07c

Branch: refs/heads/master
Commit: a439d07c2e95fe4275d74f7c13a630b5dd08f6df
Parents: 650ee15
Author: Jörn Horstmann <g...@jhorstmann.net>
Authored: Thu Dec 18 00:30:23 2014 +0100
Committer: Jörn Horstmann <g...@jhorstmann.net>
Committed: Thu Dec 18 00:30:23 2014 +0100

----------------------------------------------------------------------
 .../surefire/common/junit48/FilterFactory.java  |  52 +++--
 .../common/junit48/FilterFactoryTest.java       | 208 +++++++++++++++++++
 2 files changed, 242 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/a439d07c/surefire-providers/common-junit48/src/main/java/org/apache/maven/surefire/common/junit48/FilterFactory.java
----------------------------------------------------------------------
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 184d7d9..dbab6dd 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
@@ -19,6 +19,7 @@ package org.apache.maven.surefire.common.junit48;
  * under the License.
  */
 
+import org.apache.maven.shared.utils.StringUtils;
 import org.apache.maven.shared.utils.io.SelectorUtils;
 import org.apache.maven.surefire.booter.ProviderParameterNames;
 import org.apache.maven.surefire.group.match.AndGroupMatcher;
@@ -31,6 +32,7 @@ import org.junit.runner.Description;
 import org.junit.runner.manipulation.Filter;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.List;
@@ -126,47 +128,58 @@ public class FilterFactory
                 this.methodName = methodName;
             }
 
-            private boolean isDescriptionmatch( Description description )
+            private static boolean isSelectorPattern( String pattern )
+            {
+                return pattern.contains( "*" ) || pattern.contains( "?" );
+            }
+
+            public boolean isDescriptionmatch( Description description )
             {
                 String describedClassName = description.getClassName();
                 String describedMethodName = description.getMethodName();
 
-                if ( describedClassName != null )
+                if ( methodName != null )
+                {
+                    if ( describedMethodName == null || !SelectorUtils.match( 
methodName, describedMethodName ) )
+                    {
+                        return false;
+                    }
+                }
+
+                if ( className != null && describedClassName != null )
                 {
-                    if ( this.className.indexOf( '*' ) < 0 && 
this.className.indexOf( '?' ) < 0 )
+                    if ( !isSelectorPattern( className ) )
                     {
                         // existing implementation seems to be a simple 
contains check
-                        if ( !describedClassName.contains( this.className ) )
+                        if ( describedClassName.contains( className ) )
                         {
-                            return false;
+                            return true;
                         }
                     }
                     else
                     {
-                        if ( !SelectorUtils.match( this.className, 
describedClassName ) )
+                        if ( SelectorUtils.match( className, 
describedClassName ) )
                         {
-                            return false;
+                            return true;
                         }
                     }
-                }
 
-                if ( describedMethodName != null && !SelectorUtils.match( 
this.methodName, describedMethodName ) )
-                {
                     return false;
                 }
 
                 return true;
+
             }
         }
 
         private final String requestString;
-        private final List<RequestedTestMethod> requestedTestMethods;
+        private final Collection<RequestedTestMethod> requestedTestMethods;
 
         public MethodFilter( String requestString )
         {
-            List<RequestedTestMethod> requestedTestMethods = new 
ArrayList<RequestedTestMethod>();
+            Collection<RequestedTestMethod> requestedTestMethods = new 
ArrayList<RequestedTestMethod>();
 
-            if ( requestString.indexOf( '#' ) < 0 )
+            if ( !requestString.contains( "#" ) )
             {
                 // old way before SUREFIRE-745, filter only by method name
                 // class name filtering is done separately
@@ -177,23 +190,26 @@ public class FilterFactory
                 // possibly several classes and methods separated by comma
                 // several methods in the same class separated by plus
 
-                for ( String requestedTestMethod : requestString.split( "," ) )
+                for ( String request : StringUtils.split( requestString, "," ) 
)
                 {
-                    int index = requestedTestMethod.indexOf( '#' );
+                    int index = request.indexOf( '#' );
                     if ( index < 0 )
                     {
-                        requestedTestMethods.add( new RequestedTestMethod( 
null, requestedTestMethod ) );
+                        // request can also contain complete classes
+                        requestedTestMethods.add( new RequestedTestMethod( 
request, null ) );
                     }
                     else
                     {
-                        String className = index == 0 ? null : 
requestedTestMethod.substring( 0, index );
-                        for ( String methodName : 
requestedTestMethod.substring( index + 1 ).split( "\\+" ) )
+                        String className = index == 0 ? null : 
request.substring( 0, index );
+                        String methodNames = request.substring( index + 1 );
+                        for ( String methodName : StringUtils.split( 
methodNames, "+" ) )
                         {
                             requestedTestMethods.add( new RequestedTestMethod( 
className, methodName ) );
                         }
                     }
                 }
             }
+
             this.requestString = requestString;
             this.requestedTestMethods = requestedTestMethods;
         }

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/a439d07c/surefire-providers/common-junit48/src/test/java/org/apache/maven/surefire/common/junit48/FilterFactoryTest.java
----------------------------------------------------------------------
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
new file mode 100644
index 0000000..bf9dd98
--- /dev/null
+++ 
b/surefire-providers/common-junit48/src/test/java/org/apache/maven/surefire/common/junit48/FilterFactoryTest.java
@@ -0,0 +1,208 @@
+package org.apache.maven.surefire.common.junit48;
+
+/*
+ * 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 org.junit.runner.Description;
+import org.junit.runner.manipulation.Filter;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.runner.Description.createTestDescription;
+
+public class FilterFactoryTest
+{
+    static class Suite
+    {
+
+    }
+
+    static class FirstClass
+    {
+
+    }
+
+    static class SecondClass {
+
+    }
+
+    private final Description testMethod = createTestDescription( 
FirstClass.class, "testMethod" );
+    private final Description secondTestMethod = createTestDescription( 
FirstClass.class, "secondTestMethod" );
+    private final Description otherMethod = createTestDescription( 
FirstClass.class, "otherMethod" );
+    private final Description testMethodInSecondClass = createTestDescription( 
SecondClass.class, "testMethod" );
+    private final Description secondTestMethodInSecondClass = 
createTestDescription( SecondClass.class,
+            "secondTestMethod" );
+
+    private final String firstClassName = FirstClass.class.getName();
+    private final String secondClassName = SecondClass.class.getName();
+
+    private Filter createMethodFilter( String requestString )
+    {
+        return new FilterFactory( getClass().getClassLoader() 
).createMethodFilter( requestString );
+    }
+
+    @Test
+    public void shouldMatchExactMethodName()
+    {
+        Filter exactFilter = createMethodFilter( "testMethod" );
+        assertTrue( "exact match on name should run", exactFilter.shouldRun( 
testMethod ) );
+    }
+
+    @Test
+    public void shouldMatchExactMethodNameWithHash()
+    {
+        Filter exactFilter = createMethodFilter( "#testMethod" );
+        assertTrue( "exact match on name should run", exactFilter.shouldRun( 
testMethod ) );
+    }
+
+    @Test
+    public void shouldNotMatchExactOnOtherMethod()
+    {
+        Filter exactFilter = createMethodFilter( "testMethod" );
+        assertFalse( "should not run other methods", exactFilter.shouldRun( 
otherMethod ) );
+    }
+
+    @Test
+    public void shouldMatchWildCardsInMethodName()
+    {
+        Filter starAtEnd = createMethodFilter( "test*" );
+        assertTrue( "match ending with star should run", starAtEnd.shouldRun( 
testMethod ) );
+
+        Filter starAtBeginning = createMethodFilter( "*Method" );
+        assertTrue( "match starting with star should run", 
starAtBeginning.shouldRun( testMethod ) );
+
+        Filter starInMiddle = createMethodFilter( "test*thod" );
+        assertTrue( "match containing star should run", 
starInMiddle.shouldRun( testMethod ) );
+
+        Filter questionAtEnd = createMethodFilter( "testMetho?" );
+        assertTrue( "match ending with question mark should run", 
questionAtEnd.shouldRun( testMethod ) );
+
+        Filter questionAtBeginning = createMethodFilter( "????Method" );
+        assertTrue( "match starting with question mark should run", 
questionAtBeginning.shouldRun( testMethod ) );
+
+        Filter questionInMiddle = createMethodFilter( "testM?thod" );
+        assertTrue( "match containing question mark should run", 
questionInMiddle.shouldRun( testMethod ) );
+
+        Filter starAndQuestion = createMethodFilter( "t?st*thod" );
+        assertTrue( "match containing star and question mark should run", 
starAndQuestion.shouldRun( testMethod ) );
+    }
+
+    @Test
+    public void shouldMatchExactClassAndMethod()
+    {
+        Filter exactFilter = createMethodFilter( firstClassName + 
"#testMethod" );
+        assertTrue( "exact match on name should run", exactFilter.shouldRun( 
testMethod ) );
+    }
+
+    @Test
+    public void shouldMatchEndOfClassNameWithMethod()
+    {
+        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" );
+        assertTrue( "exact match on name should run", exactFilter.shouldRun( 
testMethod ) );
+        assertFalse( "other method should not match", exactFilter.shouldRun( 
otherMethod ) );
+    }
+
+    @Test
+    public void shouldMatchClassNameWithWildcardAndMethod()
+    {
+        Filter exactFilter = createMethodFilter( "*First*#testMethod" );
+        assertTrue( "exact match on name should run", exactFilter.shouldRun( 
testMethod ) );
+        assertFalse( "other method should not match", exactFilter.shouldRun( 
otherMethod ) );
+    }
+
+    @Test
+    public void shouldMatchClassNameWithWildcardCompletely()
+    {
+        Filter exactFilter = createMethodFilter( "First*#testMethod" );
+        assertFalse( "other method should not match", exactFilter.shouldRun( 
testMethod ) );
+        assertFalse( "other method should not match", exactFilter.shouldRun( 
otherMethod ) );
+    }
+
+
+    @Test
+    public void shouldMatchMultipleMethodsSeparatedByComman()
+    {
+        Filter exactFilter = createMethodFilter( firstClassName + 
"#testMethod,#secondTestMethod" );
+
+        assertTrue( "exact match on name should run", exactFilter.shouldRun( 
testMethod ) );
+        assertTrue( "exact match on name should run", exactFilter.shouldRun( 
secondTestMethod ) );
+        assertTrue( "exact match on name should run", exactFilter.shouldRun( 
secondTestMethodInSecondClass ) );
+
+        assertFalse( "other method should not match", exactFilter.shouldRun( 
otherMethod ) );
+    }
+
+    @Test
+    public void shouldMatchMultipleMethodsInSameClassSeparatedByPlus() {
+        Filter exactFilter = createMethodFilter( FirstClass.class.getName() + 
"#testMethod+secondTestMethod" );
+
+        assertTrue( "exact match on name should run", exactFilter.shouldRun( 
testMethod ) );
+        assertTrue( "exact match on name should run", exactFilter.shouldRun( 
secondTestMethod ) );
+
+        assertFalse( "method in another class should not match", 
exactFilter.shouldRun( secondTestMethodInSecondClass ) );
+
+        assertFalse( "other method should not match", exactFilter.shouldRun( 
otherMethod ) );
+    }
+
+    @Test
+    public void shouldRunCompleteClassWhenSeparatedByCommaWithoutHash() {
+        Filter exactFilter = createMethodFilter( firstClassName + 
"#testMethod," + secondClassName );
+
+        assertTrue( "exact match on name should run", exactFilter.shouldRun( 
testMethod ) );
+
+        assertFalse( "other method should not match", exactFilter.shouldRun( 
secondTestMethod ) );
+        assertFalse( "other method should not match", exactFilter.shouldRun( 
otherMethod ) );
+
+        assertTrue( "should run complete second class", exactFilter.shouldRun( 
testMethodInSecondClass ) );
+        assertTrue( "should run complete second class", exactFilter.shouldRun( 
secondTestMethodInSecondClass ) );
+    }
+
+    @Test
+    public void shouldRunSuitesContainingExactMethodName()
+    {
+        Description suite = Description.createSuiteDescription( Suite.class );
+        suite.addChild( testMethod );
+        suite.addChild( secondTestMethod );
+
+        Filter exactFilter = createMethodFilter( "testMethod" );
+        assertTrue( "should run suites containing matching method", 
exactFilter.shouldRun( suite ) );
+    }
+
+    @Test
+    public void shouldSkipSuitesNotContainingExactMethodName()
+    {
+        Description suite = Description.createSuiteDescription( Suite.class );
+        suite.addChild( testMethod );
+        suite.addChild( secondTestMethod );
+
+        Filter exactFilter = createMethodFilter( "otherMethod" );
+        assertFalse( "should not run method", exactFilter.shouldRun( 
testMethod ) );
+        assertFalse( "should not run method", exactFilter.shouldRun( 
secondTestMethod ) );
+        assertFalse( "should not run suites containing no matches", 
exactFilter.shouldRun( suite ) );
+    }
+
+}

Reply via email to