You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ti...@apache.org on 2017/12/12 23:37:36 UTC

maven-surefire git commit: [SUREFIRE-1452] Support filtering of tests from Base Class (TestNG)

Repository: maven-surefire
Updated Branches:
  refs/heads/SUREFIRE-1452 [created] 946bf1eb8


[SUREFIRE-1452] Support filtering of tests from Base Class (TestNG)


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

Branch: refs/heads/SUREFIRE-1452
Commit: 946bf1eb8d5b56dcd10ec44c463d2f6014d01a95
Parents: cb5173d
Author: Krishnan Mahadevan <kr...@gmail.com>
Authored: Mon Oct 9 09:35:11 2017 +0530
Committer: Tibor17 <ti...@apache.org>
Committed: Wed Dec 13 00:34:12 2017 +0100

----------------------------------------------------------------------
 .../surefire/testng/utils/MethodSelector.java   |  23 +-
 .../test/java/testng/utils/BaseClassSample.java |  28 ++
 .../java/testng/utils/ChildClassSample.java     |  25 ++
 .../java/testng/utils/MethodSelectorTest.java   | 390 +++++++++++++++++++
 4 files changed, 459 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/946bf1eb/surefire-providers/surefire-testng-utils/src/main/java/org/apache/maven/surefire/testng/utils/MethodSelector.java
----------------------------------------------------------------------
diff --git a/surefire-providers/surefire-testng-utils/src/main/java/org/apache/maven/surefire/testng/utils/MethodSelector.java b/surefire-providers/surefire-testng-utils/src/main/java/org/apache/maven/surefire/testng/utils/MethodSelector.java
index 2cd2f48..5b70ece 100644
--- a/surefire-providers/surefire-testng-utils/src/main/java/org/apache/maven/surefire/testng/utils/MethodSelector.java
+++ b/surefire-providers/surefire-testng-utils/src/main/java/org/apache/maven/surefire/testng/utils/MethodSelector.java
@@ -33,7 +33,7 @@ import org.testng.ITestNGMethod;
  * @since 2.7.3
  */
 public class MethodSelector
