3042N/A/*
3042N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3042N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3042N/A *
3042N/A * This code is free software; you can redistribute it and/or modify it
3042N/A * under the terms of the GNU General Public License version 2 only, as
3042N/A * published by the Free Software Foundation.
3042N/A *
3042N/A * This code is distributed in the hope that it will be useful, but WITHOUT
3042N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3042N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3042N/A * version 2 for more details (a copy is included in the LICENSE file that
3042N/A * accompanied this code).
3042N/A *
3042N/A * You should have received a copy of the GNU General Public License version
3042N/A * 2 along with this work; if not, write to the Free Software Foundation,
3042N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3042N/A *
3042N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3042N/A * or visit www.oracle.com if you need additional information or have any
3042N/A * questions.
3042N/A */
3042N/A
3042N/Aimport java.io.ByteArrayOutputStream;
3042N/Aimport java.io.Closeable;
3042N/Aimport java.io.File;
3042N/Aimport java.io.FileInputStream;
3042N/Aimport java.io.IOException;
3042N/Aimport java.io.InputStream;
3042N/Aimport java.io.OutputStream;
3042N/Aimport java.util.ArrayList;
3042N/Aimport java.util.List;
3042N/Aimport java.util.jar.JarFile;
3042N/Aimport java.util.jar.JarInputStream;
3042N/Aimport java.util.jar.JarOutputStream;
3042N/Aimport java.util.jar.Pack200;
3042N/A
3042N/A/*
3042N/A * @test
3042N/A * @bug 6985763
3042N/A * @summary verify that proper exceptions are thrown
3042N/A * @compile -XDignore.symbol.file Utils.java TestExceptions.java
3042N/A * @run main TestExceptions
3042N/A * @author ksrini
3042N/A */
3042N/A
3042N/Apublic class TestExceptions {
3042N/A
3042N/A static final File testJar = new File("test.jar");
3042N/A static final File testPackFile = new File("test.pack");
3042N/A
3042N/A static void init() {
3042N/A Utils.jar("cvf", testJar.getAbsolutePath(), ".");
3042N/A JarFile jf = null;
3042N/A try {
3042N/A jf = new JarFile(testJar);
3042N/A Utils.pack(jf, testPackFile);
3042N/A } catch (IOException ioe) {
3042N/A throw new Error("Initialization error", ioe);
3042N/A } finally {
3042N/A Utils.close(jf);
3042N/A }
3042N/A }
3042N/A
3042N/A // a test that closes the input jarFile.
3042N/A static void pack200Test1() {
3042N/A PackTestInput ti = null;
3042N/A // setup the scenario
3042N/A try {
3042N/A ti = new PackTestInput(new JarFile(testJar), new ByteArrayOutputStream());
3042N/A } catch (Exception e) {
3042N/A throw new Error("Initialization error", e);
3042N/A } finally {
3042N/A Utils.close(ti.getJarFile());
3042N/A }
3042N/A // test the scenario
3042N/A try {
3042N/A System.out.println(ti);
3042N/A Pack200.Packer p = Pack200.newPacker();
3042N/A p.pack(ti.getJarFile(), ti.getOutputStream());
3042N/A } catch (Exception e) {
3042N/A ti.checkException(e);
3042N/A } finally {
3042N/A if (ti != null) {
3042N/A ti.close();
3042N/A }
3042N/A }
3042N/A }
3042N/A
3042N/A // test the Pack200.pack(JarFile, OutputStream);
3042N/A static void pack200Test2() {
3042N/A List<PackTestInput> tlist = new ArrayList<PackTestInput>();
3042N/A try {
3042N/A // setup the test scenarios
3042N/A try {
3042N/A tlist.add(new PackTestInput((JarFile)null, null));
3042N/A tlist.add(new PackTestInput(new JarFile(testJar), null));
3042N/A tlist.add(new PackTestInput((JarFile)null, new ByteArrayOutputStream()));
3042N/A } catch (Exception e) {
3042N/A throw new Error("Initialization error", e);
3042N/A }
3042N/A
3042N/A // test the scenarios
3042N/A for (PackTestInput ti : tlist) {
3042N/A System.out.println(ti);
3042N/A try {
3042N/A Pack200.Packer p = Pack200.newPacker();
3042N/A p.pack(ti.getJarFile(), ti.getOutputStream());
3042N/A } catch (Exception e) {
3042N/A ti.checkException(e);
3042N/A }
3042N/A }
3042N/A } finally { // keep jprt happy
3042N/A for (TestInput ti : tlist) {
3042N/A if (ti != null) {
3042N/A ti.close();
3042N/A }
3042N/A }
3042N/A }
3042N/A }
3042N/A
3042N/A // test the Pack200.pack(JarInputStream, OutputStream);
3042N/A static void pack200Test3() {
3042N/A List<PackTestJarInputStream> tlist = new ArrayList<PackTestJarInputStream>();
3042N/A try {
3042N/A // setup the test scenarios
3042N/A try {
3042N/A tlist.add(new PackTestJarInputStream((JarInputStream)null, null));
3042N/A tlist.add(new PackTestJarInputStream((JarInputStream)null,
3042N/A new ByteArrayOutputStream()));
3042N/A tlist.add(new PackTestJarInputStream(
3042N/A new JarInputStream(new FileInputStream(testJar)), null));
3042N/A
3042N/A } catch (Exception e) {
3042N/A throw new Error("Initialization error", e);
3042N/A }
3042N/A for (PackTestJarInputStream ti : tlist) {
3042N/A System.out.println(ti);
3042N/A try {
3042N/A Pack200.Packer p = Pack200.newPacker();
3042N/A p.pack(ti.getJarInputStream(), ti.getOutputStream());
3042N/A } catch (Exception e) {
3042N/A ti.checkException(e);
3042N/A }
3042N/A }
3042N/A } finally { // keep jprt happy
3042N/A for (PackTestJarInputStream ti : tlist) {
3042N/A if (ti != null) {
3042N/A ti.close();
3042N/A }
3042N/A }
3042N/A }
3042N/A }
3042N/A
3042N/A // test the Pack200.unpack(InputStream, OutputStream);
3042N/A static void unpack200Test1() {
3042N/A List<UnpackTestInput> tlist = new ArrayList<UnpackTestInput>();
3042N/A try {
3042N/A // setup the test scenarios
3042N/A try {
3042N/A tlist.add(new UnpackTestInput((InputStream)null, null));
3042N/A tlist.add(new UnpackTestInput(new FileInputStream(testPackFile),
3042N/A null));
3042N/A tlist.add(new UnpackTestInput((InputStream) null,
3042N/A new JarOutputStream(new ByteArrayOutputStream())));
3042N/A } catch (Exception e) {
3042N/A throw new Error("Initialization error", e);
3042N/A }
3042N/A
3042N/A // test the scenarios
3042N/A for (UnpackTestInput ti : tlist) {
3042N/A System.out.println(ti);
3042N/A try {
3042N/A Pack200.Unpacker unpacker = Pack200.newUnpacker();
3042N/A unpacker.unpack(ti.getInputStream(), ti.getJarOutputStream());
3042N/A } catch (Exception e) {
3042N/A ti.checkException(e);
3042N/A }
3042N/A }
3042N/A } finally { // keep jprt happy
3042N/A for (TestInput ti : tlist) {
3042N/A if (ti != null) {
3042N/A ti.close();
3042N/A }
3042N/A }
3042N/A }
3042N/A }
3042N/A
3042N/A // test the Pack200.unpack(File, OutputStream);
3042N/A static void unpack200Test2() {
3042N/A List<UnpackTestFileInput> tlist = new ArrayList<UnpackTestFileInput>();
3042N/A try {
3042N/A // setup the test scenarios
3042N/A try {
3042N/A tlist.add(new UnpackTestFileInput((File)null, null));
3042N/A tlist.add(new UnpackTestFileInput(testPackFile, null));
3042N/A tlist.add(new UnpackTestFileInput((File)null,
3042N/A new JarOutputStream(new ByteArrayOutputStream())));
3042N/A } catch (Exception e) {
3042N/A throw new Error("Initialization error", e);
3042N/A }
3042N/A
3042N/A // test the scenarios
3042N/A for (UnpackTestFileInput ti : tlist) {
3042N/A System.out.println(ti);
3042N/A try {
3042N/A Pack200.Unpacker unpacker = Pack200.newUnpacker();
3042N/A unpacker.unpack(ti.getInputFile(), ti.getJarOutputStream());
3042N/A } catch (Exception e) {
3042N/A ti.checkException(e);
3042N/A }
3042N/A }
3042N/A } finally { // keep jprt happy
3042N/A for (TestInput ti : tlist) {
3042N/A if (ti != null) {
3042N/A ti.close();
3042N/A }
3042N/A }
3042N/A }
3042N/A }
3042N/A
3042N/A public static void main(String... args) {
3042N/A init();
3042N/A pack200Test1();
3042N/A pack200Test2();
3042N/A pack200Test3();
3042N/A unpack200Test1();
3042N/A }
3042N/A
3042N/A // containers for test inputs and management
3042N/A static abstract class TestInput {
3042N/A
3042N/A private final Object in;
3042N/A private final Object out;
3042N/A final boolean shouldNPE;
3042N/A final String testname;
3042N/A
3042N/A public TestInput(String name, Object in, Object out) {
3042N/A this.testname = name;
3042N/A this.in = in;
3042N/A this.out = out;
3042N/A shouldNPE = (in == null || out == null);
3042N/A }
3042N/A
3042N/A @Override
3042N/A public String toString() {
3042N/A StringBuilder outStr = new StringBuilder(testname);
3042N/A outStr.append(", input:").append(in);
3042N/A outStr.append(", output:").append(this.out);
3042N/A outStr.append(", should NPE:").append(shouldNPE);
3042N/A return outStr.toString();
3042N/A }
3042N/A
3042N/A void close() {
3042N/A if (in != null && (in instanceof Closeable)) {
3042N/A Utils.close((Closeable) in);
3042N/A }
3042N/A if (out != null && (out instanceof Closeable)) {
3042N/A Utils.close((Closeable) out);
3042N/A }
3042N/A }
3042N/A
3042N/A void checkException(Throwable t) {
3042N/A if (shouldNPE) {
3042N/A if (t instanceof NullPointerException) {
3042N/A System.out.println("Got expected exception");
3042N/A return;
3042N/A } else {
3042N/A throw new RuntimeException("Expected NPE, but got ", t);
3042N/A }
3042N/A }
3042N/A if (t instanceof IOException) {
3042N/A System.out.println("Got expected exception");
3042N/A return;
3042N/A } else {
3042N/A throw new RuntimeException("Expected IOException but got ", t);
3042N/A }
3042N/A }
3042N/A }
3042N/A
3042N/A static class PackTestInput extends TestInput {
3042N/A
3042N/A public PackTestInput(JarFile jf, OutputStream out) {
3042N/A super("PackTestInput", jf, out);
3042N/A }
3042N/A
3042N/A JarFile getJarFile() {
3042N/A return (JarFile) super.in;
3042N/A }
3042N/A
3042N/A OutputStream getOutputStream() {
3042N/A return (OutputStream) super.out;
3042N/A }
3042N/A };
3042N/A
3042N/A static class PackTestJarInputStream extends TestInput {
3042N/A
3042N/A public PackTestJarInputStream(JarInputStream in, OutputStream out) {
3042N/A super("PackTestJarInputStream", in, out);
3042N/A }
3042N/A
3042N/A JarInputStream getJarInputStream() {
3042N/A return (JarInputStream) super.in;
3042N/A }
3042N/A
3042N/A OutputStream getOutputStream() {
3042N/A return (OutputStream) super.out;
3042N/A }
3042N/A };
3042N/A
3042N/A static class UnpackTestInput extends TestInput {
3042N/A
3042N/A public UnpackTestInput(InputStream in, JarOutputStream out) {
3042N/A super("UnpackTestInput", in, out);
3042N/A }
3042N/A
3042N/A InputStream getInputStream() {
3042N/A return (InputStream) super.in;
3042N/A }
3042N/A
3042N/A JarOutputStream getJarOutputStream() {
3042N/A return (JarOutputStream) super.out;
3042N/A }
3042N/A };
3042N/A
3042N/A static class UnpackTestFileInput extends TestInput {
3042N/A
3042N/A public UnpackTestFileInput(File in, JarOutputStream out) {
3042N/A super("UnpackTestInput", in, out);
3042N/A }
3042N/A
3042N/A File getInputFile() {
3042N/A return (File) super.in;
3042N/A }
3042N/A
3042N/A JarOutputStream getJarOutputStream() {
3042N/A return (JarOutputStream) super.out;
3042N/A }
3042N/A };
3042N/A}