maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From krosenv...@apache.org
Subject git commit: [SUREFIRE-944] Execute tests within abstract classes with static nested clas
Date Fri, 03 May 2013 19:07:03 GMT
Updated Branches:
  refs/heads/master c808eec80 -> b45fb4a46


[SUREFIRE-944] Execute tests within abstract classes with static nested clas

Integration test and initial patch by Geoff Denning, actual fix heavily modified by me.


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

Branch: refs/heads/master
Commit: b45fb4a46322c934edc9f8cc5391723eadbcd00d
Parents: c808eec
Author: Geoff Denning <geoff.denning@elasticpath.com>
Authored: Mon Dec 31 13:51:52 2012 -0800
Committer: Kristian Rosenvold <krosenvold@apache.org>
Committed: Fri May 3 20:59:25 2013 +0200

----------------------------------------------------------------------
 .../its/Junit47StaticInnerClassTestsIT.java        |   13 +++
 .../junit47-static-inner-class-tests/pom.xml       |   44 +++++++++
 .../src/test/java/junit4/BasicTest.java            |   13 +++
 .../java/junit4/TopLevelAbstractClassTest.java     |   13 +++
 .../test/java/junit4/TopLevelInterfaceTest.java    |   13 +++
 .../surefire/common/junit4/JUnit4TestChecker.java  |   19 +++--
 .../common/junit48/JUnit48TestChecker.java         |   70 ++++++++++++++
 .../common/junit48/JUnit48TestCheckerTest.java     |   75 +++++++++++++++
 .../surefire/junitcore/JUnitCoreProvider.java      |    6 +-
 9 files changed, 256 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/b45fb4a4/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/Junit47StaticInnerClassTestsIT.java
