@Test
public void loadSnappyByDiffentClassloadersInTheSameJVM() throws Exception {
// Parent class loader cannot see Snappy.class
ClassLoader parent = this.getClass().getClassLoader().getParent();
ClassWorld cw = new ClassWorld();
ClassRealm P = cw.newRealm("P", parent);
try {
P.loadClass("org.xerial.snappy.Snappy");
fail("org.xerial.snappy.Snappy is found in the parent");
}
catch (ClassNotFoundException e) {
// OK
}
// Prepare the child class loaders which can load Snappy.class
URL classPath = new File("target/classes").toURI().toURL();
ClassRealm L1 = cw.newRealm("l1", URLClassLoader.newInstance(new URL[] { classPath }, parent));
ClassRealm L2 = cw.newRealm("l2", URLClassLoader.newInstance(new URL[] { classPath }, parent));
// Actually load Snappy.class in a child class loader
Class< ? > S1 = L1.loadClass("org.xerial.snappy.Snappy");
Method m = S1.getMethod("compress", String.class);