test/jdk/java/net/httpclient/TestKit.java
author jboes
Fri, 08 Nov 2019 11:15:16 +0000
changeset 59029 3786a0962570
parent 49765 ee6f7a61f3a5
permissions -rw-r--r--
8232853: AuthenticationFilter.Cache::remove may throw ConcurrentModificationException Summary: Change implementation to use iterator instead of plain LinkedList Reviewed-by: dfuchs, vtewari
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     1
/*
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
     2
 * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     4
 *
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     8
 *
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    13
 * accompanied this code).
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    14
 *
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    18
 *
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    21
 * questions.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    22
 */
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    23
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    24
import java.util.AbstractMap;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    25
import java.util.Collection;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    26
import java.util.Comparator;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    27
import java.util.HashMap;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    28
import java.util.Iterator;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    29
import java.util.List;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    30
import java.util.ListIterator;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    31
import java.util.Map;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    32
import java.util.Objects;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    33
import java.util.Set;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    34
import java.util.function.Supplier;
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    35
import java.util.regex.Pattern;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    36
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    37
import static java.util.Collections.emptyList;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    38
import static java.util.Collections.singleton;
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    39
import static java.util.Objects.requireNonNull;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    40
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    41
//
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    42
// A set of testing utility functions
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    43
//
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    44
public final class TestKit {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    45
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    46
    private TestKit() { }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    47
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    48
    public static void assertNotThrows(ThrowingProcedure code) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    49
        requireNonNull(code, "code");
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    50
        assertNotThrows(() -> {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    51
            code.run();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    52
            return null;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    53
        });
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    54
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    55
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    56
    public static <V> V assertNotThrows(ThrowingFunction<V> code) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    57
        requireNonNull(code, "code");
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    58
        try {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    59
            return code.run();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    60
        } catch (Throwable t) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    61
            throw new RuntimeException("Expected to run normally, but threw "
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    62
                    + t.getClass().getCanonicalName(), t);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    63
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    64
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    65
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    66
    public static <T extends Throwable> T assertThrows(Class<T> clazz,
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    67
                                                       ThrowingProcedure code) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    68
        requireNonNull(clazz, "clazz");
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    69
        requireNonNull(code, "code");
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    70
        try {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    71
            code.run();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    72
        } catch (Throwable t) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    73
            if (clazz.isInstance(t)) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    74
                return clazz.cast(t);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    75
            }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    76
            throw new RuntimeException("Expected to catch an exception of type "
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    77
                    + clazz.getCanonicalName() + ", but caught "
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    78
                    + t.getClass().getCanonicalName(), t);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    79
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    80
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    81
        throw new RuntimeException("Expected to catch an exception of type "
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    82
                + clazz.getCanonicalName() + ", but caught nothing");
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    83
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    84
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    85
    public interface ThrowingProcedure {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    86
        void run() throws Throwable;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    87
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    88
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    89
    public interface ThrowingFunction<V> {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    90
        V run() throws Throwable;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    91
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    92
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    93
    // The rationale behind asking for a regex is to not pollute variable names
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    94
    // space in the scope of assertion: if it's something as simple as checking
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    95
    // a message, we can do it inside
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    96
    public static <T extends Throwable> T assertThrows(Class<T> clazz,
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    97
                                                       String messageRegex,
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    98
                                                       ThrowingProcedure code) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    99
        requireNonNull(messageRegex, "messagePattern");
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   100
        T t = assertThrows(clazz, code);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   101
        String m = t.getMessage();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   102
        if (m == null) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   103
            throw new RuntimeException(String.format(
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   104
                    "Expected exception message to match the regex '%s', " +
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   105
                            "but the message was null", messageRegex), t);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   106
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   107
        if (!Pattern.matches(messageRegex, m)) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   108
            throw new RuntimeException(String.format(
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   109
                    "Expected exception message to match the regex '%s', " +
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   110
                            "actual message: %s", messageRegex, m), t);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   111
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   112
        return t;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   113
    }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   114
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   115
    /*
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   116
     * Asserts that the given Collection is unmodifiable: any mutator method
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   117
     * throw an UnsupportedOperationException unconditionally.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   118
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   119
    public static void assertUnmodifiableCollection(Collection<?> collection) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   120
        assertUnmodifiableCollection(collection, () -> null);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   121
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   122
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   123
    public static <E> void assertUnmodifiableCollection(Collection<E> collection,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   124
                                                        Supplier<? extends E> elementsFactory) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   125
        requireNonNull(collection, "collection");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   126
        requireNonNull(elementsFactory, "elementsFactory");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   127
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   128
        E e = elementsFactory.get();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   129
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   130
        assertUOE(() -> collection.add(e));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   131
        assertUOE(() -> collection.addAll(singleton(e)));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   132
        Iterator<?> i = collection.iterator();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   133
        if (i.hasNext()) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   134
            i.next();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   135
            assertUOE(i::remove);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   136
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   137
        assertUOE(collection::clear);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   138
        assertUOE(() -> collection.remove(e));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   139
        assertUOE(() -> collection.removeAll(singleton(e)));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   140
        assertUOE(() -> collection.removeIf(x -> true));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   141
        assertUOE(() -> collection.retainAll(emptyList()));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   142
        // No need to check toArray methods, since API guarantees arrays
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   143
        // returned by them are "safe"
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   144
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   145
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   146
    public static void assertUnmodifiableSet(Set<?> set) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   147
        assertUnmodifiableCollection(set, () -> null);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   148
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   149
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   150
    public static <E> void assertUnmodifiableSet(Set<E> set,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   151
                                                 Supplier<? extends E> elementsFactory) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   152
        assertUnmodifiableCollection(set, elementsFactory);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   153
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   154
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   155
    public static void assertUnmodifiableList(List<?> list) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   156
        assertUnmodifiableList(list, () -> null);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   157
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   158
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   159
    public static <E> void assertUnmodifiableList(List<E> list,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   160
                                                  Supplier<? extends E> elementsFactory) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   161
        assertUnmodifiableList(list, elementsFactory, 3); // This list, its sublist and its sublist's sublist
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   162
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   163
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   164
    private static <E> void assertUnmodifiableList(List<E> list,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   165
                                                   Supplier<? extends E> elementsFactory,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   166
                                                   int depth) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   167
        requireNonNull(list, "list");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   168
        requireNonNull(elementsFactory, "elementsFactory");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   169
        if (depth < 0) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   170
            throw new IllegalArgumentException("depth: " + depth);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   171
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   172
        if (depth == 0) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   173
            return;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   174
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   175
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   176
        E e = elementsFactory.get();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   177
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   178
        assertUnmodifiableCollection(list, elementsFactory);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   179
        assertUOE(() -> list.add(0, e));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   180
        assertUOE(() -> list.addAll(0, singleton(e)));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   181
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   182
        ListIterator<E> i = list.listIterator();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   183
        if (i.hasNext()) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   184
            i.next();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   185
            assertUOE(i::remove);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   186
            assertUOE(() -> i.set(e));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   187
            assertUOE(() -> i.add(e));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   188
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   189
        assertUOE(() -> list.remove((int) 0));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   190
        assertUOE(() -> list.replaceAll(x -> e));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   191
        assertUOE(() -> list.set(0, e));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   192
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   193
        // Any non-null general-purpose Comparator would do
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   194
        Comparator<Object> comparator = (a, b) -> Objects.hash(a, b);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   195
        assertUOE(() -> list.sort(comparator));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   196
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   197
        assertUnmodifiableList(list.subList(0, list.size()), elementsFactory, depth - 1);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   198
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   199
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   200
    public static void assertUnmodifiableMap(Map<?, ?> map) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   201
        assertUnmodifiableMap(map, () -> new AbstractMap.SimpleImmutableEntry<>(null, null));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   202
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   203
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   204
    public static <K, V> void assertUnmodifiableMap(Map<K, V> map,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   205
                                                    Supplier<? extends Map.Entry<? extends K, ? extends V>> entriesFactory) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   206
        requireNonNull(map, "map");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   207
        requireNonNull(entriesFactory, "entriesFactory");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   208
        assertUOE(map::clear);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   209
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   210
        Map.Entry<? extends K, ? extends V> e1 = entriesFactory.get();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   211
        K k = e1.getKey();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   212
        V v = e1.getValue();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   213
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   214
        assertUOE(() -> map.compute(k, (k1, v1) -> v));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   215
        assertUOE(() -> map.computeIfAbsent(k, (k1) -> v));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   216
        assertUOE(() -> map.computeIfPresent(k, (k1, v1) -> v));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   217
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   218
        Set<Map.Entry<K, V>> entrySet = map.entrySet();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   219
        assertUnmodifiableSet(entrySet);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   220
        for (Map.Entry<K, V> e : entrySet) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   221
            assertUOE(() -> e.setValue(null));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   222
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   223
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   224
        assertUnmodifiableSet(map.keySet());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   225
        assertUOE(() -> map.merge(k, v, (k1, v1) -> v));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   226
        assertUOE(() -> map.put(k, v));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   227
        // Map.of(k, v) wouldn't do, as it doesn't permit nulls
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   228
        Map<K, V> m = new HashMap<>();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   229
        m.put(k, v);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   230
        assertUOE(() -> map.putAll(m));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   231
        assertUOE(() -> map.putIfAbsent(k, v));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   232
        assertUOE(() -> map.remove(k));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   233
        assertUOE(() -> map.remove(k, v));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   234
        assertUOE(() -> map.replace(k, v));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   235
        assertUOE(() -> map.replace(k, v, v));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   236
        assertUOE(() -> map.replaceAll((k1, v1) -> v));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   237
        assertUnmodifiableCollection(map.values());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   238
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   239
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   240
    public static void assertUOE(ThrowingProcedure code) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   241
        assertThrows(UnsupportedOperationException.class, code);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   242
    }
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   243
}