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  package org.apache.hc.core5.http.message;
28  
29  import java.util.Iterator;
30  import java.util.NoSuchElementException;
31  
32  import org.apache.hc.core5.http.Header;
33  import org.junit.Assert;
34  import org.junit.Test;
35  
36  
37  /**
38   * Tests for {@link java.util.Iterator} of {@link org.apache.hc.core5.http.Header}s.
39   *
40   */
41  public class TestBasicHeaderIterator {
42  
43      @Test
44      public void testAllSame() {
45          final Header[] headers = new Header[]{
46              new BasicHeader("Name", "value0"),
47              new BasicHeader("nAme", "value1, value1.1"),
48              new BasicHeader("naMe", "value2=whatever"),
49              new BasicHeader("namE", "value3;tag=nil"),
50          };
51  
52          // without filter
53          Iterator<Header> hit = new BasicHeaderIterator(headers, null);
54          Assert.assertTrue(hit.hasNext());
55          Assert.assertEquals("0", headers[0], hit.next());
56          Assert.assertTrue(hit.hasNext());
57          Assert.assertEquals("1", headers[1], hit.next());
58          Assert.assertTrue(hit.hasNext());
59          Assert.assertEquals("2", headers[2], hit.next());
60          Assert.assertTrue(hit.hasNext());
61          Assert.assertEquals("3", headers[3], hit.next());
62          Assert.assertFalse(hit.hasNext());
63  
64          // with filter
65          hit = new BasicHeaderIterator(headers, "name");
66          Assert.assertTrue(hit.hasNext());
67          Assert.assertEquals("0", headers[0], hit.next());
68          Assert.assertTrue(hit.hasNext());
69          Assert.assertEquals("1", headers[1], hit.next());
70          Assert.assertTrue(hit.hasNext());
71          Assert.assertEquals("2", headers[2], hit.next());
72          Assert.assertTrue(hit.hasNext());
73          Assert.assertEquals("3", headers[3], hit.next());
74          Assert.assertFalse(hit.hasNext());
75      }
76  
77  
78      @Test
79      public void testFirstLastOneNone() {
80          final Header[] headers = new Header[]{
81              new BasicHeader("match"   , "value0"),
82              new BasicHeader("mismatch", "value1, value1.1"),
83              new BasicHeader("single"  , "value2=whatever"),
84              new BasicHeader("match"   , "value3;tag=nil"),
85          };
86  
87          // without filter
88          Iterator<Header> hit = new BasicHeaderIterator(headers, null);
89          Assert.assertTrue(hit.hasNext());
90          Assert.assertEquals("0", headers[0], hit.next());
91          Assert.assertTrue(hit.hasNext());
92          Assert.assertEquals("1", headers[1], hit.next());
93          Assert.assertTrue(hit.hasNext());
94          Assert.assertEquals("2", headers[2], hit.next());
95          Assert.assertTrue(hit.hasNext());
96          Assert.assertEquals("3", headers[3], hit.next());
97          Assert.assertFalse(hit.hasNext());
98  
99          // with filter, first & last
100         hit = new BasicHeaderIterator(headers, "match");
101         Assert.assertTrue(hit.hasNext());
102         Assert.assertEquals("0", headers[0], hit.next());
103         Assert.assertTrue(hit.hasNext());
104         Assert.assertEquals("3", headers[3], hit.next());
105         Assert.assertFalse(hit.hasNext());
106 
107         // with filter, one match
108         hit = new BasicHeaderIterator(headers, "single");
109         Assert.assertTrue(hit.hasNext());
110         Assert.assertEquals("2", headers[2], hit.next());
111         Assert.assertFalse(hit.hasNext());
112 
113         // with filter, no match
114         hit = new BasicHeaderIterator(headers, "way-off");
115         Assert.assertFalse(hit.hasNext());
116     }
117 
118 
119     @Test
120     public void testInterspersed() {
121         final Header[] headers = new Header[]{
122             new BasicHeader("yellow", "00"),
123             new BasicHeader("maroon", "01"),
124             new BasicHeader("orange", "02"),
125             new BasicHeader("orange", "03"),
126             new BasicHeader("orange", "04"),
127             new BasicHeader("yellow", "05"),
128             new BasicHeader("maroon", "06"),
129             new BasicHeader("maroon", "07"),
130             new BasicHeader("maroon", "08"),
131             new BasicHeader("yellow", "09"),
132             new BasicHeader("maroon", "0a"),
133             new BasicHeader("yellow", "0b"),
134             new BasicHeader("orange", "0c"),
135             new BasicHeader("yellow", "0d"),
136             new BasicHeader("orange", "0e"),
137         };
138 
139         // without filter
140         Iterator<Header> hit = new BasicHeaderIterator(headers, null);
141         Assert.assertTrue(hit.hasNext());
142         Assert.assertEquals("0", headers[0], hit.next());
143         Assert.assertTrue(hit.hasNext());
144         Assert.assertEquals("1", headers[1], hit.next());
145         Assert.assertTrue(hit.hasNext());
146         Assert.assertEquals("2", headers[2], hit.next());
147         Assert.assertTrue(hit.hasNext());
148         Assert.assertEquals("3", headers[3], hit.next());
149         Assert.assertTrue(hit.hasNext());
150         Assert.assertEquals("4", headers[4], hit.next());
151         Assert.assertTrue(hit.hasNext());
152         Assert.assertEquals("5", headers[5], hit.next());
153         Assert.assertTrue(hit.hasNext());
154         Assert.assertEquals("6", headers[6], hit.next());
155         Assert.assertTrue(hit.hasNext());
156         Assert.assertEquals("7", headers[7], hit.next());
157         Assert.assertTrue(hit.hasNext());
158         Assert.assertEquals("8", headers[8], hit.next());
159         Assert.assertTrue(hit.hasNext());
160         Assert.assertEquals("9", headers[9], hit.next());
161         Assert.assertTrue(hit.hasNext());
162         Assert.assertEquals("a", headers[10], hit.next());
163         Assert.assertTrue(hit.hasNext());
164         Assert.assertEquals("b", headers[11], hit.next());
165         Assert.assertTrue(hit.hasNext());
166         Assert.assertEquals("c", headers[12], hit.next());
167         Assert.assertTrue(hit.hasNext());
168         Assert.assertEquals("d", headers[13], hit.next());
169         Assert.assertTrue(hit.hasNext());
170         Assert.assertEquals("e", headers[14], hit.next());
171         Assert.assertFalse(hit.hasNext());
172 
173         // yellow 0, 5, 9, 11, 13
174         hit = new BasicHeaderIterator(headers, "Yellow");
175         Assert.assertTrue(hit.hasNext());
176         Assert.assertEquals("0", headers[0], hit.next());
177         Assert.assertTrue(hit.hasNext());
178         Assert.assertEquals("5", headers[5], hit.next());
179         Assert.assertTrue(hit.hasNext());
180         Assert.assertEquals("9", headers[9], hit.next());
181         Assert.assertTrue(hit.hasNext());
182         Assert.assertEquals("b", headers[11], hit.next());
183         Assert.assertTrue(hit.hasNext());
184         Assert.assertEquals("d", headers[13], hit.next());
185         Assert.assertFalse(hit.hasNext());
186 
187         // maroon 1, 6, 7, 8, 10
188         hit = new BasicHeaderIterator(headers, "marOOn");
189         Assert.assertTrue(hit.hasNext());
190         Assert.assertEquals("1", headers[1], hit.next());
191         Assert.assertTrue(hit.hasNext());
192         Assert.assertEquals("6", headers[6], hit.next());
193         Assert.assertTrue(hit.hasNext());
194         Assert.assertEquals("7", headers[7], hit.next());
195         Assert.assertTrue(hit.hasNext());
196         Assert.assertEquals("8", headers[8], hit.next());
197         Assert.assertTrue(hit.hasNext());
198         Assert.assertEquals("a", headers[10], hit.next());
199         Assert.assertFalse(hit.hasNext());
200 
201         // orange 2, 3, 4, 12, 14
202         hit = new BasicHeaderIterator(headers, "OranGe");
203         Assert.assertTrue(hit.hasNext());
204         Assert.assertEquals("2", headers[2], hit.next());
205         Assert.assertTrue(hit.hasNext());
206         Assert.assertEquals("3", headers[3], hit.next());
207         Assert.assertTrue(hit.hasNext());
208         Assert.assertEquals("4", headers[4], hit.next());
209         Assert.assertTrue(hit.hasNext());
210         Assert.assertEquals("b", headers[12], hit.next());
211         Assert.assertTrue(hit.hasNext());
212         Assert.assertEquals("e", headers[14], hit.next());
213         Assert.assertFalse(hit.hasNext());
214     }
215 
216 
217     @Test
218     public void testInvalid() {
219 
220         Iterator<Header> hit = null;
221         try {
222             hit = new BasicHeaderIterator(null, "whatever");
223             Assert.fail("null headers not detected");
224         } catch (final IllegalArgumentException iax) {
225             // expected
226         }
227 
228         // this is not invalid
229         hit = new BasicHeaderIterator(new Header[0], "whatever");
230         Assert.assertFalse(hit.hasNext());
231 
232         // but this is
233         try {
234             hit.next();
235             Assert.fail("next beyond end not detected");
236         } catch (final NoSuchElementException nsx) {
237             // expected
238         }
239     }
240 
241 
242     @Test
243     public void testRemaining() {
244         // to satisfy Clover and take coverage to 100%
245 
246         final Header[] headers = new Header[]{
247             new BasicHeader("Name", "value0"),
248             new BasicHeader("nAme", "value1, value1.1"),
249             new BasicHeader("naMe", "value2=whatever"),
250             new BasicHeader("namE", "value3;tag=nil"),
251         };
252 
253         // without filter, using plain next()
254         Iterator<Header> hit = new BasicHeaderIterator(headers, null);
255         Assert.assertTrue(hit.hasNext());
256         Assert.assertEquals("0", headers[0], hit.next());
257         Assert.assertTrue(hit.hasNext());
258         Assert.assertEquals("1", headers[1], hit.next());
259         Assert.assertTrue(hit.hasNext());
260         Assert.assertEquals("2", headers[2], hit.next());
261         Assert.assertTrue(hit.hasNext());
262         Assert.assertEquals("3", headers[3], hit.next());
263         Assert.assertFalse(hit.hasNext());
264 
265         hit = new BasicHeaderIterator(headers, null);
266         Assert.assertTrue(hit.hasNext());
267         try {
268             hit.remove();
269             Assert.fail("remove not detected");
270         } catch (final UnsupportedOperationException uox) {
271             // expected
272         }
273     }
274 }