You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by kr...@apache.org on 2012/02/17 17:47:04 UTC

svn commit: r1245650 - in /maven/surefire/trunk/surefire-providers/common-junit4/src: main/java/org/apache/maven/surefire/common/junit4/ test/java/org/apache/maven/surefire/junit4/

Author: krosenvold
Date: Fri Feb 17 16:47:03 2012
New Revision: 1245650

URL: http://svn.apache.org/viewvc?rev=1245650&view=rev
Log:
[SUREFIRE-825] NPE when RunWith cant be loaded

Patch and testcase by Jan Sievers, accepted unmodified.

Added:
    maven/surefire/trunk/surefire-providers/common-junit4/src/test/java/org/apache/maven/surefire/junit4/PackageFilteringClassLoader.java   (with props)
Modified:
    maven/surefire/trunk/surefire-providers/common-junit4/src/main/java/org/apache/maven/surefire/common/junit4/JUnit4TestChecker.java
    maven/surefire/trunk/surefire-providers/common-junit4/src/test/java/org/apache/maven/surefire/junit4/JUnit4TestCheckerTest.java

Modified: maven/surefire/trunk/surefire-providers/common-junit4/src/main/java/org/apache/maven/surefire/common/junit4/JUnit4TestChecker.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-providers/common-junit4/src/main/java/org/apache/maven/surefire/common/junit4/JUnit4TestChecker.java?rev=1245650&r1=1245649&r2=1245650&view=diff
==============================================================================
--- maven/surefire/trunk/surefire-providers/common-junit4/src/main/java/org/apache/maven/surefire/common/junit4/JUnit4TestChecker.java (original)
+++ maven/surefire/trunk/surefire-providers/common-junit4/src/main/java/org/apache/maven/surefire/common/junit4/JUnit4TestChecker.java Fri Feb 17 16:47:03 2012
@@ -59,10 +59,13 @@ public class JUnit4TestChecker
             return false;
         }
 
-        Annotation runWithAnnotation = testClass.getAnnotation( runWith );
-        if ( runWithAnnotation != null )
+        if ( runWith != null )
         {
-            return true;
+            Annotation runWithAnnotation = testClass.getAnnotation( runWith );
+            if ( runWithAnnotation != null )
+            {
+                return true;
+            }
         }
 
         Class classToCheck = testClass;

Modified: maven/surefire/trunk/surefire-providers/common-junit4/src/test/java/org/apache/maven/surefire/junit4/JUnit4TestCheckerTest.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-providers/common-junit4/src/test/java/org/apache/maven/surefire/junit4/JUnit4TestCheckerTest.java?rev=1245650&r1=1245649&r2=1245650&view=diff
==============================================================================
--- maven/surefire/trunk/surefire-providers/common-junit4/src/test/java/org/apache/maven/surefire/junit4/JUnit4TestCheckerTest.java (original)
+++ maven/surefire/trunk/surefire-providers/common-junit4/src/test/java/org/apache/maven/surefire/junit4/JUnit4TestCheckerTest.java Fri Feb 17 16:47:03 2012
@@ -18,6 +18,8 @@ package org.apache.maven.surefire.junit4
  * under the License.
  */
 
+import java.util.Collections;
+import java.util.Set;
 import org.apache.maven.surefire.common.junit4.JUnit4TestChecker;
 import org.apache.maven.surefire.testset.TestSetFailedException;
 
@@ -115,6 +117,22 @@ public class JUnit4TestCheckerTest
         assertFalse( jUnit4TestChecker.accept( NestedTC.Inner.class ) );
     }
 
+    @Test
+    public void testCannotLoadRunWithAnnotation()
+        throws Exception
+    {
+        Class testClass = SimpleJUnit4TestClass.class;
+        ClassLoader testClassLoader = testClass.getClassLoader();
+        // Emulate an OSGi classloader which filters on package level.
+        // Use a classloader which can only load classes in package org.junit,
+        // e.g. org.junit.Test, but no classes from other packages,
+        // in particular org.junit.runner.RunWith can't be loaded
+        Set<String> visiblePackages = Collections.singleton( "org.junit" );
+        PackageFilteringClassLoader filteringTestClassloader =
+            new PackageFilteringClassLoader( testClassLoader, visiblePackages );
+        JUnit4TestChecker checker = new JUnit4TestChecker( filteringTestClassloader );
+        assertTrue( checker.accept( testClass ) );
+    }
 
     public static class AlsoValid
         extends TestCase
@@ -218,6 +236,14 @@ public class JUnit4TestCheckerTest
 
         }
     }
+    
+    public static class SimpleJUnit4TestClass
+    {
+        @Test
+        public void testMethod()
+        {
+        }
+    }
 
     class MySuite
         extends Suite

Added: maven/surefire/trunk/surefire-providers/common-junit4/src/test/java/org/apache/maven/surefire/junit4/PackageFilteringClassLoader.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-providers/common-junit4/src/test/java/org/apache/maven/surefire/junit4/PackageFilteringClassLoader.java?rev=1245650&view=auto
==============================================================================
--- maven/surefire/trunk/surefire-providers/common-junit4/src/test/java/org/apache/maven/surefire/junit4/PackageFilteringClassLoader.java (added)
+++ maven/surefire/trunk/surefire-providers/common-junit4/src/test/java/org/apache/maven/surefire/junit4/PackageFilteringClassLoader.java Fri Feb 17 16:47:03 2012
@@ -0,0 +1,61 @@
+package org.apache.maven.surefire.junit4;
+
+/*
+ * 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 java.util.Set;
+
+/**
+ * Emulate an OSGi classloader which only loads packages that have been imported via Import-Package MANIFEST header.
+ */
+public class PackageFilteringClassLoader
+    extends ClassLoader
+{
+
+    private ClassLoader wrapped;
+
+    private Set<String> visiblePackages;
+
+    public PackageFilteringClassLoader( ClassLoader wrapped, Set<String> visiblePackages )
+    {
+        this.wrapped = wrapped;
+        this.visiblePackages = visiblePackages;
+    }
+
+    @Override
+    public Class<?> loadClass( String className )
+        throws ClassNotFoundException
+    {
+        String packageName = "";
+        int lastDot = className.lastIndexOf( '.' );
+        if ( lastDot != -1 )
+        {
+            packageName = className.substring( 0, lastDot );
+        }
+        if ( visiblePackages.contains( packageName ) )
+        {
+            return wrapped.loadClass( className );
+        }
+        else
+        {
+            throw new ClassNotFoundException( className );
+        }
+    }
+
+}

Propchange: maven/surefire/trunk/surefire-providers/common-junit4/src/test/java/org/apache/maven/surefire/junit4/PackageFilteringClassLoader.java
------------------------------------------------------------------------------
    svn:eol-style = native