001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/test/java/org/apache/http/message/TestBasicHeaderIterator.java $
003: * $Revision: 581981 $
004: * $Date: 2007-10-04 20:26:26 +0200 (Thu, 04 Oct 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: */
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.HeaderIterator;
041:
042: /**
043: * Tests for {@link BasicHeaderIterator}.
044: *
045: * @version $Revision: 581981 $
046: */
047: public class TestBasicHeaderIterator extends TestCase {
048:
049: // ------------------------------------------------------------ Constructor
050: public TestBasicHeaderIterator(String testName) {
051: super (testName);
052: }
053:
054: // ------------------------------------------------------------------- Main
055: public static void main(String args[]) {
056: String[] testCaseName = { TestBasicHeaderIterator.class
057: .getName() };
058: junit.textui.TestRunner.main(testCaseName);
059: }
060:
061: // ------------------------------------------------------- TestCase Methods
062:
063: public static Test suite() {
064: return new TestSuite(TestBasicHeaderIterator.class);
065: }
066:
067: public void testAllSame() {
068: Header[] headers = new Header[] {
069: new BasicHeader("Name", "value0"),
070: new BasicHeader("nAme", "value1, value1.1"),
071: new BasicHeader("naMe", "value2=whatever"),
072: new BasicHeader("namE", "value3;tag=nil"), };
073:
074: // without filter
075: HeaderIterator hit = new BasicHeaderIterator(headers, null);
076: assertTrue(hit.hasNext());
077: assertEquals("0", headers[0], hit.nextHeader());
078: assertTrue(hit.hasNext());
079: assertEquals("1", headers[1], hit.nextHeader());
080: assertTrue(hit.hasNext());
081: assertEquals("2", headers[2], hit.nextHeader());
082: assertTrue(hit.hasNext());
083: assertEquals("3", headers[3], hit.nextHeader());
084: assertFalse(hit.hasNext());
085:
086: // with filter
087: hit = new BasicHeaderIterator(headers, "name");
088: assertTrue(hit.hasNext());
089: assertEquals("0", headers[0], hit.nextHeader());
090: assertTrue(hit.hasNext());
091: assertEquals("1", headers[1], hit.nextHeader());
092: assertTrue(hit.hasNext());
093: assertEquals("2", headers[2], hit.nextHeader());
094: assertTrue(hit.hasNext());
095: assertEquals("3", headers[3], hit.nextHeader());
096: assertFalse(hit.hasNext());
097: }
098:
099: public void testFirstLastOneNone() {
100: Header[] headers = new Header[] {
101: new BasicHeader("match", "value0"),
102: new BasicHeader("mismatch", "value1, value1.1"),
103: new BasicHeader("single", "value2=whatever"),
104: new BasicHeader("match", "value3;tag=nil"), };
105:
106: // without filter
107: HeaderIterator hit = new BasicHeaderIterator(headers, null);
108: assertTrue(hit.hasNext());
109: assertEquals("0", headers[0], hit.nextHeader());
110: assertTrue(hit.hasNext());
111: assertEquals("1", headers[1], hit.nextHeader());
112: assertTrue(hit.hasNext());
113: assertEquals("2", headers[2], hit.nextHeader());
114: assertTrue(hit.hasNext());
115: assertEquals("3", headers[3], hit.nextHeader());
116: assertFalse(hit.hasNext());
117:
118: // with filter, first & last
119: hit = new BasicHeaderIterator(headers, "match");
120: assertTrue(hit.hasNext());
121: assertEquals("0", headers[0], hit.nextHeader());
122: assertTrue(hit.hasNext());
123: assertEquals("3", headers[3], hit.nextHeader());
124: assertFalse(hit.hasNext());
125:
126: // with filter, one match
127: hit = new BasicHeaderIterator(headers, "single");
128: assertTrue(hit.hasNext());
129: assertEquals("2", headers[2], hit.nextHeader());
130: assertFalse(hit.hasNext());
131:
132: // with filter, no match
133: hit = new BasicHeaderIterator(headers, "way-off");
134: assertFalse(hit.hasNext());
135: }
136:
137: public void testInterspersed() {
138: Header[] headers = new Header[] {
139: new BasicHeader("yellow", "00"),
140: new BasicHeader("maroon", "01"),
141: new BasicHeader("orange", "02"),
142: new BasicHeader("orange", "03"),
143: new BasicHeader("orange", "04"),
144: new BasicHeader("yellow", "05"),
145: new BasicHeader("maroon", "06"),
146: new BasicHeader("maroon", "07"),
147: new BasicHeader("maroon", "08"),
148: new BasicHeader("yellow", "09"),
149: new BasicHeader("maroon", "0a"),
150: new BasicHeader("yellow", "0b"),
151: new BasicHeader("orange", "0c"),
152: new BasicHeader("yellow", "0d"),
153: new BasicHeader("orange", "0e"), };
154:
155: // without filter
156: HeaderIterator hit = new BasicHeaderIterator(headers, null);
157: assertTrue(hit.hasNext());
158: assertEquals("0", headers[0], hit.nextHeader());
159: assertTrue(hit.hasNext());
160: assertEquals("1", headers[1], hit.nextHeader());
161: assertTrue(hit.hasNext());
162: assertEquals("2", headers[2], hit.nextHeader());
163: assertTrue(hit.hasNext());
164: assertEquals("3", headers[3], hit.nextHeader());
165: assertTrue(hit.hasNext());
166: assertEquals("4", headers[4], hit.nextHeader());
167: assertTrue(hit.hasNext());
168: assertEquals("5", headers[5], hit.nextHeader());
169: assertTrue(hit.hasNext());
170: assertEquals("6", headers[6], hit.nextHeader());
171: assertTrue(hit.hasNext());
172: assertEquals("7", headers[7], hit.nextHeader());
173: assertTrue(hit.hasNext());
174: assertEquals("8", headers[8], hit.nextHeader());
175: assertTrue(hit.hasNext());
176: assertEquals("9", headers[9], hit.nextHeader());
177: assertTrue(hit.hasNext());
178: assertEquals("a", headers[10], hit.nextHeader());
179: assertTrue(hit.hasNext());
180: assertEquals("b", headers[11], hit.nextHeader());
181: assertTrue(hit.hasNext());
182: assertEquals("c", headers[12], hit.nextHeader());
183: assertTrue(hit.hasNext());
184: assertEquals("d", headers[13], hit.nextHeader());
185: assertTrue(hit.hasNext());
186: assertEquals("e", headers[14], hit.nextHeader());
187: assertFalse(hit.hasNext());
188:
189: // yellow 0, 5, 9, 11, 13
190: hit = new BasicHeaderIterator(headers, "Yellow");
191: assertTrue(hit.hasNext());
192: assertEquals("0", headers[0], hit.nextHeader());
193: assertTrue(hit.hasNext());
194: assertEquals("5", headers[5], hit.nextHeader());
195: assertTrue(hit.hasNext());
196: assertEquals("9", headers[9], hit.nextHeader());
197: assertTrue(hit.hasNext());
198: assertEquals("b", headers[11], hit.nextHeader());
199: assertTrue(hit.hasNext());
200: assertEquals("d", headers[13], hit.nextHeader());
201: assertFalse(hit.hasNext());
202:
203: // maroon 1, 6, 7, 8, 10
204: hit = new BasicHeaderIterator(headers, "marOOn");
205: assertTrue(hit.hasNext());
206: assertEquals("1", headers[1], hit.nextHeader());
207: assertTrue(hit.hasNext());
208: assertEquals("6", headers[6], hit.nextHeader());
209: assertTrue(hit.hasNext());
210: assertEquals("7", headers[7], hit.nextHeader());
211: assertTrue(hit.hasNext());
212: assertEquals("8", headers[8], hit.nextHeader());
213: assertTrue(hit.hasNext());
214: assertEquals("a", headers[10], hit.nextHeader());
215: assertFalse(hit.hasNext());
216:
217: // orange 2, 3, 4, 12, 14
218: hit = new BasicHeaderIterator(headers, "OranGe");
219: assertTrue(hit.hasNext());
220: assertEquals("2", headers[2], hit.nextHeader());
221: assertTrue(hit.hasNext());
222: assertEquals("3", headers[3], hit.nextHeader());
223: assertTrue(hit.hasNext());
224: assertEquals("4", headers[4], hit.nextHeader());
225: assertTrue(hit.hasNext());
226: assertEquals("b", headers[12], hit.nextHeader());
227: assertTrue(hit.hasNext());
228: assertEquals("e", headers[14], hit.nextHeader());
229: assertFalse(hit.hasNext());
230: }
231:
232: public void testInvalid() {
233:
234: HeaderIterator hit = null;
235: try {
236: hit = new BasicHeaderIterator(null, "whatever");
237: fail("null headers not detected");
238: } catch (IllegalArgumentException iax) {
239: // expected
240: }
241:
242: // this is not invalid
243: hit = new BasicHeaderIterator(new Header[0], "whatever");
244: assertFalse(hit.hasNext());
245:
246: // but this is
247: try {
248: hit.nextHeader();
249: fail("next beyond end not detected");
250: } catch (NoSuchElementException nsx) {
251: // expected
252: }
253: }
254:
255: public void testRemaining() {
256: // to satisfy Clover and take coverage to 100%
257:
258: Header[] headers = new Header[] {
259: new BasicHeader("Name", "value0"),
260: new BasicHeader("nAme", "value1, value1.1"),
261: new BasicHeader("naMe", "value2=whatever"),
262: new BasicHeader("namE", "value3;tag=nil"), };
263:
264: // without filter, using plain next()
265: HeaderIterator hit = new BasicHeaderIterator(headers, null);
266: assertTrue(hit.hasNext());
267: assertEquals("0", headers[0], hit.next());
268: assertTrue(hit.hasNext());
269: assertEquals("1", headers[1], hit.next());
270: assertTrue(hit.hasNext());
271: assertEquals("2", headers[2], hit.next());
272: assertTrue(hit.hasNext());
273: assertEquals("3", headers[3], hit.next());
274: assertFalse(hit.hasNext());
275:
276: hit = new BasicHeaderIterator(headers, null);
277: assertTrue(hit.hasNext());
278: try {
279: hit.remove();
280: fail("remove not detected");
281: } catch (UnsupportedOperationException uox) {
282: // expected
283: }
284:
285: assertTrue("no next",
286: ((BasicHeaderIterator) hit).findNext(-3) < 0);
287: }
288: }
|