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 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 "7", 95 "-target", 96 "7", 97 "-Xlint:-options", 98 name + ".java" 99 }; 100 Utils.compiler(javacCmds); 101 } 102 103 static void createJavaFile(String name) { 104 PrintStream ps = null; 105 FileOutputStream fos = null; 106 File outputFile = new File(name + ".java"); 107 outputFile.delete(); 108 try { 109 fos = new FileOutputStream(outputFile); 110 ps = new PrintStream(fos); 111 ps.format("public class %s {}", name); 112 } catch (IOException ioe) { 113 throw new RuntimeException("creation of test file failed"); 114 } finally { 115 Utils.close(ps); 116 Utils.close(fos); 117 } 118 } 119 120 static void verifyPack(String filename, int expected_major, int expected_minor) { 121 122 File jarFileName = new File("test.jar"); 123 jarFileName.delete(); 124 String jargs[] = { 125 "cvf", 126 jarFileName.getName(), 127 filename 128 }; 129 Utils.jar(jargs); 130 JarFile jfin = null; 131 132 try { 133 jfin = new JarFile(jarFileName); 134 Packer packer = Pack200.newPacker(); 135 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 136 packer.pack(jfin, baos); 137 baos.flush(); 138 baos.close(); 139 byte[] buf = baos.toByteArray(); 140 141 int minor = buf[4] & 0x000000ff; 142 int major = buf[5] & 0x000000ff; 143 144 if (major != expected_major || minor != expected_minor) { 145 String msg = 146 String.format("test fails: expected:%d.%d but got %d.%d\n", 147 expected_major, expected_minor, 148 major, minor); 149 throw new Error(msg); 150 } 151 152 System.out.println(filename + ": OK"); 153 } catch (IOException ioe) { 154 throw new RuntimeException(ioe.getMessage()); 155 } finally { 156 Utils.close((Closeable) jfin); 157 } 158 } 159 }