Code example for CookieStore

Methods: add, get, getCookies, remove

0
     *  
     * @since 1.6 
     */ 
    public void test_get_LURI() throws URISyntaxException { 
        try { 
            cookieStore.get(null);
            fail("should throw NullPointerException");
        } catch (NullPointerException e) {
            // expected 
        } 
 
        URI uri1 = new URI("http://get.uri1.test.org");
        List<HttpCookie> list = cookieStore.get(uri1);
        assertTrue(list.isEmpty());
        assertTrue(list instanceof ArrayList);
 
        HttpCookie cookie1 = new HttpCookie("cookie_name1", "cookie_value1");
        HttpCookie cookie2 = new HttpCookie("cookie_name2", "cookie_value2");
        cookieStore.add(uri1, cookie1);
        cookieStore.add(uri1, cookie2);
        URI uri2 = new URI("http://get.uri2.test.org");
        HttpCookie cookie3 = new HttpCookie("cookie_name3", "cookie_value3");
        cookieStore.add(uri2, cookie3);
        list = cookieStore.get(uri1);
        assertEquals(2, list.size());
        list = cookieStore.get(uri2);
        assertEquals(1, list.size());        
 
        // domain-match cookies also be selected. 
        HttpCookie cookie4 = new HttpCookie("cookie_name4", "cookie_value4");
        cookie4.setDomain(".uri1.test.org");
        cookieStore.add(uri2, cookie4);
        list = cookieStore.get(uri1);
        assertEquals(3, list.size());
         
        cookieStore.add(uri1, cookie4);
        list = cookieStore.get(uri1);
        assertEquals(3, list.size());
        list = cookieStore.get(uri2);
        assertEquals(2, list.size());
 
        // expired cookie won't be selected. 
        HttpCookie cookie5 = new HttpCookie("cookie_name5", "cookie_value5");
        cookie5.setMaxAge(-333);
        assertTrue(cookie5.hasExpired());
        cookieStore.add(uri1, cookie5);
        list = cookieStore.get(uri1);
        assertEquals(3, list.size());
        assertFalse(cookieStore.remove(uri1, cookie5));
        list = cookieStore.getCookies();
        assertEquals(4, list.size());
         
        cookie4.setMaxAge(-123);
        list = cookieStore.get(uri1);
        assertEquals(2, list.size());
        list = cookieStore.getCookies();
        assertEquals(3, list.size());
        // expired cookies are also deleted even if it domain-matches the URI 
        HttpCookie cookie6 = new HttpCookie("cookie_name6", "cookie_value6");
        cookie6.setMaxAge(-2);
        cookie6.setDomain(".uri1.test.org");
        cookieStore.add(uri2, cookie6);
        list = cookieStore.get(uri1);
        assertEquals(2, list.size());
        assertFalse(cookieStore.remove(null, cookie6));
 
        URI uri3 = new URI("http://get.uri3.test.org");
        assertTrue(cookieStore.get(uri3).isEmpty());
        URI baduri = new URI("invalid_uri");
        assertTrue(cookieStore.get(baduri).isEmpty());
    } 
 
    /** 
     * @tests java.net.CookieStore#getCookies() 
     *