View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.core5.http.message;
29  
30  import java.util.Iterator;
31  import java.util.NoSuchElementException;
32  
33  import org.apache.hc.core5.http.Header;
34  import org.junit.Assert;
35  import org.junit.Test;
36  
37  
38  /**
39   * Tests for {@link BasicTokenIterator}.
40   *
41   */
42  public class TestBasicTokenIterator {
43  
44      @Test
45      public void testSingleHeader() {
46          Header[] headers = new Header[]{
47              new BasicHeader("Name", "token0,token1, token2 , token3")
48          };
49          Iterator<Header> hit = new BasicHeaderIterator(headers, null);
50          Iterator<String>  ti  = new BasicTokenIterator(hit);
51  
52          Assert.assertTrue(ti.hasNext());
53          Assert.assertEquals("token0", "token0", ti.next());
54          Assert.assertTrue(ti.hasNext());
55          Assert.assertEquals("token1", "token1", ti.next());
56          Assert.assertTrue(ti.hasNext());
57          Assert.assertEquals("token2", "token2", ti.next());
58          Assert.assertTrue(ti.hasNext());
59          Assert.assertEquals("token3", "token3", ti.next());
60          Assert.assertFalse(ti.hasNext());
61  
62  
63          headers = new Header[]{
64              new BasicHeader("Name", "token0")
65          };
66          hit = new BasicHeaderIterator(headers, null);
67          ti  = new BasicTokenIterator(hit);
68  
69          Assert.assertTrue(ti.hasNext());
70          Assert.assertEquals("token0", "token0", ti.next());
71          Assert.assertFalse(ti.hasNext());
72      }
73  
74  
75      @Test
76      public void testMultiHeader() {
77          final Header[] headers = new Header[]{
78              new BasicHeader("Name", "token0,token1"),
79              new BasicHeader("Name", ""),
80              new BasicHeader("Name", "token2"),
81              new BasicHeader("Name", " "),
82              new BasicHeader("Name", "token3 "),
83              new BasicHeader("Name", ","),
84              new BasicHeader("Name", "token4"),
85          };
86          final Iterator<Header> hit = new BasicHeaderIterator(headers, null);
87          final Iterator<String>  ti  = new BasicTokenIterator(hit);
88  
89          Assert.assertTrue(ti.hasNext());
90          Assert.assertEquals("token0", "token0", ti.next());
91          Assert.assertTrue(ti.hasNext());
92          Assert.assertEquals("token1", "token1", ti.next());
93          Assert.assertTrue(ti.hasNext());
94          Assert.assertEquals("token2", "token2", ti.next());
95          Assert.assertTrue(ti.hasNext());
96          Assert.assertEquals("token3", "token3", ti.next());
97          Assert.assertTrue(ti.hasNext());
98          Assert.assertEquals("token4", "token4", ti.next());
99          Assert.assertFalse(ti.hasNext());
100     }
101 
102 
103     @Test
104     public void testEmpty() {
105         final Header[] headers = new Header[]{
106             new BasicHeader("Name", " "),
107             new BasicHeader("Name", ""),
108             new BasicHeader("Name", ","),
109             new BasicHeader("Name", " ,, "),
110         };
111         Iterator<Header> hit = new BasicHeaderIterator(headers, null);
112         Iterator<String>  ti  = new BasicTokenIterator(hit);
113 
114         Assert.assertFalse(ti.hasNext());
115 
116 
117         hit = new BasicHeaderIterator(headers, "empty");
118         ti  = new BasicTokenIterator(hit);
119 
120         Assert.assertFalse(ti.hasNext());
121     }
122 
123 
124     @Test
125     public void testValueStart() {
126         final Header[] headers = new Header[]{
127             new BasicHeader("Name", "token0"),
128             new BasicHeader("Name", " token1"),
129             new BasicHeader("Name", ",token2"),
130             new BasicHeader("Name", " ,token3"),
131             new BasicHeader("Name", ", token4"),
132             new BasicHeader("Name", " , token5"),
133         };
134         final Iterator<Header> hit = new BasicHeaderIterator(headers, null);
135         final Iterator<String>  ti  = new BasicTokenIterator(hit);
136 
137         Assert.assertTrue(ti.hasNext());
138         Assert.assertEquals("token0", "token0", ti.next());
139         Assert.assertTrue(ti.hasNext());
140         Assert.assertEquals("token1", "token1", ti.next());
141         Assert.assertTrue(ti.hasNext());
142         Assert.assertEquals("token2", "token2", ti.next());
143         Assert.assertTrue(ti.hasNext());
144         Assert.assertEquals("token3", "token3", ti.next());
145         Assert.assertTrue(ti.hasNext());
146         Assert.assertEquals("token4", "token4", ti.next());
147         Assert.assertTrue(ti.hasNext());
148         Assert.assertEquals("token5", "token5", ti.next());
149         Assert.assertFalse(ti.hasNext());
150     }
151 
152 
153     @Test
154     public void testValueEnd() {
155         final Header[] headers = new Header[]{
156             new BasicHeader("Name", "token0"),
157             new BasicHeader("Name", "token1 "),
158             new BasicHeader("Name", "token2,"),
159             new BasicHeader("Name", "token3 ,"),
160             new BasicHeader("Name", "token4, "),
161             new BasicHeader("Name", "token5 , "),
162         };
163         final Iterator<Header> hit = new BasicHeaderIterator(headers, null);
164         final Iterator<String>  ti  = new BasicTokenIterator(hit);
165 
166         Assert.assertTrue(ti.hasNext());
167         Assert.assertEquals("token0", "token0", ti.next());
168         Assert.assertTrue(ti.hasNext());
169         Assert.assertEquals("token1", "token1", ti.next());
170         Assert.assertTrue(ti.hasNext());
171         Assert.assertEquals("token2", "token2", ti.next());
172         Assert.assertTrue(ti.hasNext());
173         Assert.assertEquals("token3", "token3", ti.next());
174         Assert.assertTrue(ti.hasNext());
175         Assert.assertEquals("token4", "token4", ti.next());
176         Assert.assertTrue(ti.hasNext());
177         Assert.assertEquals("token5", "token5", ti.next());
178         Assert.assertFalse(ti.hasNext());
179     }
180 
181     @Test
182     public void testWrongPublic() {
183 
184         try {
185             new BasicTokenIterator(null);
186             Assert.fail("null argument not detected");
187         } catch (final IllegalArgumentException iax) {
188             // expected
189         }
190 
191         final Header[] headers = new Header[]{
192             new BasicHeader("Name", " "),
193             new BasicHeader("Name", ""),
194             new BasicHeader("Name", ","),
195             new BasicHeader("Name", " ,, "),
196         };
197         final Iterator<Header> hit = new BasicHeaderIterator(headers, null);
198         final Iterator<String>  ti  = new BasicTokenIterator(hit);
199 
200         try {
201             // call next() instead of next() to get that covered, too
202             ti.next();
203             Assert.fail("next after end not detected");
204         } catch (final NoSuchElementException nsx) {
205             // expected
206         }
207 
208         try {
209             ti.remove();
210             Assert.fail("unsupported remove not detected");
211         } catch (final UnsupportedOperationException uox) {
212             // expected
213         }
214     }
215 
216 }