Path: blob/master/test/jdk/java/lang/module/Packages/GetPackagesTest.java
41152 views
/*1* Copyright (c) 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* @library /test/lib26* @build m/*27* @run main/othervm --add-modules m test.GetPackagesTest28* @summary test the packages returned by Module::getPackages for an unnamed29* module does not include the packages for named modules30*/3132package test;3334import java.util.Set;35import static jdk.test.lib.Asserts.*;3637public class GetPackagesTest {38public static void main(String... args) throws Exception {39// module m contains the package "p"40Class<?> c = Class.forName("p.Main");41Module m = c.getModule();42Module test = GetPackagesTest.class.getModule();4344// module m and unnamed module are defined by the same class loader45assertTrue(m.isNamed());46assertFalse(test.isNamed());47assertTrue(m.getClassLoader() == test.getClassLoader());4849// verify Module::getPackages on an unnamed module only contains50// the packages defined to the unnamed module51assertEquals(m.getPackages(), Set.of("p"));5253Set<String> pkgs = test.getPackages();54assertTrue(pkgs.contains("test"));55assertFalse(pkgs.contains("p"));56}57}585960