Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/tools/pack200/PackageVersionTest.java
38833 views
/*1* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 6712743 6991164 716840126* @summary verify package versions27* @compile -XDignore.symbol.file Utils.java PackageVersionTest.java28* @run main PackageVersionTest29* @author ksrini30*/3132import java.io.ByteArrayOutputStream;33import java.io.Closeable;34import java.io.File;35import java.io.FileOutputStream;36import java.io.IOException;37import java.io.PrintStream;38import java.util.jar.JarFile;39import java.util.jar.Pack200;40import java.util.jar.Pack200.Packer;41import java.util.jar.Pack200.Unpacker;4243public class PackageVersionTest {44private static final File javaHome = new File(System.getProperty("java.home"));4546public final static int JAVA5_PACKAGE_MAJOR_VERSION = 150;47public final static int JAVA5_PACKAGE_MINOR_VERSION = 7;4849public final static int JAVA6_PACKAGE_MAJOR_VERSION = 160;50public final static int JAVA6_PACKAGE_MINOR_VERSION = 1;5152public final static int JAVA7_PACKAGE_MAJOR_VERSION = 170;53public final static int JAVA7_PACKAGE_MINOR_VERSION = 1;5455public static void main(String... args) throws IOException {56if (!javaHome.getName().endsWith("jre")) {57throw new RuntimeException("Error: requires an SDK to run");58}5960File out = new File("test.pack");61createClassFile("Test5");62createClassFile("Test6");63createClassFile("Test7");6465verify6991164();66verifyPack("Test5.class", JAVA5_PACKAGE_MAJOR_VERSION,67JAVA5_PACKAGE_MINOR_VERSION);6869verifyPack("Test6.class", JAVA6_PACKAGE_MAJOR_VERSION,70JAVA6_PACKAGE_MINOR_VERSION);7172// a jar file devoid of indy classes must generate 160.1 package file73verifyPack("Test7.class", JAVA6_PACKAGE_MAJOR_VERSION,74JAVA6_PACKAGE_MINOR_VERSION);7576// test for resource file, ie. no class files77verifyPack("Test6.java", JAVA5_PACKAGE_MAJOR_VERSION,78JAVA5_PACKAGE_MINOR_VERSION);79Utils.cleanup();80}8182static void verify6991164() {83Unpacker unpacker = Pack200.newUnpacker();84String versionStr = unpacker.toString();85String expected = "Pack200, Vendor: " +86System.getProperty("java.vendor") + ", Version: " +87JAVA7_PACKAGE_MAJOR_VERSION + "." + JAVA7_PACKAGE_MINOR_VERSION;88if (!versionStr.equals(expected)) {89System.out.println("Expected: " + expected);90System.out.println("Obtained: " + versionStr);91throw new RuntimeException("did not get expected string " + expected);92}93}9495static void createClassFile(String name) {96createJavaFile(name);97String target = name.substring(name.length() - 1);98String javacCmds[] = {99"-source",100"5",101"-target",102name.substring(name.length() - 1),103name + ".java"104};105Utils.compiler(javacCmds);106}107108static void createJavaFile(String name) {109PrintStream ps = null;110FileOutputStream fos = null;111File outputFile = new File(name + ".java");112outputFile.delete();113try {114fos = new FileOutputStream(outputFile);115ps = new PrintStream(fos);116ps.format("public class %s {}", name);117} catch (IOException ioe) {118throw new RuntimeException("creation of test file failed");119} finally {120Utils.close(ps);121Utils.close(fos);122}123}124125static void verifyPack(String filename, int expected_major, int expected_minor) {126127File jarFileName = new File("test.jar");128jarFileName.delete();129String jargs[] = {130"cvf",131jarFileName.getName(),132filename133};134Utils.jar(jargs);135JarFile jfin = null;136137try {138jfin = new JarFile(jarFileName);139Packer packer = Pack200.newPacker();140ByteArrayOutputStream baos = new ByteArrayOutputStream();141packer.pack(jfin, baos);142baos.flush();143baos.close();144byte[] buf = baos.toByteArray();145146int minor = buf[4] & 0x000000ff;147int major = buf[5] & 0x000000ff;148149if (major != expected_major || minor != expected_minor) {150String msg =151String.format("test fails: expected:%d.%d but got %d.%d\n",152expected_major, expected_minor,153major, minor);154throw new Error(msg);155}156157System.out.println(filename + ": OK");158} catch (IOException ioe) {159throw new RuntimeException(ioe.getMessage());160} finally {161Utils.close((Closeable) jfin);162}163}164}165166167