1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit;
16
17 import static org.junit.Assert.assertEquals;
18 import static org.junit.Assert.assertFalse;
19 import static org.junit.Assert.assertTrue;
20
21 import java.util.Date;
22
23 import org.htmlunit.util.Cookie;
24 import org.junit.Test;
25
26
27
28
29
30
31
32
33
34
35 public class CookieManager3Test {
36
37
38
39
40 @Test
41 public void basicBehavior() {
42
43 final CookieManager mgr = new CookieManager();
44 assertTrue(mgr.isCookiesEnabled());
45 assertTrue(mgr.getCookies().isEmpty());
46
47
48 final Cookie cookie = new Cookie("localhost", "a", "b");
49 mgr.addCookie(cookie);
50 assertFalse(mgr.getCookies().isEmpty());
51
52
53 mgr.removeCookie(cookie);
54 assertTrue(mgr.getCookies().isEmpty());
55
56
57 mgr.addCookie(cookie);
58 assertFalse(mgr.getCookies().isEmpty());
59
60
61 mgr.clearCookies();
62 assertTrue(mgr.getCookies().isEmpty());
63
64
65 mgr.addCookie(cookie);
66 assertEquals(1, mgr.getCookies().size());
67
68
69 mgr.setCookiesEnabled(false);
70 assertFalse(mgr.isCookiesEnabled());
71 assertTrue(mgr.getCookies().isEmpty());
72
73
74 final Cookie cookie2 = new Cookie("a", "b", "c", "d", new Date(System.currentTimeMillis() + 5000), false);
75 mgr.addCookie(cookie2);
76 assertTrue(mgr.getCookies().isEmpty());
77 assertFalse(mgr.clearExpired(new Date(System.currentTimeMillis() + 10_000)));
78
79
80 mgr.setCookiesEnabled(true);
81 assertTrue(mgr.isCookiesEnabled());
82 assertEquals(1, mgr.getCookies().size());
83
84
85 assertFalse(mgr.clearExpired(new Date(System.currentTimeMillis() + 10_000)));
86 assertEquals(1, mgr.getCookies().size());
87
88 mgr.addCookie(cookie2);
89 assertEquals(2, mgr.getCookies().size());
90 assertTrue(mgr.clearExpired(new Date(System.currentTimeMillis() + 10_000)));
91 assertEquals(1, mgr.getCookies().size());
92 }
93 }