1 /*
   2  * Copyright (c) 2006, 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 4679743
  27  * @summary Test parts of InflaterOutputStream code that don't really do I/O.
  28  */
  29 
  30 import java.io.*;
  31 import java.util.zip.*;
  32 
  33 public class ConstructInflaterOutput {
  34 
  35     static class MyInflater extends Inflater {
  36         private boolean ended = false;
  37         boolean getEnded() { return ended; }
  38         public void end() {
  39             fail("MyInflater had end() called");
  40             super.end();
  41         }
  42     }
  43 
  44     public static void realMain(String[] args) throws Throwable {
  45         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  46         MyInflater inf = new MyInflater();
  47         InflaterOutputStream ios = null;
  48         byte[] b = new byte[512];
  49 
  50         // Check construction
  51         //
  52         try {
  53             ios = new InflaterOutputStream(null);
  54             fail();
  55         } catch (NullPointerException ex) {
  56             pass();
  57         }
  58 
  59         try {
  60             ios = new InflaterOutputStream(baos, null);
  61             fail();
  62         } catch (NullPointerException ex) {
  63             pass();
  64         }
  65 
  66         try {
  67             ios = new InflaterOutputStream(baos, inf, 0);
  68             fail();
  69         } catch (IllegalArgumentException ex) {
  70             pass();
  71         }
  72 
  73         // Check sanity checks in write methods
  74         //
  75         ios = new InflaterOutputStream(baos, inf);
  76 
  77         try {
  78             ios.write(null, 5, 2);
  79             fail();
  80         } catch (NullPointerException ex) {
  81             pass();
  82         }
  83 
  84         try {
  85             ios.write(b, -1, 0);
  86             fail();
  87         } catch (IndexOutOfBoundsException ex) {
  88             pass();
  89         }
  90 
  91         try {
  92             ios.write(b, 0, -1);
  93             fail();
  94         } catch (IndexOutOfBoundsException ex) {
  95             pass();
  96         }
  97 
  98         try {
  99             ios.write(b, 0, 600);
 100             fail();
 101         } catch (IndexOutOfBoundsException ex) {
 102             pass();
 103         }
 104 
 105         ios.flush();
 106         check(!inf.getEnded());
 107         ios.flush();
 108         check(!inf.getEnded());
 109         ios.finish();
 110         check(!inf.getEnded());
 111         ios.close();
 112         check(!inf.getEnded());
 113         try {
 114             ios.finish();
 115             fail();
 116         } catch (IOException ex) {
 117             pass();
 118         }
 119         try {
 120             ios.write(13);
 121             fail();
 122         } catch (IOException ex) {
 123             pass();
 124         }
 125 
 126         ios = new InflaterOutputStream(baos);
 127         ios.flush();
 128         ios.finish();
 129         ios.close();
 130         try {
 131             ios.flush();
 132         } catch (IOException ex) {
 133             pass();
 134         }
 135     }
 136 
 137     //--------------------- Infrastructure ---------------------------
 138     static volatile int passed = 0, failed = 0;
 139     static void pass() {passed++;}
 140     static void fail() {failed++; Thread.dumpStack();}
 141     static void fail(String msg) {System.out.println(msg); fail();}
 142     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
 143     static void check(boolean cond) {if (cond) pass(); else fail();}
 144     static void equal(Object x, Object y) {
 145         if (x == null ? y == null : x.equals(y)) pass();
 146         else fail(x + " not equal to " + y);}
 147     public static void main(String[] args) throws Throwable {
 148         try {realMain(args);} catch (Throwable t) {unexpected(t);}
 149         System.out.println("\nPassed = " + passed + " failed = " + failed);
 150         if (failed > 0) throw new AssertionError("Some tests failed");}
 151 }