/* * Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code 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 General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/* * @test * @summary Test Package object is local to each ClassLoader. * There can be one Package object of "foo" name defined by * different class loader. * @compile Foo.java * @run testng GetPackages
*/
publicclass GetPackages { final TestClassLoader loader; finalClass<?> fooClass; /* * Each TestClassLoader defines a "foo.Foo" class which has a unique * Package object of "foo" regardless whether its ancestor class loader * defines a package "foo" or not.
*/
GetPackages(TestClassLoader loader) throws ClassNotFoundException { this.loader = loader; this.fooClass = loader.loadClass("foo.Foo");
}
/** For TestNG */ public GetPackages() {
loader = null;
fooClass = null;
}
/* * Check package "foo" defined locally in the TestClassLoader * as well as its ancestors.
*/ void checkPackage() throws ClassNotFoundException { // Name of an unnamed package is empty string
assertEquals(this.getClass().getPackage().getName(), "");
assertEquals(fooClass.getClassLoader(), loader);
Package p = loader.getDefinedPackage("foo");
assertEquals(p.getName(), "foo");
assertEquals(p, loader.getPackage("foo"));
/* * Check the number of package "foo" defined to this class loader and * its ancestors
*/ Package[] getPackagesFromLoader() { return loader.packagesInClassLoaderChain();
}
/* * Package.getPackages is caller-sensitve. Call through Foo class * to find all Packages visible to this TestClassLoader and its ancestors
*/ Package[] getPackagesFromFoo() throws Exception {
Method m = fooClass.getMethod("getPackages"); return (Package[])m.invoke(null);
}
@DataProvider(name = "loaders") publicstatic Object[][] testLoaders() {
TestClassLoader loader1 = new TestClassLoader(null);
TestClassLoader loader2 = new TestClassLoader(loader1);
TestClassLoader loader3 = new TestClassLoader(loader2);
// Verify the number of expected Package object of "foo" visible // to the class loader returnnew Object[][] {
{ loader1, 1 },
{ loader2, 2 },
{ loader3, 3 }
};
}
@Test(dataProvider = "loaders") publicstaticvoid test(TestClassLoader loader, long expected) throws Exception {
GetPackages test = new GetPackages(loader); // check package "foo" existence
test.checkPackage();
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.