Code example for CookieStore

Methods: addgetgetCookiesgetURIsremoveremoveAll

0
     */ 
    public void test_remove_LURI_LHttpCookie() throws URISyntaxException { 
        URI uri1 = new URI("http://remove1.test.com");
        HttpCookie cookie1 = new HttpCookie("cookie_name1", "cookie_value1");
        try { 
            cookieStore.remove(uri1, null);
            fail("should throw NullPointerException");
        } catch (NullPointerException e) {
            // expected 
        } 
        assertFalse(cookieStore.remove(uri1, cookie1));
        assertFalse(cookieStore.remove(null, cookie1));
 
        cookieStore.add(uri1, cookie1);
        URI uri2 = new URI("http://remove2.test.com");
        HttpCookie cookie2 = new HttpCookie("cookie_name2", "cookie_value2");
        cookieStore.add(uri2, cookie2);
        assertTrue(cookieStore.remove(uri1, cookie1));
        assertFalse(cookieStore.remove(uri1, cookie1));
        assertEquals(2, cookieStore.getURIs().size());
        assertEquals(1, cookieStore.getCookies().size());
        assertTrue(cookieStore.remove(uri2, cookie2));
        assertFalse(cookieStore.remove(uri2, cookie2));
        assertEquals(2, cookieStore.getURIs().size());
        assertEquals(0, cookieStore.getCookies().size());
         
        assertTrue(cookieStore.removeAll());
        cookieStore.add(uri1, cookie1);
        cookieStore.add(uri2, cookie2);
        HttpCookie cookie3 = new HttpCookie("cookie_name3", "cookie_value3");
        assertFalse(cookieStore.remove(null, cookie3));
        assertTrue(cookieStore.remove(null, cookie1));
        assertFalse(cookieStore.remove(null, cookie1));
        assertEquals(2, cookieStore.getURIs().size());
        assertEquals(1, cookieStore.getCookies().size());
        assertTrue(cookieStore.remove(null, cookie2));
        assertFalse(cookieStore.remove(null, cookie2));
        assertEquals(2, cookieStore.getURIs().size());
        assertEquals(0, cookieStore.getCookies().size());
 
        cookieStore.removeAll();
        // expired cookies can also be deleted. 
        cookie2.setMaxAge(-34857);
        cookieStore.add(uri2, cookie2);
        assertTrue(cookieStore.remove(uri2, cookie2));
        assertFalse(cookieStore.remove(uri2, cookie2));
        assertEquals(0, cookieStore.getCookies().size());
 
        cookie2.setMaxAge(34857);
        cookieStore.add(uri1, cookie1);
        cookieStore.add(uri2, cookie1);
        cookieStore.add(uri2, cookie2);
        assertTrue(cookieStore.remove(uri1, cookie1));
        assertFalse(cookieStore.remove(uri1, cookie1));
        assertFalse(cookieStore.get(uri2).contains(cookie1));
        assertTrue(cookieStore.get(uri2).contains(cookie2));
        assertEquals(0, cookieStore.get(uri1).size());
        cookieStore.remove(uri2, cookie2);
 
        cookieStore.removeAll();
        cookieStore.add(uri2, cookie2);
        cookieStore.add(uri1, cookie1);
        assertEquals(2, cookieStore.getCookies().size());
        assertTrue(cookieStore.remove(uri2, cookie1));
        assertFalse(cookieStore.remove(uri2, cookie1));
        assertEquals(2, cookieStore.getURIs().size());
        assertEquals(1, cookieStore.getCookies().size());
        assertTrue(cookieStore.getCookies().contains(cookie2));
         
        cookieStore.removeAll();
        URI uri3 = new URI("http://remove3.test.com");
        URI uri4 = new URI("http://test.com");
        HttpCookie cookie4 = new HttpCookie("cookie_name4", "cookie_value4");
        cookie4.setDomain(".test.com");
        cookie2.setMaxAge(-34857);
        cookie3.setMaxAge(-22);
        cookie4.setMaxAge(-45);
        cookieStore.add(uri1, cookie1);
        cookieStore.add(uri2, cookie2);
        cookieStore.add(uri3, cookie3);
        cookieStore.add(uri4, cookie4);
        assertEquals(0, cookieStore.get(uri2).size());
        assertFalse(cookieStore.remove(uri2, cookie2));
        assertTrue(cookieStore.remove(uri3, cookie3));
        assertFalse(cookieStore.remove(uri4, cookie4));
    } 
 
    /** 
     * @tests java.net.CookieStore#test_removeAll() 
     *