1 /* 2 * Copyright (c) 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 import java.lang.module.ModuleDescriptor; 25 import java.lang.module.ModuleDescriptor.*; 26 import java.lang.module.ModuleFinder; 27 import java.lang.module.ModuleReference; 28 import java.util.List; 29 import java.util.Map; 30 import java.util.Set; 31 32 import jdk.internal.misc.JavaLangModuleAccess; 33 import jdk.internal.misc.SharedSecrets; 34 import org.testng.annotations.Test; 35 import static org.testng.Assert.*; 36 37 /** 38 * @test 39 * @modules java.base/jdk.internal.misc 40 * @run testng SystemModulesTest 41 * @summary Verify the properties of ModuleDescriptor created 42 * by SystemModules 43 */ 44 45 public class SystemModulesTest { 46 private static final JavaLangModuleAccess jlma = SharedSecrets.getJavaLangModuleAccess(); 47 48 /** 49 * Verify ModuleDescriptor contains unmodifiable sets 50 */ 51 @Test 52 public void testUnmodifableDescriptors() throws Exception { 53 ModuleFinder.ofSystem().findAll() 54 .stream() 55 .map(ModuleReference::descriptor) 56 .forEach(this::testModuleDescriptor); 57 } 58 59 private void testModuleDescriptor(ModuleDescriptor md) { 60 assertUnmodifiable(md.packages(), "package"); 61 assertUnmodifiable(md.requires(), 62 jlma.newRequires(Set.of(Requires.Modifier.TRANSITIVE), "require", null)); 63 for (Requires req : md.requires()) { 64 assertUnmodifiable(req.modifiers(), Requires.Modifier.TRANSITIVE); 65 } 66 67 assertUnmodifiable(md.exports(), jlma.newExports(Set.of(), "export", Set.of())); 68 for (Exports exp : md.exports()) { 69 assertUnmodifiable(exp.modifiers(), Exports.Modifier.SYNTHETIC); 70 assertUnmodifiable(exp.targets(), "target"); 71 } 72 73 assertUnmodifiable(md.opens(), jlma.newOpens(Set.of(), "open", Set.of())); 74 for (Opens opens : md.opens()) { 75 assertUnmodifiable(opens.modifiers(), Opens.Modifier.SYNTHETIC); 76 assertUnmodifiable(opens.targets(), "target"); 77 } 78 79 assertUnmodifiable(md.uses(), "use"); 80 81 assertUnmodifiable(md.provides(), 82 jlma.newProvides("provide", List.of("provide"))); 83 for (Provides provides : md.provides()) { 84 assertUnmodifiable(provides.providers(), "provide"); 85 } 86 87 } 88 89 private <T> void assertUnmodifiable(Set<T> set, T dummy) { 90 try { 91 set.add(dummy); 92 fail("Should throw UnsupportedOperationException"); 93 } catch (UnsupportedOperationException e) { 94 // pass 95 } catch (Exception e) { 96 fail("Should throw UnsupportedOperationException"); 97 } 98 } 99 100 private <T> void assertUnmodifiable(List<T> list, T dummy) { 101 try { 102 list.add(dummy); 103 fail("Should throw UnsupportedOperationException"); 104 } catch (UnsupportedOperationException e) { 105 // pass 106 } catch (Exception e) { 107 fail("Should throw UnsupportedOperationException"); 108 } 109 } 110 111 private <T, V> void assertUnmodifiable(Map<T, V> set, T dummyKey, V dummyValue) { 112 try { 113 set.put(dummyKey, dummyValue); 114 fail("Should throw UnsupportedOperationException"); 115 } catch (UnsupportedOperationException e) { 116 // pass 117 } catch (Exception e) { 118 fail("Should throw UnsupportedOperationException"); 119 } 120 } 121 122 }