jdk/test/java/util/zip/ConstructDeflaterInput.java
author ctornqvi
Thu, 16 May 2013 15:31:00 +0200
changeset 17598 f78d22d3c6c2
parent 5506 202f599c92aa
child 35959 c4c9ee2cb926
permissions -rw-r--r--
8008169: test/runtime/7158804/Test7158804.sh has bad copyright header Summary: Re-wrote test in Java in addition to fixing the Copyright notice. Also reviewed by leonid.mesnik@oracle.com Reviewed-by: coleenp, ctornqvi Contributed-by: Mikhailo Seledtsov <mikhailo.seledtsov@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4679743
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test parts of DeflaterInputStream code that don't really do I/O.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.zip.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public class ConstructDeflaterInput {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    static class MyDeflater extends Deflater {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
        private boolean ended = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
        boolean getEnded() { return ended; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        public void end() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
            fail("MyDeflater had end() called");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
            super.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public static void realMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        ByteArrayInputStream bais = new ByteArrayInputStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            "hello, world".getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        MyDeflater def = new MyDeflater();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        DeflaterInputStream dis = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        byte[] b = new byte[512];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        // Check construction
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            dis = new DeflaterInputStream(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            fail();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        } catch (NullPointerException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            dis = new DeflaterInputStream(bais, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            fail();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        } catch (NullPointerException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            dis = new DeflaterInputStream(bais, def, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            fail();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        } catch (IllegalArgumentException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        // Check sanity checks in read methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        dis = new DeflaterInputStream(bais, def);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            dis.read(null, 5, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            fail();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        } catch (NullPointerException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            dis.read(b, -1, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            fail();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        } catch (IndexOutOfBoundsException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            dis.read(b, 0, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            fail();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        } catch (IndexOutOfBoundsException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            dis.read(b, 0, 600);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            fail();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        } catch (IndexOutOfBoundsException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            len = dis.read(b, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            check(len == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        } catch (IndexOutOfBoundsException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            fail("Read of length 0 should return 0, but returned " + len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            dis.skip(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            fail();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        } catch (IllegalArgumentException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        // Check unsupported operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        check(!dis.markSupported());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        check(dis.available() == 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        check(!def.getEnded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            dis.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            fail();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        // Check close
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        dis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        check(!def.getEnded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            dis.available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            fail();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            int x = dis.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            fail();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            dis.skip(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            fail();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    static void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    static void fail(String msg) {System.out.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    static void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        System.out.println("\nPassed = " + passed + " failed = " + failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (failed > 0) throw new AssertionError("Some tests failed");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
}