-
Notifications
You must be signed in to change notification settings - Fork 6.1k
8365086: CookieStore.getURIs() and get(URI) should return an immutable List #26698
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
aace799
914fde9
7b92b0a
f6ff8fa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think trying There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. I've updated the PR to include that additional check. |
||
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)); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise you get a warning that the comment block should start with a
/**
:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello Volkan, is that warning generated by a IDE? I don't see it in IntelliJ:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we usually put the jtreg directive block before the imports.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jaikiran, I remember seeing it highlighted somewhere, but apparently I am mistaken. Nevermind. It is all good.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello Chen,
It's a mix of before imports and above the class declaration in the JDK repo. I personally prefer just above the class since it's easily visible. Several of the tests in the networking area follow this currently.