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