-    implements IMethodSelector
+        implements IMethodSelector
 {
     private static volatile TestListResolver testListResolver = null;
 
@@ -46,11 +46,11 @@ public class MethodSelector
     public boolean includeMethod( IMethodSelectorContext context, ITestNGMethod testngMethod, boolean isTestMethod )
     {
         return testngMethod.isBeforeClassConfiguration() || testngMethod.isBeforeGroupsConfiguration()
-            || testngMethod.isBeforeMethodConfiguration() || testngMethod.isBeforeSuiteConfiguration()
-            || testngMethod.isBeforeTestConfiguration() || testngMethod.isAfterClassConfiguration()
-            || testngMethod.isAfterGroupsConfiguration() || testngMethod.isAfterMethodConfiguration()
-            || testngMethod.isAfterSuiteConfiguration() || testngMethod.isAfterTestConfiguration()
-            || shouldRun( testngMethod );
+                || testngMethod.isBeforeMethodConfiguration() || testngMethod.isBeforeSuiteConfiguration()
+                || testngMethod.isBeforeTestConfiguration() || testngMethod.isAfterClassConfiguration()
+                || testngMethod.isAfterGroupsConfiguration() || testngMethod.isAfterMethodConfiguration()
+                || testngMethod.isAfterSuiteConfiguration() || testngMethod.isAfterTestConfiguration()
+                || shouldRun( testngMethod );
     }
 
     public static void setTestListResolver( TestListResolver testListResolver )
@@ -62,6 +62,15 @@ public class MethodSelector
     {
         TestListResolver resolver = testListResolver;
         boolean hasTestResolver = resolver != null && !resolver.isEmpty();
-        return hasTestResolver && resolver.shouldRun( test.getRealClass(), test.getMethodName() );
+        if ( hasTestResolver )
+        {
+            boolean resolved = false;
+            for ( Class<?> clazz = test.getRealClass(); clazz != null && !resolved; clazz = clazz.getSuperclass() )
+            {
+                resolved = resolver.shouldRun( clazz, test.getMethodName() );
+            }
+            return resolved;
+        }
+        return false;
     }
 }

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/946bf1eb/surefire-providers/surefire-testng-utils/src/test/java/testng/utils/BaseClassSample.java
----------------------------------------------------------------------
diff --git a/surefire-providers/surefire-testng-utils/src/test/java/testng/utils/BaseClassSample.java b/surefire-providers/surefire-testng-utils/src/test/java/testng/utils/BaseClassSample.java
new file mode 100644
index 0000000..f8b748e
--- /dev/null
+++ b/surefire-providers/surefire-testng-utils/src/test/java/testng/utils/BaseClassSample.java
@@ -0,0 +1,28 @@
+package testng.utils;
+
+/*
+ * 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 abstract class BaseClassSample
+{
+    public void baseClassMethodToBeIncluded()
+    {
+
+    }
+}

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/946bf1eb/surefire-providers/surefire-testng-utils/src/test/java/testng/utils/ChildClassSample.java
----------------------------------------------------------------------
diff --git a/surefire-providers/surefire-testng-utils/src/test/java/testng/utils/ChildClassSample.java b/surefire-providers/surefire-testng-utils/src/test/java/testng/utils/ChildClassSample.java
new file mode 100644
index 0000000..0449a82
--- /dev/null
+++ b/surefire-providers/surefire-testng-utils/src/test/java/testng/utils/ChildClassSample.java
@@ -0,0 +1,25 @@
+package testng.utils;
+
+/*
+ * 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 ChildClassSample
+        extends BaseClassSample
+{
+}

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/946bf1eb/surefire-providers/surefire-testng-utils/src/test/java/testng/utils/MethodSelectorTest.java
----------------------------------------------------------------------
diff --git a/surefire-providers/surefire-testng-utils/src/test/java/testng/utils/MethodSelectorTest.java b/surefire-providers/surefire-testng-utils/src/test/java/testng/utils/MethodSelectorTest.java
new file mode 100644
index 0000000..c4bc9cf
--- /dev/null
+++ b/surefire-providers/surefire-testng-utils/src/test/java/testng/utils/MethodSelectorTest.java
@@ -0,0 +1,390 @@
+package testng.utils;
+
+/*
+ * 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 junit.framework.TestCase;
+import org.apache.maven.surefire.testng.utils.MethodSelector;
+import org.apache.maven.surefire.testset.TestListResolver;
+import org.testng.IClass;
+import org.testng.IRetryAnalyzer;
+import org.testng.ITestClass;
+import org.testng.ITestNGMethod;
+import org.testng.internal.DefaultMethodSelectorContext;
+
+import java.lang.reflect.Method;
+
+public class MethodSelectorTest
+        extends TestCase
+{
+    public void testInclusionOfMethodFromBaseClass()
+    {
+        MethodSelector selector = new MethodSelector();
+        DefaultMethodSelectorContext context = new DefaultMethodSelectorContext();
+        ITestNGMethod testngMethod = new FakeTestNGMethod( ChildClassSample.class, "baseClassMethodToBeIncluded" );
+        TestListResolver resolver =
+                new TestListResolver( BaseClassSample.class.getName() + "#baseClassMethodToBeIncluded" );
+        MethodSelector.setTestListResolver( resolver );
+        boolean include = selector.includeMethod( context, testngMethod, true );
+        assertTrue( include );
+    }
+
+    public void testNoInclusionOfMethodFromBaseClass()
+    {
+        MethodSelector selector = new MethodSelector();
+        DefaultMethodSelectorContext context = new DefaultMethodSelectorContext();
+        ITestNGMethod testngMethod = new FakeTestNGMethod( ChildClassSample.class, "baseClassMethodToBeIncluded" );
+        TestListResolver resolver = new TestListResolver( BaseClassSample.class.getName() + "#nonExistedMethod" );
+        MethodSelector.setTestListResolver( resolver );
+        boolean include = selector.includeMethod( context, testngMethod, true );
+        assertFalse( include );
+    }
+
+    private static class FakeTestNGMethod
+            implements ITestNGMethod
+    {
+        private final Class<?> clazz;
+        private final String methodName;
+
+        FakeTestNGMethod( Class<?> clazz, String methodName )
+        {
+            this.clazz = clazz;
+            this.methodName = methodName;
+        }
+
+        @Override
+        public Class getRealClass()
+        {
+            return clazz;
+        }
+
+        @Override
+        public ITestClass getTestClass()
+        {
+            return null;
+        }
+
+        @Override
+        public void setTestClass( ITestClass iTestClass )
+        {
+
+        }
+
+        @Override
+        public Method getMethod()
+        {
+            return null;
+        }
+
+        @Override
+        public String getMethodName()
+        {
+            return methodName;
+        }
+
+        @Override
+        public Object[] getInstances()
+        {
+            return new Object[0];
+        }
+
+        @Override
+        public long[] getInstanceHashCodes()
+        {
+            return new long[0];
+        }
+
+        @Override
+        public String[] getGroups()
+        {
+            return new String[0];
+        }
+
+        @Override
+        public String[] getGroupsDependedUpon()
+        {
+            return new String[0];
+        }
+
+        @Override
+        public String getMissingGroup()
+        {
+            return null;
+        }
+
+        @Override
+        public void setMissingGroup( String s )
+        {
+
+        }
+
+        @Override
+        public String[] getBeforeGroups()
+        {
+            return new String[0];
+        }
+
+        @Override
+        public String[] getAfterGroups()
+        {
+            return new String[0];
+        }
+
+        @Override
+        public String[] getMethodsDependedUpon()
+        {
+            return new String[0];
+        }
+
+        @Override
+        public void addMethodDependedUpon( String s )
+        {
+
+        }
+
+        @Override
+        public boolean isTest()
+        {
+            return false;
+        }
+
+        @Override
+        public boolean isBeforeMethodConfiguration()
+        {
+            return false;
+        }
+
+        @Override
+        public boolean isAfterMethodConfiguration()
+        {
+            return false;
+        }
+
+        @Override
+        public boolean isBeforeClassConfiguration()
+        {
+            return false;
+        }
+
+        @Override
+        public boolean isAfterClassConfiguration()
+        {
+            return false;
+        }
+
+        @Override
+        public boolean isBeforeSuiteConfiguration()
+        {
+            return false;
+        }
+
+        @Override
+        public boolean isAfterSuiteConfiguration()
+        {
+            return false;
+        }
+
+        @Override
+        public boolean isBeforeTestConfiguration()
+        {
+            return false;
+        }
+
+        @Override
+        public boolean isAfterTestConfiguration()
+        {
+            return false;
+        }
+
+        @Override
+        public boolean isBeforeGroupsConfiguration()
+        {
+            return false;
+        }
+
+        @Override
+        public boolean isAfterGroupsConfiguration()
+        {
+            return false;
+        }
+
+        @Override
+        public long getTimeOut()
+        {
+            return 0;
+        }
+
+        @Override
+        public int getInvocationCount()
+        {
+            return 0;
+        }
+
+        @Override
+        public void setInvocationCount( int i )
+        {
+
+        }
+
+        @Override
+        public int getSuccessPercentage()
+        {
+            return 0;
+        }
+
+        @Override
+        public String getId()
+        {
+            return null;
+        }
+
+        @Override
+        public void setId( String s )
+        {
+
+        }
+
+        @Override
+        public long getDate()
+        {
+            return 0;
+        }
+
+        @Override
+        public void setDate( long l )
+        {
+
+        }
+
+        @Override
+        public boolean canRunFromClass( IClass iClass )
+        {
+            return false;
+        }
+
+        @Override
+        public boolean isAlwaysRun()
+        {
+            return false;
+        }
+
+        @Override
+        public int getThreadPoolSize()
+        {
+            return 0;
+        }
+
+        @Override
+        public void setThreadPoolSize( int i )
+        {
+
+        }
+
+        @Override
+        public String getDescription()
+        {
+            return null;
+        }
+
+        @Override
+        public void incrementCurrentInvocationCount()
+        {
+
+        }
+
+        @Override
+        public int getCurrentInvocationCount()
+        {
+            return 0;
+        }
+
+        @Override
+        public void setParameterInvocationCount( int i )
+        {
+
+        }
+
+        @Override
+        public int getParameterInvocationCount()
+        {
+            return 0;
+        }
+
+        @Override
+        public ITestNGMethod clone()
+        {
+            try
+            {
+                return ( ITestNGMethod ) super.clone();
+            }
+            catch ( CloneNotSupportedException e )
+            {
+                throw new RuntimeException( e );
+            }
+        }
+
+        @Override
+        public IRetryAnalyzer getRetryAnalyzer()
+        {
+            return null;
+        }
+
+        @Override
+        public void setRetryAnalyzer( IRetryAnalyzer iRetryAnalyzer )
+        {
+
+        }
+
+        @Override
+        public boolean skipFailedInvocations()
+        {
+            return false;
+        }
+
+        @Override
+        public void setSkipFailedInvocations( boolean b )
+        {
+
+        }
+
+        @Override
+        public long getInvocationTimeOut()
+        {
+            return 0;
+        }
+
+        @Override
+        public boolean ignoreMissingDependencies()
+        {
+            return false;
+        }
+
+        @Override
+        public void setIgnoreMissingDependencies( boolean b )
+        {
+
+        }
+
+        @Override
+        public int compareTo( Object o )
+        {
+            return 0;
+        }
+    }
+}