Path: blob/master/test/jdk/java/net/URLClassLoader/definePackage/SplitPackage.java
41153 views
/*1* Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 815366526* @summary Test two URLClassLoader define Package object of the same name27* @library /test/lib28* @build jdk.test.lib.compiler.CompilerUtils29* @modules jdk.compiler30* @run testng SplitPackage31*/3233import java.net.URL;34import java.net.URLClassLoader;35import java.nio.file.Files;36import java.nio.file.Path;37import java.nio.file.Paths;38import java.nio.file.StandardCopyOption;39import java.util.jar.Manifest;40import jdk.test.lib.compiler.CompilerUtils;4142import org.testng.annotations.BeforeTest;43import org.testng.annotations.Test;44import static org.testng.Assert.*;4546public class SplitPackage {47private static final Path SRC_DIR = Paths.get(System.getProperty("test.src", "."));48private static final Path FOO_DIR = Paths.get("foo");49private static final Path BAR_DIR = Paths.get("bar");5051@BeforeTest52private void setup() throws Exception {53Files.createDirectory(BAR_DIR);5455Path pkgDir = Paths.get("p");56// compile classes in package p57assertTrue(CompilerUtils.compile(SRC_DIR.resolve(pkgDir), BAR_DIR));5859// move p.Foo to a different directory60Path foo = pkgDir.resolve("Foo.class");61Files.createDirectories(FOO_DIR.resolve(pkgDir));62Files.move(BAR_DIR.resolve(foo), FOO_DIR.resolve(foo),63StandardCopyOption.REPLACE_EXISTING);64}6566@Test67public void test() throws Exception {68URLClassLoader loader1 = new URLClassLoader(new URL[] {69FOO_DIR.toUri().toURL()70});71Loader loader2 = new Loader(new URL[] {72BAR_DIR.toUri().toURL()73}, loader1);7475Class<?> foo = Class.forName("p.Foo", true, loader2);76Class<?> bar = Class.forName("p.Bar", true, loader2);77Class<?> baz = Class.forName("p.Baz", true, loader2);7879Package pForFoo = loader1.getDefinedPackage("p");80Package pForBar = loader2.getDefinedPackage("p");8182assertEquals(pForFoo.getName(), pForBar.getName());83assertTrue(pForFoo != pForBar);8485try {86loader2.defineSplitPackage("p");87} catch (IllegalArgumentException e) {8889}90}9192static class Loader extends URLClassLoader {93Loader(URL[] urls, URLClassLoader parent) {94super(urls, parent);95}9697public Package defineSplitPackage(String name) {98Manifest manifest = new Manifest();99return super.definePackage(name, manifest, null);100}101}102}103104105106