1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/oac.hc3x/trunk/src/test/org/apache/commons/httpclient/cookie/TestCookie.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.Iterator;
33  import java.util.SortedSet;
34  import java.util.TreeSet;
35  import java.util.Vector;
36  
37  import junit.framework.Test;
38  import junit.framework.TestSuite;
39  
40  import org.apache.commons.httpclient.Cookie;
41  import org.apache.commons.httpclient.Header;
42  
43  
44  /***
45   * Test cases for Cookie
46   *
47   * @author BC Holmes
48   * @author Rod Waldhoff
49   * @author dIon Gillard
50   * @author <a href="mailto:JEvans@Cyveillance.com">John Evans</a>
51   * @author Marc A. Saegesser
52   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
53   * @version $Revision: 1425331 $
54   */
55  public class TestCookie extends TestCookieBase {
56  
57  
58      // ------------------------------------------------------------ Constructor
59  
60      public TestCookie(String name) {
61          super(name);
62      }
63  
64      // ------------------------------------------------------- TestCase Methods
65  
66      public static Test suite() {
67          return new TestSuite(TestCookie.class);
68      }
69  
70      /***
71       * Tests default constructor.
72       */
73      public void testDefaultConstuctor() {
74          Cookie dummy = new Cookie();
75          assertEquals( "noname=", dummy.toExternalForm() );
76      }
77  
78      public void testComparator() throws Exception {
79          Header setCookie = null;
80          Cookie[] parsed = null;
81          Vector cookies = new Vector();
82          // Cookie 0
83          setCookie = new Header("Set-Cookie","cookie-name=cookie-value;Path=/commons;Domain=.apache.org;Expires=Thu, 01-Jan-1970 00:00:10 GMT");
84          CookieSpec cookiespec = new CookieSpecBase();
85          parsed = cookieParse(cookiespec, ".apache.org", 80, "/commons/httpclient", true, setCookie);
86          cookies.add(parsed[0]);
87          // Cookie 1
88          setCookie = new Header("Set-Cookie","cookie-name=cookie-value;Path=/commons/bif;Domain=.apache.org;Expires=Thu, 01-Jan-1970 00:00:10 GMT");
89          parsed = cookieParse(cookiespec, ".apache.org", 80, "/commons/bif/httpclient", true, setCookie);
90          cookies.add(parsed[0]);
91          // Cookie 2
92          setCookie = new Header("Set-Cookie","cookie-name=cookie-value;Path=/commons;Domain=.baz.org;Expires=Thu, 01-Jan-1970 00:00:10 GMT");
93          parsed = cookieParse(cookiespec, ".baz.org", 80, "/commons/httpclient", true, setCookie);
94          cookies.add(parsed[0]);
95          // Cookie 3
96          setCookie = new Header("Set-Cookie","cookie-name=cookie-value;Path=/commons/bif;Domain=.baz.org;Expires=Thu, 01-Jan-1970 00:00:10 GMT");
97          parsed = cookieParse(cookiespec, ".baz.org", 80, "/commons/bif/httpclient", true, setCookie);
98          cookies.add(parsed[0]);
99          // Cookie 4
100         setCookie = new Header("Set-Cookie","cookie-name=cookie-value;Path=/commons;Domain=.baz.com;Expires=Thu, 01-Jan-1970 00:00:10 GMT");
101         parsed = cookieParse(cookiespec, ".baz.com", 80, "/commons/httpclient", true, setCookie);
102         cookies.add(parsed[0]);
103         // The order should be:
104         // 1, 0, 3, 2, 4
105         parsed = (Cookie[])cookies.toArray(new Cookie[0]);
106         SortedSet set = new TreeSet(parsed[0]);
107         int pass = 0;
108         for (Iterator itr = set.iterator(); itr.hasNext();) {
109             Cookie cookie = (Cookie)itr.next();
110             switch (pass) {
111                 case 0:
112                     assertTrue("0th cookie should be cookie[1]", cookie == parsed[1]);
113                     break;
114                 case 1:
115                     assertTrue("1st cookie should be cookie[0]", cookie == parsed[0]);
116                     break;
117                 case 2:
118                     assertTrue("2nd cookie should be cookie[3]", cookie == parsed[3]);
119                     break;
120                 case 3:
121                     assertTrue("3rd cookie should be cookie[2]", cookie == parsed[2]);
122                     break;
123                 case 4:
124                     assertTrue("4th cookie should be cookie[4]", cookie == parsed[4]);
125                     break;
126                 default:
127                     fail("This should never happen.");
128             }
129             pass++;
130         }
131         try {
132             parsed[0].compare("foo", "bar");
133             fail("Should have thrown an exception trying to compare non-cookies");
134         }
135         catch (ClassCastException ex) {
136             // expected
137         }
138     }
139 }
140