001: /*
002: * $Header: /cvsroot/httpc-cookie2/httpc-cookie2/httpcookie2SVN-patch.082805-2100.diff,v 1.1 2005/08/29 05:01:58 sjain700 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.*;
033:
034: import junit.framework.Test;
035: import junit.framework.TestSuite;
036:
037: import org.apache.commons.httpclient.Cookie;
038: import org.apache.commons.httpclient.Header;
039:
040: /**
041: * Test cases for {@link Cookie2}.
042: *
043: * @author Samit Jain (jain.samit@gmail.com)
044: */
045: public class TestCookie2 extends TestCookieBase {
046:
047: // ------------------------------------------------------------ Constructor
048:
049: public TestCookie2(String name) {
050: super (name);
051: }
052:
053: // ------------------------------------------------------- TestCase Methods
054:
055: public static Test suite() {
056: return new TestSuite(TestCookie2.class);
057: }
058:
059: /**
060: * Tests default constructor.
061: */
062: public void testDefaultConstuctor() {
063: Cookie2 dummy = new Cookie2();
064: // check cookie properties (default values)
065: assertNull(dummy.getPorts());
066: assertFalse(dummy.getSecure());
067: assertFalse(dummy.isExpired());
068: assertFalse(dummy.isDomainAttributeSpecified());
069: assertFalse(dummy.isPathAttributeSpecified());
070: assertFalse(dummy.isPortAttributeSpecified());
071: assertFalse(dummy.isVersionAttributeSpecified());
072: assertFalse(dummy.isPersistent());
073:
074: Cookie2 dummy2 = new Cookie2();
075: assertEquals(dummy, dummy2);
076: }
077:
078: public void testComparator() throws Exception {
079: Header setCookie2 = null;
080: Cookie[] parsed = null;
081: List cookies = new LinkedList();
082: CookieSpec cookiespec = new RFC2965Spec();
083: // Cookie 0
084: setCookie2 = new Header("Set-Cookie2",
085: "cookie-name=Cookie0; Version=1");
086: parsed = cookieParse(cookiespec, "domain.com", 80,
087: "/path/path1", true, setCookie2);
088: cookies.add(parsed[0]);
089: // Cookie 1
090: setCookie2 = new Header("Set-Cookie2",
091: "cookie-name=Cookie1; Version=1");
092: parsed = cookieParse(cookiespec, "domain.com", 80, "/path",
093: true, setCookie2);
094: cookies.add(parsed[0]);
095: // Cookie 2
096: setCookie2 = new Header("Set-Cookie2",
097: "cookie-name=Cookie2; Version=1");
098: parsed = cookieParse(cookiespec, "domain.com", 80, "/", true,
099: setCookie2);
100: cookies.add(parsed[0]);
101: // Cookie 3
102: setCookie2 = new Header("Set-Cookie2",
103: "cookie-name=Cookie3; Version=1");
104: parsed = cookieParse(cookiespec, "domain.com", 80,
105: "/path/path1/path2", true, setCookie2);
106: cookies.add(parsed[0]);
107: // Cookie 4
108: setCookie2 = new Header("Set-Cookie2",
109: "cookie-name=Cookie4; Version=1");
110: parsed = cookieParse(cookiespec, "domain.com", 80,
111: "/path/path1/path2/path3", true, setCookie2);
112: cookies.add(parsed[0]);
113:
114: // The ascending order should be:
115: // 2, 1, 0, 3, 4
116: int[] expectedOrder = new int[] { 2, 1, 0, 3, 4 };
117: Set sortedCookies = new TreeSet(parsed[0]);
118: sortedCookies.addAll(cookies);
119:
120: int pass = 0;
121: for (Iterator itr = sortedCookies.iterator(); itr.hasNext(); ++pass) {
122: Cookie2 cookie = (Cookie2) itr.next();
123: assertTrue("sortedCookies[" + pass + "] should be cookies["
124: + expectedOrder[pass] + "]", cookie == cookies
125: .get(expectedOrder[pass]));
126: }
127:
128: try {
129: parsed[0].compare(parsed[0], "foo");
130: fail("Should have thrown an exception trying to compare non-cookies");
131: } catch (ClassCastException expected) {
132: }
133: }
134: }
|