Path: blob/master/test/jdk/java/lang/Class/GetPackageTest.java
41149 views
/*1* Copyright (c) 2015, 2016, 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 Basic test for Class.getPackage26* @compile Foo.java27* @run testng GetPackageTest28*/2930import org.testng.annotations.BeforeTest;31import org.testng.annotations.DataProvider;32import org.testng.annotations.Test;3334import java.io.IOException;35import java.io.UncheckedIOException;36import java.math.BigInteger;37import java.nio.file.Files;38import java.nio.file.Path;39import java.nio.file.Paths;40import java.util.Properties;4142import static org.testng.Assert.*;4344public class GetPackageTest {45private static Class<?> fooClass; // definePackage is not called for Foo class4647@BeforeTest48public static void loadFooClass() throws ClassNotFoundException {49TestClassLoader loader = new TestClassLoader();50fooClass = loader.loadClass("foo.Foo");51assertEquals(fooClass.getClassLoader(), loader);52}5354@DataProvider(name = "testClasses")55public Object[][] testClasses() {56return new Object[][] {57// primitive type, void, array types58{ int.class, null },59{ long[].class, null },60{ Object[][].class, null },61{ void.class, null },6263// unnamed package64{ GetPackageTest.class, "" },6566// named package67{ fooClass, "foo" },68{ Object.class, "java.lang" },69{ Properties.class, "java.util" },70{ BigInteger.class, "java.math" },71{ Test.class, "org.testng.annotations" },72};73}7475@Test(dataProvider = "testClasses")76public void testGetPackage(Class<?> type, String expected) {77Package p = type.getPackage();78if (expected == null) {79assertTrue(p == null);80} else {81assertEquals(p.getName(), expected);82}83}8485static class TestClassLoader extends ClassLoader {86public TestClassLoader() {87super();88}8990public TestClassLoader(ClassLoader parent) {91super(parent);92}9394@Override95protected Class<?> findClass(String name) throws ClassNotFoundException {96Path p = Paths.get(System.getProperty("test.classes", "."));9798try {99byte[] bb = Files.readAllBytes(p.resolve("foo/Foo.class"));100return defineClass(name, bb, 0, bb.length);101} catch (IOException e) {102throw new UncheckedIOException(e);103}104}105@Override106protected Class<?> loadClass(String cn, boolean resolve) throws ClassNotFoundException {107if (!cn.equals("foo.Foo"))108return super.loadClass(cn, resolve);109return findClass(cn);110}111112}113}114115116117118