jdk/test/tools/pack200/PackageVersionTest.java
changeset 6314 8ab691ddb904
parent 5811 f4d1f45c0058
child 6891 f8a528363fa5
equal deleted inserted replaced
6313:470912c9e214 6314:8ab691ddb904
    20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    21  * or visit www.oracle.com if you need additional information or have any
    21  * or visit www.oracle.com if you need additional information or have any
    22  * questions.
    22  * questions.
    23  */
    23  */
    24 
    24 
    25 /**
    25 /*
    26   * @test
    26  * @test
    27   * @bug 6712743
    27  * @bug 6712743
    28   * @summary verify package versioning
    28  * @summary verify package versions
    29   * @compile -XDignore.symbol.file PackageVersionTest.java
    29  * @compile -XDignore.symbol.file Utils.java PackageVersionTest.java
    30   * @run main PackageVersionTest
    30  * @run main PackageVersionTest
    31   */
    31  * @author ksrini
       
    32  */
    32 
    33 
    33 import java.io.ByteArrayOutputStream;
    34 import java.io.ByteArrayOutputStream;
    34 import java.io.Closeable;
    35 import java.io.Closeable;
    35 import java.io.File;
    36 import java.io.File;
    36 import java.io.FileOutputStream;
    37 import java.io.FileOutputStream;
    72         // test for resource file, ie. no class files
    73         // test for resource file, ie. no class files
    73         verifyPack("Test6.java", JAVA5_PACKAGE_MAJOR_VERSION,
    74         verifyPack("Test6.java", JAVA5_PACKAGE_MAJOR_VERSION,
    74                 JAVA5_PACKAGE_MINOR_VERSION);
    75                 JAVA5_PACKAGE_MINOR_VERSION);
    75     }
    76     }
    76 
    77 
    77     static void close(Closeable c) {
       
    78         if (c == null) {
       
    79             return;
       
    80         }
       
    81         try {
       
    82             c.close();
       
    83         } catch (IOException ignore) {}
       
    84     }
       
    85 
    78 
    86     static void createClassFile(String name) {
    79     static void createClassFile(String name) {
    87         createJavaFile(name);
    80         createJavaFile(name);
    88         String target = name.substring(name.length() - 1);
    81         String target = name.substring(name.length() - 1);
    89         String javacCmds[] = {
    82         String javacCmds[] = {
    91             "5",
    84             "5",
    92             "-target",
    85             "-target",
    93             name.substring(name.length() - 1),
    86             name.substring(name.length() - 1),
    94             name + ".java"
    87             name + ".java"
    95         };
    88         };
    96         compileJava(javacCmds);
    89         Utils.compiler(javacCmds);
    97     }
    90     }
    98 
    91 
    99     static void createJavaFile(String name) {
    92     static void createJavaFile(String name) {
   100         PrintStream ps = null;
    93         PrintStream ps = null;
   101         FileOutputStream fos = null;
    94         FileOutputStream fos = null;
   106             ps = new PrintStream(fos);
    99             ps = new PrintStream(fos);
   107             ps.format("public class %s {}", name);
   100             ps.format("public class %s {}", name);
   108         } catch (IOException ioe) {
   101         } catch (IOException ioe) {
   109             throw new RuntimeException("creation of test file failed");
   102             throw new RuntimeException("creation of test file failed");
   110         } finally {
   103         } finally {
   111             close(ps);
   104             Utils.close(ps);
   112             close(fos);
   105             Utils.close(fos);
   113         }
       
   114     }
       
   115 
       
   116     static void compileJava(String... javacCmds) {
       
   117         if (com.sun.tools.javac.Main.compile(javacCmds) != 0) {
       
   118             throw new RuntimeException("compilation failed");
       
   119         }
       
   120     }
       
   121 
       
   122     static void makeJar(String... jargs) {
       
   123         sun.tools.jar.Main jarTool =
       
   124                 new sun.tools.jar.Main(System.out, System.err, "jartool");
       
   125         if (!jarTool.run(jargs)) {
       
   126             throw new RuntimeException("jar command failed");
       
   127         }
   106         }
   128     }
   107     }
   129 
   108 
   130     static void verifyPack(String filename, int expected_major, int expected_minor) {
   109     static void verifyPack(String filename, int expected_major, int expected_minor) {
   131 
   110 
   134         String jargs[] = {
   113         String jargs[] = {
   135             "cvf",
   114             "cvf",
   136             jarFileName.getName(),
   115             jarFileName.getName(),
   137             filename
   116             filename
   138         };
   117         };
   139         makeJar(jargs);
   118         Utils.jar(jargs);
   140         JarFile jfin = null;
   119         JarFile jfin = null;
   141 
   120 
   142         try {
   121         try {
   143             jfin = new JarFile(jarFileName);
   122             jfin = new JarFile(jarFileName);
   144             Packer packer = Pack200.newPacker();
   123             Packer packer = Pack200.newPacker();
   161 
   140 
   162             System.out.println(filename + ": OK");
   141             System.out.println(filename + ": OK");
   163         } catch (IOException ioe) {
   142         } catch (IOException ioe) {
   164             throw new RuntimeException(ioe.getMessage());
   143             throw new RuntimeException(ioe.getMessage());
   165         } finally {
   144         } finally {
   166             close(jfin);
   145             Utils.close((Closeable) jfin);
   167         }
   146         }
   168     }
   147     }
   169 }
   148 }