File: MoveJDKTest.java

package info (click to toggle)
openjdk-11 11.0.28~3ea-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 780,896 kB
  • sloc: java: 5,208,133; xml: 1,192,267; cpp: 1,138,346; ansic: 461,925; javascript: 162,416; sh: 16,738; objc: 13,721; python: 4,757; asm: 3,570; makefile: 2,962; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (170 lines) | stat: -rw-r--r-- 7,233 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */

/*
 * @test
 * @summary Test that CDS still works when the JDK is moved to a new directory
 * @bug 8272345
 * @requires vm.cds
 * @requires vm.flagless
 * @comment This test doesn't work on Windows because it depends on symlinks
 * @requires os.family != "windows"
 * @library /test/lib
 * @modules java.base/jdk.internal.misc
 *          java.management
 *          jdk.jartool/sun.tools.jar
 * @compile test-classes/Hello.java
 * @run driver MoveJDKTest
 */

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import jdk.test.lib.process.OutputAnalyzer;

public class MoveJDKTest {
    public static void main(String[] args) throws Exception {
        String java_home_src = System.getProperty("java.home");
        String java_home_dst = System.getProperty("user.dir") + File.separator + "moved_jdk";

        TestCommon.startNewArchiveName();
        String jsaFile = TestCommon.getCurrentArchiveName();
        String jsaOpt = "-XX:SharedArchiveFile=" + jsaFile;
        {
            ProcessBuilder pb = makeBuilder(java_home_src + "/bin/java", "-Xshare:dump", jsaOpt);
            TestCommon.executeAndLog(pb, "dump")
                      .shouldHaveExitValue(0);
        }
        {
            ProcessBuilder pb = makeBuilder(java_home_src + "/bin/java",
                                            "-Xshare:auto",
                                            jsaOpt,
                                            "-Xlog:class+path=info",
                                            "-version");
            OutputAnalyzer out = TestCommon.executeAndLog(pb, "exec-src");
            out.shouldHaveExitValue(0);
            out.shouldNotContain("shared class paths mismatch");
            out.shouldNotContain("BOOT classpath mismatch");
        }

        clone(new File(java_home_src), new File(java_home_dst));
        System.out.println("============== Cloned JDK at " + java_home_dst);

        // Test runtime with cloned JDK
        {
            ProcessBuilder pb = makeBuilder(java_home_dst + "/bin/java",
                                            "-Xshare:auto",
                                            jsaOpt,
                                            "-Xlog:class+path=info",
                                            "-version");
            OutputAnalyzer out = TestCommon.executeAndLog(pb, "exec-dst");
            out.shouldHaveExitValue(0);
            out.shouldNotContain("shared class paths mismatch");
            out.shouldNotContain("BOOT classpath mismatch");
        }

        // Test with bad JAR file name, hello.modules
        String helloJar = JarBuilder.getOrCreateHelloJar();
        String fake_modules = copyFakeModulesFromHelloJar();
        String dumptimeBootAppendOpt = "-Xbootclasspath/a:" + fake_modules;
        {
            ProcessBuilder pb = makeBuilder(java_home_src + "/bin/java",
                                            "-Xshare:dump",
                                            dumptimeBootAppendOpt,
                                            jsaOpt);
            TestCommon.executeAndLog(pb, "dump")
                      .shouldHaveExitValue(0);
        }
        {
            String runtimeBootAppendOpt = dumptimeBootAppendOpt + System.getProperty("path.separator") + helloJar;
            ProcessBuilder pb = makeBuilder(java_home_dst + "/bin/java",
                                            "-Xshare:auto",
                                            runtimeBootAppendOpt,
                                            jsaOpt,
                                            "-Xlog:class+path=info",
                                            "-version");
            OutputAnalyzer out = TestCommon.executeAndLog(pb, "exec-dst");
            out.shouldHaveExitValue(0);
            out.shouldNotContain("shared class paths mismatch");
            out.shouldNotContain("BOOT classpath mismatch");
        }
    }

    // Do a cheap clone of the JDK. Most files can be sym-linked. However, $JAVA_HOME/bin/java and $JAVA_HOME/lib/.../libjvm.so"
    // must be copied, because the java.home property is derived from the canonicalized paths of these 2 files.
    static void clone(File src, File dst) throws Exception {
        if (dst.exists()) {
            if (!dst.isDirectory()) {
                throw new RuntimeException("Not a directory :" + dst);
            }
        } else {
            if (!dst.mkdir()) {
                throw new RuntimeException("Cannot create directory: " + dst);
            }
        }
        final String jvmLib = System.mapLibraryName("jvm");
        for (String child : src.list()) {
            if (child.equals(".") || child.equals("..")) {
                continue;
            }

            File child_src = new File(src, child);
            File child_dst = new File(dst, child);
            if (child_dst.exists()) {
                throw new RuntimeException("Already exists: " + child_dst);
            }
            if (child_src.isFile()) {
                if (child.equals(jvmLib) || child.equals("java")) {
                    Files.copy(child_src.toPath(), /* copy data to -> */ child_dst.toPath());
                } else {
                    Files.createSymbolicLink(child_dst.toPath(),  /* link to -> */ child_src.toPath());
                }
            } else {
                if (child_src.exists()) {clone(child_src, child_dst);}
            }
        }
    }

    static ProcessBuilder makeBuilder(String... args) throws Exception {
        System.out.print("[");
        for (String s : args) {
            System.out.print(" " + s);
        }
        System.out.println(" ]");
        return new ProcessBuilder(args);
    }

    private static String copyFakeModulesFromHelloJar() throws Exception {
        String classDir = System.getProperty("test.classes");
        String newFile = "hello.modules";
        String path = classDir + File.separator + newFile;

        Files.copy(Paths.get(classDir, "hello.jar"),
            Paths.get(classDir, newFile),
            StandardCopyOption.REPLACE_EXISTING);
        return path;
    }
}