Skip to content
Permalink
Browse files

[WFCORE-4405] Binary jar files in model-test

  • Loading branch information
gaol committed Apr 9, 2019
1 parent d1b0113 commit 39b20604ece8f07038ecb6ed3b871fe0bd2fd258
@@ -34,10 +34,13 @@
import org.jboss.as.model.test.api.SingleChildFirst2;
import org.jboss.as.model.test.api.SingleParentFirst;
import org.jboss.as.model.test.api.Welcome;
import org.jboss.as.model.test.child.WelcomeChild;
import org.jboss.as.model.test.parent.WelcomeParent;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.jboss.shrinkwrap.impl.base.exporter.zip.ZipExporterImpl;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

/**
@@ -49,11 +52,30 @@
public ChildFirstClassLoadingTest() {
}

private static URL childJarURL, parentJarURL;

@BeforeClass
public static void createJars() throws Exception {
String tempDir = System.getProperty("java.io.tmpdir");

JavaArchive childJar = ShrinkWrap.create(JavaArchive.class, "child.jar").addClasses(WelcomeChild.class).addAsServiceProvider(Welcome.class, WelcomeChild.class);
File childFile = new File(tempDir + File.separator + childJar.getName());
new ZipExporterImpl(childJar).exportTo(childFile, true);
childJarURL = childFile.toURI().toURL();
childFile.deleteOnExit();

JavaArchive parentJar = ShrinkWrap.create(JavaArchive.class, "parent.jar").addClasses(WelcomeParent.class).addAsServiceProvider(Welcome.class, WelcomeParent.class);
File parentFile = new File(tempDir + File.separator + parentJar.getName());
new ZipExporterImpl(parentJar).exportTo(parentFile, true);
parentJarURL = parentFile.toURI().toURL();
parentFile.deleteOnExit();
}

@Test
public void testWithoutExclusion() throws Exception {
URLClassLoader parent = new URLClassLoader(new URL[]{ChildFirstClassLoadingTest.class.getResource("parent.jar")}, this.getClass().getClassLoader());
URLClassLoader parent = new URLClassLoader(new URL[]{parentJarURL}, this.getClass().getClassLoader());
parent.loadClass("org.jboss.as.model.test.parent.WelcomeParent");
ChildFirstClassLoader child = new ChildFirstClassLoader(parent, new HashSet<Pattern>(), new HashSet<Pattern>(), null, null, new URL[]{ChildFirstClassLoadingTest.class.getResource("child.jar")});
ChildFirstClassLoader child = new ChildFirstClassLoader(parent, new HashSet<Pattern>(), new HashSet<Pattern>(), null, null, new URL[]{childJarURL});
Class<?> welcomeParent = child.loadClass("org.jboss.as.model.test.parent.WelcomeParent");
Class<?> welcomeChild = child.loadClass("org.jboss.as.model.test.child.WelcomeChild");
Class<?> welcome = this.getClass().getClassLoader().loadClass("org.jboss.as.model.test.api.Welcome");
@@ -63,11 +85,11 @@ public void testWithoutExclusion() throws Exception {

@Test(expected = NoClassDefFoundError.class)
public void testWithExclusion() throws Exception {
URLClassLoader parent = new URLClassLoader(new URL[]{ChildFirstClassLoadingTest.class.getResource("parent.jar")}, this.getClass().getClassLoader());
URLClassLoader parent = new URLClassLoader(new URL[]{parentJarURL}, this.getClass().getClassLoader());
parent.loadClass("org.jboss.as.model.test.parent.WelcomeParent");
ChildFirstClassLoader child = new ChildFirstClassLoader(parent, new HashSet<Pattern>(), new HashSet<Pattern>(),
SingleClassFilter.createFilter(Welcome.class),
null, new URL[]{ChildFirstClassLoadingTest.class.getResource("child.jar")});
null, new URL[]{childJarURL});
Class<?> welcomeParent = child.loadClass("org.jboss.as.model.test.parent.WelcomeParent");
Class<?> welcomeChild = child.loadClass("org.jboss.as.model.test.child.WelcomeChild");
}
@@ -118,9 +140,9 @@ public void testSingleClassFromJar() throws Exception {

@Test
public void testServiceLoaderWithoutExclusion() throws Exception {
URLClassLoader parent = new URLClassLoader(new URL[]{ChildFirstClassLoadingTest.class.getResource("parent.jar")}, this.getClass().getClassLoader());
URLClassLoader parent = new URLClassLoader(new URL[]{parentJarURL}, this.getClass().getClassLoader());
parent.loadClass("org.jboss.as.model.test.parent.WelcomeParent");
ChildFirstClassLoader child = new ChildFirstClassLoader(parent, new HashSet<Pattern>(), new HashSet<Pattern>(), null, null, new URL[]{ChildFirstClassLoadingTest.class.getResource("child.jar")});
ChildFirstClassLoader child = new ChildFirstClassLoader(parent, new HashSet<Pattern>(), new HashSet<Pattern>(), null, null, new URL[]{childJarURL});
Class<?> welcomeParent = child.loadClass("org.jboss.as.model.test.parent.WelcomeParent");
Class<?> welcomeChild = child.loadClass("org.jboss.as.model.test.child.WelcomeChild");
Class<?> welcome = this.getClass().getClassLoader().loadClass("org.jboss.as.model.test.api.Welcome");
@@ -146,9 +168,9 @@ public void testServiceLoaderWithWildcardExclusion() throws Exception {
}

private void serviceLoaderWithExclusionTest(String exclusion) throws Exception {
URLClassLoader parent = new URLClassLoader(new URL[]{ChildFirstClassLoadingTest.class.getResource("parent.jar")}, this.getClass().getClassLoader());
URLClassLoader parent = new URLClassLoader(new URL[]{parentJarURL}, this.getClass().getClassLoader());
parent.loadClass("org.jboss.as.model.test.parent.WelcomeParent");
ChildFirstClassLoader child = new ChildFirstClassLoader(parent, new HashSet<Pattern>(), new HashSet<Pattern>(), null, Pattern.compile(exclusion), new URL[]{ChildFirstClassLoadingTest.class.getResource("child.jar")});
ChildFirstClassLoader child = new ChildFirstClassLoader(parent, new HashSet<Pattern>(), new HashSet<Pattern>(), null, Pattern.compile(exclusion), new URL[]{childJarURL});
Class<?> welcomeParent = child.loadClass("org.jboss.as.model.test.parent.WelcomeParent");
Class<?> welcomeChild = child.loadClass("org.jboss.as.model.test.child.WelcomeChild");
Class<?> welcome = this.getClass().getClassLoader().loadClass("org.jboss.as.model.test.api.Welcome");
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2019 Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file
* in the distribution for a full listing of individual contributors.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/

package org.jboss.as.model.test.child;

import org.jboss.as.model.test.api.Welcome;

/**
* @author <a href="mailto:lgao@redhat.com">Lin Gao</a>
*/
public class WelcomeChild implements Welcome {

public String hello(String name) {
return "The child is saying hello " + name;
}

}
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2019 Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file
* in the distribution for a full listing of individual contributors.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/

package org.jboss.as.model.test.parent;

import org.jboss.as.model.test.api.Welcome;

/**
* @author <a href="mailto:lgao@redhat.com">Lin Gao</a>
*/

public class WelcomeParent implements Welcome {

public String hello(String name) {
return "The parent is saying hello " + name;
}

}
Binary file not shown.
Binary file not shown.

0 comments on commit 39b2060

Please sign in to comment.
You can’t perform that action at this time.