Path: blob/master/test/jdk/java/lang/Package/GetPackages.java
41149 views
/*1* Copyright (c) 2015, 2020, 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* @summary Test Package object is local to each ClassLoader.26* There can be one Package object of "foo" name defined by27* different class loader.28* @compile Foo.java29* @run testng GetPackages30*/3132import java.io.IOException;33import java.io.UncheckedIOException;34import java.nio.file.Files;35import java.nio.file.Path;36import java.nio.file.Paths;37import java.util.Arrays;38import java.lang.reflect.*;3940import org.testng.annotations.DataProvider;41import org.testng.annotations.Test;42import static org.testng.Assert.*;4344public class GetPackages {45final TestClassLoader loader;46final Class<?> fooClass;47/*48* Each TestClassLoader defines a "foo.Foo" class which has a unique49* Package object of "foo" regardless whether its ancestor class loader50* defines a package "foo" or not.51*/52GetPackages(TestClassLoader loader) throws ClassNotFoundException {53this.loader = loader;54this.fooClass = loader.loadClass("foo.Foo");55}5657/** For TestNG */58public GetPackages() {59loader = null;60fooClass = null;61}6263/*64* Check package "foo" defined locally in the TestClassLoader65* as well as its ancestors.66*/67void checkPackage() throws ClassNotFoundException {68// Name of an unnamed package is empty string69assertEquals(this.getClass().getPackage().getName(), "");7071assertEquals(fooClass.getClassLoader(), loader);7273Package p = loader.getDefinedPackage("foo");74assertEquals(p.getName(), "foo");75assertEquals(p, loader.getPackage("foo"));7677if (loader.getParent() != null) {78Package p2 = ((TestClassLoader)loader.getParent()).getDefinedPackage("foo");79assertTrue(p != p2);80}8182long count = Arrays.stream(loader.getDefinedPackages())83.map(Package::getName)84.filter(pn -> pn.equals("foo"))85.count();86assertEquals(count, 1);87}8889/*90* Check the number of package "foo" defined to this class loader and91* its ancestors92*/93Package[] getPackagesFromLoader() {94return loader.packagesInClassLoaderChain();95}9697/*98* Package.getPackages is caller-sensitve. Call through Foo class99* to find all Packages visible to this TestClassLoader and its ancestors100*/101Package[] getPackagesFromFoo() throws Exception {102Method m = fooClass.getMethod("getPackages");103return (Package[])m.invoke(null);104}105106private static long numFooPackages(Package[] pkgs) throws Exception {107return Arrays.stream(pkgs)108.filter(p -> p.getName().equals("foo"))109.count();110}111112@DataProvider(name = "loaders")113public static Object[][] testLoaders() {114TestClassLoader loader1 = new TestClassLoader(null);115TestClassLoader loader2 = new TestClassLoader(loader1);116TestClassLoader loader3 = new TestClassLoader(loader2);117118// Verify the number of expected Package object of "foo" visible119// to the class loader120return new Object[][] {121{ loader1, 1 },122{ loader2, 2 },123{ loader3, 3 }124};125}126127@Test(dataProvider = "loaders")128public static void test(TestClassLoader loader, long expected) throws Exception {129GetPackages test = new GetPackages(loader);130// check package "foo" existence131test.checkPackage();132133assertEquals(numFooPackages(test.getPackagesFromLoader()), expected);134assertEquals(numFooPackages(test.getPackagesFromFoo()), expected);135}136}137138class TestClassLoader extends ClassLoader {139public TestClassLoader() {140super();141}142143public TestClassLoader(ClassLoader parent) {144super(parent);145}146147public Package getPackage(String pn) {148return super.getPackage(pn);149}150151public Package[] packagesInClassLoaderChain() {152return super.getPackages();153}154155@Override156protected Class<?> findClass(String name) throws ClassNotFoundException {157Path p = Paths.get(System.getProperty("test.classes", "."));158159try {160byte[] bb = Files.readAllBytes(p.resolve("foo/Foo.class"));161return defineClass(name, bb, 0, bb.length);162} catch (IOException e) {163throw new UncheckedIOException(e);164}165}166@Override167protected Class<?> loadClass(String cn, boolean resolve) throws ClassNotFoundException {168if (!cn.equals("foo.Foo"))169return super.loadClass(cn, resolve);170return findClass(cn);171}172}173174175