34323
|
1 |
/*
|
|
2 |
* Copyright (c) 2015, 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 |
r 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 |
import java.io.File;
|
|
24 |
import java.io.IOException;
|
|
25 |
import java.util.ArrayList;
|
|
26 |
import java.util.List;
|
|
27 |
|
|
28 |
/*
|
|
29 |
* @test
|
|
30 |
* @bug 8066272
|
|
31 |
* @summary tests a simple multi-versioned jar file
|
|
32 |
* @compile -XDignore.symbol.file Utils.java MultiRelease.java
|
|
33 |
* @run main MultiRelease
|
|
34 |
* @author ksrini
|
|
35 |
*/
|
|
36 |
|
|
37 |
public class MultiRelease {
|
|
38 |
private static final File cwd = new File(".");
|
|
39 |
private static int pass = 0;
|
|
40 |
private static int fail = 0;
|
|
41 |
// specify alternate name via arguments to verify
|
|
42 |
// if permanent fix works
|
|
43 |
|
|
44 |
private static final String PropKey = "pack200.MultiRelease.META-INF";
|
|
45 |
private static final String MetaInfName = System.getProperty(PropKey, "META-INF");
|
|
46 |
|
|
47 |
public static void main(String... args) throws Exception {
|
|
48 |
new MultiRelease().run();
|
|
49 |
}
|
|
50 |
|
|
51 |
void run() throws Exception {
|
|
52 |
List<TestCase> testCases = new ArrayList<>();
|
|
53 |
testCases.add(new TestCase1());
|
|
54 |
testCases.add(new TestCase2());
|
|
55 |
for (TestCase tc : testCases) {
|
|
56 |
tc.run();
|
|
57 |
}
|
|
58 |
if (fail > 0) {
|
|
59 |
throw new Exception(fail + "/" + testCases.size() + " tests fails");
|
|
60 |
} else {
|
|
61 |
System.out.println("All tests(" + pass + ") passes");
|
|
62 |
}
|
|
63 |
}
|
|
64 |
|
|
65 |
/*
|
|
66 |
* An abstract class to eliminate test boiler plating.
|
|
67 |
*/
|
|
68 |
static abstract class TestCase {
|
|
69 |
final File tcwd;
|
|
70 |
final File metaInfDir;
|
|
71 |
final File versionsDir;
|
|
72 |
final File manifestFile;
|
|
73 |
|
|
74 |
TestCase(String directory) throws IOException {
|
|
75 |
System.out.println("initializing directories");
|
|
76 |
tcwd = new File(cwd, directory);
|
|
77 |
metaInfDir = mkdir(new File(tcwd, MetaInfName));
|
|
78 |
versionsDir = mkdir(new File(metaInfDir, "versions"));
|
|
79 |
manifestFile = new File(tcwd, "manifest.tmp");
|
|
80 |
List<String> scratch = new ArrayList<>();
|
|
81 |
scratch.add("Multi-Release: true");
|
|
82 |
Utils.createFile(manifestFile, scratch);
|
|
83 |
}
|
|
84 |
|
|
85 |
File mkdir(File f) throws IOException {
|
|
86 |
if (f.exists() && f.isDirectory() && f.canRead() && f.canWrite()) {
|
|
87 |
return f;
|
|
88 |
}
|
|
89 |
if (!f.mkdirs()) {
|
|
90 |
throw new IOException("mkdirs failed: " + f.getAbsolutePath());
|
|
91 |
}
|
|
92 |
return f;
|
|
93 |
}
|
|
94 |
|
|
95 |
abstract void emitClassFiles() throws Exception;
|
|
96 |
|
|
97 |
void run() {
|
|
98 |
try {
|
|
99 |
emitClassFiles();
|
|
100 |
// jar the file up
|
|
101 |
File testFile = new File(tcwd, "test" + Utils.JAR_FILE_EXT);
|
|
102 |
Utils.jar("cvfm",
|
|
103 |
testFile.getAbsolutePath(),
|
|
104 |
manifestFile.getAbsolutePath(),
|
|
105 |
"-C",
|
|
106 |
tcwd.getAbsolutePath(),
|
|
107 |
".");
|
|
108 |
File outFile = new File(tcwd, "test-repacked" + Utils.JAR_FILE_EXT);
|
|
109 |
List<String> cmdsList = new ArrayList<>();
|
|
110 |
|
|
111 |
cmdsList.add(Utils.getPack200Cmd());
|
|
112 |
cmdsList.add("-J-ea");
|
|
113 |
cmdsList.add("-J-esa");
|
|
114 |
cmdsList.add("-v");
|
|
115 |
cmdsList.add("--repack");
|
|
116 |
cmdsList.add(outFile.getAbsolutePath());
|
|
117 |
cmdsList.add(testFile.getAbsolutePath());
|
|
118 |
List<String> output = Utils.runExec(cmdsList);
|
|
119 |
Utils.doCompareVerify(testFile.getAbsoluteFile(), outFile.getAbsoluteFile());
|
|
120 |
pass++;
|
|
121 |
} catch (Exception e) {
|
|
122 |
e.printStackTrace(System.err);
|
|
123 |
fail++;
|
|
124 |
}
|
|
125 |
}
|
|
126 |
}
|
|
127 |
|
|
128 |
static class TestCase1 extends TestCase {
|
|
129 |
private TestCase1(String directory) throws IOException {
|
|
130 |
super(directory);
|
|
131 |
}
|
|
132 |
|
|
133 |
public TestCase1() throws Exception {
|
|
134 |
this("case1");
|
|
135 |
}
|
|
136 |
|
|
137 |
@Override
|
|
138 |
void emitClassFiles() throws Exception {
|
|
139 |
emitClassFile("");
|
|
140 |
emitClassFile("7");
|
|
141 |
emitClassFile("8");
|
|
142 |
emitClassFile("9");
|
|
143 |
}
|
|
144 |
|
|
145 |
/*
|
|
146 |
* Adds different variants of types
|
|
147 |
*/
|
|
148 |
void emitClassFile(String version) throws IOException {
|
|
149 |
final File outDir = mkdir(version.isEmpty()
|
|
150 |
? tcwd
|
|
151 |
: new File(versionsDir, version));
|
|
152 |
|
|
153 |
final File srcDir = mkdir(version.isEmpty()
|
|
154 |
? new File(tcwd, "src")
|
|
155 |
: new File(new File(versionsDir, version), "src"));
|
|
156 |
|
|
157 |
final String fname = "Foo";
|
|
158 |
final File srcFile = new File(srcDir, fname + Utils.JAVA_FILE_EXT);
|
|
159 |
List<String> scratch = new ArrayList<>();
|
|
160 |
|
|
161 |
scratch.add("package pkg;");
|
|
162 |
switch (version) {
|
|
163 |
case "7":
|
|
164 |
scratch.add("public class Foo {");
|
|
165 |
scratch.add("public static final class Bar {}");
|
|
166 |
break;
|
|
167 |
case "8":
|
|
168 |
scratch.add("public abstract class Foo {");
|
|
169 |
scratch.add("public final class Bar {}");
|
|
170 |
break;
|
|
171 |
case "9":
|
|
172 |
scratch.add("public interface Foo {");
|
|
173 |
scratch.add("public final class Bar {}");
|
|
174 |
break;
|
|
175 |
default:
|
|
176 |
scratch.add("public class Foo {");
|
|
177 |
scratch.add("public final class Bar {}");
|
|
178 |
break;
|
|
179 |
}
|
|
180 |
scratch.add("}");
|
|
181 |
|
|
182 |
Utils.createFile(srcFile, scratch);
|
|
183 |
Utils.compiler("-d",
|
|
184 |
outDir.getAbsolutePath(),
|
|
185 |
srcFile.getAbsolutePath());
|
|
186 |
}
|
|
187 |
}
|
|
188 |
|
|
189 |
static class TestCase2 extends TestCase {
|
|
190 |
private TestCase2(String directory) throws IOException {
|
|
191 |
super(directory);
|
|
192 |
}
|
|
193 |
|
|
194 |
TestCase2() throws Exception {
|
|
195 |
this("case2");
|
|
196 |
}
|
|
197 |
|
|
198 |
@Override
|
|
199 |
void emitClassFiles() throws Exception {
|
|
200 |
emitClassFile("");
|
|
201 |
emitClassFile("8");
|
|
202 |
}
|
|
203 |
|
|
204 |
/*
|
|
205 |
* Adds different variants of types and tries to invoke an
|
|
206 |
* interface or concrete method defined by them.
|
|
207 |
*/
|
|
208 |
void emitClassFile(String version) throws IOException {
|
|
209 |
|
|
210 |
final File outDir = mkdir(version.isEmpty()
|
|
211 |
? tcwd
|
|
212 |
: new File(versionsDir, version));
|
|
213 |
|
|
214 |
final File srcDir = mkdir(version.isEmpty()
|
|
215 |
? new File(tcwd, "src")
|
|
216 |
: new File(new File(versionsDir, version), "src"));
|
|
217 |
|
|
218 |
List<String> scratch = new ArrayList<>();
|
|
219 |
final String fname1 = "Ab";
|
|
220 |
final File srcFile1 = new File(srcDir, fname1 + Utils.JAVA_FILE_EXT);
|
|
221 |
|
|
222 |
final String fname2 = "AbNormal";
|
|
223 |
final File srcFile2 = new File(srcDir, fname2 + Utils.JAVA_FILE_EXT);
|
|
224 |
switch (version) {
|
|
225 |
case "8":
|
|
226 |
scratch.clear();
|
|
227 |
scratch.add("import java.io.IOException;");
|
|
228 |
scratch.add("public interface " + fname1 + "{");
|
|
229 |
scratch.add(" public abstract void close() throws IOException ;");
|
|
230 |
scratch.add("}");
|
|
231 |
Utils.createFile(srcFile1, scratch);
|
|
232 |
break;
|
|
233 |
default:
|
|
234 |
scratch.clear();
|
|
235 |
scratch.add("import java.io.IOException;");
|
|
236 |
scratch.add("public abstract class " + fname1 + "{");
|
|
237 |
scratch.add(" public abstract void close() throws IOException ;");
|
|
238 |
scratch.add("}");
|
|
239 |
Utils.createFile(srcFile1, scratch);
|
|
240 |
}
|
|
241 |
|
|
242 |
scratch.clear();
|
|
243 |
scratch.add("import java.io.IOException;");
|
|
244 |
scratch.add("public class " + fname2 + "{");
|
|
245 |
scratch.add(" public void doSomething(Ab ab) throws IOException {");
|
|
246 |
scratch.add(" ab.close();");
|
|
247 |
scratch.add(" }");
|
|
248 |
scratch.add("}");
|
|
249 |
|
|
250 |
Utils.createFile(srcFile2, scratch);
|
|
251 |
Utils.compiler("-d",
|
|
252 |
outDir.getAbsolutePath(),
|
|
253 |
srcFile1.getAbsolutePath(),
|
|
254 |
srcFile2.getAbsolutePath());
|
|
255 |
}
|
|
256 |
}
|
|
257 |
}
|