test/hotspot/jtreg/runtime/cds/appcds/jigsaw/classpathtests/ClassPathTests.java
changeset 57567 b000362a89a0
parent 51992 266a89a5d1af
child 57705 7cf02b2c1455
equal deleted inserted replaced
57566:ad84ae073248 57567:b000362a89a0
       
     1 /*
       
     2  * Copyright (c) 2015, 2019, 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 /**
       
    26  * @test
       
    27  * @requires vm.cds & !vm.graal.enabled
       
    28  * @library ../..
       
    29  * @library /test/lib
       
    30  * @modules java.compiler
       
    31  *          java.base/jdk.internal.misc
       
    32  *          jdk.jartool/sun.tools.jar
       
    33  * @compile src/jdk/test/Main.java
       
    34  * @compile src/com/sun/tools/javac/Main.jasm
       
    35  * @compile src/com/sun/tools/javac/MyMain.jasm
       
    36  * @compile ../../../javax/annotation/processing/FilerException.jasm
       
    37  * @run driver ClassPathTests
       
    38  * @summary AppCDS tests for testing classpath/package conflicts
       
    39  */
       
    40 
       
    41 /*
       
    42  * These tests will verify that AppCDS will correctly handle archived classes
       
    43  * on the classpath that are in a package that is also exported by the jimage.
       
    44  * These classes should fail to load unless --limit-modules is used to hide the
       
    45  * package exported by the jimage. There are 8 variants of this test:
       
    46  *   - With a jimage app package and with a jimage ext package
       
    47  *   - With --limit-modules and without --limit-modules
       
    48  *   - With AppCDS and without AppCDS (to verify behaviour is the same for both).
       
    49  *
       
    50  * There is also a 9th test to verify that when --limit-modules is used, a jimage
       
    51  * class in the archive can be replaced by a classpath class with the
       
    52  * same name and package.
       
    53  */
       
    54 
       
    55 import java.lang.reflect.InvocationTargetException;
       
    56 import java.lang.reflect.Method;
       
    57 import java.nio.file.Path;
       
    58 import java.nio.file.Paths;
       
    59 
       
    60 import jdk.test.lib.Asserts;
       
    61 import jdk.test.lib.cds.CDSOptions;
       
    62 import jdk.test.lib.cds.CDSTestUtils;
       
    63 import jdk.test.lib.process.ProcessTools;
       
    64 import jdk.test.lib.process.OutputAnalyzer;
       
    65 
       
    66 import jtreg.SkippedException;
       
    67 
       
    68 
       
    69 public class ClassPathTests {
       
    70     private static final String TEST_SRC = System.getProperty("test.src");
       
    71     private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
       
    72     private static final Path CLASSES_DIR = Paths.get("classes");
       
    73 
       
    74     // the test module
       
    75     private static final String MAIN_CLASS = "jdk.test.Main";
       
    76     private static final String LIMITMODS_MAIN_CLASS = "jdk.test.LimitModsMain";
       
    77 
       
    78     // test classes to archive. These are both in UPGRADED_MODULES
       
    79     private static final String JIMAGE_CLASS      = "com/sun/tools/javac/Main";
       
    80     private static final String APP_ARCHIVE_CLASS = "com/sun/tools/javac/MyMain";
       
    81     private static final String PLATFORM_ARCHIVE_CLASS = "javax/annotation/processing/FilerException";
       
    82     private static final String[] ARCHIVE_CLASSES = {APP_ARCHIVE_CLASS, PLATFORM_ARCHIVE_CLASS, JIMAGE_CLASS};
       
    83     private static final int NUMBER_OF_TEST_CASES = 10;
       
    84 
       
    85     private static String appJar;
       
    86     private static String testArchiveName;
       
    87 
       
    88 
       
    89     public static void main(String[] args) throws Throwable {
       
    90         ClassPathTests tests = new ClassPathTests();
       
    91         tests.dumpArchive();
       
    92 
       
    93         Method[] methods = tests.getClass().getDeclaredMethods();
       
    94         int numOfTestMethodsRun = 0;
       
    95         for (Method m : methods) {
       
    96             if (m.getName().startsWith("test")) {
       
    97                 System.out.println("About to run test method: " + m.getName());
       
    98                 try {
       
    99                     m.invoke(tests);
       
   100                 } catch (InvocationTargetException ite) {
       
   101                     Throwable throwable = ite.getCause();
       
   102                     if (throwable instanceof SkippedException) {
       
   103                         throw throwable;
       
   104                     } else {
       
   105                         throw ite;
       
   106                     }
       
   107                 }
       
   108                 numOfTestMethodsRun++;
       
   109             }
       
   110         }
       
   111 
       
   112         Asserts.assertTrue((numOfTestMethodsRun == NUMBER_OF_TEST_CASES),
       
   113             "Expected " + NUMBER_OF_TEST_CASES + " test methods to run, actual number is "
       
   114             + numOfTestMethodsRun);
       
   115     }
       
   116 
       
   117     private void dumpArchive() throws Exception {
       
   118         // Create a jar file with all the classes related to this test.
       
   119         JarBuilder.build( "classpathtests",
       
   120                           APP_ARCHIVE_CLASS, PLATFORM_ARCHIVE_CLASS, JIMAGE_CLASS,
       
   121                           "jdk/test/Main");
       
   122         appJar = TestCommon.getTestJar("classpathtests.jar");
       
   123 
       
   124         // dump the archive with altnernate jdk.comiler and jdk.activation classes in the class list
       
   125         OutputAnalyzer output1  = TestCommon.dump(appJar, TestCommon.list(ARCHIVE_CLASSES));
       
   126         TestCommon.checkDump(output1);
       
   127         // The PLATFORM_ARCHIVE_CLASS belongs to the java.compiler
       
   128         // module will be found from the module during dumping.
       
   129         // The JIMAGE_CLASS will be found from the jdk.compiler module during
       
   130         // dumping.
       
   131         // The APP_ARCHIVE_CLASS, which belongs to a package within the
       
   132         // jdk.compiler module, will not be found during dump time.
       
   133         for (String archiveClass : ARCHIVE_CLASSES) {
       
   134             if (archiveClass.equals(APP_ARCHIVE_CLASS)) {
       
   135                 output1.shouldContain("Preload Warning: Cannot find " + archiveClass);
       
   136             } else {
       
   137                 output1.shouldNotContain("Preload Warning: Cannot find " + archiveClass);
       
   138             }
       
   139         }
       
   140 
       
   141         testArchiveName = TestCommon.getCurrentArchiveName();
       
   142     }
       
   143 
       
   144     // #1: Archived classpath class in same package as jimage app class. With AppCDS.
       
   145     // Should fail to load.
       
   146     public void testAppClassWithAppCDS() throws Exception {
       
   147         OutputAnalyzer output = TestCommon.exec(
       
   148             appJar, MAIN_CLASS,
       
   149             "Test #1", APP_ARCHIVE_CLASS, "false"); // last 3 args passed to test
       
   150         TestCommon.checkExec(output);
       
   151     }
       
   152 
       
   153     // #2: Archived classpath class in same package as jimage app class. Without AppCDS.
       
   154     // Should fail to load.
       
   155     public void testAppClassWithoutAppCDS() throws Exception {
       
   156         CDSOptions opts = (new CDSOptions())
       
   157             .addPrefix("-cp", appJar)
       
   158             .setArchiveName(testArchiveName)
       
   159             .addSuffix(MAIN_CLASS, "Test #2", APP_ARCHIVE_CLASS, "false");
       
   160 
       
   161         CDSTestUtils.runWithArchiveAndCheck(opts);
       
   162     }
       
   163 
       
   164     // #3: Archived classpath class in same package as jimage ext class. With AppCDS.
       
   165     // The class should be loaded from the module.
       
   166     public void testExtClassWithAppCDS() throws Exception {
       
   167         OutputAnalyzer output = TestCommon.exec(
       
   168             appJar, MAIN_CLASS,
       
   169             "Test #3", PLATFORM_ARCHIVE_CLASS, "true", "EXT"); // last 4 args passed to test
       
   170         TestCommon.checkExec(output);
       
   171     }
       
   172 
       
   173     // #4: Archived classpath class in same package as jimage ext class. Without AppCDS.
       
   174     // The class should be loaded from the module.
       
   175     public void testExtClassWithoutAppCDS() throws Exception {
       
   176         CDSOptions opts = (new CDSOptions())
       
   177             .addPrefix("-cp", appJar)
       
   178             .setArchiveName(testArchiveName)
       
   179             .addSuffix(MAIN_CLASS, "Test #4", PLATFORM_ARCHIVE_CLASS, "true", "EXT");
       
   180 
       
   181         CDSTestUtils.runWithArchiveAndCheck(opts);
       
   182     }
       
   183 
       
   184     // #5: Archived classpath class in same package as jimage app class. With AppCDS.
       
   185     // Should load with CDS disabled because --limit-modules is used.
       
   186     public void testAppClassWithLimitModsWithAppCDS() throws Exception {
       
   187         TestCommon.run("-cp", appJar,
       
   188                        "--limit-modules", "java.base",
       
   189                        MAIN_CLASS,
       
   190                        "Test #5", APP_ARCHIVE_CLASS, "true") // last 3 args passed to test
       
   191             .assertSilentlyDisabledCDS(out -> {
       
   192                 out.shouldHaveExitValue(0);
       
   193             });
       
   194     }
       
   195 
       
   196     // #6: Archived classpath class in same package as jimage app class. Without AppCDS.
       
   197     // Should load with CDS disabled because --limit-modules is used.
       
   198     public void testAppClassWithLimitModsWithoutAppCDS() throws Exception {
       
   199         CDSOptions opts = (new CDSOptions())
       
   200             .addPrefix("-cp", appJar, "--limit-modules", "java.base")
       
   201             .setArchiveName(testArchiveName)
       
   202             .addSuffix(MAIN_CLASS, "Test #6", APP_ARCHIVE_CLASS, "true");
       
   203         CDSTestUtils.run(opts)
       
   204             .assertSilentlyDisabledCDS(out -> {
       
   205                 out.shouldHaveExitValue(0);
       
   206             });
       
   207     }
       
   208 
       
   209     // #7: Archived classpath class in same package as jimage ext class. With AppCDS.
       
   210     // Should load with CDS disabled because --limit-modules is used.
       
   211     public void testExtClassWithLimitModsWithAppCDS() throws Exception {
       
   212         TestCommon.run("-cp", appJar,
       
   213                        "--limit-modules", "java.base",
       
   214                        MAIN_CLASS,
       
   215                        "Test #7", PLATFORM_ARCHIVE_CLASS, "true") // last 3 args passed to test
       
   216             .assertSilentlyDisabledCDS(out -> {
       
   217                 out.shouldHaveExitValue(0);
       
   218             });
       
   219     }
       
   220 
       
   221     // #8: Archived classpath class in same package as jimage ext class. Without AppCDS.
       
   222     // Should load with CDS disabled because --limit-modules is used.
       
   223     public void testExtClassWithLimitModsWithoutAppCDS() throws Exception {
       
   224         CDSOptions opts = (new CDSOptions())
       
   225             .addPrefix("-cp", appJar, "--limit-modules", "java.base")
       
   226             .setArchiveName(testArchiveName)
       
   227             .addSuffix(MAIN_CLASS, "Test #8", PLATFORM_ARCHIVE_CLASS, "true");
       
   228         CDSTestUtils.run(opts)
       
   229             .assertSilentlyDisabledCDS(out -> {
       
   230                 out.shouldHaveExitValue(0);
       
   231             });
       
   232     }
       
   233 
       
   234     // #9: Archived classpath class with same name as jimage app class. With AppCDS.
       
   235     // Should load with CDS disabled because --limit-modules is used.
       
   236     public void testReplacingJImageClassWithAppCDS() throws Exception {
       
   237         TestCommon.run("-cp", appJar,
       
   238                        "--limit-modules", "java.base",
       
   239                        MAIN_CLASS,
       
   240                        "Test #9", JIMAGE_CLASS, "true") // last 3 args passed to test
       
   241             .assertSilentlyDisabledCDS(out -> {
       
   242                 out.shouldHaveExitValue(0);
       
   243             });
       
   244     }
       
   245 
       
   246     // #10: Archived classpath class with same name as jimage app class. Without AppCDS.
       
   247     // Should load because --limit-modules is used. Note the archive will actually contain
       
   248     // the original jimage version of the class, but AppCDS should refuse to load it
       
   249     // since --limit-modules is used. This should result in the -cp version being used.
       
   250     public void testReplacingJImageClassWithoutAppCDS() throws Exception {
       
   251         CDSOptions opts = (new CDSOptions())
       
   252             .addPrefix("-cp", appJar, "--limit-modules", "java.base")
       
   253             .setArchiveName(testArchiveName)
       
   254             .addSuffix(MAIN_CLASS, "Test #10", JIMAGE_CLASS, "true");
       
   255         CDSTestUtils.run(opts)
       
   256             .assertSilentlyDisabledCDS(out -> {
       
   257                 out.shouldHaveExitValue(0);
       
   258             });
       
   259     }
       
   260 }