jdk/test/java/net/CookieHandler/NullUriCookieTest.java
changeset 10698 b2da906f8080
child 12192 dd8301ce22b6
equal deleted inserted replaced
10697:ecee258b7d87 10698:b2da906f8080
       
     1 /*
       
     2  * Copyright (c) 2011, 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 /*
       
    25  * @test
       
    26  * @bug 6953455
       
    27  * @summary CookieStore.add() cannot handle null URI parameter
       
    28  */
       
    29 
       
    30 import java.net.CookieManager;
       
    31 import java.net.CookieStore;
       
    32 import java.net.HttpCookie;
       
    33 import java.net.URI;
       
    34 import java.net.URISyntaxException;
       
    35 import java.util.List;
       
    36 
       
    37 public class NullUriCookieTest {
       
    38     static boolean fail = false;
       
    39 
       
    40     public static void main(String[] args) throws Exception {
       
    41         checkCookieNullUri();
       
    42     }
       
    43 
       
    44     static void checkCookieNullUri() throws Exception {
       
    45         //get a cookie store implementation and add a cookie to the store with null URI
       
    46         CookieStore cookieStore = (new CookieManager()).getCookieStore();
       
    47         HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");
       
    48         cookie.setDomain("foo.com");
       
    49         cookieStore.add(null, cookie);
       
    50 
       
    51         //Retrieve added cookie
       
    52         URI uri = new URI("http://foo.com");
       
    53         List<HttpCookie> addedCookieList = cookieStore.get(uri);
       
    54 
       
    55         //Verify CookieStore behaves well
       
    56         if (addedCookieList.size() != 1) {
       
    57            fail = true;
       
    58         }
       
    59         checkFail("Abnormal size of cookie jar");
       
    60 
       
    61         for (HttpCookie chip : addedCookieList) {
       
    62             if (!chip.equals(cookie)) {
       
    63                  fail = true;
       
    64             }
       
    65         }
       
    66         checkFail("Cookie not retrieved from Cookie Jar");
       
    67         boolean ret = cookieStore.remove(null,cookie);
       
    68         if (!ret) {
       
    69             fail = true;
       
    70         }
       
    71         checkFail("Abnormal removal behaviour from Cookie Jar");
       
    72     }
       
    73 
       
    74     static void checkFail(String exp) {
       
    75         if (fail) {
       
    76             throw new RuntimeException(exp);
       
    77         }
       
    78     }
       
    79 }
       
    80