----------------------------------------------------------------------
diff --git a/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/Junit47StaticInnerClassTestsIT.java
b/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/Junit47StaticInnerClassTestsIT.java
new file mode 100644
index 0000000..5bdc29f
--- /dev/null
+++ b/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/Junit47StaticInnerClassTestsIT.java
@@ -0,0 +1,13 @@
+package org.apache.maven.surefire.its;
+
+import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase;
+import org.junit.Test;
+
+public class Junit47StaticInnerClassTestsIT extends SurefireJUnit4IntegrationTestCase
+{
+
+    @Test
+	public void testStaticInnerClassTests() {
+		executeErrorFreeTest( "junit47-static-inner-class-tests", 3 );
+	}
+}

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/b45fb4a4/surefire-integration-tests/src/test/resources/junit47-static-inner-class-tests/pom.xml
----------------------------------------------------------------------
diff --git a/surefire-integration-tests/src/test/resources/junit47-static-inner-class-tests/pom.xml
b/surefire-integration-tests/src/test/resources/junit47-static-inner-class-tests/pom.xml
new file mode 100644
index 0000000..a7ac055
--- /dev/null
+++ b/surefire-integration-tests/src/test/resources/junit47-static-inner-class-tests/pom.xml
@@ -0,0 +1,44 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>org.apache.maven.plugins.surefire</groupId>
+  <artifactId>junit4-test</artifactId>
+  <packaging>jar</packaging>
+  <version>1.0-SNAPSHOT</version>
+  <name>junit47-static-inner-class-tests</name>
+  <url>http://maven.apache.org</url>
+  <dependencies>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>4.8.1</version>
+    </dependency>
+  </dependencies>
+  <build>
+     <plugins>
+        <plugin>
+           <groupId>org.apache.maven.plugins</groupId>
+           <artifactId>maven-compiler-plugin</artifactId>
+           <configuration>
+              <source>1.5</source>
+              <target>1.5</target>
+           </configuration>
+        </plugin>
+        <plugin>
+           <groupId>org.apache.maven.plugins</groupId>
+           <artifactId>maven-surefire-plugin</artifactId>
+           <version>${surefire.version}</version>
+           <configuration>
+             <forkMode>never</forkMode>
+           </configuration>
+          <dependencies>
+            <dependency>
+              <groupId>org.apache.maven.surefire</groupId>
+              <artifactId>surefire-junit47</artifactId>
+              <version>${surefire.version}</version>
+            </dependency>
+          </dependencies>
+        </plugin>
+     </plugins>
+  </build>
+</project>

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/b45fb4a4/surefire-integration-tests/src/test/resources/junit47-static-inner-class-tests/src/test/java/junit4/BasicTest.java
----------------------------------------------------------------------
diff --git a/surefire-integration-tests/src/test/resources/junit47-static-inner-class-tests/src/test/java/junit4/BasicTest.java
b/surefire-integration-tests/src/test/resources/junit47-static-inner-class-tests/src/test/java/junit4/BasicTest.java
new file mode 100644
index 0000000..20d8785
--- /dev/null
+++ b/surefire-integration-tests/src/test/resources/junit47-static-inner-class-tests/src/test/java/junit4/BasicTest.java
@@ -0,0 +1,13 @@
+package junit4;
+import org.junit.Test;
+import org.junit.experimental.runners.Enclosed;
+import org.junit.runner.RunWith;
+
+@RunWith(Enclosed.class)
+public class BasicTest {
+	public static class InnerTest {
+		@Test
+		public void testSomething() {
+		}
+	}
+}

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/b45fb4a4/surefire-integration-tests/src/test/resources/junit47-static-inner-class-tests/src/test/java/junit4/TopLevelAbstractClassTest.java
----------------------------------------------------------------------
diff --git a/surefire-integration-tests/src/test/resources/junit47-static-inner-class-tests/src/test/java/junit4/TopLevelAbstractClassTest.java
b/surefire-integration-tests/src/test/resources/junit47-static-inner-class-tests/src/test/java/junit4/TopLevelAbstractClassTest.java
new file mode 100644
index 0000000..6193586
--- /dev/null
+++ b/surefire-integration-tests/src/test/resources/junit47-static-inner-class-tests/src/test/java/junit4/TopLevelAbstractClassTest.java
@@ -0,0 +1,13 @@
+package junit4;
+import org.junit.Test;
+import org.junit.experimental.runners.Enclosed;
+import org.junit.runner.RunWith;
+
+@RunWith(Enclosed.class)
+public abstract class TopLevelAbstractClassTest {
+	public static class InnerTest {
+		@Test
+		public void testSomething() {
+		}
+	}
+}

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/b45fb4a4/surefire-integration-tests/src/test/resources/junit47-static-inner-class-tests/src/test/java/junit4/TopLevelInterfaceTest.java
----------------------------------------------------------------------
diff --git a/surefire-integration-tests/src/test/resources/junit47-static-inner-class-tests/src/test/java/junit4/TopLevelInterfaceTest.java
b/surefire-integration-tests/src/test/resources/junit47-static-inner-class-tests/src/test/java/junit4/TopLevelInterfaceTest.java
new file mode 100644
index 0000000..2f777e3
--- /dev/null
+++ b/surefire-integration-tests/src/test/resources/junit47-static-inner-class-tests/src/test/java/junit4/TopLevelInterfaceTest.java
@@ -0,0 +1,13 @@
+package junit4;
+import org.junit.Test;
+import org.junit.experimental.runners.Enclosed;
+import org.junit.runner.RunWith;
+
+@RunWith(Enclosed.class)
+public interface TopLevelInterfaceTest {
+	public static class InnerTest {
+		@Test
+		public void testSomething() {
+		}
+	}
+}

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/b45fb4a4/surefire-providers/common-junit4/src/main/java/org/apache/maven/surefire/common/junit4/JUnit4TestChecker.java
----------------------------------------------------------------------
diff --git a/surefire-providers/common-junit4/src/main/java/org/apache/maven/surefire/common/junit4/JUnit4TestChecker.java
b/surefire-providers/common-junit4/src/main/java/org/apache/maven/surefire/common/junit4/JUnit4TestChecker.java
index d121d3c..4996f64 100644
--- a/surefire-providers/common-junit4/src/main/java/org/apache/maven/surefire/common/junit4/JUnit4TestChecker.java
+++ b/surefire-providers/common-junit4/src/main/java/org/apache/maven/surefire/common/junit4/JUnit4TestChecker.java
@@ -23,9 +23,10 @@ import java.lang.annotation.Annotation;
 import java.lang.reflect.Method;
 import org.apache.maven.surefire.NonAbstractClassFilter;
 import org.apache.maven.surefire.common.junit3.JUnit3TestChecker;
-import org.apache.maven.surefire.util.ReflectionUtils;
 import org.apache.maven.surefire.util.ScannerFilter;
 
