Path: blob/master/test/jdk/tools/jlink/plugins/SystemModuleDescriptors/SystemModulesTest.java
41153 views
/*1* Copyright (c) 2016, 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*/2223import java.lang.module.ModuleDescriptor;24import java.lang.module.ModuleDescriptor.*;25import java.lang.module.ModuleFinder;26import java.lang.module.ModuleReference;27import java.io.IOException;28import java.io.UncheckedIOException;29import java.nio.file.Files;30import java.nio.file.Path;31import java.nio.file.Paths;32import java.util.List;33import java.util.Map;34import java.util.Set;3536import jdk.internal.access.JavaLangModuleAccess;37import jdk.internal.access.SharedSecrets;38import org.testng.annotations.Test;39import static org.testng.Assert.*;4041/**42* @test43* @bug 8142968 817338144* @modules java.base/jdk.internal.access45* @modules java.base/jdk.internal.module46* @modules java.base/jdk.internal.org.objectweb.asm47* @build ModuleTargetHelper48* @run testng SystemModulesTest49* @summary Verify the properties of ModuleDescriptor created50* by SystemModules51*/5253public class SystemModulesTest {54private static final JavaLangModuleAccess JLMA =55SharedSecrets.getJavaLangModuleAccess();56private static final String OS_NAME = System.getProperty("os.name");57private static final String OS_ARCH = System.getProperty("os.arch");58// system modules containing no package59private static final Set<String> EMPTY_MODULES =60Set.of("java.se", "jdk.jdwp.agent", "jdk.pack");6162@Test63public void testSystemModules() {64Path jimage = Paths.get(System.getProperty("java.home"), "lib", "modules");65if (Files.notExists(jimage))66return;6768ModuleFinder.ofSystem().findAll().stream()69.forEach(this::checkAttributes);70}7172// JMOD files are created with OS name and arch matching the bundle name73private boolean checkOSName(String name) {74if (OS_NAME.startsWith("Windows")) {75return name.equals("windows");76}7778switch (OS_NAME) {79case "Linux":80return name.equals("linux");81case "Mac OS X":82return name.equals("macos");83default:84// skip validation on unknown platform85System.out.println("Skip checking OS name in ModuleTarget: " + name);86return true;87}88}8990private boolean checkOSArch(String name) {91if (name.equals(OS_ARCH))92return true;9394switch (OS_ARCH) {95case "i386":96case "x86":97return name.equals("x86");98case "amd64":99case "x86_64":100return name.equals("amd64");101default:102// skip validation on unknown platform103System.out.println("Skip checking OS arch in ModuleTarget: " + name);104return true;105}106}107108private void checkAttributes(ModuleReference modRef) {109try {110ModuleTargetHelper.ModuleTarget mt = ModuleTargetHelper.read(modRef);111String[] values = mt.targetPlatform().split("-");112assertTrue(checkOSName(values[0]));113assertTrue(checkOSArch(values[1]));114} catch (IOException exp) {115throw new UncheckedIOException(exp);116}117}118119/**120* Verify ModuleDescriptor contains unmodifiable sets121*/122@Test123public void testUnmodifableDescriptors() throws Exception {124ModuleFinder.ofSystem().findAll()125.stream()126.map(ModuleReference::descriptor)127.forEach(this::testModuleDescriptor);128}129130private void testModuleDescriptor(ModuleDescriptor md) {131assertUnmodifiable(md.packages(), "package");132assertUnmodifiable(md.requires(),133JLMA.newRequires(Set.of(Requires.Modifier.TRANSITIVE),134"require", null));135for (Requires req : md.requires()) {136assertUnmodifiable(req.modifiers(), Requires.Modifier.TRANSITIVE);137}138139assertUnmodifiable(md.exports(), JLMA.newExports(Set.of(), "export", Set.of()));140for (Exports exp : md.exports()) {141assertUnmodifiable(exp.modifiers(), Exports.Modifier.SYNTHETIC);142assertUnmodifiable(exp.targets(), "target");143}144145assertUnmodifiable(md.opens(), JLMA.newOpens(Set.of(), "open", Set.of()));146for (Opens opens : md.opens()) {147assertUnmodifiable(opens.modifiers(), Opens.Modifier.SYNTHETIC);148assertUnmodifiable(opens.targets(), "target");149}150151assertUnmodifiable(md.uses(), "use");152153assertUnmodifiable(md.provides(),154JLMA.newProvides("provide", List.of("provide")));155for (Provides provides : md.provides()) {156assertUnmodifiable(provides.providers(), "provide");157}158159}160161private <T> void assertUnmodifiable(Set<T> set, T dummy) {162try {163set.add(dummy);164fail("Should throw UnsupportedOperationException");165} catch (UnsupportedOperationException e) {166// pass167} catch (Exception e) {168fail("Should throw UnsupportedOperationException");169}170}171172private <T> void assertUnmodifiable(List<T> list, T dummy) {173try {174list.add(dummy);175fail("Should throw UnsupportedOperationException");176} catch (UnsupportedOperationException e) {177// pass178} catch (Exception e) {179fail("Should throw UnsupportedOperationException");180}181}182183private <T, V> void assertUnmodifiable(Map<T, V> set, T dummyKey, V dummyValue) {184try {185set.put(dummyKey, dummyValue);186fail("Should throw UnsupportedOperationException");187} catch (UnsupportedOperationException e) {188// pass189} catch (Exception e) {190fail("Should throw UnsupportedOperationException");191}192}193194}195196197