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