001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/test/java/org/apache/http/message/TestBasicHeaderElementIterator.java $
003: * $Revision: 612566 $
004: * $Date: 2008-01-16 21:48:27 +0100 (Wed, 16 Jan 2008) $
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: */
030:
031: package org.apache.http.message;
032:
033: import java.util.NoSuchElementException;
034:
035: import junit.framework.Test;
036: import junit.framework.TestCase;
037: import junit.framework.TestSuite;
038:
039: import org.apache.http.Header;
040: import org.apache.http.HeaderElement;
041: import org.apache.http.HeaderElementIterator;
042: import org.apache.http.message.BasicHeader;
043: import org.apache.http.message.BasicHeaderElementIterator;
044: import org.apache.http.message.BasicHeaderIterator;
045:
046: /**
047: * Tests for {@link BasicHeaderElementIterator}.
048: *
049: * @version $Revision: 612566 $
050: *
051: * @author Andrea Selva <selva.andre at gmail.com>
052: */
053: public class TestBasicHeaderElementIterator extends TestCase {
054:
055: // ------------------------------------------------------------ Constructor
056: public TestBasicHeaderElementIterator(String testName) {
057: super (testName);
058: }
059:
060: // ------------------------------------------------------------------- Main
061: public static void main(String args[]) {
062: String[] testCaseName = { TestBasicHeaderElementIterator.class
063: .getName() };
064: junit.textui.TestRunner.main(testCaseName);
065: }
066:
067: // ------------------------------------------------------- TestCase Methods
068: public static Test suite() {
069: return new TestSuite(TestBasicHeaderElementIterator.class);
070: }
071:
072: public void testMultiHeader() {
073: Header[] headers = new Header[] {
074: new BasicHeader("Name", "value0"),
075: new BasicHeader("Name", "value1") };
076:
077: HeaderElementIterator hei = new BasicHeaderElementIterator(
078: new BasicHeaderIterator(headers, "Name"));
079:
080: assertTrue(hei.hasNext());
081: HeaderElement elem = (HeaderElement) hei.next();
082: assertEquals("The two header values must be equal", "value0",
083: elem.getName());
084:
085: assertTrue(hei.hasNext());
086: elem = (HeaderElement) hei.next();
087: assertEquals("The two header values must be equal", "value1",
088: elem.getName());
089:
090: assertFalse(hei.hasNext());
091: try {
092: hei.next();
093: fail("NoSuchElementException should have been thrown");
094: } catch (NoSuchElementException ex) {
095: // expected
096: }
097:
098: assertFalse(hei.hasNext());
099: try {
100: hei.next();
101: fail("NoSuchElementException should have been thrown");
102: } catch (NoSuchElementException ex) {
103: // expected
104: }
105: }
106:
107: public void testMultiHeaderSameLine() {
108: Header[] headers = new Header[] {
109: new BasicHeader("name", "value0,value1"),
110: new BasicHeader("nAme", "cookie1=1,cookie2=2") };
111:
112: HeaderElementIterator hei = new BasicHeaderElementIterator(
113: new BasicHeaderIterator(headers, "Name"));
114:
115: HeaderElement elem = (HeaderElement) hei.next();
116: assertEquals("The two header values must be equal", "value0",
117: elem.getName());
118: elem = (HeaderElement) hei.next();
119: assertEquals("The two header values must be equal", "value1",
120: elem.getName());
121: elem = (HeaderElement) hei.next();
122: assertEquals("The two header values must be equal", "cookie1",
123: elem.getName());
124: assertEquals("The two header values must be equal", "1", elem
125: .getValue());
126:
127: elem = (HeaderElement) hei.next();
128: assertEquals("The two header values must be equal", "cookie2",
129: elem.getName());
130: assertEquals("The two header values must be equal", "2", elem
131: .getValue());
132: }
133:
134: public void testFringeCases() {
135: Header[] headers = new Header[] {
136: new BasicHeader("Name", null),
137: new BasicHeader("Name", " "),
138: new BasicHeader("Name", ",,,") };
139:
140: HeaderElementIterator hei = new BasicHeaderElementIterator(
141: new BasicHeaderIterator(headers, "Name"));
142:
143: assertFalse(hei.hasNext());
144: try {
145: hei.next();
146: fail("NoSuchElementException should have been thrown");
147: } catch (NoSuchElementException ex) {
148: // expected
149: }
150:
151: assertFalse(hei.hasNext());
152: try {
153: hei.next();
154: fail("NoSuchElementException should have been thrown");
155: } catch (NoSuchElementException ex) {
156: // expected
157: }
158: }
159:
160: }
|