]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3697 Classloader of batch bootstrapper is not fully isolated
authorSimon Brandhof <simon.brandhof@gmail.com>
Thu, 16 Aug 2012 15:47:49 +0000 (17:47 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Thu, 16 Aug 2012 15:47:49 +0000 (17:47 +0200)
sonar-batch-bootstrapper/pom.xml
sonar-batch-bootstrapper/src/main/java/org/sonar/batch/bootstrapper/BootstrapClassLoader.java
sonar-batch-bootstrapper/src/test/java/org/sonar/batch/bootstrapper/BootstrapClassLoaderTest.java
sonar-batch-bootstrapper/src/test/java/org/sonar/batch/bootstrapper/BootstrapperTest.java
sonar-batch-bootstrapper/src/test/java/org/sonar/batch/bootstrapper/BootstrapperVersionTest.java

index b1e0366c6559c25e7a86a25c692a1d9a404d94ff..136dd99f4911abc5b74820441701d9085ca6bf5e 100644 (file)
@@ -19,8 +19,8 @@
       <scope>test</scope>
     </dependency>
     <dependency>
-      <groupId>org.hamcrest</groupId>
-      <artifactId>hamcrest-all</artifactId>
+      <groupId>org.easytesting</groupId>
+      <artifactId>fest-assert</artifactId>
       <scope>test</scope>
     </dependency>
   </dependencies>
index 3b302743ad39c76fd9092f5a8abd5e65347aa3b1..53f0d413cbc35a05b481ea6feb86e4a80ab94dbb 100644 (file)
@@ -29,7 +29,7 @@ import java.util.Enumeration;
  */
 public class BootstrapClassLoader extends URLClassLoader {
 
-  private String[] unmaskedPackages;
+  private final String[] unmaskedPackages;
 
   public BootstrapClassLoader(ClassLoader parent, String... unmaskedPackages) {
     super(new URL[0], parent);
@@ -74,11 +74,19 @@ public class BootstrapClassLoader extends URLClassLoader {
     if (c == null) {
       try {
         // Load from parent
-        if ((getParent() != null) && canLoadFromParent(name)) {
+        if (getParent() != null && canLoadFromParent(name)) {
           c = getParent().loadClass(name);
         } else {
           // Load from system
-          c = getSystemClassLoader().loadClass(name);
+
+          // I don't know for other vendors, but for Oracle JVM :
+          // - ClassLoader.getSystemClassLoader() is sun.misc.Launcher$AppClassLoader. It contains app classpath.
+          // - ClassLoader.getSystemClassLoader().getParent() is sun.misc.Launcher$ExtClassLoader. It contains core JVM
+          ClassLoader systemClassLoader = getSystemClassLoader();
+          if (systemClassLoader.getParent() != null) {
+            systemClassLoader = systemClassLoader.getParent();
+          }
+          c = systemClassLoader.loadClass(name);
         }
       } catch (ClassNotFoundException e) {
         // If still not found, then invoke findClass in order
index a96d50c3a9b8248556b012a05b4310011989fb46..05c4718b4a2faa2f11c9c04fa91aa42ca6d56266 100644 (file)
  */
 package org.sonar.batch.bootstrapper;
 
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.ExpectedException;
 
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertThat;
+import static org.fest.assertions.Assertions.assertThat;
 
 public class BootstrapClassLoaderTest {
 
+  @Rule
+  public ExpectedException thrown = ExpectedException.none();
+
   @Test
   public void shouldRestrictLoadingFromParent() throws Exception {
     BootstrapClassLoader classLoader = new BootstrapClassLoader(getClass().getClassLoader(), "org.sonar.ant");
-    assertThat(classLoader.canLoadFromParent("org.sonar.ant.Launcher"), is(true));
-    assertThat(classLoader.canLoadFromParent("org.objectweb.asm.ClassVisitor"), is(false));
+    assertThat(classLoader.canLoadFromParent("org.sonar.ant.Launcher")).isTrue();
+    assertThat(classLoader.canLoadFromParent("org.objectweb.asm.ClassVisitor")).isFalse();
+  }
+
+  @Test
+  public void use_isolated_system_classloader_when_parent_is_excluded() throws ClassNotFoundException {
+    thrown.expect(ClassNotFoundException.class);
+    thrown.expectMessage("org.junit.Test");
+    ClassLoader parent = getClass().getClassLoader();
+    BootstrapClassLoader classLoader = new BootstrapClassLoader(parent);
+
+    // JUnit is available in the parent classloader (classpath used to execute this test) but not in the core JVM
+    assertThat(classLoader.loadClass("java.lang.String", false)).isNotNull();
+    classLoader.loadClass("org.junit.Test", false);
   }
 
+  @Test
+  public void find_in_parent_when_matches_unmasked_packages() throws ClassNotFoundException {
+    ClassLoader parent = getClass().getClassLoader();
+    BootstrapClassLoader classLoader = new BootstrapClassLoader(parent, "org.junit");
+
+    // JUnit is available in the parent classloader (classpath used to execute this test) but not in the core JVM
+    assertThat(classLoader.loadClass("org.junit.Test", false)).isNotNull();
+  }
 }
index 8f3cc41871e259a1ea4898a2bd5f464249a1205b..d649b4e3faa87a232c3cb56d2d3c82abe4874195 100644 (file)
  */
 package org.sonar.batch.bootstrapper;
 
-import static org.hamcrest.Matchers.*;
-import static org.junit.Assert.assertThat;
+import org.junit.Test;
 
 import java.io.File;
 import java.io.IOException;
 
-import org.junit.Test;
+import static org.fest.assertions.Assertions.assertThat;
 
 public class BootstrapperTest {
 
   @Test
   public void shouldRemoveLastUrlSlash() {
     Bootstrapper bootstrapper = new Bootstrapper("", "http://test/", new File("target"));
-    assertThat(bootstrapper.getServerUrl(), is("http://test"));
+    assertThat(bootstrapper.getServerUrl()).isEqualTo("http://test");
   }
 
   @Test(expected = Exception.class)
@@ -46,9 +45,9 @@ public class BootstrapperTest {
     Bootstrapper bootstrapper = new Bootstrapper("test/0.1", "http://unknown.foo", new File("target"));
     String userAgent = bootstrapper.getUserAgent();
 
-    assertThat(userAgent.length(), greaterThan(0));
-    assertThat(userAgent, startsWith("sonar-bootstrapper/"));
-    assertThat(userAgent, endsWith(" test/0.1"));
+    assertThat(userAgent.length()).isGreaterThan(0);
+    assertThat(userAgent).startsWith("sonar-bootstrapper/");
+    assertThat(userAgent).endsWith(" test/0.1");
   }
 
   @Test
@@ -59,7 +58,7 @@ public class BootstrapperTest {
         return "2.6";
       }
     };
-    assertThat(bootstrapper.getServerVersion(), is("2.6"));
+    assertThat(bootstrapper.getServerVersion()).isEqualTo("2.6");
   }
 
 }
index 22115b05df8dd2181ebc249ad4b3007ed42fb52b..f9a7651f2f7513a6494e04b1980e643e78ea5b5f 100644 (file)
  */
 package org.sonar.batch.bootstrapper;
 
-import static org.hamcrest.Matchers.containsString;
-import static org.hamcrest.Matchers.not;
-import static org.junit.Assert.assertThat;
-
 import org.junit.Test;
 
+import static org.fest.assertions.Assertions.assertThat;
+
 public class BootstrapperVersionTest {
 
   @Test
   public void shouldLoadVersion() {
     String version = BootstrapperVersion.getVersion();
-    assertThat(version, containsString("."));
-    assertThat(version, not(containsString("$")));
+    assertThat(version).contains(".");
+    assertThat(version).doesNotContain("$");
   }
 
 }