Path: blob/master/test/jdk/java/net/CookieHandler/NullUriCookieTest.java
41149 views
/*1* Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 6953455 704565526* @summary CookieStore.add() cannot handle null URI parameter27* and An empty InMemoryCookieStore should not return true for removeAll28*/2930import java.net.CookieManager;31import java.net.CookieStore;32import java.net.HttpCookie;33import java.net.URI;34import java.net.URISyntaxException;35import java.util.List;3637public class NullUriCookieTest {38static boolean fail = false;3940public static void main(String[] args) throws Exception {41checkCookieNullUri();42}4344static void checkCookieNullUri() throws Exception {45//get a cookie store implementation and add a cookie to the store with null URI46CookieStore cookieStore = (new CookieManager()).getCookieStore();47//Check if removeAll() retrurns false on an empty CookieStore48if (cookieStore.removeAll()) {49fail = true;50}51checkFail("removeAll on empty store should return false");52HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");53cookie.setDomain("foo.com");54cookieStore.add(null, cookie);5556//Retrieve added cookie57URI uri = new URI("http://foo.com");58List<HttpCookie> addedCookieList = cookieStore.get(uri);5960//Verify CookieStore behaves well61if (addedCookieList.size() != 1) {62fail = true;63}64checkFail("Abnormal size of cookie jar");6566for (HttpCookie chip : addedCookieList) {67if (!chip.equals(cookie)) {68fail = true;69}70}71checkFail("Cookie not retrieved from Cookie Jar");72boolean ret = cookieStore.remove(null,cookie);73if (!ret) {74fail = true;75}76checkFail("Abnormal removal behaviour from Cookie Jar");77}7879static void checkFail(String exp) {80if (fail) {81throw new RuntimeException(exp);82}83}84}85868788