1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/oac.hc3x/trunk/src/test/org/apache/commons/httpclient/cookie/TestCookie2.java $
3    * $Revision: 1425331 $
4    * $Date: 2012-12-22 18:29:41 +0000 (Sat, 22 Dec 2012) $
5    * ====================================================================
6    *
7    *  Licensed to the Apache Software Foundation (ASF) under one or more
8    *  contributor license agreements.  See the NOTICE file distributed with
9    *  this work for additional information regarding copyright ownership.
10   *  The ASF licenses this file to You under the Apache License, Version 2.0
11   *  (the "License"); you may not use this file except in compliance with
12   *  the License.  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
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      // ------------------------------------------------------------ Constructor
50  
51      public TestCookie2(String name) {
52          super(name);
53      }
54  
55      // ------------------------------------------------------- TestCase Methods
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          // check cookie properties (default values)
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          // Cookie 0
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          // Cookie 1
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          // Cookie 2
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          // Cookie 3
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         // Cookie 4
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         // The ascending order should be:
110         // 2, 1, 0, 3, 4
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