jdk/test/tools/jlink/plugins/InstalledModuleDescriptors/InstalledModulesTest.java
changeset 38766 12239516ceb1
parent 38765 b64f3ec3420e
child 38767 a0240b701a5a
equal deleted inserted replaced
38765:b64f3ec3420e 38766:12239516ceb1
     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.Requires.Modifier;
       
    26 import java.lang.module.ModuleFinder;
       
    27 import java.lang.module.ModuleReference;
       
    28 import java.util.Collections;
       
    29 import java.util.EnumSet;
       
    30 import java.util.Map;
       
    31 import java.util.Set;
       
    32 
       
    33 import jdk.internal.misc.JavaLangModuleAccess;
       
    34 import jdk.internal.misc.SharedSecrets;
       
    35 import org.testng.annotations.Test;
       
    36 import static org.testng.Assert.*;
       
    37 
       
    38 /**
       
    39  * @test
       
    40  * @modules java.base/jdk.internal.misc
       
    41  * @run testng InstalledModulesTest
       
    42  * @summary Verify the properties of ModuleDescriptor created
       
    43  *          by InstalledModules
       
    44  */
       
    45 
       
    46 public class InstalledModulesTest {
       
    47     private static final JavaLangModuleAccess jlma = SharedSecrets.getJavaLangModuleAccess();
       
    48 
       
    49     /**
       
    50      * Verify ModuleDescriptor contains unmodifiable sets
       
    51      */
       
    52     @Test
       
    53     public void testUnmodifableDescriptors() throws Exception {
       
    54         ModuleFinder.ofSystem().findAll()
       
    55                     .stream()
       
    56                     .map(ModuleReference::descriptor)
       
    57                     .forEach(this::testModuleDescriptor);
       
    58     }
       
    59 
       
    60     private void testModuleDescriptor(ModuleDescriptor md) {
       
    61         assertUnmodifiable(md.conceals(), "conceal");
       
    62         assertUnmodifiable(md.packages(), "package");
       
    63         assertUnmodifiable(md.requires(),
       
    64                            jlma.newRequires(EnumSet.allOf(Modifier.class), "require"));
       
    65         assertUnmodifiable(md.exports(), jlma.newExports("export"));
       
    66         assertUnmodifiable(md.uses(), "use");
       
    67         assertUnmodifiable(md.provides(), "provide",
       
    68                            jlma.newProvides("provide", Collections.singleton("provide")));
       
    69 
       
    70     }
       
    71 
       
    72     private <T> void assertUnmodifiable(Set<T> set, T dummy) {
       
    73         try {
       
    74             set.add(dummy);
       
    75             fail("Should throw UnsupportedOperationException");
       
    76         } catch (UnsupportedOperationException e) {
       
    77             // pass
       
    78         } catch (Exception e) {
       
    79             fail("Should throw UnsupportedOperationException");
       
    80         }
       
    81     }
       
    82 
       
    83     private <T, V> void assertUnmodifiable(Map<T, V> set, T dummyKey, V dummyValue) {
       
    84         try {
       
    85             set.put(dummyKey, dummyValue);
       
    86             fail("Should throw UnsupportedOperationException");
       
    87         } catch (UnsupportedOperationException e) {
       
    88             // pass
       
    89         } catch (Exception e) {
       
    90             fail("Should throw UnsupportedOperationException");
       
    91         }
       
    92     }
       
    93 
       
    94 }