maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tibordig...@apache.org
Subject [4/4] maven-surefire git commit: [SUREFIRE-1275] Test regex patterns should be cached
Date Mon, 22 Aug 2016 18:23:59 GMT
[SUREFIRE-1275] Test regex patterns should be cached


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

Branch: refs/heads/master
Commit: 02d665cef290fb5ba02169367e4bc2e2a8f494f7
Parents: 4ad2e4c
Author: Tibor17 <tibor17@lycos.com>
Authored: Mon Aug 22 20:23:01 2016 +0200
Committer: Tibor17 <tibor17@lycos.com>
Committed: Mon Aug 22 20:23:01 2016 +0200

----------------------------------------------------------------------
 .../maven/surefire/testset/ResolvedTest.java    | 127 +++++++++++++------
 .../surefire/testset/TestListResolver.java      |  13 +-
 .../surefire/testset/TestListResolverTest.java  |   7 +
 .../common/junit48/FilterFactoryTest.java       |   2 +-
 4 files changed, 107 insertions(+), 42 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/02d665ce/surefire-api/src/main/java/org/apache/maven/surefire/testset/ResolvedTest.java
----------------------------------------------------------------------
diff --git a/surefire-api/src/main/java/org/apache/maven/surefire/testset/ResolvedTest.java
b/surefire-api/src/main/java/org/apache/maven/surefire/testset/ResolvedTest.java
index cf3af6f..b86a9c5 100644
--- a/surefire-api/src/main/java/org/apache/maven/surefire/testset/ResolvedTest.java
+++ b/surefire-api/src/main/java/org/apache/maven/surefire/testset/ResolvedTest.java
@@ -20,8 +20,12 @@ package org.apache.maven.surefire.testset;
  */
 
 import org.apache.maven.shared.utils.StringUtils;
+import org.apache.maven.shared.utils.io.MatchPatterns;
+
+import java.util.regex.Pattern;
 
 import static java.io.File.separatorChar;
+import static java.util.regex.Pattern.compile;
 import static org.apache.maven.shared.utils.StringUtils.isBlank;
 import static org.apache.maven.shared.utils.io.MatchPatterns.from;
 import static org.apache.maven.shared.utils.io.SelectorUtils.PATTERN_HANDLER_SUFFIX;
@@ -62,6 +66,10 @@ public final class ResolvedTest
 
     private final String description;
 