+import static org.apache.maven.surefire.util.ReflectionUtils.tryLoadClass;
+
 /**
  * @author Kristian Rosenvold
  */
@@ -42,7 +43,7 @@ public class JUnit4TestChecker
     public JUnit4TestChecker( ClassLoader testClassLoader )
     {
         this.jUnit3TestChecker = new JUnit3TestChecker( testClassLoader );
-        this.runWith = getJUnitClass( testClassLoader, org.junit.runner.RunWith.class.getName()
);
+        this.runWith = tryLoadClass( testClassLoader, org.junit.runner.RunWith.class.getName()
);
         this.nonAbstractClassFilter = new NonAbstractClassFilter();
     }
 
@@ -59,7 +60,7 @@ public class JUnit4TestChecker
             return false;
         }
 
-        if ( runWith != null )
+        if ( isRunWithPresentInClassLoader() )
         {
             Annotation runWithAnnotation = testClass.getAnnotation( runWith );
             if ( runWithAnnotation != null )
@@ -68,6 +69,11 @@ public class JUnit4TestChecker
             }
         }
 
+        return lookForTestAnnotatedMethods( testClass );
+    }
+
+    private boolean lookForTestAnnotatedMethods( Class testClass )
+    {
         Class classToCheck = testClass;
         while ( classToCheck != null )
         {
@@ -80,7 +86,7 @@ public class JUnit4TestChecker
         return false;
     }
 
-    private boolean checkforTestAnnotatedMethod( Class testClass )
+    public boolean checkforTestAnnotatedMethod( Class testClass )
     {
         for ( Method lMethod : testClass.getDeclaredMethods() )
         {
@@ -95,9 +101,8 @@ public class JUnit4TestChecker
         return false;
     }
 
-    private Class getJUnitClass( ClassLoader classLoader, String className )
+    public boolean isRunWithPresentInClassLoader()
     {
-        return ReflectionUtils.tryLoadClass( classLoader, className );
+        return runWith != null;
     }
-
 }

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/b45fb4a4/surefire-providers/common-junit48/src/main/java/org/apache/maven/surefire/common/junit48/JUnit48TestChecker.java
----------------------------------------------------------------------
diff --git a/surefire-providers/common-junit48/src/main/java/org/apache/maven/surefire/common/junit48/JUnit48TestChecker.java
b/surefire-providers/common-junit48/src/main/java/org/apache/maven/surefire/common/junit48/JUnit48TestChecker.java
new file mode 100644
index 0000000..914c587
--- /dev/null
+++ b/surefire-providers/common-junit48/src/main/java/org/apache/maven/surefire/common/junit48/JUnit48TestChecker.java
@@ -0,0 +1,70 @@
+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.apache.maven.surefire.NonAbstractClassFilter;
+import org.apache.maven.surefire.common.junit4.JUnit4TestChecker;
+import org.apache.maven.surefire.util.ScannerFilter;
+import org.junit.experimental.runners.Enclosed;
+
+
+/**
+ * Looks for additional junit48-like features
+ * @author Geoff Denning
+ * @author Kristian Rosenvold
+ */
+public class JUnit48TestChecker
+    implements ScannerFilter
+{
+    private final NonAbstractClassFilter nonAbstractClassFilter;
+
+    private final JUnit4TestChecker jUnit4TestChecker;
+
+
+    public JUnit48TestChecker( ClassLoader testClassLoader )
+    {
+        this.jUnit4TestChecker = new JUnit4TestChecker( testClassLoader );
+        this.nonAbstractClassFilter = new NonAbstractClassFilter();
+    }
+
+    public boolean accept( Class testClass )
+    {
+        return jUnit4TestChecker.accept( testClass ) || isAbstractWithEnclosedRunner( testClass
);
+    }
+
+    @SuppressWarnings( { "unchecked" } )
+    private boolean isAbstractWithEnclosedRunner( Class testClass )
+    {
+        return jUnit4TestChecker.isRunWithPresentInClassLoader() &&  isAbstract(
testClass ) && isRunWithEnclosedRunner(testClass);
+    }
+
+    private boolean isRunWithEnclosedRunner(Class testClass )
+    {
+        @SuppressWarnings( "unchecked" ) org.junit.runner.RunWith runWithAnnotation =
+            (org.junit.runner.RunWith) testClass.getAnnotation( org.junit.runner.RunWith.class
);
+        return ( runWithAnnotation != null && Enclosed.class.equals( runWithAnnotation.value()));
+    }
+
+    private boolean isAbstract( Class testClass )
+    {
+        return !nonAbstractClassFilter.accept( testClass );
+    }
+}

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/b45fb4a4/surefire-providers/common-junit48/src/test/java/org/apache/maven/surefire/common/junit48/JUnit48TestCheckerTest.java
----------------------------------------------------------------------
diff --git a/surefire-providers/common-junit48/src/test/java/org/apache/maven/surefire/common/junit48/JUnit48TestCheckerTest.java
b/surefire-providers/common-junit48/src/test/java/org/apache/maven/surefire/common/junit48/JUnit48TestCheckerTest.java
new file mode 100644
index 0000000..9ea0829
--- /dev/null
+++ b/surefire-providers/common-junit48/src/test/java/org/apache/maven/surefire/common/junit48/JUnit48TestCheckerTest.java
@@ -0,0 +1,75 @@
+package org.apache.maven.surefire.common.junit48;
+
+import org.junit.Test;
+import org.junit.experimental.runners.Enclosed;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+/*
+ * 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.
+ */
+
+/**
+ * @author Kristian Rosenvold
+ */
+public class JUnit48TestCheckerTest
+{
+    @Test
+    public void valid48Class()
+        throws Exception
+    {
+        JUnit48TestChecker tc = new JUnit48TestChecker( this.getClass().getClassLoader()
);
+        assertTrue( tc.accept( BasicTest.class ) );
+    }
+
+    @Test
+    public void notValid48Class()
+        throws Exception
+    {
+        JUnit48TestChecker tc = new JUnit48TestChecker( this.getClass().getClassLoader()
);
+        assertFalse( tc.accept( BasicTest2.class ) );
+    }
+
+
+    @RunWith( Enclosed.class )
+    public abstract static class BasicTest
+    {
+        public static class InnerTest
+        {
+            @Test
+            public void testSomething()
+            {
+            }
+        }
+    }
+
+    @RunWith( Parameterized.class )
+    public abstract static class BasicTest2
+    {
+        public static class InnerTest
+        {
+            @Test
+            public void testSomething()
+            {
+            }
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/b45fb4a4/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/JUnitCoreProvider.java
----------------------------------------------------------------------
diff --git a/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/JUnitCoreProvider.java
b/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/JUnitCoreProvider.java
index 11e9d9a..14af56c 100644
--- a/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/JUnitCoreProvider.java
+++ b/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/JUnitCoreProvider.java
@@ -23,10 +23,11 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
+
 import org.apache.maven.surefire.common.junit4.JUnit4RunListenerFactory;
-import org.apache.maven.surefire.common.junit4.JUnit4TestChecker;
 import org.apache.maven.surefire.common.junit48.FilterFactory;
 import org.apache.maven.surefire.common.junit48.JUnit48Reflector;
+import org.apache.maven.surefire.common.junit48.JUnit48TestChecker;
 import org.apache.maven.surefire.providerapi.AbstractProvider;
 import org.apache.maven.surefire.providerapi.ProviderParameters;
 import org.apache.maven.surefire.report.ConsoleLogger;
@@ -42,7 +43,6 @@ import org.apache.maven.surefire.util.ScanResult;
 import org.apache.maven.surefire.util.ScannerFilter;
 import org.apache.maven.surefire.util.TestsToRun;
 import org.apache.maven.surefire.util.internal.StringUtils;
-
 import org.junit.runner.manipulation.Filter;
 
 /**
@@ -79,7 +79,7 @@ public class JUnitCoreProvider
         this.scanResult = providerParameters.getScanResult();
         this.runOrderCalculator = providerParameters.getRunOrderCalculator();
         this.jUnitCoreParameters = new JUnitCoreParameters( providerParameters.getProviderProperties()
);
-        this.scannerFilter = new JUnit4TestChecker( testClassLoader );
+        this.scannerFilter = new JUnit48TestChecker( testClassLoader );
         this.requestedTestMethod = providerParameters.getTestRequest().getRequestedTestMethod();
 
         customRunListeners =


Mime
View raw message