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