|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +import java.net.CookieManager; |
| 25 | +import java.net.CookieStore; |
| 26 | +import java.net.HttpCookie; |
| 27 | +import java.net.URI; |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.List; |
| 30 | + |
| 31 | +import org.junit.jupiter.params.ParameterizedTest; |
| 32 | +import org.junit.jupiter.params.provider.Arguments; |
| 33 | +import org.junit.jupiter.params.provider.MethodSource; |
| 34 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 35 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 36 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 37 | + |
| 38 | +/* |
| 39 | + * @test |
| 40 | + * @bug 8365086 |
| 41 | + * @summary verify that the implementation of java.net.CookieStore works |
| 42 | + * as expected |
| 43 | + * @run junit CookieStoreTest |
| 44 | + */ |
| 45 | +class CookieStoreTest { |
| 46 | + |
| 47 | + // neither the scheme, host nor the port matters in this test |
| 48 | + private static final URI COOKIE_TEST_URI = URI.create("https://127.0.0.1:12345"); |
| 49 | + |
| 50 | + static List<Arguments> cookieStores() { |
| 51 | + final List<Arguments> params = new ArrayList<>(); |
| 52 | + // empty CookieStore |
| 53 | + params.add(Arguments.of(new CookieManager().getCookieStore(), true)); |
| 54 | + |
| 55 | + final CookieStore cookieStore = new CookieManager().getCookieStore(); |
| 56 | + cookieStore.add(COOKIE_TEST_URI, new HttpCookie("foo", "bar")); |
| 57 | + // non-empty CookieStore |
| 58 | + params.add(Arguments.of(cookieStore, false)); |
| 59 | + |
| 60 | + return params; |
| 61 | + } |
| 62 | + |
| 63 | + /* |
| 64 | + * Verify that the List returned by CookieStore.getURIs() is immutable. |
| 65 | + */ |
| 66 | + @ParameterizedTest |
| 67 | + @MethodSource("cookieStores") |
| 68 | + void testImmutableGetURIs(final CookieStore cookieStore, final boolean expectEmpty) { |
| 69 | + final List<URI> uris = cookieStore.getURIs(); |
| 70 | + assertNotNull(uris, "CookieStore.getURIs() returned null"); |
| 71 | + assertEquals(expectEmpty, uris.isEmpty(), "CookieStore.getURIs() returned: " + uris); |
| 72 | + assertImmutableList(uris, COOKIE_TEST_URI); |
| 73 | + } |
| 74 | + |
| 75 | + /* |
| 76 | + * Verify that the List returned by CookieStore.getCookies() is immutable. |
| 77 | + */ |
| 78 | + @ParameterizedTest |
| 79 | + @MethodSource("cookieStores") |
| 80 | + void testImmutableGetCookies(final CookieStore cookieStore, final boolean expectEmpty) { |
| 81 | + final List<HttpCookie> cookies = cookieStore.getCookies(); |
| 82 | + assertNotNull(cookies, "CookieStore.getCookies() returned null"); |
| 83 | + assertEquals(expectEmpty, cookies.isEmpty(), "CookieStore.getCookies() returned: " + cookies); |
| 84 | + assertImmutableList(cookies, new HttpCookie("hello", "world")); |
| 85 | + } |
| 86 | + |
| 87 | + /* |
| 88 | + * Verify that the List returned by CookieStore.get(URI) is immutable. |
| 89 | + */ |
| 90 | + @ParameterizedTest |
| 91 | + @MethodSource("cookieStores") |
| 92 | + void testImmutableGetCookiesForURI(final CookieStore cookieStore, final boolean expectEmpty) { |
| 93 | + final List<HttpCookie> cookies = cookieStore.get(COOKIE_TEST_URI); |
| 94 | + assertNotNull(cookies, "CookieStore.get(URI) returned null"); |
| 95 | + assertEquals(expectEmpty, cookies.isEmpty(), "CookieStore.get(URI) returned: " + cookies); |
| 96 | + assertImmutableList(cookies, new HttpCookie("hello", "world")); |
| 97 | + } |
| 98 | + |
| 99 | + /* |
| 100 | + * Verifies that the attempt to modify the contents of the list will fail |
| 101 | + * due to the list being immutable. |
| 102 | + */ |
| 103 | + private static <T> void assertImmutableList(final List<T> list, T elementToAddOrRemove) { |
| 104 | + // the list is expected to be immutable, so each of these operations must fail |
| 105 | + assertThrows(UnsupportedOperationException.class, () -> list.add(elementToAddOrRemove)); |
| 106 | + assertThrows(UnsupportedOperationException.class, () -> list.remove(elementToAddOrRemove)); |
| 107 | + assertThrows(UnsupportedOperationException.class, list::clear); |
| 108 | + // even try the replace operation when the list isn't empty |
| 109 | + if (!list.isEmpty()) { |
| 110 | + assertThrows(UnsupportedOperationException.class, () -> list.set(0, elementToAddOrRemove)); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments