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 8148624
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         volatile boolean ended = false;
end()37         public void end() {
38             ended = true;
39             super.end();
40         }
41     }
42 
realMain(String[] args)43     public static void realMain(String[] args) throws Throwable {
44         final MyInflater inf = new MyInflater();
45         ByteArrayOutputStream baos = new ByteArrayOutputStream();
46         InflaterOutputStream ios = null;
47         byte[] b = new byte[512];
48 
49         // Check construction
50         //
51         try {
52             ios = new InflaterOutputStream(null);
53             fail();
54         } catch (NullPointerException ex) {
55             pass();
56         }
57 
58         try {
59             ios = new InflaterOutputStream(baos, null);
60             fail();
61         } catch (NullPointerException ex) {
62             pass();
63         }
64 
65         try {
66             ios = new InflaterOutputStream(baos, inf, 0);
67             fail();
68         } catch (IllegalArgumentException ex) {
69             pass();
70         }
71 
72         // Check sanity checks in write methods
73         //
74         ios = new InflaterOutputStream(baos, inf);
75 
76         try {
77             ios.write(null, 5, 2);
78             fail();
79         } catch (NullPointerException ex) {
80             pass();
81         }
82 
83         try {
84             ios.write(b, -1, 0);
85             fail();
86         } catch (IndexOutOfBoundsException ex) {
87             pass();
88         }
89 
90         try {
91             ios.write(b, 0, -1);
92             fail();
93         } catch (IndexOutOfBoundsException ex) {
94             pass();
95         }
96 
97         try {
98             ios.write(b, 0, 600);
99             fail();
100         } catch (IndexOutOfBoundsException ex) {
101             pass();
102         }
103 
104         ios.flush();
105         check(!inf.ended);
106         ios.flush();
107         check(!inf.ended);
108         ios.finish();
109         check(!inf.ended);
110         ios.close();
111         check(!inf.ended);
112         try {
113             ios.finish();
114             fail();
115         } catch (IOException ex) {
116             pass();
117         }
118         try {
119             ios.write(13);
120             fail();
121         } catch (IOException ex) {
122             pass();
123         }
124 
125         ios = new InflaterOutputStream(baos);
126         ios.flush();
127         ios.finish();
128         ios.close();
129         try {
130             ios.flush();
131         } catch (IOException ex) {
132             pass();
133         }
134         java.lang.ref.Reference.reachabilityFence(inf);
135     }
136 
137     //--------------------- Infrastructure ---------------------------
138     static volatile int passed = 0, failed = 0;
pass()139     static void pass() {passed++;}
fail()140     static void fail() {failed++; Thread.dumpStack();}
fail(String msg)141     static void fail(String msg) {System.out.println(msg); fail();}
unexpected(Throwable t)142     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
check(boolean cond)143     static void check(boolean cond) {if (cond) pass(); else fail();}
equal(Object x, Object y)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);}
main(String[] args)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 }
152