Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/tools/pack200/PackageVersionTest.java
38833 views
1
/*
2
* Copyright (c) 2010, 2013, 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
* @test
26
* @bug 6712743 6991164 7168401
27
* @summary verify package versions
28
* @compile -XDignore.symbol.file Utils.java PackageVersionTest.java
29
* @run main PackageVersionTest
30
* @author ksrini
31
*/
32
33
import java.io.ByteArrayOutputStream;
34
import java.io.Closeable;
35
import java.io.File;
36
import java.io.FileOutputStream;
37
import java.io.IOException;
38
import java.io.PrintStream;
39
import java.util.jar.JarFile;
40
import java.util.jar.Pack200;
41
import java.util.jar.Pack200.Packer;
42
import java.util.jar.Pack200.Unpacker;
43
44
public class PackageVersionTest {
45
private static final File javaHome = new File(System.getProperty("java.home"));
46
47
public final static int JAVA5_PACKAGE_MAJOR_VERSION = 150;
48
public final static int JAVA5_PACKAGE_MINOR_VERSION = 7;
49
50
public final static int JAVA6_PACKAGE_MAJOR_VERSION = 160;
51
public final static int JAVA6_PACKAGE_MINOR_VERSION = 1;
52
53
public final static int JAVA7_PACKAGE_MAJOR_VERSION = 170;
54
public final static int JAVA7_PACKAGE_MINOR_VERSION = 1;
55
56
public static void main(String... args) throws IOException {
57
if (!javaHome.getName().endsWith("jre")) {
58
throw new RuntimeException("Error: requires an SDK to run");
59
}
60
61
File out = new File("test.pack");
62
createClassFile("Test5");
63
createClassFile("Test6");
64
createClassFile("Test7");
65
66
verify6991164();
67
verifyPack("Test5.class", JAVA5_PACKAGE_MAJOR_VERSION,
68
JAVA5_PACKAGE_MINOR_VERSION);
69
70
verifyPack("Test6.class", JAVA6_PACKAGE_MAJOR_VERSION,
71
JAVA6_PACKAGE_MINOR_VERSION);
72
73
// a jar file devoid of indy classes must generate 160.1 package file
74
verifyPack("Test7.class", JAVA6_PACKAGE_MAJOR_VERSION,
75
JAVA6_PACKAGE_MINOR_VERSION);
76
77
// test for resource file, ie. no class files
78
verifyPack("Test6.java", JAVA5_PACKAGE_MAJOR_VERSION,
79
JAVA5_PACKAGE_MINOR_VERSION);
80
Utils.cleanup();
81
}
82
83
static void verify6991164() {
84
Unpacker unpacker = Pack200.newUnpacker();
85
String versionStr = unpacker.toString();
86
String expected = "Pack200, Vendor: " +
87
System.getProperty("java.vendor") + ", Version: " +
88
JAVA7_PACKAGE_MAJOR_VERSION + "." + JAVA7_PACKAGE_MINOR_VERSION;
89
if (!versionStr.equals(expected)) {
90
System.out.println("Expected: " + expected);
91
System.out.println("Obtained: " + versionStr);
92
throw new RuntimeException("did not get expected string " + expected);
93
}
94
}
95
96
static void createClassFile(String name) {
97
createJavaFile(name);
98
String target = name.substring(name.length() - 1);
99
String javacCmds[] = {
100
"-source",
101
"5",
102
"-target",
103
name.substring(name.length() - 1),
104
name + ".java"
105
};
106
Utils.compiler(javacCmds);
107
}
108
109
static void createJavaFile(String name) {
110
PrintStream ps = null;
111
FileOutputStream fos = null;
112
File outputFile = new File(name + ".java");
113
outputFile.delete();
114
try {
115
fos = new FileOutputStream(outputFile);
116
ps = new PrintStream(fos);
117
ps.format("public class %s {}", name);
118
} catch (IOException ioe) {
119
throw new RuntimeException("creation of test file failed");
120
} finally {
121
Utils.close(ps);
122
Utils.close(fos);
123
}
124
}
125
126
static void verifyPack(String filename, int expected_major, int expected_minor) {
127
128
File jarFileName = new File("test.jar");
129
jarFileName.delete();
130
String jargs[] = {
131
"cvf",
132
jarFileName.getName(),
133
filename
134
};
135
Utils.jar(jargs);
136
JarFile jfin = null;
137
138
try {
139
jfin = new JarFile(jarFileName);
140
Packer packer = Pack200.newPacker();
141
ByteArrayOutputStream baos = new ByteArrayOutputStream();
142
packer.pack(jfin, baos);
143
baos.flush();
144
baos.close();
145
byte[] buf = baos.toByteArray();
146
147
int minor = buf[4] & 0x000000ff;
148
int major = buf[5] & 0x000000ff;
149
150
if (major != expected_major || minor != expected_minor) {
151
String msg =
152
String.format("test fails: expected:%d.%d but got %d.%d\n",
153
expected_major, expected_minor,
154
major, minor);
155
throw new Error(msg);
156
}
157
158
System.out.println(filename + ": OK");
159
} catch (IOException ioe) {
160
throw new RuntimeException(ioe.getMessage());
161
} finally {
162
Utils.close((Closeable) jfin);
163
}
164
}
165
}
166
167