Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/tools/pack200/TestExceptions.java
38833 views
/*1* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.ByteArrayOutputStream;24import java.io.Closeable;25import java.io.File;26import java.io.FileInputStream;27import java.io.IOException;28import java.io.InputStream;29import java.io.OutputStream;30import java.util.ArrayList;31import java.util.List;32import java.util.jar.JarFile;33import java.util.jar.JarInputStream;34import java.util.jar.JarOutputStream;35import java.util.jar.Pack200;3637/*38* @test39* @bug 698576340* @summary verify that proper exceptions are thrown41* @compile -XDignore.symbol.file Utils.java TestExceptions.java42* @run main TestExceptions43* @author ksrini44*/4546public class TestExceptions {4748static final File testJar = new File("test.jar");49static final File testPackFile = new File("test.pack");5051static void init() {52Utils.jar("cvf", testJar.getAbsolutePath(), ".");53JarFile jf = null;54try {55jf = new JarFile(testJar);56Utils.pack(jf, testPackFile);57} catch (IOException ioe) {58throw new Error("Initialization error", ioe);59} finally {60Utils.close(jf);61}62}6364// a test that closes the input jarFile.65static void pack200Test1() {66PackTestInput ti = null;67// setup the scenario68try {69ti = new PackTestInput(new JarFile(testJar), new ByteArrayOutputStream());70} catch (Exception e) {71throw new Error("Initialization error", e);72} finally {73Utils.close(ti.getJarFile());74}75// test the scenario76try {77System.out.println(ti);78Pack200.Packer p = Pack200.newPacker();79p.pack(ti.getJarFile(), ti.getOutputStream());80} catch (Exception e) {81ti.checkException(e);82} finally {83if (ti != null) {84ti.close();85}86}87}8889// test the Pack200.pack(JarFile, OutputStream);90static void pack200Test2() {91List<PackTestInput> tlist = new ArrayList<PackTestInput>();92try {93// setup the test scenarios94try {95tlist.add(new PackTestInput((JarFile)null, null));96tlist.add(new PackTestInput(new JarFile(testJar), null));97tlist.add(new PackTestInput((JarFile)null, new ByteArrayOutputStream()));98} catch (Exception e) {99throw new Error("Initialization error", e);100}101102// test the scenarios103for (PackTestInput ti : tlist) {104System.out.println(ti);105try {106Pack200.Packer p = Pack200.newPacker();107p.pack(ti.getJarFile(), ti.getOutputStream());108} catch (Exception e) {109ti.checkException(e);110}111}112} finally { // keep jprt happy113for (TestInput ti : tlist) {114if (ti != null) {115ti.close();116}117}118}119}120121// test the Pack200.pack(JarInputStream, OutputStream);122static void pack200Test3() {123List<PackTestJarInputStream> tlist = new ArrayList<PackTestJarInputStream>();124try {125// setup the test scenarios126try {127tlist.add(new PackTestJarInputStream((JarInputStream)null, null));128tlist.add(new PackTestJarInputStream((JarInputStream)null,129new ByteArrayOutputStream()));130tlist.add(new PackTestJarInputStream(131new JarInputStream(new FileInputStream(testJar)), null));132133} catch (Exception e) {134throw new Error("Initialization error", e);135}136for (PackTestJarInputStream ti : tlist) {137System.out.println(ti);138try {139Pack200.Packer p = Pack200.newPacker();140p.pack(ti.getJarInputStream(), ti.getOutputStream());141} catch (Exception e) {142ti.checkException(e);143}144}145} finally { // keep jprt happy146for (PackTestJarInputStream ti : tlist) {147if (ti != null) {148ti.close();149}150}151}152}153154// test the Pack200.unpack(InputStream, OutputStream);155static void unpack200Test1() {156List<UnpackTestInput> tlist = new ArrayList<UnpackTestInput>();157try {158// setup the test scenarios159try {160tlist.add(new UnpackTestInput((InputStream)null, null));161tlist.add(new UnpackTestInput(new FileInputStream(testPackFile),162null));163tlist.add(new UnpackTestInput((InputStream) null,164new JarOutputStream(new ByteArrayOutputStream())));165} catch (Exception e) {166throw new Error("Initialization error", e);167}168169// test the scenarios170for (UnpackTestInput ti : tlist) {171System.out.println(ti);172try {173Pack200.Unpacker unpacker = Pack200.newUnpacker();174unpacker.unpack(ti.getInputStream(), ti.getJarOutputStream());175} catch (Exception e) {176ti.checkException(e);177}178}179} finally { // keep jprt happy180for (TestInput ti : tlist) {181if (ti != null) {182ti.close();183}184}185}186}187188// test the Pack200.unpack(File, OutputStream);189static void unpack200Test2() {190List<UnpackTestFileInput> tlist = new ArrayList<UnpackTestFileInput>();191try {192// setup the test scenarios193try {194tlist.add(new UnpackTestFileInput((File)null, null));195tlist.add(new UnpackTestFileInput(testPackFile, null));196tlist.add(new UnpackTestFileInput((File)null,197new JarOutputStream(new ByteArrayOutputStream())));198} catch (Exception e) {199throw new Error("Initialization error", e);200}201202// test the scenarios203for (UnpackTestFileInput ti : tlist) {204System.out.println(ti);205try {206Pack200.Unpacker unpacker = Pack200.newUnpacker();207unpacker.unpack(ti.getInputFile(), ti.getJarOutputStream());208} catch (Exception e) {209ti.checkException(e);210}211}212} finally { // keep jprt happy213for (TestInput ti : tlist) {214if (ti != null) {215ti.close();216}217}218}219}220221public static void main(String... args) throws IOException {222init();223pack200Test1();224pack200Test2();225pack200Test3();226unpack200Test1();227Utils.cleanup();228}229230// containers for test inputs and management231static abstract class TestInput {232233private final Object in;234private final Object out;235final boolean shouldNPE;236final String testname;237238public TestInput(String name, Object in, Object out) {239this.testname = name;240this.in = in;241this.out = out;242shouldNPE = (in == null || out == null);243}244245@Override246public String toString() {247StringBuilder outStr = new StringBuilder(testname);248outStr.append(", input:").append(in);249outStr.append(", output:").append(this.out);250outStr.append(", should NPE:").append(shouldNPE);251return outStr.toString();252}253254void close() {255if (in != null && (in instanceof Closeable)) {256Utils.close((Closeable) in);257}258if (out != null && (out instanceof Closeable)) {259Utils.close((Closeable) out);260}261}262263void checkException(Throwable t) {264if (shouldNPE) {265if (t instanceof NullPointerException) {266System.out.println("Got expected exception");267return;268} else {269throw new RuntimeException("Expected NPE, but got ", t);270}271}272if (t instanceof IOException) {273System.out.println("Got expected exception");274return;275} else {276throw new RuntimeException("Expected IOException but got ", t);277}278}279}280281static class PackTestInput extends TestInput {282283public PackTestInput(JarFile jf, OutputStream out) {284super("PackTestInput", jf, out);285}286287JarFile getJarFile() {288return (JarFile) super.in;289}290291OutputStream getOutputStream() {292return (OutputStream) super.out;293}294};295296static class PackTestJarInputStream extends TestInput {297298public PackTestJarInputStream(JarInputStream in, OutputStream out) {299super("PackTestJarInputStream", in, out);300}301302JarInputStream getJarInputStream() {303return (JarInputStream) super.in;304}305306OutputStream getOutputStream() {307return (OutputStream) super.out;308}309};310311static class UnpackTestInput extends TestInput {312313public UnpackTestInput(InputStream in, JarOutputStream out) {314super("UnpackTestInput", in, out);315}316317InputStream getInputStream() {318return (InputStream) super.in;319}320321JarOutputStream getJarOutputStream() {322return (JarOutputStream) super.out;323}324};325326static class UnpackTestFileInput extends TestInput {327328public UnpackTestFileInput(File in, JarOutputStream out) {329super("UnpackTestInput", in, out);330}331332File getInputFile() {333return (File) super.in;334}335336JarOutputStream getJarOutputStream() {337return (JarOutputStream) super.out;338}339};340}341342343