SystemModulesTest.java revision 14667:1003b17156f3
1193323Sed/*
2193323Sed * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3193323Sed * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4193323Sed *
5193323Sed * This code is free software; you can redistribute it and/or modify it
6193323Sed * under the terms of the GNU General Public License version 2 only, as
7193323Sed * published by the Free Software Foundation.
8193323Sed *
9193323Sed * This code is distributed in the hope that it will be useful, but WITHOUT
10193323Sed * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11193323Sed * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12193323Sed * version 2 for more details (a copy is included in the LICENSE file that
13193323Sed * accompanied this code).
14193323Sed *
15193323Sed * You should have received a copy of the GNU General Public License version
16193323Sed * 2 along with this work; if not, write to the Free Software Foundation,
17193323Sed * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18218893Sdim *
19193323Sed * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20193323Sed * or visit www.oracle.com if you need additional information or have any
21193323Sed * questions.
22224145Sdim */
23193323Sed
24198090Srdivackyimport java.lang.module.ModuleDescriptor;
25193323Sedimport java.lang.module.ModuleDescriptor.Requires.Modifier;
26193323Sedimport java.lang.module.ModuleFinder;
27193323Sedimport java.lang.module.ModuleReference;
28224145Sdimimport java.util.Collections;
29226633Sdimimport java.util.EnumSet;
30224145Sdimimport java.util.Map;
31224145Sdimimport java.util.Set;
32224145Sdim
33224145Sdimimport jdk.internal.misc.JavaLangModuleAccess;
34234353Sdimimport jdk.internal.misc.SharedSecrets;
35234353Sdimimport org.testng.annotations.Test;
36193323Sedimport static org.testng.Assert.*;
37193323Sed
38193323Sed/**
39193323Sed * @test
40193323Sed * @modules java.base/jdk.internal.misc
41193323Sed * @run testng SystemModulesTest
42224145Sdim * @summary Verify the properties of ModuleDescriptor created
43224145Sdim *          by SystemModules
44224145Sdim */
45193323Sed
46193323Sedpublic class SystemModulesTest {
47193323Sed    private static final JavaLangModuleAccess jlma = SharedSecrets.getJavaLangModuleAccess();
48193323Sed
49234353Sdim    /**
50234353Sdim     * Verify ModuleDescriptor contains unmodifiable sets
51234353Sdim     */
52234353Sdim    @Test
53234353Sdim    public void testUnmodifableDescriptors() throws Exception {
54234353Sdim        ModuleFinder.ofSystem().findAll()
55234353Sdim                    .stream()
56218893Sdim                    .map(ModuleReference::descriptor)
57224145Sdim                    .forEach(this::testModuleDescriptor);
58218893Sdim    }
59218893Sdim
60218893Sdim    private void testModuleDescriptor(ModuleDescriptor md) {
61218893Sdim        assertUnmodifiable(md.conceals(), "conceal");
62193323Sed        assertUnmodifiable(md.packages(), "package");
63193323Sed        assertUnmodifiable(md.requires(),
64193323Sed                           jlma.newRequires(EnumSet.allOf(Modifier.class), "require"));
65218893Sdim        assertUnmodifiable(md.exports(), jlma.newExports("export"));
66193323Sed        assertUnmodifiable(md.uses(), "use");
67239462Sdim        assertUnmodifiable(md.provides(), "provide",
68239462Sdim                           jlma.newProvides("provide", Collections.singleton("provide")));
69239462Sdim
70239462Sdim    }
71239462Sdim
72239462Sdim    private <T> void assertUnmodifiable(Set<T> set, T dummy) {
73239462Sdim        try {
74239462Sdim            set.add(dummy);
75239462Sdim            fail("Should throw UnsupportedOperationException");
76239462Sdim        } catch (UnsupportedOperationException e) {
77239462Sdim            // pass
78239462Sdim        } catch (Exception e) {
79239462Sdim            fail("Should throw UnsupportedOperationException");
80239462Sdim        }
81239462Sdim    }
82239462Sdim
83239462Sdim    private <T, V> void assertUnmodifiable(Map<T, V> set, T dummyKey, V dummyValue) {
84239462Sdim        try {
85218893Sdim            set.put(dummyKey, dummyValue);
86239462Sdim            fail("Should throw UnsupportedOperationException");
87239462Sdim        } catch (UnsupportedOperationException e) {
88193323Sed            // pass
89193323Sed        } catch (Exception e) {
90193323Sed            fail("Should throw UnsupportedOperationException");
91193323Sed        }
92193323Sed    }
93193323Sed
94198090Srdivacky}
95198090Srdivacky