1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package org.apache.commons.httpclient.cookie;
31
32 import java.util.*;
33
34 import junit.framework.Test;
35 import junit.framework.TestSuite;
36
37 import org.apache.commons.httpclient.Cookie;
38 import org.apache.commons.httpclient.Header;
39
40
41 /***
42 * Test cases for {@link Cookie2}.
43 *
44 * @author Samit Jain (jain.samit@gmail.com)
45 */
46 public class TestCookie2 extends TestCookieBase {
47
48
49
50
51 public TestCookie2(String name) {
52 super(name);
53 }
54
55
56
57 public static Test suite() {
58 return new TestSuite(TestCookie2.class);
59 }
60
61 /***
62 * Tests default constructor.
63 */
64 public void testDefaultConstuctor() {
65 Cookie2 dummy = new Cookie2();
66
67 assertNull(dummy.getPorts());
68 assertFalse(dummy.getSecure());
69 assertFalse(dummy.isExpired());
70 assertFalse(dummy.isDomainAttributeSpecified());
71 assertFalse(dummy.isPathAttributeSpecified());
72 assertFalse(dummy.isPortAttributeSpecified());
73 assertFalse(dummy.isVersionAttributeSpecified());
74 assertFalse(dummy.isPersistent());
75
76 Cookie2 dummy2 = new Cookie2();
77 assertEquals(dummy, dummy2);
78 }
79
80 public void testComparator() throws Exception {
81 Header setCookie2 = null;
82 Cookie[] parsed = null;
83 List cookies = new LinkedList();
84 CookieSpec cookiespec = new RFC2965Spec();
85
86 setCookie2 = new Header("Set-Cookie2","cookie-name=Cookie0; Version=1");
87 parsed = cookieParse(cookiespec, "domain.com", 80,
88 "/path/path1", true, setCookie2);
89 cookies.add(parsed[0]);
90
91 setCookie2 = new Header("Set-Cookie2","cookie-name=Cookie1; Version=1");
92 parsed = cookieParse(cookiespec, "domain.com", 80, "/path", true, setCookie2);
93 cookies.add(parsed[0]);
94
95 setCookie2 = new Header("Set-Cookie2","cookie-name=Cookie2; Version=1");
96 parsed = cookieParse(cookiespec, "domain.com", 80, "/", true, setCookie2);
97 cookies.add(parsed[0]);
98
99 setCookie2 = new Header("Set-Cookie2","cookie-name=Cookie3; Version=1");
100 parsed = cookieParse(cookiespec, "domain.com", 80,
101 "/path/path1/path2", true, setCookie2);
102 cookies.add(parsed[0]);
103
104 setCookie2 = new Header("Set-Cookie2","cookie-name=Cookie4; Version=1");
105 parsed = cookieParse(cookiespec, "domain.com", 80,
106 "/path/path1/path2/path3", true, setCookie2);
107 cookies.add(parsed[0]);
108
109
110
111 int[] expectedOrder = new int[] {2, 1, 0, 3, 4};
112 Set sortedCookies = new TreeSet(parsed[0]);
113 sortedCookies.addAll(cookies);
114
115 int pass = 0;
116 for (Iterator itr = sortedCookies.iterator(); itr.hasNext(); ++pass) {
117 Cookie2 cookie = (Cookie2) itr.next();
118 assertTrue("sortedCookies[" + pass + "] should be cookies[" + expectedOrder[pass] + "]",
119 cookie == cookies.get(expectedOrder[pass]));
120 }
121
122 try {
123 parsed[0].compare(parsed[0], "foo");
124 fail("Should have thrown an exception trying to compare non-cookies");
125 } catch (ClassCastException expected) {}
126 }
127 }
128