private ShrinkWrapClassLoader getClassLoader(Archive<?> archive) { return (archive.getName().endsWith(SUFFIX_WEB_ARCHIVE)) ? new WebArchiveClassLoader(archive.getClass().getClassLoader(), archive) : new ShrinkWrapClassLoader(archive.getClass().getClassLoader(), archive); }
public static <I, T extends I> I invoke(Class<T> clazz, JavaArchive... classPathArchives) { JavaArchive[] copy = new JavaArchive[classPathArchives.length + 1]; System.arraycopy(classPathArchives, 0, copy, 0, classPathArchives.length); copy[copy.length - 1] = ShrinkWrap.create(JavaArchive.class).addClass(SerializationUtils.class); ClassLoader separatedClassLoader = new ShrinkWrapClassLoader(ClassLoaderUtils.getBootstrapClassLoader(), copy); return invoke(clazz, separatedClassLoader); }
Validate.notNull(archive, "Archive must be specified"); this.classLoader = new ShrinkWrapClassLoader(archive.getClass().getClassLoader(), archive);
/** * Creates the {@link ShrinkWrapClassLoaderTestCase#shrinkWrapClassLoader} used to load classes from an * {@link Archive}. The {@link ClassLoader} will be isolated from the application classpath by specifying a null * parent explicitly. */ @Before public void createClassLoader() { shrinkWrapClassLoader = new ShrinkWrapClassLoader((ClassLoader)null , archive); }
final ShrinkWrapClassLoader isolatedArchiveCL = new ShrinkWrapClassLoader((ClassLoader) null, archive);
private MigrationResult migrate() throws InspectionTransformationException { byte[] oldClassFile = transformed.toBytecode(); String oldClassName = transformed.getTransformedClass().getName(); String newClassName = transformed.getOriginalClass().getName(); Inspection serverInspection = transformed.getTransformedInspection(); NamedAsset transformedAsset = transformed.toShrinkWrapAsset(); try { ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); JavaArchive archive = ShrinkWrap.create(JavaArchive.class).add(transformedAsset); ShrinkWrapClassLoader shrinkWrapClassLoader = new ShrinkWrapClassLoader(ClassLoaderUtils.getBootstrapClassLoader(), archive); SeparatedClassLoader separatedClassLoader = new SeparatedClassLoader(shrinkWrapClassLoader, contextClassLoader); return SeparateInvocator.<Migration, MigrationImpl>invoke(MigrationImpl.class, separatedClassLoader).process( oldClassName, newClassName, oldClassFile, serverInspection); } catch (Throwable e) { throw new IllegalStateException("Cannot migrate transformed inspection back to original name", e); } }
/** * Ensures that we can open up an asset that doesn't exist via a {@link URL} from the {@link ShrinkWrapClassLoader} * (ie. should throw {@link FileNotFoundException} * * SHRINKWRAP-308 */ @Test(expected = FileNotFoundException.class) public void shouldNotBeAbleToOpenStreamOnNonexistantAsset() throws IOException { // Make a new Archive with some content in a directory final String nestedResourceName = "nested/test"; final Asset testAsset = new StringAsset("testContent"); final GenericArchive archive = ShrinkWrap.create(GenericArchive.class).add(testAsset, nestedResourceName); // Make a CL to load the content final ClassLoader swCl = new ShrinkWrapClassLoader(archive); // Get the URL to something that doesn't exist final URL nestedResourceUrl = swCl.getResource(nestedResourceName); final URL nestedResourceThatDoesntExistUrl = new URL(nestedResourceUrl, "../fake"); // openStream on the URL that doesn't exist should throw FNFE nestedResourceThatDoesntExistUrl.openStream(); }
/** * Ensures that we can open up directory content as obtained via a {@link URL} from the * {@link ShrinkWrapClassLoader} (ie. should return null, not throw an exception) * * SHRINKWRAP-306 */ @Test public void shouldBeAbleToOpenStreamOnDirectoryUrl() throws IOException { // Make a new Archive with some content in a directory final String nestedResourceName = "nested/test"; final Asset testAsset = new StringAsset("testContent"); final GenericArchive archive = ShrinkWrap.create(GenericArchive.class).add(testAsset, nestedResourceName); // Make a CL to load the content final ClassLoader swCl = new ShrinkWrapClassLoader(archive); // Get the URL to the parent directory final URL nestedResourceUrl = swCl.getResource(nestedResourceName); final URL nestedResourceUpALevelUrl = new URL(nestedResourceUrl, "../"); // openStream on the URL to the parent directory; should return null, not throw an exception final InputStream in = nestedResourceUpALevelUrl.openStream(); Assert.assertNull("URLs pointing to a directory should openStream as null", in); }
/** * Ensures we can load a Class instance from the {@link ShrinkWrapClassLoader} in a web archive */ @Test public void shouldBeAbleToLoadClassFromWebArchive() throws ClassNotFoundException { /** * Web Archive to be read via a {@link ShrinkWrapClassLoaderTestCase#shrinkWrapClassLoader}. * Places classes under WEB-INF/classes, so with this different root needs the CL to respect that * and know what to do */ final WebArchive webArchive = ShrinkWrap.create(WebArchive.class).addClass( applicationClassLoaderClass); final ClassLoader webArchiveClassLoader = new ShrinkWrapClassLoader((ClassLoader) null, "WEB-INF/classes", webArchive); // Load the test class from the CL final Class<?> loadedTestClass = Class.forName(applicationClassLoaderClass.getName(), false, webArchiveClassLoader); final ClassLoader loadedTestClassClassLoader = loadedTestClass.getClassLoader(); log.info("Got " + loadedTestClass + " from " + loadedTestClassClassLoader); // Assertions Assert.assertNotNull("Test class could not be found via the ClassLoader", loadedTestClass); Assert.assertSame("Test class should have been loaded via the web archive ClassLoader", webArchiveClassLoader, loadedTestClassClassLoader); Assert.assertNotSame("Class Loaded from the CL should not be the same as the one on the appCL", loadedTestClass, applicationClassLoaderClass); }
@Test public void shouldBeAbleToLoadVetoedClasses() throws Exception { Archive<JavaArchive> jarWithVetoedServiceImpl = createJarWithVetoedServices(); ClassLoader emptyParent = null; ShrinkWrapClassLoader swClassloader = new ShrinkWrapClassLoader(emptyParent, jarWithVetoedServiceImpl); ClassLoader emptyClassLoader = new ClassLoader(null) { }; ClassLoader originalClassLoader = SecurityActions.getThreadContextClassLoader(); Map<Class<?>, Set<Class<?>>> vetoed = null; Class<?> service; try { Thread.currentThread().setContextClassLoader(emptyClassLoader); service = swClassloader.loadClass("org.jboss.arquillian.core.impl.loadable.util.FakeService"); swClassloader.loadClass("org.jboss.arquillian.core.impl.loadable.util.ShouldBeIncluded"); swClassloader.loadClass("org.jboss.arquillian.core.impl.loadable.util.ShouldBeExcluded"); vetoed = new JavaSPIExtensionLoader().loadVetoed(swClassloader); } finally { Thread.currentThread().setContextClassLoader(originalClassLoader); } Assert.assertEquals("Unexpected number of vetoed services", 1, vetoed.size()); Assert.assertEquals("Unexpected number of vetoed services impl", 2, vetoed.get(service).size()); }
@Test public void shouldBeAbleToAddSelectedProviderFromClassLoader() throws Exception { Archive<JavaArchive> jarWithDefaultServiceImpl = createJarWithDefaultServiceImpl(); Archive<JavaArchive> jarThatReplaceServiceImpl = createJarThatReplaceServiceImpl(); ClassLoader emptyParent = null; ShrinkWrapClassLoader swClassloader = new ShrinkWrapClassLoader(emptyParent, jarThatReplaceServiceImpl, jarWithDefaultServiceImpl); ClassLoader emptyClassLoader = new ClassLoader(null) { }; ClassLoader originalClassLoader = SecurityActions.getThreadContextClassLoader(); Collection<?> providers = null; Class<?> expectedImplClass = null; try { Thread.currentThread().setContextClassLoader(emptyClassLoader); Class<?> serviceClass = swClassloader.loadClass("org.jboss.arquillian.core.impl.loadable.util.FakeService"); expectedImplClass = swClassloader.loadClass("org.jboss.arquillian.core.impl.loadable.util.ShouldBeIncluded"); providers = new JavaSPIExtensionLoader().all(swClassloader, serviceClass); } finally { Thread.currentThread().setContextClassLoader(originalClassLoader); } Assert.assertEquals("Unexpected number of providers loaded", 1, providers.size()); Assert.assertEquals("Wrong provider loaded", expectedImplClass, providers.iterator().next().getClass()); }
@Test public void shouldBeAbleToLoadVetoedClasses() throws Exception { Archive<JavaArchive> jarWithVetoedServiceImpl = createJarWithVetoedServices(); ClassLoader emptyParent = null; ShrinkWrapClassLoader swClassloader = new ShrinkWrapClassLoader(emptyParent, jarWithVetoedServiceImpl); ClassLoader emptyClassLoader = new ClassLoader(null) { }; ClassLoader originalClassLoader = SecurityActions.getThreadContextClassLoader(); Map<Class<?>, Set<Class<?>>> vetoed = null; Class<?> service; try { Thread.currentThread().setContextClassLoader(emptyClassLoader); service = swClassloader.loadClass("org.jboss.arquillian.core.impl.loadable.util.FakeService"); swClassloader.loadClass("org.jboss.arquillian.core.impl.loadable.util.ShouldBeIncluded"); swClassloader.loadClass("org.jboss.arquillian.core.impl.loadable.util.ShouldBeExcluded"); vetoed = new JavaSPIExtensionLoader().loadVetoed(swClassloader); } finally { Thread.currentThread().setContextClassLoader(originalClassLoader); } Assert.assertEquals("Unexpected number of vetoed services", 1, vetoed.size()); Assert.assertEquals("Unexpected number of vetoed services impl", 2, vetoed.get(service).size()); }
@Test public void shouldBeAbleToAddSelectedProviderFromClassLoader() throws Exception { Archive<JavaArchive> jarWithDefaultServiceImpl = createJarWithDefaultServiceImpl(); Archive<JavaArchive> jarThatReplaceServiceImpl = createJarThatReplaceServiceImpl(); ClassLoader emptyParent = null; ShrinkWrapClassLoader swClassloader = new ShrinkWrapClassLoader(emptyParent, jarThatReplaceServiceImpl, jarWithDefaultServiceImpl); ClassLoader emptyClassLoader = new ClassLoader(null) { }; ClassLoader originalClassLoader = SecurityActions.getThreadContextClassLoader(); Collection<?> providers = null; Class<?> expectedImplClass = null; try { Thread.currentThread().setContextClassLoader(emptyClassLoader); Class<?> serviceClass = swClassloader.loadClass("org.jboss.arquillian.core.impl.loadable.util.FakeService"); expectedImplClass = swClassloader.loadClass("org.jboss.arquillian.core.impl.loadable.util.ShouldBeIncluded"); providers = new JavaSPIExtensionLoader().all(swClassloader, serviceClass); } finally { Thread.currentThread().setContextClassLoader(originalClassLoader); } Assert.assertEquals("Unexpected number of providers loaded", 1, providers.size()); Assert.assertEquals("Wrong provider loaded", expectedImplClass, providers.iterator().next().getClass()); }