<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>
*/
public class BootstrapClassLoader extends URLClassLoader {
- private String[] unmaskedPackages;
+ private final String[] unmaskedPackages;
public BootstrapClassLoader(ClassLoader parent, String... unmaskedPackages) {
super(new URL[0], parent);
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
*/
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();
+ }
}
*/
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)
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
return "2.6";
}
};
- assertThat(bootstrapper.getServerVersion(), is("2.6"));
+ assertThat(bootstrapper.getServerVersion()).isEqualTo("2.6");
}
}
*/
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("$");
}
}