jdk/test/tools/launcher/modules/addexports/manifest/AddExportsAndOpensInManifest.java
changeset 42338 a60f280f803c
child 45652 33342314ce89
equal deleted inserted replaced
42148:7a4a59859ac0 42338:a60f280f803c
       
     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 /**
       
    25  * @test
       
    26  * @library /lib/testlibrary
       
    27  * @modules jdk.compiler
       
    28  * @build AddExportsAndOpensInManifest Test2 JarUtils jdk.testlibrary.*
       
    29  * @compile --add-exports=java.base/jdk.internal.misc=ALL-UNNAMED Test1.java
       
    30  * @run testng AddExportsAndOpensInManifest
       
    31  * @summary Basic test for Add-Exports and Add-Opens attributes in the
       
    32  *          manifest of a main application JAR
       
    33  */
       
    34 
       
    35 import java.nio.file.Files;
       
    36 import java.nio.file.Path;
       
    37 import java.nio.file.Paths;
       
    38 import java.util.jar.Attributes;
       
    39 import java.util.jar.Manifest;
       
    40 
       
    41 import jdk.testlibrary.OutputAnalyzer;
       
    42 import jdk.testlibrary.ProcessTools;
       
    43 
       
    44 import org.testng.annotations.Test;
       
    45 import static org.testng.Assert.*;
       
    46 
       
    47 
       
    48 @Test
       
    49 public class AddExportsAndOpensInManifest {
       
    50 
       
    51     /**
       
    52      * Package Test1 and Test2 into a JAR file with the given attributes
       
    53      * in the JAR manifest, then execute the JAR file with `java -jar`.
       
    54      */
       
    55     private OutputAnalyzer runTest(String attributes) throws Exception {
       
    56         Manifest man = new Manifest();
       
    57         Attributes attrs = man.getMainAttributes();
       
    58         attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0");
       
    59 
       
    60         for (String nameAndValue : attributes.split(",")) {
       
    61             String[] s = nameAndValue.split("=");
       
    62             if (s.length != 2)
       
    63                 throw new RuntimeException("Malformed: " + nameAndValue);
       
    64             String name = s[0];
       
    65             String value = s[1];
       
    66             attrs.put(new Attributes.Name(name), value);
       
    67         }
       
    68 
       
    69         // create the JAR file with Test1 and Test2
       
    70         Path jarfile = Paths.get("test.jar");
       
    71         Files.deleteIfExists(jarfile);
       
    72 
       
    73         Path classes = Paths.get(System.getProperty("test.classes", ""));
       
    74         JarUtils.createJarFile(jarfile, man, classes,
       
    75                 Paths.get("Test1.class"), Paths.get("Test2.class"));
       
    76 
       
    77         // java -jar test.jar
       
    78         return ProcessTools.executeTestJava("-jar", jarfile.toString())
       
    79                 .outputTo(System.out)
       
    80                 .errorTo(System.out);
       
    81     }
       
    82 
       
    83     /**
       
    84      * Run test with the given JAR attributes, expecting the test to pass
       
    85      */
       
    86     private void runExpectingPass(String attrs) throws Exception {
       
    87         int exitValue = runTest(attrs).getExitValue();
       
    88         assertTrue(exitValue == 0);
       
    89     }
       
    90 
       
    91     /**
       
    92      * Run test with the given JAR attributes, expecting the test to fail
       
    93      * with at least the given output
       
    94      */
       
    95     private void runExpectingFail(String attrs, String errorString) throws Exception {
       
    96         int exitValue = runTest(attrs).shouldContain(errorString).getExitValue();
       
    97         assertTrue(exitValue != 0);
       
    98     }
       
    99 
       
   100 
       
   101     /**
       
   102      * Run tests to make sure that they fail in the expected way.
       
   103      */
       
   104     public void testSanity() throws Exception {
       
   105         runExpectingFail("Main-Class=Test1", "IllegalAccessError");
       
   106         runExpectingFail("Main-Class=Test2", "InaccessibleObjectException");
       
   107     }
       
   108 
       
   109     /**
       
   110      * Run tests with the Add-Exports attribute in the main manifest.
       
   111      */
       
   112     public void testWithAddExports() throws Exception {
       
   113         runExpectingPass("Main-Class=Test1,Add-Exports=java.base/jdk.internal.misc");
       
   114         runExpectingFail("Main-Class=Test2,Add-Exports=java.base/jdk.internal.misc",
       
   115                          "InaccessibleObjectException");
       
   116 
       
   117         // run with leading and trailing spaces
       
   118         runExpectingPass("Main-Class=Test1,Add-Exports=  java.base/jdk.internal.misc");
       
   119         runExpectingPass("Main-Class=Test1,Add-Exports=java.base/jdk.internal.misc  ");
       
   120 
       
   121         // run with multiple values
       
   122         runExpectingPass("Main-Class=Test1,Add-Exports=java.base/jdk.internal.misc"
       
   123                 + " java.base/jdk.internal.loader");
       
   124         runExpectingPass("Main-Class=Test1,Add-Exports=java.base/jdk.internal.loader"
       
   125                 + " java.base/jdk.internal.misc");
       
   126 
       
   127         // run with duplicate values
       
   128         runExpectingPass("Main-Class=Test1,Add-Exports=java.base/jdk.internal.misc"
       
   129                 + " java.base/jdk.internal.misc");
       
   130     }
       
   131 
       
   132     /**
       
   133      * Run tests with the Add-Opens attribute in the main manifest.
       
   134      */
       
   135     public void testWithAddOpens() throws Exception {
       
   136         runExpectingPass("Main-Class=Test1,Add-Opens=java.base/jdk.internal.misc");
       
   137         runExpectingPass("Main-Class=Test2,Add-Opens=java.base/jdk.internal.misc");
       
   138 
       
   139         // run with leading and trailing spaces
       
   140         runExpectingPass("Main-Class=Test1,Add-Opens=  java.base/jdk.internal.misc");
       
   141         runExpectingPass("Main-Class=Test1,Add-Opens=java.base/jdk.internal.misc  ");
       
   142 
       
   143         // run with multiple values
       
   144         runExpectingPass("Main-Class=Test1,Add-Opens=java.base/jdk.internal.misc"
       
   145                 + " java.base/jdk.internal.loader");
       
   146         runExpectingPass("Main-Class=Test1,Add-Opens=java.base/jdk.internal.loader"
       
   147                 + " java.base/jdk.internal.misc");
       
   148 
       
   149         // run with duplicate values
       
   150         runExpectingPass("Main-Class=Test1,Add-Opens=java.base/jdk.internal.misc"
       
   151                 + " java.base/jdk.internal.misc");
       
   152     }
       
   153 
       
   154     /**
       
   155      * Run tests a bad module or package name
       
   156      */
       
   157     public void testWithBadModuleOrPackage() throws Exception {
       
   158         // Add-Exports with bad module name
       
   159         String attrs = "Main-Class=Test1,Add-Exports=java.DoesNotExist/jdk.internal.misc";
       
   160         runExpectingFail(attrs, "IllegalAccessError");
       
   161 
       
   162         // Add-Exports with bad package name
       
   163         attrs = "Main-Class=Test1,Add-Exports=java.base/jdk.internal.DoesNotExit";
       
   164         runExpectingFail(attrs, "IllegalAccessError");
       
   165 
       
   166         // Add-Opens with bad module name
       
   167         attrs = "Main-Class=Test1,Add-Opens=java.DoesNotExist/jdk.internal.misc";
       
   168         runExpectingFail(attrs, "IllegalAccessError");
       
   169 
       
   170         // Add-Opens with bad package name
       
   171         attrs = "Main-Class=Test1,Add-Opens=java.base/jdk.internal.DoesNotExit";
       
   172         runExpectingFail(attrs, "IllegalAccessError");
       
   173     }
       
   174 
       
   175 }