1/*
2 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @summary Basic test for Class.getPackage
27 * @compile Foo.java
28 * @run testng GetPackageTest
29 */
30
31import org.testng.annotations.BeforeTest;
32import org.testng.annotations.DataProvider;
33import org.testng.annotations.Test;
34
35import java.io.IOException;
36import java.io.UncheckedIOException;
37import java.math.BigInteger;
38import java.nio.file.Files;
39import java.nio.file.Path;
40import java.nio.file.Paths;
41import java.util.Properties;
42
43import static org.testng.Assert.*;
44
45public class GetPackageTest {
46    private static Class<?> fooClass; // definePackage is not called for Foo class
47
48    @BeforeTest
49    public static void loadFooClass() throws ClassNotFoundException {
50        TestClassLoader loader = new TestClassLoader();
51        fooClass = loader.loadClass("foo.Foo");
52        assertEquals(fooClass.getClassLoader(), loader);
53    }
54
55    @DataProvider(name = "testClasses")
56    public Object[][] testClasses() {
57        return new Object[][] {
58                // primitive type, void, array types
59                { int.class,            null },
60                { long[].class,         null },
61                { Object[][].class,     null },
62                { void.class,           null },
63
64                // unnamed package
65                { GetPackageTest.class, "" },
66
67                // named package
68                { fooClass,             "foo" },
69                { Object.class,         "java.lang" },
70                { Properties.class,     "java.util" },
71                { BigInteger.class,     "java.math" },
72                { Test.class,           "org.testng.annotations" },
73        };
74    }
75
76    @Test(dataProvider = "testClasses")
77    public void testGetPackage(Class<?> type, String expected) {
78        Package p = type.getPackage();
79        if (expected == null) {
80            assertTrue(p == null);
81        } else {
82            assertEquals(p.getName(), expected);
83        }
84    }
85
86    static class TestClassLoader extends ClassLoader {
87        public TestClassLoader() {
88            super();
89        }
90
91        public TestClassLoader(ClassLoader parent) {
92            super(parent);
93        }
94
95        @Override
96        protected Class<?> findClass(String name) throws ClassNotFoundException {
97            Path p = Paths.get(System.getProperty("test.classes", "."));
98
99            try {
100                byte[] bb = Files.readAllBytes(p.resolve("foo/Foo.class"));
101                return defineClass(name, bb, 0, bb.length);
102            } catch (IOException e) {
103                throw new UncheckedIOException(e);
104            }
105        }
106        @Override
107        protected Class<?> loadClass(String cn, boolean resolve) throws ClassNotFoundException {
108            if (!cn.equals("foo.Foo"))
109                return super.loadClass(cn, resolve);
110            return findClass(cn);
111        }
112
113    }
114}
115
116
117