58678
|
1 |
/*
|
|
2 |
* Copyright (c) 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 |
* @summary Test relative paths specified in the -cp.
|
|
28 |
* @requires vm.cds
|
|
29 |
* @library /test/lib
|
|
30 |
* @modules jdk.jartool/sun.tools.jar
|
|
31 |
* @compile test-classes/Hello.java
|
|
32 |
* @compile test-classes/HelloMore.java
|
|
33 |
* @run driver RelativePath
|
|
34 |
*/
|
|
35 |
|
|
36 |
import java.io.File;
|
|
37 |
import java.nio.file.Files;
|
|
38 |
import java.nio.file.Path;
|
|
39 |
import java.nio.file.Paths;
|
|
40 |
import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES;
|
|
41 |
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
|
|
42 |
import java.util.Arrays;
|
|
43 |
import jdk.test.lib.Platform;
|
|
44 |
|
|
45 |
public class RelativePath {
|
|
46 |
|
|
47 |
private static final Path USER_DIR = Paths.get(System.getProperty("user.dir"));
|
|
48 |
|
|
49 |
public static void main(String[] args) throws Exception {
|
|
50 |
String appJar = JarBuilder.getOrCreateHelloJar();
|
|
51 |
String appJar2 = JarBuilder.build("AppendClasspath_HelloMore", "HelloMore");
|
|
52 |
|
|
53 |
// dump an archive with only the jar name in the -cp
|
|
54 |
int idx = appJar.lastIndexOf(File.separator);
|
|
55 |
String jarName = appJar.substring(idx + 1);
|
|
56 |
String jarDir = appJar.substring(0, idx);
|
|
57 |
TestCommon.testDump(jarDir, jarName, TestCommon.list("Hello"));
|
|
58 |
|
|
59 |
// copy the jar file to another dir. Specify the jar file without
|
|
60 |
// a directory path.
|
|
61 |
Path srcPath = Paths.get(appJar);
|
|
62 |
Path destDir = Files.createTempDirectory(USER_DIR, "deploy");
|
|
63 |
Path destPath = destDir.resolve(jarName);
|
|
64 |
Files.copy(srcPath, destPath, REPLACE_EXISTING, COPY_ATTRIBUTES);
|
|
65 |
TestCommon.runWithRelativePath(
|
|
66 |
destDir.toString(),
|
|
67 |
"-Xshare:on",
|
|
68 |
"-XX:SharedArchiveFile=" + TestCommon.getCurrentArchiveName(),
|
|
69 |
"-cp", jarName + File.pathSeparator + appJar2,
|
|
70 |
"-Xlog:class+load=trace,class+path=info",
|
|
71 |
"HelloMore")
|
|
72 |
.assertNormalExit(output -> {
|
|
73 |
output.shouldContain("Hello source: shared objects file")
|
|
74 |
.shouldHaveExitValue(0);
|
|
75 |
});
|
|
76 |
|
|
77 |
// Long path test
|
|
78 |
// Create a long directory path and copy the appJar there.
|
|
79 |
final int MAX_PATH = 260;
|
|
80 |
destDir = Paths.get(jarDir);
|
|
81 |
int subDirLen = MAX_PATH - jarDir.length() - 3;
|
|
82 |
if (subDirLen > 0) {
|
|
83 |
char[] chars = new char[subDirLen];
|
|
84 |
Arrays.fill(chars, 'x');
|
|
85 |
String subPath = new String(chars);
|
|
86 |
destDir = Paths.get(jarDir, subPath);
|
|
87 |
}
|
|
88 |
File longDir = destDir.toFile();
|
|
89 |
longDir.mkdir();
|
|
90 |
String destJar = longDir.getPath() + File.separator + jarName;
|
|
91 |
Files.copy(Paths.get(appJar), Paths.get(destJar), REPLACE_EXISTING);
|
|
92 |
// Create an archive with the appJar in the long directory path.
|
|
93 |
TestCommon.testDump(destJar, TestCommon.list("Hello"));
|
|
94 |
|
|
95 |
// Run with -cp containing the appJar and another jar appended.
|
|
96 |
TestCommon.run(
|
|
97 |
"-cp", destJar + File.pathSeparator + appJar2,
|
|
98 |
"-Xlog:class+load=trace,class+path=info",
|
|
99 |
"HelloMore")
|
|
100 |
.assertNormalExit(output -> {
|
|
101 |
output.shouldContain("Hello source: shared objects file")
|
|
102 |
.shouldHaveExitValue(0);
|
|
103 |
});
|
|
104 |
|
|
105 |
// Dump an archive with a specified JAR file in -classpath
|
|
106 |
TestCommon.testDump(appJar, TestCommon.list("Hello"));
|
|
107 |
|
|
108 |
// compose a relative path to the hello.jar
|
|
109 |
String newHello = TestCommon.composeRelPath(appJar);
|
|
110 |
|
|
111 |
// create a sym link to the original hello.jar
|
|
112 |
File linkedHello = null;
|
|
113 |
if (!Platform.isWindows()) {
|
|
114 |
linkedHello = TestCommon.createSymLink(appJar);
|
|
115 |
}
|
|
116 |
|
|
117 |
// PASS:1) same appJar but referred to via a relative path
|
|
118 |
TestCommon.run(
|
|
119 |
"-cp", newHello + File.pathSeparator + appJar2,
|
|
120 |
"-Xlog:class+load=trace,class+path=info",
|
|
121 |
"HelloMore")
|
|
122 |
.assertNormalExit();
|
|
123 |
|
|
124 |
// PASS:2) relative path starting with "."
|
|
125 |
TestCommon.runWithRelativePath(
|
|
126 |
jarDir,
|
|
127 |
"-Xshare:on",
|
|
128 |
"-XX:SharedArchiveFile=" + TestCommon.getCurrentArchiveName(),
|
|
129 |
"-cp", "." + File.separator + jarName + File.pathSeparator + appJar2,
|
|
130 |
"-Xlog:class+load=trace,class+path=info",
|
|
131 |
"HelloMore")
|
|
132 |
.assertNormalExit(output -> {
|
|
133 |
output.shouldContain("Hello source: shared objects file")
|
|
134 |
.shouldHaveExitValue(0);
|
|
135 |
});
|
|
136 |
|
|
137 |
// PASS:3) relative path starting with ".."
|
|
138 |
idx = jarDir.lastIndexOf(File.separator);
|
|
139 |
String jarSubDir = jarDir.substring(idx + 1);
|
|
140 |
TestCommon.runWithRelativePath(
|
|
141 |
jarDir,
|
|
142 |
"-Xshare:on",
|
|
143 |
"-XX:SharedArchiveFile=" + TestCommon.getCurrentArchiveName(),
|
|
144 |
"-cp", ".." + File.separator + jarSubDir + File.separator + jarName
|
|
145 |
+ File.pathSeparator + appJar2,
|
|
146 |
"-Xlog:class+load=trace,class+path=info",
|
|
147 |
"HelloMore")
|
|
148 |
.assertNormalExit(output -> {
|
|
149 |
output.shouldContain("Hello source: shared objects file")
|
|
150 |
.shouldHaveExitValue(0);
|
|
151 |
});
|
|
152 |
|
|
153 |
// PASS:4) a jar linked to the original hello.jar
|
|
154 |
if (!Platform.isWindows()) {
|
|
155 |
TestCommon.run(
|
|
156 |
"-cp", linkedHello.getPath() + File.pathSeparator + appJar2,
|
|
157 |
"HelloMore")
|
|
158 |
.assertNormalExit();
|
|
159 |
}
|
|
160 |
|
|
161 |
final String errorMessage1 = "Unable to use shared archive";
|
|
162 |
final String errorMessage2 = "shared class paths mismatch";
|
|
163 |
// FAIL: 1) runtime with classpath different from the one used in dump time
|
|
164 |
// (runtime has an extra jar file prepended to the class path)
|
|
165 |
TestCommon.run(
|
|
166 |
"-cp", appJar2 + File.pathSeparator + newHello,
|
|
167 |
"HelloMore")
|
|
168 |
.assertAbnormalExit(errorMessage1, errorMessage2);
|
|
169 |
|
|
170 |
}
|
|
171 |
}
|