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 DeflaterInputStream code that don't really do I/O. 28 */ 29 30 import java.io.*; 31 import java.util.zip.*; 32 33 public class ConstructDeflaterInput { 34 35 static class MyDeflater extends Deflater { 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 MyDeflater def = new MyDeflater(); 45 ByteArrayInputStream bais = new ByteArrayInputStream( 46 "hello, world".getBytes()); 47 DeflaterInputStream dis = null; 48 byte[] b = new byte[512]; 49 50 // Check construction 51 // 52 try { 53 dis = new DeflaterInputStream(null); 54 fail(); 55 } catch (NullPointerException ex) { 56 pass(); 57 } 58 59 try { 60 dis = new DeflaterInputStream(bais, null); 61 fail(); 62 } catch (NullPointerException ex) { 63 pass(); 64 } 65 66 try { 67 dis = new DeflaterInputStream(bais, def, 0); 68 fail(); 69 } catch (IllegalArgumentException ex) { 70 pass(); 71 } 72 73 // Check sanity checks in read methods 74 // 75 dis = new DeflaterInputStream(bais, def); 76 77 try { 78 dis.read(null, 5, 2); 79 fail(); 80 } catch (NullPointerException ex) { 81 pass(); 82 } 83 84 try { 85 dis.read(b, -1, 0); 86 fail(); 87 } catch (IndexOutOfBoundsException ex) { 88 pass(); 89 } 90 91 try { 92 dis.read(b, 0, -1); 93 fail(); 94 } catch (IndexOutOfBoundsException ex) { 95 pass(); 96 } 97 98 try { 99 dis.read(b, 0, 600); 100 fail(); 101 } catch (IndexOutOfBoundsException ex) { 102 pass(); 103 } 104 105 int len = 0; 106 try { 107 len = dis.read(b, 0, 0); 108 check(len == 0); 109 } catch (IndexOutOfBoundsException ex) { 110 fail("Read of length 0 should return 0, but returned " + len); 111 } 112 113 try { 114 dis.skip(-1); 115 fail(); 116 } catch (IllegalArgumentException ex) { 117 pass(); 118 } 119 120 // Check unsupported operations 121 // 122 check(!dis.markSupported()); 123 check(dis.available() == 1); 124 check(!def.ended); 125 try { 126 dis.reset(); 127 fail(); 128 } catch (IOException ex) { 129 pass(); 130 } 131 132 // Check close 133 // 134 dis.close(); 135 check(!def.ended); 136 137 try { 138 dis.available(); 139 fail(); 140 } catch (IOException ex) { 141 pass(); 142 } 143 144 try { 145 int x = dis.read(); 146 fail(); 147 } catch (IOException ex) { 148 pass(); 149 } 150 151 try { 152 dis.skip(1); 153 fail(); 154 } catch (IOException ex) { 155 pass(); 156 } 157 java.lang.ref.Reference.reachabilityFence(def); 158 } 159 160 //--------------------- Infrastructure --------------------------- 161 static volatile int passed = 0, failed = 0; pass()162 static void pass() {passed++;} fail()163 static void fail() {failed++; Thread.dumpStack();} fail(String msg)164 static void fail(String msg) {System.out.println(msg); fail();} unexpected(Throwable t)165 static void unexpected(Throwable t) {failed++; t.printStackTrace();} check(boolean cond)166 static void check(boolean cond) {if (cond) pass(); else fail();} equal(Object x, Object y)167 static void equal(Object x, Object y) { 168 if (x == null ? y == null : x.equals(y)) pass(); 169 else fail(x + " not equal to " + y);} main(String[] args)170 public static void main(String[] args) throws Throwable { 171 try {realMain(args);} catch (Throwable t) {unexpected(t);} 172 System.out.println("\nPassed = " + passed + " failed = " + failed); 173 if (failed > 0) throw new AssertionError("Some tests failed");} 174 } 175