jdk/test/tools/pack200/TestExceptions.java
changeset 7171 ee97f78e7482
child 18594 b6a3c9f71ac8
equal deleted inserted replaced
7170:285c02ecbb8a 7171:ee97f78e7482
       
     1 /*
       
     2  * Copyright (c) 2010, 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 import java.io.ByteArrayOutputStream;
       
    25 import java.io.Closeable;
       
    26 import java.io.File;
       
    27 import java.io.FileInputStream;
       
    28 import java.io.IOException;
       
    29 import java.io.InputStream;
       
    30 import java.io.OutputStream;
       
    31 import java.util.ArrayList;
       
    32 import java.util.List;
       
    33 import java.util.jar.JarFile;
       
    34 import java.util.jar.JarInputStream;
       
    35 import java.util.jar.JarOutputStream;
       
    36 import java.util.jar.Pack200;
       
    37 
       
    38 /*
       
    39  * @test
       
    40  * @bug 6985763
       
    41  * @summary verify that proper exceptions are thrown
       
    42  * @compile -XDignore.symbol.file Utils.java TestExceptions.java
       
    43  * @run main TestExceptions
       
    44  * @author ksrini
       
    45  */
       
    46 
       
    47 public class TestExceptions {
       
    48 
       
    49     static final File testJar = new File("test.jar");
       
    50     static final File testPackFile = new File("test.pack");
       
    51 
       
    52     static void init() {
       
    53         Utils.jar("cvf", testJar.getAbsolutePath(), ".");
       
    54         JarFile jf = null;
       
    55         try {
       
    56             jf = new JarFile(testJar);
       
    57             Utils.pack(jf, testPackFile);
       
    58         } catch (IOException ioe) {
       
    59             throw new Error("Initialization error", ioe);
       
    60         } finally {
       
    61             Utils.close(jf);
       
    62         }
       
    63     }
       
    64 
       
    65     // a test that closes the input jarFile.
       
    66     static void pack200Test1() {
       
    67         PackTestInput ti = null;
       
    68         // setup the scenario
       
    69         try {
       
    70             ti = new PackTestInput(new JarFile(testJar), new ByteArrayOutputStream());
       
    71         } catch (Exception e) {
       
    72             throw new Error("Initialization error", e);
       
    73         } finally {
       
    74             Utils.close(ti.getJarFile());
       
    75         }
       
    76         // test the scenario
       
    77         try {
       
    78             System.out.println(ti);
       
    79             Pack200.Packer p = Pack200.newPacker();
       
    80             p.pack(ti.getJarFile(), ti.getOutputStream());
       
    81         } catch (Exception e) {
       
    82             ti.checkException(e);
       
    83         } finally {
       
    84             if (ti != null) {
       
    85                 ti.close();
       
    86             }
       
    87         }
       
    88     }
       
    89 
       
    90     // test the Pack200.pack(JarFile, OutputStream);
       
    91     static void pack200Test2() {
       
    92         List<PackTestInput> tlist = new ArrayList<PackTestInput>();
       
    93         try {
       
    94             // setup the test scenarios
       
    95             try {
       
    96                 tlist.add(new PackTestInput((JarFile)null, null));
       
    97                 tlist.add(new PackTestInput(new JarFile(testJar), null));
       
    98                 tlist.add(new PackTestInput((JarFile)null, new ByteArrayOutputStream()));
       
    99             } catch (Exception e) {
       
   100                 throw new Error("Initialization error", e);
       
   101             }
       
   102 
       
   103             // test the scenarios
       
   104             for (PackTestInput ti : tlist) {
       
   105                 System.out.println(ti);
       
   106                 try {
       
   107                     Pack200.Packer p = Pack200.newPacker();
       
   108                     p.pack(ti.getJarFile(), ti.getOutputStream());
       
   109                 } catch (Exception e) {
       
   110                     ti.checkException(e);
       
   111                 }
       
   112             }
       
   113         } finally { // keep jprt happy
       
   114             for (TestInput ti : tlist) {
       
   115                 if (ti != null) {
       
   116                     ti.close();
       
   117                 }
       
   118             }
       
   119         }
       
   120     }
       
   121 
       
   122     // test the Pack200.pack(JarInputStream, OutputStream);
       
   123     static void pack200Test3() {
       
   124         List<PackTestJarInputStream> tlist = new ArrayList<PackTestJarInputStream>();
       
   125         try {
       
   126             // setup the test scenarios
       
   127             try {
       
   128                 tlist.add(new PackTestJarInputStream((JarInputStream)null, null));
       
   129                 tlist.add(new PackTestJarInputStream((JarInputStream)null,
       
   130                         new ByteArrayOutputStream()));
       
   131                 tlist.add(new PackTestJarInputStream(
       
   132                         new JarInputStream(new FileInputStream(testJar)), null));
       
   133 
       
   134             } catch (Exception e) {
       
   135                 throw new Error("Initialization error", e);
       
   136             }
       
   137             for (PackTestJarInputStream ti : tlist) {
       
   138                 System.out.println(ti);
       
   139                 try {
       
   140                     Pack200.Packer p = Pack200.newPacker();
       
   141                     p.pack(ti.getJarInputStream(), ti.getOutputStream());
       
   142                 } catch (Exception e) {
       
   143                     ti.checkException(e);
       
   144                 }
       
   145             }
       
   146         } finally { // keep jprt happy
       
   147             for (PackTestJarInputStream ti : tlist) {
       
   148                 if (ti != null) {
       
   149                     ti.close();
       
   150                 }
       
   151             }
       
   152         }
       
   153     }
       
   154 
       
   155     // test the Pack200.unpack(InputStream, OutputStream);
       
   156     static void unpack200Test1() {
       
   157         List<UnpackTestInput> tlist = new ArrayList<UnpackTestInput>();
       
   158         try {
       
   159             // setup the test scenarios
       
   160             try {
       
   161                 tlist.add(new UnpackTestInput((InputStream)null, null));
       
   162                 tlist.add(new UnpackTestInput(new FileInputStream(testPackFile),
       
   163                         null));
       
   164                 tlist.add(new UnpackTestInput((InputStream) null,
       
   165                         new JarOutputStream(new ByteArrayOutputStream())));
       
   166             } catch (Exception e) {
       
   167                 throw new Error("Initialization error", e);
       
   168             }
       
   169 
       
   170             // test the scenarios
       
   171             for (UnpackTestInput ti : tlist) {
       
   172                 System.out.println(ti);
       
   173                 try {
       
   174                     Pack200.Unpacker unpacker = Pack200.newUnpacker();
       
   175                     unpacker.unpack(ti.getInputStream(), ti.getJarOutputStream());
       
   176                 } catch (Exception e) {
       
   177                     ti.checkException(e);
       
   178                 }
       
   179             }
       
   180         } finally { // keep jprt happy
       
   181             for (TestInput ti : tlist) {
       
   182                 if (ti != null) {
       
   183                     ti.close();
       
   184                 }
       
   185             }
       
   186         }
       
   187     }
       
   188 
       
   189     // test the Pack200.unpack(File, OutputStream);
       
   190     static void unpack200Test2() {
       
   191         List<UnpackTestFileInput> tlist = new ArrayList<UnpackTestFileInput>();
       
   192         try {
       
   193             // setup the test scenarios
       
   194             try {
       
   195                 tlist.add(new UnpackTestFileInput((File)null, null));
       
   196                 tlist.add(new UnpackTestFileInput(testPackFile, null));
       
   197                 tlist.add(new UnpackTestFileInput((File)null,
       
   198                         new JarOutputStream(new ByteArrayOutputStream())));
       
   199             } catch (Exception e) {
       
   200                 throw new Error("Initialization error", e);
       
   201             }
       
   202 
       
   203             // test the scenarios
       
   204             for (UnpackTestFileInput ti : tlist) {
       
   205                 System.out.println(ti);
       
   206                 try {
       
   207                     Pack200.Unpacker unpacker = Pack200.newUnpacker();
       
   208                     unpacker.unpack(ti.getInputFile(), ti.getJarOutputStream());
       
   209                 } catch (Exception e) {
       
   210                     ti.checkException(e);
       
   211                 }
       
   212             }
       
   213         } finally { // keep jprt happy
       
   214             for (TestInput ti : tlist) {
       
   215                 if (ti != null) {
       
   216                     ti.close();
       
   217                 }
       
   218             }
       
   219         }
       
   220     }
       
   221 
       
   222     public static void main(String... args) {
       
   223         init();
       
   224         pack200Test1();
       
   225         pack200Test2();
       
   226         pack200Test3();
       
   227         unpack200Test1();
       
   228     }
       
   229 
       
   230     // containers for test inputs and management
       
   231     static abstract class TestInput {
       
   232 
       
   233         private final Object in;
       
   234         private final Object out;
       
   235         final boolean shouldNPE;
       
   236         final String testname;
       
   237 
       
   238         public TestInput(String name, Object in, Object out) {
       
   239             this.testname = name;
       
   240             this.in = in;
       
   241             this.out = out;
       
   242             shouldNPE = (in == null || out == null);
       
   243         }
       
   244 
       
   245         @Override
       
   246         public String toString() {
       
   247             StringBuilder outStr = new StringBuilder(testname);
       
   248             outStr.append(", input:").append(in);
       
   249             outStr.append(", output:").append(this.out);
       
   250             outStr.append(", should NPE:").append(shouldNPE);
       
   251             return outStr.toString();
       
   252         }
       
   253 
       
   254         void close() {
       
   255             if (in != null && (in instanceof Closeable)) {
       
   256                 Utils.close((Closeable) in);
       
   257             }
       
   258             if (out != null && (out instanceof Closeable)) {
       
   259                 Utils.close((Closeable) out);
       
   260             }
       
   261         }
       
   262 
       
   263         void checkException(Throwable t) {
       
   264             if (shouldNPE) {
       
   265                 if (t instanceof NullPointerException) {
       
   266                     System.out.println("Got expected exception");
       
   267                     return;
       
   268                 } else {
       
   269                     throw new RuntimeException("Expected NPE, but got ", t);
       
   270                 }
       
   271             }
       
   272             if (t instanceof IOException) {
       
   273                 System.out.println("Got expected exception");
       
   274                 return;
       
   275             } else {
       
   276                 throw new RuntimeException("Expected IOException but got ", t);
       
   277             }
       
   278         }
       
   279     }
       
   280 
       
   281     static class PackTestInput extends TestInput {
       
   282 
       
   283         public PackTestInput(JarFile jf, OutputStream out) {
       
   284             super("PackTestInput", jf, out);
       
   285         }
       
   286 
       
   287         JarFile getJarFile() {
       
   288             return (JarFile) super.in;
       
   289         }
       
   290 
       
   291         OutputStream getOutputStream() {
       
   292             return (OutputStream) super.out;
       
   293         }
       
   294     };
       
   295 
       
   296     static class PackTestJarInputStream extends TestInput {
       
   297 
       
   298         public PackTestJarInputStream(JarInputStream in, OutputStream out) {
       
   299             super("PackTestJarInputStream", in, out);
       
   300         }
       
   301 
       
   302         JarInputStream getJarInputStream() {
       
   303             return (JarInputStream) super.in;
       
   304         }
       
   305 
       
   306         OutputStream getOutputStream() {
       
   307             return (OutputStream) super.out;
       
   308         }
       
   309     };
       
   310 
       
   311     static class UnpackTestInput extends TestInput {
       
   312 
       
   313         public UnpackTestInput(InputStream in, JarOutputStream out) {
       
   314             super("UnpackTestInput", in, out);
       
   315         }
       
   316 
       
   317         InputStream getInputStream() {
       
   318             return (InputStream) super.in;
       
   319         }
       
   320 
       
   321         JarOutputStream getJarOutputStream() {
       
   322             return (JarOutputStream) super.out;
       
   323         }
       
   324     };
       
   325 
       
   326     static class UnpackTestFileInput extends TestInput {
       
   327 
       
   328         public UnpackTestFileInput(File in, JarOutputStream out) {
       
   329             super("UnpackTestInput", in, out);
       
   330         }
       
   331 
       
   332         File getInputFile() {
       
   333             return (File) super.in;
       
   334         }
       
   335 
       
   336         JarOutputStream getJarOutputStream() {
       
   337             return (JarOutputStream) super.out;
       
   338         }
       
   339     };
       
   340 }