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("Test6"); 63 createClassFile("Test7"); 64 65 verify6991164(); 66 67 verifyPack("Test6.class", JAVA6_PACKAGE_MAJOR_VERSION, 68 JAVA6_PACKAGE_MINOR_VERSION); 69 70 // a jar file devoid of indy classes must generate 160.1 package file 71 verifyPack("Test7.class", JAVA6_PACKAGE_MAJOR_VERSION, 72 JAVA6_PACKAGE_MINOR_VERSION); 73 74 // test for resource file, ie. no class files 75 verifyPack("Test6.java", JAVA5_PACKAGE_MAJOR_VERSION, 76 JAVA5_PACKAGE_MINOR_VERSION); 77 Utils.cleanup(); 78 } 79 80 static void verify6991164() { 81 Unpacker unpacker = Pack200.newUnpacker(); 82 String versionStr = unpacker.toString(); 83 String expected = "Pack200, Vendor: " + 84 System.getProperty("java.vendor") + ", Version: " + 85 JAVA7_PACKAGE_MAJOR_VERSION + "." + JAVA7_PACKAGE_MINOR_VERSION; 86 if (!versionStr.equals(expected)) { 87 System.out.println("Expected: " + expected); 88 System.out.println("Obtained: " + versionStr); 89 throw new RuntimeException("did not get expected string " + expected); 90 } 91 } 92 93 static void createClassFile(String name) { 94 createJavaFile(name); 95 String target = name.substring(name.length() - 1); 96 String javacCmds[] = { 97 "-source", 98 "6", 99 "-target", 100 name.substring(name.length() - 1), 101 name + ".java" 102 }; 103 Utils.compiler(javacCmds); 104 } 105 106 static void createJavaFile(String name) { 107 PrintStream ps = null; 108 FileOutputStream fos = null; 109 File outputFile = new File(name + ".java"); 110 outputFile.delete(); 111 try { 112 fos = new FileOutputStream(outputFile); 113 ps = new PrintStream(fos); 114 ps.format("public class %s {}", name); 115 } catch (IOException ioe) { 116 throw new RuntimeException("creation of test file failed"); 117 } finally { 118 Utils.close(ps); 119 Utils.close(fos); 120 } 121 } 122 123 static void verifyPack(String filename, int expected_major, int expected_minor) { 124 125 File jarFileName = new File("test.jar"); 126 jarFileName.delete(); 127 String jargs[] = { 128 "cvf", 129 jarFileName.getName(), 130 filename 131 }; 132 Utils.jar(jargs); 133 JarFile jfin = null; 134 135 try { 136 jfin = new JarFile(jarFileName); 137 Packer packer = Pack200.newPacker(); 138 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 139 packer.pack(jfin, baos); 140 baos.flush(); 141 baos.close(); 142 byte[] buf = baos.toByteArray(); 143 144 int minor = buf[4] & 0x000000ff; 145 int major = buf[5] & 0x000000ff; 146 147 if (major != expected_major || minor != expected_minor) { 148 String msg = 149 String.format("test fails: expected:%d.%d but got %d.%d\n", 150 expected_major, expected_minor, 151 major, minor); 152 throw new Error(msg); 153 } 154 155 System.out.println(filename + ": OK"); 156 } catch (IOException ioe) { 157 throw new RuntimeException(ioe.getMessage()); 158 } finally { 159 Utils.close((Closeable) jfin); 160 } 161 } 162 }