001: /*
002: * $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 $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: * ====================================================================
006: *
007: * Licensed to the Apache Software Foundation (ASF) under one or more
008: * contributor license agreements. See the NOTICE file distributed with
009: * this work for additional information regarding copyright ownership.
010: * The ASF licenses this file to You under the Apache License, Version 2.0
011: * (the "License"); you may not use this file except in compliance with
012: * the License. You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: * ====================================================================
022: *
023: * This software consists of voluntary contributions made by many
024: * individuals on behalf of the Apache Software Foundation. For more
025: * information on the Apache Software Foundation, please see
026: * <http://www.apache.org/>.
027: *
028: */
029:
030: package org.apache.commons.httpclient.cookie;
031:
032: import java.util.Iterator;
033: import java.util.SortedSet;
034: import java.util.TreeSet;
035: import java.util.Vector;
036:
037: import junit.framework.Test;
038: import junit.framework.TestSuite;
039:
040: import org.apache.commons.httpclient.Cookie;
041: import org.apache.commons.httpclient.Header;
042:
043: /**
044: * Test cases for Cookie
045: *
046: * @author BC Holmes
047: * @author Rod Waldhoff
048: * @author dIon Gillard
049: * @author <a href="mailto:JEvans@Cyveillance.com">John Evans</a>
050: * @author Marc A. Saegesser
051: * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
052: * @version $Revision: 480424 $
053: */
054: public class TestCookie extends TestCookieBase {
055:
056: // ------------------------------------------------------------ Constructor
057:
058: public TestCookie(String name) {
059: super (name);
060: }
061:
062: // ------------------------------------------------------- TestCase Methods
063:
064: public static Test suite() {
065: return new TestSuite(TestCookie.class);
066: }
067:
068: /**
069: * Tests default constructor.
070: */
071: public void testDefaultConstuctor() {
072: Cookie dummy = new Cookie();
073: assertEquals("noname=", dummy.toExternalForm());
074: }
075:
076: public void testComparator() throws Exception {
077: Header setCookie = null;
078: Cookie[] parsed = null;
079: Vector cookies = new Vector();
080: // Cookie 0
081: setCookie = new Header(
082: "Set-Cookie",
083: "cookie-name=cookie-value;Path=/commons;Domain=.apache.org;Expires=Thu, 01-Jan-1970 00:00:10 GMT");
084: CookieSpec cookiespec = new CookieSpecBase();
085: parsed = cookieParse(cookiespec, ".apache.org", 80,
086: "/commons/httpclient", true, setCookie);
087: cookies.add(parsed[0]);
088: // Cookie 1
089: setCookie = new Header(
090: "Set-Cookie",
091: "cookie-name=cookie-value;Path=/commons/bif;Domain=.apache.org;Expires=Thu, 01-Jan-1970 00:00:10 GMT");
092: parsed = cookieParse(cookiespec, ".apache.org", 80,
093: "/commons/bif/httpclient", true, setCookie);
094: cookies.add(parsed[0]);
095: // Cookie 2
096: setCookie = new Header(
097: "Set-Cookie",
098: "cookie-name=cookie-value;Path=/commons;Domain=.baz.org;Expires=Thu, 01-Jan-1970 00:00:10 GMT");
099: parsed = cookieParse(cookiespec, ".baz.org", 80,
100: "/commons/httpclient", true, setCookie);
101: cookies.add(parsed[0]);
102: // Cookie 3
103: setCookie = new Header(
104: "Set-Cookie",
105: "cookie-name=cookie-value;Path=/commons/bif;Domain=.baz.org;Expires=Thu, 01-Jan-1970 00:00:10 GMT");
106: parsed = cookieParse(cookiespec, ".baz.org", 80,
107: "/commons/bif/httpclient", true, setCookie);
108: cookies.add(parsed[0]);
109: // Cookie 4
110: setCookie = new Header(
111: "Set-Cookie",
112: "cookie-name=cookie-value;Path=/commons;Domain=.baz.com;Expires=Thu, 01-Jan-1970 00:00:10 GMT");
113: parsed = cookieParse(cookiespec, ".baz.com", 80,
114: "/commons/httpclient", true, setCookie);
115: cookies.add(parsed[0]);
116: // The order should be:
117: // 1, 0, 3, 2, 4
118: parsed = (Cookie[]) cookies.toArray(new Cookie[0]);
119: SortedSet set = new TreeSet(parsed[0]);
120: int pass = 0;
121: for (Iterator itr = set.iterator(); itr.hasNext();) {
122: Cookie cookie = (Cookie) itr.next();
123: switch (pass) {
124: case 0:
125: assertTrue("0th cookie should be cookie[1]",
126: cookie == parsed[1]);
127: break;
128: case 1:
129: assertTrue("1st cookie should be cookie[0]",
130: cookie == parsed[0]);
131: break;
132: case 2:
133: assertTrue("2nd cookie should be cookie[3]",
134: cookie == parsed[3]);
135: break;
136: case 3:
137: assertTrue("3rd cookie should be cookie[2]",
138: cookie == parsed[2]);
139: break;
140: case 4:
141: assertTrue("4th cookie should be cookie[4]",
142: cookie == parsed[4]);
143: break;
144: default:
145: fail("This should never happen.");
146: }
147: pass++;
148: }
149: try {
150: parsed[0].compare("foo", "bar");
151: fail("Should have thrown an exception trying to compare non-cookies");
152: } catch (ClassCastException ex) {
153: // expected
154: }
155: }
156: }
|