test/jdk/java/net/httpclient/websocket/Support.java
author chegar
Thu, 26 Apr 2018 14:42:36 +0100
branchhttp-client-branch
changeset 56486 b664a887e277
parent 56451 9585061fdb04
permissions -rw-r--r--
http-client-branch: set NO_PROXY in several WebSocket tests
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     1
/*
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     4
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     8
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    13
 * accompanied this code).
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    14
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    18
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    21
 * questions.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    22
 */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    23
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    24
import java.io.IOException;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    25
import java.nio.ByteBuffer;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    26
import java.nio.channels.SocketChannel;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    27
import java.nio.charset.StandardCharsets;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    28
import java.util.Arrays;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    29
import java.util.concurrent.CompletableFuture;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    30
import java.util.concurrent.CompletionException;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    31
import java.util.concurrent.CompletionStage;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    32
import java.util.concurrent.TimeUnit;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    33
import java.util.concurrent.TimeoutException;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    34
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    35
import static org.testng.Assert.assertThrows;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    36
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    37
public class Support {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    38
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    39
    private Support() { }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    40
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    41
    public static void assertFails(Class<? extends Throwable> clazz,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    42
                                    CompletionStage<?> stage) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    43
        Support.assertCompletesExceptionally(clazz, stage);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    44
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    45
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    46
    public static void assertCompletesExceptionally(Class<? extends Throwable> clazz,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    47
                                                    CompletionStage<?> stage) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    48
        CompletableFuture<?> cf =
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    49
                CompletableFuture.completedFuture(null).thenCompose(x -> stage);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    50
        assertThrows(clazz, () -> {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    51
            try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    52
                cf.join();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    53
            } catch (CompletionException e) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    54
                throw e.getCause();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    55
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    56
        });
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    57
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    58
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    59
    public static void assertHangs(CompletionStage<?> stage) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    60
        Support.assertDoesNotCompleteWithin(5, TimeUnit.SECONDS, stage);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    61
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    62
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    63
    public static void assertDoesNotCompleteWithin(long timeout,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    64
                                                   TimeUnit unit,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    65
                                                   CompletionStage<?> stage) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    66
        CompletableFuture<?> cf =
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    67
                CompletableFuture.completedFuture(null).thenCompose(x -> stage);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    68
        assertThrows(TimeoutException.class, () -> cf.get(timeout, unit));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    69
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    70
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    71
    public static ByteBuffer fullCopy(ByteBuffer src) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    72
        ByteBuffer copy = ByteBuffer.allocate(src.capacity());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    73
        int p = src.position();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    74
        int l = src.limit();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    75
        src.clear();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    76
        copy.put(src).position(p).limit(l);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    77
        src.position(p).limit(l);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    78
        return copy;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    79
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    80
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    81
    public static DummyWebSocketServer serverWithCannedData(int... data) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    82
        byte[] copy = new byte[data.length];
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    83
        for (int i = 0; i < data.length; i++) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    84
            copy[i] = (byte) data[i];
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    85
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    86
        return serverWithCannedData(copy);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    87
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    88
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    89
    public static DummyWebSocketServer serverWithCannedData(byte... data) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    90
        byte[] copy = Arrays.copyOf(data, data.length);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    91
        return new DummyWebSocketServer() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    92
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    93
            protected void write(SocketChannel ch) throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    94
                int off = 0; int n = 1; // 1 byte at a time
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    95
                while (off + n < copy.length + n) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    96
//                    try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    97
//                        TimeUnit.MICROSECONDS.sleep(500);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    98
//                    } catch (InterruptedException e) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    99
//                        return;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   100
//                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   101
                    int len = Math.min(copy.length - off, n);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   102
                    ByteBuffer bytes = ByteBuffer.wrap(copy, off, len);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   103
                    off += len;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   104
                    ch.write(bytes);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   105
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   106
                super.write(ch);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   107
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   108
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   109
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   110
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   111
    /*
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   112
     * This server does not read from the wire, allowing its client to fill up
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   113
     * their send buffer. Used to test scenarios with outstanding send
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   114
     * operations.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   115
     */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   116
    public static DummyWebSocketServer notReadingServer() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   117
        return new DummyWebSocketServer() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   118
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   119
            protected void read(SocketChannel ch) throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   120
                try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   121
                    Thread.sleep(Long.MAX_VALUE);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   122
                } catch (InterruptedException e) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   123
                    throw new IOException(e);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   124
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   125
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   126
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   127
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   128
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   129
    public static DummyWebSocketServer writingServer(int... data) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   130
        byte[] copy = new byte[data.length];
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   131
        for (int i = 0; i < data.length; i++) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   132
            copy[i] = (byte) data[i];
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   133
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   134
        return new DummyWebSocketServer() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   135
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   136
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   137
            protected void read(SocketChannel ch) throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   138
                try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   139
                    Thread.sleep(Long.MAX_VALUE);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   140
                } catch (InterruptedException e) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   141
                    throw new IOException(e);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   142
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   143
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   144
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   145
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   146
            protected void write(SocketChannel ch) throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   147
                int off = 0; int n = 1; // 1 byte at a time
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   148
                while (off + n < copy.length + n) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   149
//                    try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   150
//                        TimeUnit.MICROSECONDS.sleep(500);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   151
//                    } catch (InterruptedException e) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   152
//                        return;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   153
//                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   154
                    int len = Math.min(copy.length - off, n);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   155
                    ByteBuffer bytes = ByteBuffer.wrap(copy, off, len);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   156
                    off += len;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   157
                    ch.write(bytes);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   158
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   159
                super.write(ch);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   160
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   161
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   162
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   163
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   164
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   165
    public static String stringWith2NBytes(int n) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   166
        // -- Russian Alphabet (33 characters, 2 bytes per char) --
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   167
        char[] abc = {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   168
                0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0401, 0x0416,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   169
                0x0417, 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   170
                0x041F, 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   171
                0x0427, 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   172
                0x042F,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   173
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   174
        // repeat cyclically
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   175
        StringBuilder sb = new StringBuilder(n);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   176
        for (int i = 0, j = 0; i < n; i++, j = (j + 1) % abc.length) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   177
            sb.append(abc[j]);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   178
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   179
        String s = sb.toString();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   180
        assert s.length() == n && s.getBytes(StandardCharsets.UTF_8).length == 2 * n;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   181
        return s;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   182
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   183
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   184
    public static String malformedString() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   185
        return new String(new char[]{0xDC00, 0xD800});
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   186
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   187
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   188
    public static String incompleteString() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   189
        return new String(new char[]{0xD800});
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   190
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   191
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   192
    public static String stringWithNBytes(int n) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   193
        char[] chars = new char[n];
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   194
        Arrays.fill(chars, 'A');
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   195
        return new String(chars);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   196
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   197
}