1   /*
2    * $Header: /cvsroot/httpc-cookie2/httpc-cookie2/httpcookie2SVN-patch.082805-2100.diff,v 1.1 2005/08/29 05:01:58 sjain700 Exp $
3    * $Revision:400312 $
4    * $Date:2006-05-06 14:49:41 +0200 (Sat, 06 May 2006) $
5    * ====================================================================
6    *
7    *  Copyright 1999-2004 The Apache Software Foundation
8    *
9    *  Licensed under the Apache License, Version 2.0 (the "License");
10   *  you may not use this file except in compliance with the License.
11   *  You may obtain a copy of the License at
12   *
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   *
15   *  Unless required by applicable law or agreed to in writing, software
16   *  distributed under the License is distributed on an "AS IS" BASIS,
17   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   *  See the License for the specific language governing permissions and
19   *  limitations under the License.
20   * ====================================================================
21   *
22   * This software consists of voluntary contributions made by many
23   * individuals on behalf of the Apache Software Foundation.  For more
24   * information on the Apache Software Foundation, please see
25   * <http://www.apache.org/>.
26   *
27   */
28  
29  package org.apache.commons.httpclient.cookie;
30  
31  import java.util.*;
32  
33  import junit.framework.Test;
34  import junit.framework.TestSuite;
35  
36  import org.apache.commons.httpclient.Cookie;
37  import org.apache.commons.httpclient.Header;
38  
39  
40  /***
41   * Test cases for {@link Cookie2}.
42   *
43   * @author Samit Jain (jain.samit@gmail.com)
44   */
45  public class TestCookie2 extends TestCookieBase {
46  
47  
48      // ------------------------------------------------------------ Constructor
49  
50      public TestCookie2(String name) {
51          super(name);
52      }
53  
54      // ------------------------------------------------------- TestCase Methods
55  
56      public static Test suite() {
57          return new TestSuite(TestCookie2.class);
58      }
59  
60      /***
61       * Tests default constructor.
62       */
63      public void testDefaultConstuctor() {
64          Cookie2 dummy = new Cookie2();
65          // check cookie properties (default values)
66          assertNull(dummy.getPorts());
67          assertFalse(dummy.getSecure());
68          assertFalse(dummy.isExpired());
69          assertFalse(dummy.isDomainAttributeSpecified());
70          assertFalse(dummy.isPathAttributeSpecified());
71          assertFalse(dummy.isPortAttributeSpecified());
72          assertFalse(dummy.isVersionAttributeSpecified());
73          assertFalse(dummy.isPersistent());
74  
75          Cookie2 dummy2 = new Cookie2();
76          assertEquals(dummy, dummy2);
77      }
78  
79      public void testComparator() throws Exception {
80          Header setCookie2 = null;
81          Cookie[] parsed = null;
82          List cookies = new LinkedList();
83          CookieSpec cookiespec = new RFC2965Spec();
84          // Cookie 0
85          setCookie2 = new Header("Set-Cookie2","cookie-name=Cookie0; Version=1");
86          parsed = cookieParse(cookiespec, "domain.com", 80,
87                               "/path/path1", true, setCookie2);
88          cookies.add(parsed[0]);
89          // Cookie 1
90          setCookie2 = new Header("Set-Cookie2","cookie-name=Cookie1; Version=1");
91          parsed = cookieParse(cookiespec, "domain.com", 80, "/path", true, setCookie2);
92          cookies.add(parsed[0]);
93          // Cookie 2
94          setCookie2 = new Header("Set-Cookie2","cookie-name=Cookie2; Version=1");
95          parsed = cookieParse(cookiespec, "domain.com", 80, "/", true, setCookie2);
96          cookies.add(parsed[0]);
97          // Cookie 3
98          setCookie2 = new Header("Set-Cookie2","cookie-name=Cookie3; Version=1");
99          parsed = cookieParse(cookiespec, "domain.com", 80,
100                              "/path/path1/path2", true, setCookie2);
101         cookies.add(parsed[0]);
102         // Cookie 4
103         setCookie2 = new Header("Set-Cookie2","cookie-name=Cookie4; Version=1");
104         parsed = cookieParse(cookiespec, "domain.com", 80,
105                              "/path/path1/path2/path3", true, setCookie2);
106         cookies.add(parsed[0]);
107 
108         // The ascending order should be:
109         // 2, 1, 0, 3, 4
110         int[] expectedOrder = new int[] {2, 1, 0, 3, 4};
111         Set sortedCookies = new TreeSet(parsed[0]);
112         sortedCookies.addAll(cookies);
113 
114         int pass = 0;
115         for (Iterator itr = sortedCookies.iterator(); itr.hasNext(); ++pass) {
116             Cookie2 cookie = (Cookie2) itr.next();
117             assertTrue("sortedCookies[" + pass + "] should be cookies[" + expectedOrder[pass] + "]",
118                        cookie == cookies.get(expectedOrder[pass]));
119         }
120 
121         try {
122             parsed[0].compare(parsed[0], "foo");
123             fail("Should have thrown an exception trying to compare non-cookies");
124         } catch (ClassCastException expected) {}
125     }
126 }
127