Skip to content

Instantly share code, notes, and snippets.

@raphw
Last active August 29, 2015 14:27
Show Gist options
  • Save raphw/5923800325383bba6c48 to your computer and use it in GitHub Desktop.
Save raphw/5923800325383bba6c48 to your computer and use it in GitHub Desktop.
Sealed package quirk.
public class SealedQuirk {
private static final String FOO = "foo", BAR = "bar", TMP = "tmp";
private ClassLoader singleClassLoader, sealedChildClassLoader, sealedParentClassLoader;
@Before
public void setUp() throws Exception {
File folder = Files.createTempDirectory(TMP).toFile();
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
manifest.getMainAttributes().put(Attributes.Name.SEALED, Boolean.TRUE.toString());
URL foo = new ByteBuddy().subclass(Object.class).name("sealed.Foo").make().toJar(new File(folder, FOO), manifest).toURI().toURL();
URL bar = new ByteBuddy().subclass(Object.class).name("sealed.Bar").make().toJar(new File(folder, BAR)).toURI().toURL();
singleClassLoader = new URLClassLoader(new URL[]{foo, bar});
sealedChildClassLoader = new URLClassLoader(new URL[]{foo}, new URLClassLoader(new URL[] {bar}));
sealedParentClassLoader = new URLClassLoader(new URL[]{bar}, new URLClassLoader(new URL[] {foo}));
}
@Test // throws exception
public void testSingleClassLoaderSealedFirst() throws Exception {
singleClassLoader.loadClass("sealed.Foo");
singleClassLoader.loadClass("sealed.Bar");
}
@Test // throws exception
public void testSealedParentClassLoaderSealedFirst() throws Exception {
sealedParentClassLoader.loadClass("sealed.Foo");
sealedParentClassLoader.loadClass("sealed.Bar");
}
@Test // throws exception
public void testSealedChildClassLoaderSealedFirst() throws Exception {
sealedChildClassLoader.loadClass("sealed.Foo");
sealedChildClassLoader.loadClass("sealed.Bar");
}
@Test // throws no exception
public void testSingleClassLoaderSealedLast() throws Exception {
singleClassLoader.loadClass("sealed.Bar");
singleClassLoader.loadClass("sealed.Foo");
}
@Test // throws no exception
public void testSealedParentClassLoaderSealedLast() throws Exception {
sealedParentClassLoader.loadClass("sealed.Bar");
sealedParentClassLoader.loadClass("sealed.Foo");
}
@Test // throws exception
public void testSealedChildClassLoaderSealedLast() throws Exception {
sealedChildClassLoader.loadClass("sealed.Bar");
sealedChildClassLoader.loadClass("sealed.Foo");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment