1 /*
  2  * Copyright (c) 2020, 2024, 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 // this file is auto-generated by CDSMHTest_generate.sh. Do not edit manually.
 25 
 26 /*
 27  * @test
 28  * @summary Run the MethodHandlesCastFailureTest.java test in static CDS archive mode.
 29  * @requires vm.cds & vm.compMode != "Xcomp"
 30  * @comment Some of the tests run excessively slowly with -Xcomp. The original
 31  *          tests aren't executed with -Xcomp in the CI pipeline, so let's exclude
 32  *          the generated tests from -Xcomp execution as well.
 33  * @library /test/lib /test/hotspot/jtreg/runtime/cds/appcds
 34  *          /test/hotspot/jtreg/runtime/cds/appcds/dynamicArchive/test-classes
 35  * @compile ../../../../../../jdk/java/lang/invoke/MethodHandlesTest.java
 36  *        ../../../../../../lib/jdk/test/lib/Utils.java
 37  *        ../../../../../../jdk/java/lang/invoke/MethodHandlesCastFailureTest.java
 38  *        ../../../../../../jdk/java/lang/invoke/remote/RemoteExample.java
 39  *        ../../../../../../jdk/java/lang/invoke/common/test/java/lang/invoke/lib/CodeCacheOverflowProcessor.java
 40  *        ../dynamicArchive/test-classes/TestMHApp.java
 41  * @build jdk.test.whitebox.WhiteBox
 42  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 43  * @run junit/othervm/timeout=480 -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. MethodHandlesCastFailureTest
 44  */
 45 
 46 import org.junit.Test;
 47 
 48 import java.io.File;
 49 import java.nio.file.Path;
 50 
 51 import jdk.test.lib.cds.CDSOptions;
 52 import jdk.test.lib.cds.CDSTestUtils;
 53 import jdk.test.lib.process.OutputAnalyzer;
 54 import jdk.test.lib.Platform;
 55 
 56 public class MethodHandlesCastFailureTest {
 57     @Test
 58     public void test() throws Exception {
 59         testImpl();
 60     }
 61 
 62     private static final String classDir = System.getProperty("test.classes");
 63     private static final String mainClass = "TestMHApp";
 64     private static final String javaClassPath = System.getProperty("java.class.path");
 65     private static final String ps = System.getProperty("path.separator");
 66     private static final String testPackageName = "test.java.lang.invoke";
 67     private static final String testClassName = "MethodHandlesCastFailureTest";
 68 
 69     static void testImpl() throws Exception {
 70         String appJar = JarBuilder.build("MH", new File(classDir), null);
 71         String classList = testClassName + ".list";
 72         String archiveName = testClassName + ".jsa";
 73         // Disable VerifyDpendencies when running with debug build because
 74         // the test requires a lot more time to execute with the option enabled.
 75         String verifyOpt =
 76             Platform.isDebugBuild() ? "-XX:-VerifyDependencies" : "-showversion";
 77 
 78         String junitJar = Path.of(Test.class.getProtectionDomain().getCodeSource().getLocation().toURI()).toString();
 79 
 80         String jars = appJar + ps + junitJar;
 81 
 82         // dump class list
 83         CDSTestUtils.dumpClassList(classList, "-cp", jars, verifyOpt, mainClass,
 84                                    testPackageName + "." + testClassName);
 85 
 86         // create archive with the class list
 87         CDSOptions opts = (new CDSOptions())
 88             .addPrefix("-XX:ExtraSharedClassListFile=" + classList,
 89                        "-cp", jars,
 90                        "-Xlog:class+load,cds")
 91             .setArchiveName(archiveName);
 92         OutputAnalyzer output = CDSTestUtils.createArchiveAndCheck(opts);
 93         if (testClassName.equals("MethodHandlesInvokersTest")) {
 94             output.shouldNotContain("Failed to generate LambdaForm holder classes. Is your classlist out of date?");
 95         }
 96 
 97         // run with archive
 98         CDSOptions runOpts = (new CDSOptions())
 99             .addPrefix("-cp", jars, "-Xlog:class+load,cds=debug", verifyOpt)
100             .setArchiveName(archiveName)
101             .setUseVersion(false)
102             .addSuffix(mainClass, testPackageName + "." + testClassName);
103         output = CDSTestUtils.runWithArchive(runOpts);
104         output.shouldMatch(".class.load. test.java.lang.invoke.MethodHandlesCastFailureTest[$][$]Lambda.*/0x.*source:.*shared.*objects.*file")
105               .shouldHaveExitValue(0);
106     }
107 }