Code example for CookieStore

Methods: addgetgetCookiesremove

0
    public void test_add_LURI_LHttpCookie() throws URISyntaxException { 
        URI uri = new URI("http://harmony.test.unit.org");
        HttpCookie cookie = new HttpCookie("name1", "value1");
        cookie.setDiscard(true);
        try { 
            cookieStore.add(null, cookie);
            fail("should throw NullPointerException");
        } catch (NullPointerException e) {
            // expected 
        } 
 
        try { 
            cookieStore.add(uri, null);
            fail("should throw NullPointerException");
        } catch (NullPointerException e) {
            // expected 
        } 
 
        try { 
            cookieStore.add(null, null);
            fail("should throw NullPointerException");
        } catch (NullPointerException e) {
            // expected 
        } 
         
        cookieStore.add(uri, cookie);
        List<HttpCookie> list = cookieStore.get(uri);
        assertEquals(1, list.size());
        assertTrue(list.contains(cookie));
 
        HttpCookie cookie2 = new HttpCookie("  NaME1   ", "  TESTVALUE1   ");
        cookieStore.add(uri, cookie2);
        list = cookieStore.get(uri);
        assertEquals(1, list.size());
        assertEquals("  TESTVALUE1   ", list.get(0).getValue());
        assertTrue(list.contains(cookie2));        
         
        // domain and path attributes works 
        HttpCookie anotherCookie = new HttpCookie("name1", "value1");
        anotherCookie.setDomain("domain");
        anotherCookie.setPath("Path"); 
        cookieStore.add(uri, anotherCookie);
        list = cookieStore.get(uri);
        assertEquals(2, list.size());
        assertNull(list.get(0).getDomain());
        assertEquals("domain", list.get(1).getDomain());
        assertEquals("Path", list.get(1).getPath());
 
        URI uri2 = new URI("http://.test.unit.org");
        HttpCookie cookie3 = new HttpCookie("NaME2", "VALUE2");
        cookieStore.add(uri2, cookie3);
        list = cookieStore.get(uri2);
        assertEquals(1, list.size());
        assertEquals("VALUE2", list.get(0).getValue());
        list = cookieStore.getCookies();
        assertEquals(3, list.size());
         
        // expired cookie won't be selected. 
        HttpCookie cookie4 = new HttpCookie("cookie4", "value4");
        cookie4.setMaxAge(-2);
        assertTrue(cookie4.hasExpired());
        cookieStore.add(uri2, cookie4);
        list = cookieStore.getCookies();
        assertEquals(3, list.size());
        assertFalse(cookieStore.remove(uri2, cookie4));
 
        cookie4.setMaxAge(3000);
        cookie4.setDomain("domain");
        cookie4.setPath("path");
        cookieStore.add(uri2, cookie4);
        list = cookieStore.get(uri2);
        assertEquals(2, list.size());
         
        cookieStore.add(uri, cookie4);
        list = cookieStore.get(uri);
        assertEquals(3, list.size());
        list = cookieStore.get(uri2);
        assertEquals(2, list.size());
        list = cookieStore.getCookies();
        assertEquals(4, list.size());
                 
        URI baduri = new URI("bad_url");
        HttpCookie cookie6 = new HttpCookie("cookie5", "value5");
        cookieStore.add(baduri, cookie6);
        list = cookieStore.get(baduri);
        assertTrue(list.contains(cookie6));
    } 
 
    /** 
     * @tests java.net.CookieStore#get(URI) 
Stop searching for code, let great code find you!  Add Codota to your java IDE