001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/test/java/org/apache/http/message/TestAbstractMessage.java $
003: * $Revision: 505893 $
004: * $Date: 2007-02-11 12:31:59 +0100 (Sun, 11 Feb 2007) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.message;
033:
034: import org.apache.http.Header;
035: import org.apache.http.HttpMessage;
036: import org.apache.http.params.BasicHttpParams;
037: import org.apache.http.mockup.HttpMessageMockup;
038: import org.apache.http.params.HttpParams;
039:
040: import junit.framework.Test;
041: import junit.framework.TestCase;
042: import junit.framework.TestSuite;
043:
044: /**
045: * Unit tests for {@link Header}.
046: *
047: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
048: */
049: public class TestAbstractMessage extends TestCase {
050:
051: public TestAbstractMessage(String testName) {
052: super (testName);
053: }
054:
055: public static void main(String args[]) {
056: String[] testCaseName = { TestAbstractMessage.class.getName() };
057: junit.textui.TestRunner.main(testCaseName);
058: }
059:
060: public static Test suite() {
061: return new TestSuite(TestAbstractMessage.class);
062: }
063:
064: public void testBasicProperties() {
065: HttpMessage message = new HttpMessageMockup();
066: assertNotNull(message.getParams());
067: assertNotNull(message.headerIterator());
068: Header[] headers = message.getAllHeaders();
069: assertNotNull(headers);
070: assertEquals(0, headers.length);
071: }
072:
073: public void testBasicHeaderOps() {
074: HttpMessage message = new HttpMessageMockup();
075: assertFalse(message.containsHeader("whatever"));
076:
077: message.addHeader("name", "1");
078: message.addHeader("name", "2");
079:
080: Header[] headers = message.getAllHeaders();
081: assertNotNull(headers);
082: assertEquals(2, headers.length);
083:
084: Header h = message.getFirstHeader("name");
085: assertNotNull(h);
086: assertEquals("1", h.getValue());
087:
088: message.setHeader("name", "3");
089: h = message.getFirstHeader("name");
090: assertNotNull(h);
091: assertEquals("3", h.getValue());
092: h = message.getLastHeader("name");
093: assertNotNull(h);
094: assertEquals("2", h.getValue());
095:
096: // Should have no effect
097: message.addHeader(null);
098: message.setHeader(null);
099:
100: headers = message.getHeaders("name");
101: assertNotNull(headers);
102: assertEquals(2, headers.length);
103: assertEquals("3", headers[0].getValue());
104: assertEquals("2", headers[1].getValue());
105:
106: message.addHeader("name", "4");
107:
108: headers[1] = new BasicHeader("name", "5");
109: message.setHeaders(headers);
110:
111: headers = message.getHeaders("name");
112: assertNotNull(headers);
113: assertEquals(2, headers.length);
114: assertEquals("3", headers[0].getValue());
115: assertEquals("5", headers[1].getValue());
116:
117: message.setHeader("whatever", null);
118: message.removeHeaders("name");
119: message.removeHeaders(null);
120: headers = message.getAllHeaders();
121: assertNotNull(headers);
122: assertEquals(1, headers.length);
123: assertEquals(null, headers[0].getValue());
124:
125: message.removeHeader(message.getFirstHeader("whatever"));
126: headers = message.getAllHeaders();
127: assertNotNull(headers);
128: assertEquals(0, headers.length);
129: }
130:
131: public void testParameters() {
132: HttpMessage message = new HttpMessageMockup();
133: assertNotNull(message.getParams());
134: HttpParams params = new BasicHttpParams();
135: message.setParams(params);
136: assertTrue(params == message.getParams());
137: }
138:
139: public void testInvalidInput() {
140: HttpMessage message = new HttpMessageMockup();
141: try {
142: message.setParams(null);
143: fail("IllegalArgumentException should have been thrown");
144: } catch (IllegalArgumentException ex) {
145: // expected
146: }
147: try {
148: message.addHeader(null, null);
149: fail("IllegalArgumentException should have been thrown");
150: } catch (IllegalArgumentException ex) {
151: // expected
152: }
153: try {
154: message.setHeader(null, null);
155: fail("IllegalArgumentException should have been thrown");
156: } catch (IllegalArgumentException ex) {
157: // expected
158: }
159: }
160:
161: }
|