001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/test/java/org/apache/http/message/TestBasicTokenIterator.java $
003: * $Revision: 601000 $
004: * $Date: 2007-12-04 18:03:49 +0100 (Tue, 04 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: */
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: import org.apache.http.TokenIterator;
042: import org.apache.http.ParseException;
043:
044: /**
045: * Tests for {@link BasicTokenIterator}.
046: *
047: * @version $Revision: 601000 $
048: */
049: public class TestBasicTokenIterator extends TestCase {
050:
051: // ------------------------------------------------------------ Constructor
052: public TestBasicTokenIterator(String testName) {
053: super (testName);
054: }
055:
056: // ------------------------------------------------------------------- Main
057: public static void main(String args[]) {
058: String[] testCaseName = { TestBasicTokenIterator.class
059: .getName() };
060: junit.textui.TestRunner.main(testCaseName);
061: }
062:
063: // ------------------------------------------------------- TestCase Methods
064:
065: public static Test suite() {
066: return new TestSuite(TestBasicTokenIterator.class);
067: }
068:
069: public void testSingleHeader() {
070: Header[] headers = new Header[] { new BasicHeader("Name",
071: "token0,token1, token2 , token3") };
072: HeaderIterator hit = new BasicHeaderIterator(headers, null);
073: TokenIterator ti = new BasicTokenIterator(hit);
074:
075: assertTrue(ti.hasNext());
076: assertEquals("token0", "token0", ti.nextToken());
077: assertTrue(ti.hasNext());
078: assertEquals("token1", "token1", ti.nextToken());
079: assertTrue(ti.hasNext());
080: assertEquals("token2", "token2", ti.nextToken());
081: assertTrue(ti.hasNext());
082: assertEquals("token3", "token3", ti.nextToken());
083: assertFalse(ti.hasNext());
084:
085: headers = new Header[] { new BasicHeader("Name", "token0") };
086: hit = new BasicHeaderIterator(headers, null);
087: ti = new BasicTokenIterator(hit);
088:
089: assertTrue(ti.hasNext());
090: assertEquals("token0", "token0", ti.nextToken());
091: assertFalse(ti.hasNext());
092: }
093:
094: public void testMultiHeader() {
095: Header[] headers = new Header[] {
096: new BasicHeader("Name", "token0,token1"),
097: new BasicHeader("Name", ""),
098: new BasicHeader("Name", "token2"),
099: new BasicHeader("Name", " "),
100: new BasicHeader("Name", "token3 "),
101: new BasicHeader("Name", ","),
102: new BasicHeader("Name", "token4"), };
103: HeaderIterator hit = new BasicHeaderIterator(headers, null);
104: TokenIterator ti = new BasicTokenIterator(hit);
105:
106: assertTrue(ti.hasNext());
107: assertEquals("token0", "token0", ti.nextToken());
108: assertTrue(ti.hasNext());
109: assertEquals("token1", "token1", ti.nextToken());
110: assertTrue(ti.hasNext());
111: assertEquals("token2", "token2", ti.nextToken());
112: assertTrue(ti.hasNext());
113: assertEquals("token3", "token3", ti.nextToken());
114: assertTrue(ti.hasNext());
115: assertEquals("token4", "token4", ti.nextToken());
116: assertFalse(ti.hasNext());
117: }
118:
119: public void testEmpty() {
120: Header[] headers = new Header[] { new BasicHeader("Name", " "),
121: new BasicHeader("Name", ""),
122: new BasicHeader("Name", ","),
123: new BasicHeader("Name", " ,, "), };
124: HeaderIterator hit = new BasicHeaderIterator(headers, null);
125: TokenIterator ti = new BasicTokenIterator(hit);
126:
127: assertFalse(ti.hasNext());
128:
129: hit = new BasicHeaderIterator(headers, "empty");
130: ti = new BasicTokenIterator(hit);
131:
132: assertFalse(ti.hasNext());
133: }
134:
135: public void testValueStart() {
136: Header[] headers = new Header[] {
137: new BasicHeader("Name", "token0"),
138: new BasicHeader("Name", " token1"),
139: new BasicHeader("Name", ",token2"),
140: new BasicHeader("Name", " ,token3"),
141: new BasicHeader("Name", ", token4"),
142: new BasicHeader("Name", " , token5"), };
143: HeaderIterator hit = new BasicHeaderIterator(headers, null);
144: TokenIterator ti = new BasicTokenIterator(hit);
145:
146: assertTrue(ti.hasNext());
147: assertEquals("token0", "token0", ti.nextToken());
148: assertTrue(ti.hasNext());
149: assertEquals("token1", "token1", ti.nextToken());
150: assertTrue(ti.hasNext());
151: assertEquals("token2", "token2", ti.nextToken());
152: assertTrue(ti.hasNext());
153: assertEquals("token3", "token3", ti.nextToken());
154: assertTrue(ti.hasNext());
155: assertEquals("token4", "token4", ti.nextToken());
156: assertTrue(ti.hasNext());
157: assertEquals("token5", "token5", ti.nextToken());
158: assertFalse(ti.hasNext());
159: }
160:
161: public void testValueEnd() {
162: Header[] headers = new Header[] {
163: new BasicHeader("Name", "token0"),
164: new BasicHeader("Name", "token1 "),
165: new BasicHeader("Name", "token2,"),
166: new BasicHeader("Name", "token3 ,"),
167: new BasicHeader("Name", "token4, "),
168: new BasicHeader("Name", "token5 , "), };
169: HeaderIterator hit = new BasicHeaderIterator(headers, null);
170: TokenIterator ti = new BasicTokenIterator(hit);
171:
172: assertTrue(ti.hasNext());
173: assertEquals("token0", "token0", ti.nextToken());
174: assertTrue(ti.hasNext());
175: assertEquals("token1", "token1", ti.nextToken());
176: assertTrue(ti.hasNext());
177: assertEquals("token2", "token2", ti.nextToken());
178: assertTrue(ti.hasNext());
179: assertEquals("token3", "token3", ti.nextToken());
180: assertTrue(ti.hasNext());
181: assertEquals("token4", "token4", ti.nextToken());
182: assertTrue(ti.hasNext());
183: assertEquals("token5", "token5", ti.nextToken());
184: assertFalse(ti.hasNext());
185: }
186:
187: public void testTokenChar() {
188: Header[] headers = new Header[] { new BasicHeader("Name",
189: "token0") };
190: HeaderIterator hit = new BasicHeaderIterator(headers, null);
191: BasicTokenIterator bti = new BasicTokenIterator(hit);
192:
193: assertTrue("letter", bti.isTokenChar('j'));
194: assertFalse("control", bti.isTokenChar('\b'));
195: assertFalse("separator", bti.isTokenChar('?'));
196: assertTrue("other", bti.isTokenChar('-'));
197: }
198:
199: public void testInvalid() {
200: Header[] headers = new Header[] {
201: new BasicHeader("in", "token0=token1"),
202: new BasicHeader("no", "token0 token1"),
203: new BasicHeader("pre", "<token0,token1"),
204: new BasicHeader("post", "token0,token1="), };
205: HeaderIterator hit = new BasicHeaderIterator(headers, "in");
206: TokenIterator ti = new BasicTokenIterator(hit);
207:
208: // constructor located token0
209: assertTrue(ti.hasNext());
210: try {
211: ti.nextToken();
212: fail("invalid infix character not detected");
213: } catch (ParseException px) {
214: // expected
215: }
216:
217: // constructor located token0
218: hit = new BasicHeaderIterator(headers, "no");
219: ti = new BasicTokenIterator(hit);
220: assertTrue(ti.hasNext());
221: try {
222: ti.nextToken();
223: fail("missing token separator not detected");
224: } catch (ParseException px) {
225: // expected
226: }
227:
228: // constructor seeks for the first token
229: hit = new BasicHeaderIterator(headers, "pre");
230: try {
231: new BasicTokenIterator(hit);
232: fail("invalid prefix character not detected");
233: } catch (ParseException px) {
234: // expected
235: }
236:
237: hit = new BasicHeaderIterator(headers, "post");
238: ti = new BasicTokenIterator(hit);
239:
240: assertTrue(ti.hasNext());
241: assertEquals("token0", "token0", ti.nextToken());
242: assertTrue(ti.hasNext());
243: // failure after the last must not go unpunished
244: try {
245: ti.nextToken();
246: fail("invalid postfix character not detected");
247: } catch (ParseException px) {
248: // expected
249: }
250: }
251:
252: public void testWrongPublic() {
253:
254: try {
255: new BasicTokenIterator(null);
256: fail("null argument not detected");
257: } catch (IllegalArgumentException iax) {
258: // expected
259: }
260:
261: Header[] headers = new Header[] { new BasicHeader("Name", " "),
262: new BasicHeader("Name", ""),
263: new BasicHeader("Name", ","),
264: new BasicHeader("Name", " ,, "), };
265: HeaderIterator hit = new BasicHeaderIterator(headers, null);
266: TokenIterator ti = new BasicTokenIterator(hit);
267:
268: try {
269: // call next() instead of nextToken() to get that covered, too
270: ti.next();
271: fail("next after end not detected");
272: } catch (NoSuchElementException nsx) {
273: // expected
274: }
275:
276: try {
277: ti.remove();
278: fail("unsupported remove not detected");
279: } catch (UnsupportedOperationException uox) {
280: // expected
281: }
282: }
283:
284: public void testWrongProtected() {
285:
286: Header[] headers = new Header[] { new BasicHeader("Name",
287: "token1,token2") };
288: HeaderIterator hit = new BasicHeaderIterator(headers, null);
289: BasicTokenIterator bti = new BasicTokenIterator(hit);
290:
291: try {
292: bti.findTokenStart(-1);
293: fail("tokenStart: negative index not detected");
294: } catch (IllegalArgumentException iax) {
295: // expected
296: }
297:
298: try {
299: bti.findTokenSeparator(-1);
300: fail("tokenSeparator: negative index not detected");
301: } catch (IllegalArgumentException iax) {
302: // expected
303: }
304:
305: try {
306: bti.findTokenEnd(-1);
307: fail("tokenEnd: negative index not detected");
308: } catch (IllegalArgumentException iax) {
309: // expected
310: }
311: }
312:
313: }
|