1 /*
 2  * Copyright (c) 2025, 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  * @summary -XX:AOTMode=record should not interfere with app execution: (1) thread creation; (2) exit code
28  * @requires vm.cds.supports.aot.class.linking
29  * @comment work around JDK-8345635
30  * @requires !vm.jvmci.enabled
31  * @library /test/jdk/lib/testlibrary /test/lib
32  * @build TrainingRun
33  * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar app.jar MyTestApp
34  * @run driver TrainingRun AOT
35  */
36 
37 import jdk.test.lib.cds.CDSAppTester;
38 import jdk.test.lib.helpers.ClassFileInstaller;
39 import jdk.test.lib.process.OutputAnalyzer;
40 
41 public class TrainingRun {
42     static final String appJar = ClassFileInstaller.getJarPath("app.jar");
43     static final String mainClass = "MyTestApp";
44 
45     public static void main(String[] args) throws Exception {
46         (new Tester()).run(args);
47     }
48 
49     static class Tester extends CDSAppTester {
50         public Tester() {
51             super(mainClass);
52 
53             // CDSAppTester usually wants the app to return exit value 0, but this test
54             // checks whether the training run can return 2.
55             setCheckExitValue(false);
56         }
57 
58         @Override
59         public String classpath(RunMode runMode) {
60             return appJar;
61         }
62 
63         @Override
64         public String[] appCommandLine(RunMode runMode) {
65             return new String[] {
66                 mainClass,
67             };
68         }
69 
70         @Override
71         public void checkExecution(OutputAnalyzer out, RunMode runMode) {
72             if (runMode.isApplicationExecuted()) {
73                 out.shouldHaveExitValue(2);
74                 out.shouldContain("Hello: x is 1");
75             }
76         }
77     }
78 }
79 
80 class MyTestApp {
81     volatile static int x = 0;
82 
83     public static void main(String args[]) throws Exception {
84         Thread t = new Thread(() -> {
85                 x = 1;
86         });
87         t.start();
88         t.join();
89 
90         if (x != 1) {
91             throw new RuntimeException("x should be 1 but is " + x);
92         }
93         System.out.println("Hello: x is " + x);
94         System.out.println("I am calling System.exit(2)");
95         System.exit(2);
96     }
97 }