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