+    private final ClassMatcher classMatcher = new ClassMatcher();
+
+    private final MethodMatcher methodMatcher = new MethodMatcher();
+
     /**
      * '*' means zero or more characters<br>
      * '?' means one and only one character
@@ -269,49 +277,12 @@ public final class ResolvedTest
 
     private boolean matchClass( String testClassFile )
     {
-        return classPattern == null || matchTestClassFile( testClassFile );
+        return classPattern == null || classMatcher.matchTestClassFile( testClassFile );
     }
 
     private boolean matchMethod( String methodName )
     {
-        return methodPattern == null || methodName == null || matchMethodName( methodName
);
-    }
-
-    private boolean matchTestClassFile( String testClassFile )
-    {
-        return isRegexTestClassPattern ? matchClassRegexPatter( testClassFile ) : matchClassPatter(
testClassFile );
-    }
-
-    private boolean matchMethodName( String methodName )
-    {
-        return matchPath( methodPattern, methodName );
-    }
-
-    private boolean matchClassPatter( String testClassFile )
-    {
-        //@todo We have to use File.separator only because the MatchPatterns is using it
internally - cannot override.
-        String classPattern = this.classPattern;
-        if ( separatorChar != '/' )
-        {
-            testClassFile = testClassFile.replace( '/', separatorChar );
-            classPattern = classPattern.replace( '/', separatorChar );
-        }
-
-        if ( classPattern.endsWith( WILDCARD_FILENAME_POSTFIX ) || classPattern.endsWith(
CLASS_FILE_EXTENSION ) )
-        {
-            return from( classPattern ).matches( testClassFile, true );
-        }
-        else
-        {
-            String[] classPatterns = { classPattern + CLASS_FILE_EXTENSION, classPattern
};
-            return from( classPatterns ).matches( testClassFile, true );
-        }
-    }
-
-    private boolean matchClassRegexPatter( String testClassFile )
-    {
-        String realFile = separatorChar == '/' ? testClassFile : testClassFile.replace( '/',
separatorChar );
-        return from( classPattern ).matches( realFile, true );
+        return methodPattern == null || methodName == null || methodMatcher.matchMethodName(
methodName );
     }
 
     private static String tryBlank( String s )
@@ -391,4 +362,82 @@ public final class ResolvedTest
             return cls;
         }
     }
+
+    private final class ClassMatcher
+    {
+        private volatile MatchPatterns cache;
+
+        boolean matchTestClassFile( String testClassFile )
+        {
+            return ResolvedTest.this.isRegexTestClassPattern
+                           ? matchClassRegexPatter( testClassFile )
+                           : matchClassPatter( testClassFile );
+        }
+
+        private MatchPatterns of( String... sources )
+        {
+            if ( cache == null )
+            {
+                cache = from( sources );
+                return cache;
+            }
+            else
+            {
+                return cache;
+            }
+        }
+
+        private boolean matchClassPatter( String testClassFile )
+        {
+            //@todo We have to use File.separator only because the MatchPatterns is using
it internally - cannot override.
+            String classPattern = ResolvedTest.this.classPattern;
+            if ( separatorChar != '/' )
+            {
+                testClassFile = testClassFile.replace( '/', separatorChar );
+                classPattern = classPattern.replace( '/', separatorChar );
+            }
+
+            if ( classPattern.endsWith( WILDCARD_FILENAME_POSTFIX ) || classPattern.endsWith(
CLASS_FILE_EXTENSION ) )
+            {
+                return of( classPattern ).matches( testClassFile, true );
+            }
+            else
+            {
+                String[] classPatterns = { classPattern + CLASS_FILE_EXTENSION, classPattern
};
+                return of( classPatterns ).matches( testClassFile, true );
+            }
+        }
+
+        private boolean matchClassRegexPatter( String testClassFile )
+        {
+            String realFile = separatorChar == '/' ? testClassFile : testClassFile.replace(
'/', separatorChar );
+            return of( classPattern ).matches( realFile, true );
+        }
+    }
+
+    private final class MethodMatcher
+    {
+        private volatile Pattern cache;
+
+        boolean matchMethodName( String methodName )
+        {
+            if ( ResolvedTest.this.isRegexTestMethodPattern )
+            {
+                if ( cache == null )
+                {
+                    int from = REGEX_HANDLER_PREFIX.length();
+                    int to = ResolvedTest.this.methodPattern.length() - PATTERN_HANDLER_SUFFIX.length();
+                    String pattern = ResolvedTest.this.methodPattern.substring( from, to
);
+                    cache = compile( pattern );
+                }
+
+                return cache.matcher( methodName )
+                               .matches();
+            }
+            else
+            {
+                return matchPath( ResolvedTest.this.methodPattern, methodName );
+            }
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/02d665ce/surefire-api/src/main/java/org/apache/maven/surefire/testset/TestListResolver.java
----------------------------------------------------------------------
diff --git a/surefire-api/src/main/java/org/apache/maven/surefire/testset/TestListResolver.java
b/surefire-api/src/main/java/org/apache/maven/surefire/testset/TestListResolver.java
index 3eeb23a..f43d51e 100644
--- a/surefire-api/src/main/java/org/apache/maven/surefire/testset/TestListResolver.java
+++ b/surefire-api/src/main/java/org/apache/maven/surefire/testset/TestListResolver.java
@@ -381,8 +381,9 @@ public class TestListResolver
         if ( indexOfRegex != -1 )
         {
             if ( indexOfRegex != 0
-                || !pattern.endsWith( PATTERN_HANDLER_SUFFIX )
-                || pattern.indexOf( REGEX_HANDLER_PREFIX, prefixLength ) != -1 )
+                         || !pattern.endsWith( PATTERN_HANDLER_SUFFIX )
+                         || !isRegexMinLength( pattern )
+                         || pattern.indexOf( REGEX_HANDLER_PREFIX, prefixLength ) != -1 )
             {
                 String msg = "Illegal test|includes|excludes regex '%s'. Expected %%regex[class#method]
"
                     + "or !%%regex[class#method] " + "with optional class or #method.";
@@ -396,6 +397,14 @@ public class TestListResolver
         }
     }
 
+
+    static boolean isRegexMinLength( String pattern )
+    {
+        //todo bug in maven-shared-utils: '+1' should not appear in the condition
+        //todo cannot reuse code from SelectorUtils.java because method isRegexPrefixedPattern
is in private package.
+        return pattern.length() > REGEX_HANDLER_PREFIX.length() + PATTERN_HANDLER_SUFFIX.length()
+ 1;
+    }
+
     static String[] unwrapRegex( String regex )
     {
         regex = regex.trim();

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/02d665ce/surefire-api/src/test/java/org/apache/maven/surefire/testset/TestListResolverTest.java
----------------------------------------------------------------------
diff --git a/surefire-api/src/test/java/org/apache/maven/surefire/testset/TestListResolverTest.java
b/surefire-api/src/test/java/org/apache/maven/surefire/testset/TestListResolverTest.java
index 81e876b..fbd5108 100644
--- a/surefire-api/src/test/java/org/apache/maven/surefire/testset/TestListResolverTest.java
+++ b/surefire-api/src/test/java/org/apache/maven/surefire/testset/TestListResolverTest.java
@@ -84,6 +84,13 @@ public class TestListResolverTest
         }
     }
 
+    public void testMinRegexLength()
+    {
+        assertFalse( TestListResolver.isRegexMinLength( "%regex[]" ) );
+        assertFalse( TestListResolver.isRegexMinLength( "%regex[ ]" ) );
+        assertTrue( TestListResolver.isRegexMinLength( "%regex[*Test]" ) );
+    }
+
     public void testRemoveExclamationMark()
     {
         String pattern = TestListResolver.removeExclamationMark( "!%regex[]" );

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/02d665ce/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
index e9891cf..b91cdd8 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
@@ -188,7 +188,7 @@ public class FilterFactoryTest
     @Test
     public void testNegativeEmptyRegex()
     {
-        TestListResolver resolver = new TestListResolver( "%regex[ ]" );
+        TestListResolver resolver = new TestListResolver( "%regex[   ]" );
         assertTrue( resolver.getExcludedPatterns().isEmpty() );
         assertTrue( resolver.getIncludedPatterns().isEmpty() );
         assertTrue( resolver.isEmpty() );


Mime
View raw message