1 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 55 public class TestCookie extends TestCookieBase { 56 57 58 60 public TestCookie(String name) { 61 super(name); 62 } 63 64 66 public static Test suite() { 67 return new TestSuite(TestCookie.class); 68 } 69 70 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 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 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 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 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 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 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 } 138 } 139 } 140 141 | Popular Tags |