Code example for CookieStore

Methods: add, getCookies

0
     * @tests java.net.CookieStore#getCookies() 
     *  
     * @since 1.6 
     */ 
    public void test_getCookies() throws URISyntaxException { 
        List<HttpCookie> list = cookieStore.getCookies();
        assertTrue(list.isEmpty());
        assertTrue(list instanceof RandomAccess);
 
        HttpCookie cookie1 = new HttpCookie("cookie_name", "cookie_value");
        URI uri1 = new URI("http://getcookies1.test.org");
        cookieStore.add(uri1, cookie1);
        list = cookieStore.getCookies();
        assertTrue(list.contains(cookie1));
 
        HttpCookie cookie2 = new HttpCookie("cookie_name2", "cookie_value2");
        URI uri2 = new URI("http://getcookies2.test.org");
        cookieStore.add(uri2, cookie2);
        list = cookieStore.getCookies();
        assertEquals(2, list.size());
        assertTrue(list.contains(cookie1));
        assertTrue(list.contains(cookie2));
 
        // duplicated cookie won't be selected. 
        cookieStore.add(uri2, cookie1);
        list = cookieStore.getCookies();
        assertEquals(2, list.size());
        // expired cookie won't be selected. 
        HttpCookie cookie3 = new HttpCookie("cookie_name3", "cookie_value3");
        cookie3.setMaxAge(-1357);
        cookieStore.add(uri1, cookie3);
        list = cookieStore.getCookies();
        assertEquals(2, list.size());
 
        try { 
            list.add(new HttpCookie("readOnlyName", "readOnlyValue"));
            fail("should throw UnsupportedOperationException");