test/jdk/java/io/InputStream/TransferTo.java
author dholmes
Wed, 06 Nov 2019 21:18:42 -0500
changeset 58956 9a0a5e70eeb2
parent 47216 71c04702a3d5
permissions -rw-r--r--
8233454: Test fails with assert(!is_init_completed(), "should only happen during init") after JDK-8229516 Reviewed-by: jiefu, dcubed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28065
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
     1
/*
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
     4
 *
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
     7
 * published by the Free Software Foundation.
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
     8
 *
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    13
 * accompanied this code).
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    14
 *
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    18
 *
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    21
 * questions.
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    22
 */
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    23
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    24
import java.io.ByteArrayOutputStream;
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    25
import java.io.FilterInputStream;
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    26
import java.io.FilterOutputStream;
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    27
import java.io.IOException;
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    28
import java.io.InputStream;
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    29
import java.io.OutputStream;
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    30
import java.util.Arrays;
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    31
import java.util.Random;
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    32
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    33
import static java.lang.String.format;
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    34
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    35
/*
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    36
 * @test
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    37
 * @bug 8066867
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    38
 * @summary tests whether java.io.InputStream.transferTo conforms to its
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    39
 *          contract defined in the javadoc
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 28065
diff changeset
    40
 * @key randomness
28065
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    41
 */
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    42
public class TransferTo {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    43
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    44
    public static void main(String[] args) throws IOException {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    45
        ifOutIsNullThenNpeIsThrown();
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    46
        ifExceptionInInputNeitherStreamIsClosed();
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    47
        ifExceptionInOutputNeitherStreamIsClosed();
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    48
        onReturnNeitherStreamIsClosed();
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    49
        onReturnInputIsAtEnd();
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    50
        contents();
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    51
    }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    52
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    53
    private static void ifOutIsNullThenNpeIsThrown() throws IOException {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    54
        try (InputStream in = input()) {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    55
            assertThrowsNPE(() -> in.transferTo(null), "out");
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    56
        }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    57
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    58
        try (InputStream in = input((byte) 1)) {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    59
            assertThrowsNPE(() -> in.transferTo(null), "out");
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    60
        }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    61
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    62
        try (InputStream in = input((byte) 1, (byte) 2)) {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    63
            assertThrowsNPE(() -> in.transferTo(null), "out");
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    64
        }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    65
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    66
        InputStream in = null;
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    67
        try {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    68
            InputStream fin = in = new ThrowingInputStream();
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    69
            // null check should precede everything else:
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    70
            // InputStream shouldn't be touched if OutputStream is null
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    71
            assertThrowsNPE(() -> fin.transferTo(null), "out");
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    72
        } finally {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    73
            if (in != null)
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    74
                try {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    75
                    in.close();
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    76
                } catch (IOException ignored) { }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    77
        }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    78
    }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    79
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    80
    private static void ifExceptionInInputNeitherStreamIsClosed()
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    81
            throws IOException {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    82
        transferToThenCheckIfAnyClosed(input(0, new byte[]{1, 2, 3}), output());
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    83
        transferToThenCheckIfAnyClosed(input(1, new byte[]{1, 2, 3}), output());
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    84
        transferToThenCheckIfAnyClosed(input(2, new byte[]{1, 2, 3}), output());
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    85
    }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    86
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    87
    private static void ifExceptionInOutputNeitherStreamIsClosed()
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    88
            throws IOException {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    89
        transferToThenCheckIfAnyClosed(input(new byte[]{1, 2, 3}), output(0));
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    90
        transferToThenCheckIfAnyClosed(input(new byte[]{1, 2, 3}), output(1));
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    91
        transferToThenCheckIfAnyClosed(input(new byte[]{1, 2, 3}), output(2));
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    92
    }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    93
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    94
    private static void transferToThenCheckIfAnyClosed(InputStream input,
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    95
                                                       OutputStream output)
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    96
            throws IOException {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    97
        try (CloseLoggingInputStream in = new CloseLoggingInputStream(input);
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    98
             CloseLoggingOutputStream out =
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
    99
                     new CloseLoggingOutputStream(output)) {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   100
            boolean thrown = false;
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   101
            try {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   102
                in.transferTo(out);
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   103
            } catch (IOException ignored) {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   104
                thrown = true;
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   105
            }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   106
            if (!thrown)
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   107
                throw new AssertionError();
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   108
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   109
            if (in.wasClosed() || out.wasClosed()) {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   110
                throw new AssertionError();
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   111
            }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   112
        }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   113
    }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   114
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   115
    private static void onReturnNeitherStreamIsClosed()
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   116
            throws IOException {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   117
        try (CloseLoggingInputStream in =
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   118
                     new CloseLoggingInputStream(input(new byte[]{1, 2, 3}));
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   119
             CloseLoggingOutputStream out =
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   120
                     new CloseLoggingOutputStream(output())) {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   121
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   122
            in.transferTo(out);
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   123
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   124
            if (in.wasClosed() || out.wasClosed()) {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   125
                throw new AssertionError();
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   126
            }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   127
        }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   128
    }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   129
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   130
    private static void onReturnInputIsAtEnd() throws IOException {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   131
        try (InputStream in = input(new byte[]{1, 2, 3});
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   132
             OutputStream out = output()) {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   133
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   134
            in.transferTo(out);
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   135
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   136
            if (in.read() != -1) {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   137
                throw new AssertionError();
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   138
            }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   139
        }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   140
    }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   141
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   142
    private static void contents() throws IOException {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   143
        checkTransferredContents(new byte[0]);
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   144
        checkTransferredContents(createRandomBytes(1024, 4096));
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   145
        // to span through several batches
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   146
        checkTransferredContents(createRandomBytes(16384, 16384));
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   147
    }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   148
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   149
    private static void checkTransferredContents(byte[] bytes)
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   150
            throws IOException {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   151
        try (InputStream in = input(bytes);
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   152
             ByteArrayOutputStream out = new ByteArrayOutputStream()) {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   153
            in.transferTo(out);
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   154
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   155
            byte[] outBytes = out.toByteArray();
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   156
            if (!Arrays.equals(bytes, outBytes)) {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   157
                throw new AssertionError(
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   158
                        format("bytes.length=%s, outBytes.length=%s",
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   159
                                bytes.length, outBytes.length));
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   160
            }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   161
        }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   162
    }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   163
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   164
    private static byte[] createRandomBytes(int min, int maxRandomAdditive) {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   165
        Random rnd = new Random();
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   166
        byte[] bytes = new byte[min + rnd.nextInt(maxRandomAdditive)];
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   167
        rnd.nextBytes(bytes);
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   168
        return bytes;
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   169
    }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   170
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   171
    private static OutputStream output() {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   172
        return output(-1);
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   173
    }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   174
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   175
    private static OutputStream output(int exceptionPosition) {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   176
        return new OutputStream() {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   177
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   178
            int pos;
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   179
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   180
            @Override
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   181
            public void write(int b) throws IOException {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   182
                if (pos++ == exceptionPosition)
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   183
                    throw new IOException();
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   184
            }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   185
        };
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   186
    }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   187
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   188
    private static InputStream input(byte... bytes) {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   189
        return input(-1, bytes);
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   190
    }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   191
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   192
    private static InputStream input(int exceptionPosition, byte... bytes) {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   193
        return new InputStream() {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   194
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   195
            int pos;
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   196
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   197
            @Override
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   198
            public int read() throws IOException {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   199
                if (pos == exceptionPosition) {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   200
                    // because of the pesky IOException swallowing in
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   201
                    // java.io.InputStream.read(byte[], int, int)
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   202
                    // pos++;
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   203
                    throw new IOException();
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   204
                }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   205
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   206
                if (pos >= bytes.length)
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   207
                    return -1;
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   208
                return bytes[pos++] & 0xff;
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   209
            }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   210
        };
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   211
    }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   212
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   213
    private static class ThrowingInputStream extends InputStream {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   214
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   215
        boolean closed;
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   216
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   217
        @Override
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   218
        public int read(byte[] b) throws IOException {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   219
            throw new IOException();
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   220
        }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   221
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   222
        @Override
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   223
        public int read(byte[] b, int off, int len) throws IOException {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   224
            throw new IOException();
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   225
        }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   226
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   227
        @Override
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   228
        public long skip(long n) throws IOException {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   229
            throw new IOException();
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   230
        }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   231
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   232
        @Override
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   233
        public int available() throws IOException {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   234
            throw new IOException();
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   235
        }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   236
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   237
        @Override
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   238
        public void close() throws IOException {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   239
            if (!closed) {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   240
                closed = true;
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   241
                throw new IOException();
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   242
            }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   243
        }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   244
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   245
        @Override
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   246
        public void reset() throws IOException {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   247
            throw new IOException();
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   248
        }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   249
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   250
        @Override
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   251
        public int read() throws IOException {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   252
            throw new IOException();
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   253
        }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   254
    }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   255
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   256
    private static class CloseLoggingInputStream extends FilterInputStream {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   257
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   258
        boolean closed;
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   259
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   260
        CloseLoggingInputStream(InputStream in) {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   261
            super(in);
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   262
        }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   263
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   264
        @Override
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   265
        public void close() throws IOException {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   266
            closed = true;
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   267
            super.close();
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   268
        }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   269
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   270
        boolean wasClosed() {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   271
            return closed;
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   272
        }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   273
    }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   274
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   275
    private static class CloseLoggingOutputStream extends FilterOutputStream {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   276
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   277
        boolean closed;
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   278
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   279
        CloseLoggingOutputStream(OutputStream out) {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   280
            super(out);
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   281
        }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   282
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   283
        @Override
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   284
        public void close() throws IOException {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   285
            closed = true;
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   286
            super.close();
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   287
        }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   288
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   289
        boolean wasClosed() {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   290
            return closed;
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   291
        }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   292
    }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   293
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   294
    public interface Thrower {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   295
        public void run() throws Throwable;
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   296
    }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   297
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   298
    public static void assertThrowsNPE(Thrower thrower, String message) {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   299
        assertThrows(thrower, NullPointerException.class, message);
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   300
    }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   301
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   302
    public static <T extends Throwable> void assertThrows(Thrower thrower,
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   303
                                                          Class<T> throwable,
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   304
                                                          String message) {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   305
        Throwable thrown;
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   306
        try {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   307
            thrower.run();
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   308
            thrown = null;
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   309
        } catch (Throwable caught) {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   310
            thrown = caught;
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   311
        }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   312
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   313
        if (!throwable.isInstance(thrown)) {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   314
            String caught = thrown == null ?
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   315
                    "nothing" : thrown.getClass().getCanonicalName();
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   316
            throw new AssertionError(
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   317
                    format("Expected to catch %s, but caught %s",
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   318
                            throwable, caught), thrown);
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   319
        }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   320
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   321
        if (thrown != null && !message.equals(thrown.getMessage())) {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   322
            throw new AssertionError(
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   323
                    format("Expected exception message to be '%s', but it's '%s'",
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   324
                            message, thrown.getMessage()));
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   325
        }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   326
    }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents:
diff changeset
   327
}