001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/TestHeaderElement.java,v 1.7 2004/02/22 18:08:49 olegk Exp $
003: * $Revision: 604625 $
004: * $Date: 2007-12-16 15:11:11 +0100 (Sun, 16 Dec 2007) $
005: * ====================================================================
006: * Licensed to the Apache Software Foundation (ASF) under one
007: * or more contributor license agreements. See the NOTICE file
008: * distributed with this work for additional information
009: * regarding copyright ownership. The ASF licenses this file
010: * to you under the Apache License, Version 2.0 (the
011: * "License"); you may not use this file except in compliance
012: * with 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,
017: * software distributed under the License is distributed on an
018: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
019: * KIND, either express or implied. See the License for the
020: * specific language governing permissions and limitations
021: * under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: * [Additional notices, if required by prior licensing conditions]
030: *
031: */
032:
033: package org.apache.http.message;
034:
035: import junit.framework.Test;
036: import junit.framework.TestCase;
037: import junit.framework.TestSuite;
038:
039: import org.apache.http.HeaderElement;
040: import org.apache.http.NameValuePair;
041:
042: /**
043: * Simple tests for {@link HeaderElement}.
044: *
045: * @author Rodney Waldhoff
046: * @author <a href="mailto:bcholmes@interlog.com">B.C. Holmes</a>
047: * @author <a href="mailto:jericho@thinkfree.com">Park, Sung-Gu</a>
048: * @author <a href="mailto:oleg at ural.ru">oleg Kalnichevski</a>
049: * @version $Id: TestHeaderElement.java 604625 2007-12-16 14:11:11Z olegk $
050: */
051: public class TestHeaderElement extends TestCase {
052:
053: // ------------------------------------------------------------ Constructor
054: public TestHeaderElement(String testName) {
055: super (testName);
056: }
057:
058: // ------------------------------------------------------------------- Main
059: public static void main(String args[]) {
060: String[] testCaseName = { TestHeaderElement.class.getName() };
061: junit.textui.TestRunner.main(testCaseName);
062: }
063:
064: // ------------------------------------------------------- TestCase Methods
065:
066: public static Test suite() {
067: return new TestSuite(TestHeaderElement.class);
068: }
069:
070: public void testConstructor3() throws Exception {
071: HeaderElement element = new BasicHeaderElement("name", "value",
072: new NameValuePair[] {
073: new BasicNameValuePair("param1", "value1"),
074: new BasicNameValuePair("param2", "value2") });
075: assertEquals("name", element.getName());
076: assertEquals("value", element.getValue());
077: assertEquals(2, element.getParameters().length);
078: assertEquals("value1", element.getParameterByName("param1")
079: .getValue());
080: assertEquals("value2", element.getParameterByName("param2")
081: .getValue());
082: }
083:
084: public void testConstructor2() throws Exception {
085: HeaderElement element = new BasicHeaderElement("name", "value");
086: assertEquals("name", element.getName());
087: assertEquals("value", element.getValue());
088: assertEquals(0, element.getParameters().length);
089: }
090:
091: public void testInvalidName() {
092: try {
093: new BasicHeaderElement(null, null, null);
094: fail("IllegalArgumentException should have been thrown");
095: } catch (IllegalArgumentException ex) {
096: //expected
097: }
098: }
099:
100: public void testParamByName() throws Exception {
101: String s = "name = value; param1 = value1; param2 = value2";
102: HeaderElement element = BasicHeaderValueParser
103: .parseHeaderElement(s, null);
104: assertEquals("value1", element.getParameterByName("param1")
105: .getValue());
106: assertEquals("value2", element.getParameterByName("param2")
107: .getValue());
108: assertNull(element.getParameterByName("param3"));
109: try {
110: element.getParameterByName(null);
111: fail("IllegalArgumentException should have been thrown");
112: } catch (IllegalArgumentException ex) {
113: //expected
114: }
115: }
116:
117: public void testHashCode() {
118: HeaderElement element1 = new BasicHeaderElement("name",
119: "value", new NameValuePair[] {
120: new BasicNameValuePair("param1", "value1"),
121: new BasicNameValuePair("param2", "value2") });
122: HeaderElement element2 = new BasicHeaderElement("name",
123: "value", new NameValuePair[] {
124: new BasicNameValuePair("param2", "value2"),
125: new BasicNameValuePair("param1", "value1") });
126: HeaderElement element3 = new BasicHeaderElement("name", "value");
127: HeaderElement element4 = new BasicHeaderElement("name", "value");
128: HeaderElement element5 = new BasicHeaderElement("name",
129: "value", new NameValuePair[] {
130: new BasicNameValuePair("param1", "value1"),
131: new BasicNameValuePair("param2", "value2") });
132: assertTrue(element1.hashCode() != element2.hashCode());
133: assertTrue(element1.hashCode() != element3.hashCode());
134: assertTrue(element2.hashCode() != element3.hashCode());
135: assertTrue(element3.hashCode() == element4.hashCode());
136: assertTrue(element1.hashCode() == element5.hashCode());
137: }
138:
139: public void testEquals() {
140: HeaderElement element1 = new BasicHeaderElement("name",
141: "value", new NameValuePair[] {
142: new BasicNameValuePair("param1", "value1"),
143: new BasicNameValuePair("param2", "value2") });
144: HeaderElement element2 = new BasicHeaderElement("name",
145: "value", new NameValuePair[] {
146: new BasicNameValuePair("param2", "value2"),
147: new BasicNameValuePair("param1", "value1") });
148: HeaderElement element3 = new BasicHeaderElement("name", "value");
149: HeaderElement element4 = new BasicHeaderElement("name", "value");
150: HeaderElement element5 = new BasicHeaderElement("name",
151: "value", new NameValuePair[] {
152: new BasicNameValuePair("param1", "value1"),
153: new BasicNameValuePair("param2", "value2") });
154: assertTrue(element1.equals(element1));
155: assertTrue(!element1.equals(element2));
156: assertTrue(!element1.equals(element3));
157: assertTrue(!element2.equals(element3));
158: assertTrue(element3.equals(element4));
159: assertTrue(element1.equals(element5));
160: assertFalse(element1.equals(null));
161: assertFalse(element1
162: .equals("name = value; param1 = value1; param2 = value2"));
163: }
164:
165: public void testToString() {
166: String s = "name=value; param1=value1; param2=value2";
167: HeaderElement element = BasicHeaderValueParser
168: .parseHeaderElement(s, null);
169: assertEquals(s, element.toString());
170: s = "name; param1=value1; param2=value2";
171: element = BasicHeaderValueParser.parseHeaderElement(s, null);
172: assertEquals(s, element.toString());
173: }
174:
175: public void testCloning() throws Exception {
176: BasicHeaderElement orig = new BasicHeaderElement("name",
177: "value", new NameValuePair[] {
178: new BasicNameValuePair("param1", "value1"),
179: new BasicNameValuePair("param2", "value2") });
180: BasicHeaderElement clone = (BasicHeaderElement) orig.clone();
181: assertEquals(orig, clone);
182: }
183:
184: }
|