001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/test/java/org/apache/http/message/TestHeaderGroup.java $
003: * $Revision: 604625 $
004: * $Date: 2007-12-16 15:11:11 +0100 (Sun, 16 Dec 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 java.util.Iterator;
035:
036: import org.apache.http.Header;
037: import org.apache.http.message.BasicHeader;
038: import org.apache.http.message.HeaderGroup;
039:
040: import junit.framework.Test;
041: import junit.framework.TestCase;
042: import junit.framework.TestSuite;
043:
044: /**
045: * Unit tests for {@link HeaderGroup}.
046: *
047: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
048: */
049: public class TestHeaderGroup extends TestCase {
050:
051: public TestHeaderGroup(String testName) {
052: super (testName);
053: }
054:
055: public static void main(String args[]) {
056: String[] testCaseName = { TestHeaderGroup.class.getName() };
057: junit.textui.TestRunner.main(testCaseName);
058: }
059:
060: public static Test suite() {
061: return new TestSuite(TestHeaderGroup.class);
062: }
063:
064: public void testConstructor() {
065: HeaderGroup headergroup = new HeaderGroup();
066: assertNotNull(headergroup.getAllHeaders());
067: assertEquals(0, headergroup.getAllHeaders().length);
068: }
069:
070: public void testClear() {
071: HeaderGroup headergroup = new HeaderGroup();
072: headergroup.addHeader(new BasicHeader("name", "value"));
073: assertEquals(1, headergroup.getAllHeaders().length);
074: headergroup.clear();
075: assertEquals(0, headergroup.getAllHeaders().length);
076: }
077:
078: public void testAddRemoveHeader() {
079: HeaderGroup headergroup = new HeaderGroup();
080: Header header = new BasicHeader("name", "value");
081: headergroup.addHeader(header);
082: headergroup.addHeader(null);
083: assertEquals(1, headergroup.getAllHeaders().length);
084: headergroup.removeHeader(header);
085: headergroup.removeHeader(null);
086: assertEquals(0, headergroup.getAllHeaders().length);
087: }
088:
089: public void testUpdateHeader() {
090: HeaderGroup headergroup = new HeaderGroup();
091: Header header1 = new BasicHeader("name1", "value1");
092: Header header2 = new BasicHeader("name2", "value2");
093: Header header3 = new BasicHeader("name3", "value3");
094: headergroup.addHeader(header1);
095: headergroup.addHeader(header2);
096: headergroup.addHeader(header3);
097: headergroup.updateHeader(new BasicHeader("name2", "newvalue"));
098: headergroup.updateHeader(new BasicHeader("name4", "value4"));
099: headergroup.updateHeader(null);
100: assertEquals(4, headergroup.getAllHeaders().length);
101: assertEquals("newvalue", headergroup.getFirstHeader("name2")
102: .getValue());
103: }
104:
105: public void testSetHeaders() {
106: HeaderGroup headergroup = new HeaderGroup();
107: Header header1 = new BasicHeader("name1", "value1");
108: Header header2 = new BasicHeader("name2", "value2");
109: Header header3 = new BasicHeader("name3", "value3");
110: headergroup.addHeader(header1);
111: headergroup.setHeaders(new Header[] { header2, header3 });
112: assertEquals(2, headergroup.getAllHeaders().length);
113: assertEquals(0, headergroup.getHeaders("name1").length);
114: assertFalse(headergroup.containsHeader("name1"));
115: assertEquals(1, headergroup.getHeaders("name2").length);
116: assertTrue(headergroup.containsHeader("name2"));
117: assertEquals(1, headergroup.getHeaders("name3").length);
118: assertTrue(headergroup.containsHeader("name3"));
119: headergroup.setHeaders(null);
120: assertEquals(0, headergroup.getAllHeaders().length);
121: }
122:
123: public void testFirstLastHeaders() {
124: HeaderGroup headergroup = new HeaderGroup();
125: Header header1 = new BasicHeader("name", "value1");
126: Header header2 = new BasicHeader("name", "value2");
127: Header header3 = new BasicHeader("name", "value3");
128: headergroup
129: .setHeaders(new Header[] { header1, header2, header3 });
130:
131: assertNull(headergroup.getFirstHeader("whatever"));
132: assertNull(headergroup.getLastHeader("whatever"));
133:
134: assertEquals("value1", headergroup.getFirstHeader("name")
135: .getValue());
136: assertEquals("value3", headergroup.getLastHeader("name")
137: .getValue());
138: }
139:
140: public void testCondensedHeader() {
141: HeaderGroup headergroup = new HeaderGroup();
142: assertNull(headergroup.getCondensedHeader("name"));
143:
144: Header header1 = new BasicHeader("name", "value1");
145: Header header2 = new BasicHeader("name", "value2");
146: Header header3 = new BasicHeader("name", "value3");
147: headergroup
148: .setHeaders(new Header[] { header1, header2, header3 });
149:
150: assertEquals("value1, value2, value3", headergroup
151: .getCondensedHeader("name").getValue());
152:
153: headergroup.setHeaders(new Header[] { header1 });
154: assertEquals(header1, headergroup.getCondensedHeader("name"));
155: }
156:
157: public void testIterator() {
158: HeaderGroup headergroup = new HeaderGroup();
159: Iterator i = headergroup.iterator();
160: assertNotNull(i);
161: assertFalse(i.hasNext());
162: }
163:
164: public void testCloning() throws Exception {
165: HeaderGroup orig = new HeaderGroup();
166: Header header1 = new BasicHeader("name", "value1");
167: Header header2 = new BasicHeader("name", "value2");
168: Header header3 = new BasicHeader("name", "value3");
169: orig.setHeaders(new Header[] { header1, header2, header3 });
170: HeaderGroup clone = (HeaderGroup) orig.clone();
171: Header[] headers1 = orig.getAllHeaders();
172: Header[] headers2 = clone.getAllHeaders();
173: assertNotNull(headers1);
174: assertNotNull(headers2);
175: assertEquals(headers1.length, headers2.length);
176: for (int i = 0; i < headers1.length; i++) {
177: assertEquals(headers1[i], headers2[i]);
178: }
179: }
180:
181: }
|