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.http.message;
29  
30  import java.io.ByteArrayInputStream;
31  import java.io.ByteArrayOutputStream;
32  import java.io.ObjectInputStream;
33  import java.io.ObjectOutputStream;
34  
35  import org.apache.http.Header;
36  import org.apache.http.HeaderIterator;
37  import org.junit.Assert;
38  import org.junit.Test;
39  
40  /**
41   * Unit tests for {@link HeaderGroup}.
42   *
43   */
44  public class TestHeaderGroup {
45  
46      @Test
47      public void testConstructor() {
48          final HeaderGroup headergroup = new HeaderGroup();
49          Assert.assertNotNull(headergroup.getAllHeaders());
50          Assert.assertEquals(0, headergroup.getAllHeaders().length);
51      }
52  
53      @Test
54      public void testClear() {
55          final HeaderGroup headergroup = new HeaderGroup();
56          headergroup.addHeader(new BasicHeader("name", "value"));
57          Assert.assertEquals(1, headergroup.getAllHeaders().length);
58          headergroup.clear();
59          Assert.assertEquals(0, headergroup.getAllHeaders().length);
60      }
61  
62      @Test
63      public void testAddRemoveHeader() {
64          final HeaderGroup headergroup = new HeaderGroup();
65          final Header header = new BasicHeader("name", "value");
66          headergroup.addHeader(header);
67          headergroup.addHeader(null);
68          Assert.assertEquals(1, headergroup.getAllHeaders().length);
69          headergroup.removeHeader(header);
70          headergroup.removeHeader(null);
71          Assert.assertEquals(0, headergroup.getAllHeaders().length);
72      }
73  
74      @Test
75      public void testUpdateHeader() {
76          final HeaderGroup headergroup = new HeaderGroup();
77          final Header header1 = new BasicHeader("name1", "value1");
78          final Header header2 = new BasicHeader("name2", "value2");
79          final Header header3 = new BasicHeader("name3", "value3");
80          headergroup.addHeader(header1);
81          headergroup.addHeader(header2);
82          headergroup.addHeader(header3);
83          headergroup.updateHeader(new BasicHeader("name2", "newvalue"));
84          headergroup.updateHeader(new BasicHeader("name4", "value4"));
85          headergroup.updateHeader(null);
86          Assert.assertEquals(4, headergroup.getAllHeaders().length);
87          Assert.assertEquals("newvalue", headergroup.getFirstHeader("name2").getValue());
88      }
89  
90      @Test
91      public void testSetHeaders() {
92          final HeaderGroup headergroup = new HeaderGroup();
93          final Header header1 = new BasicHeader("name1", "value1");
94          final Header header2 = new BasicHeader("name2", "value2");
95          final Header header3 = new BasicHeader("name3", "value3");
96          headergroup.addHeader(header1);
97          headergroup.setHeaders(new Header[] { header2, header3 });
98          Assert.assertEquals(2, headergroup.getAllHeaders().length);
99          Assert.assertEquals(0, headergroup.getHeaders("name1").length);
100         Assert.assertFalse(headergroup.containsHeader("name1"));
101         Assert.assertEquals(1, headergroup.getHeaders("name2").length);
102         Assert.assertTrue(headergroup.containsHeader("name2"));
103         Assert.assertEquals(1, headergroup.getHeaders("name3").length);
104         Assert.assertTrue(headergroup.containsHeader("name3"));
105         headergroup.setHeaders(null);
106         Assert.assertEquals(0, headergroup.getAllHeaders().length);
107     }
108 
109     @Test
110     public void testFirstLastHeaders() {
111         final HeaderGroup headergroup = new HeaderGroup();
112         final Header header1 = new BasicHeader("name", "value1");
113         final Header header2 = new BasicHeader("name", "value2");
114         final Header header3 = new BasicHeader("name", "value3");
115         headergroup.setHeaders(new Header[] { header1, header2, header3 });
116 
117         Assert.assertNull(headergroup.getFirstHeader("whatever"));
118         Assert.assertNull(headergroup.getLastHeader("whatever"));
119 
120         Assert.assertEquals("value1", headergroup.getFirstHeader("name").getValue());
121         Assert.assertEquals("value3", headergroup.getLastHeader("name").getValue());
122     }
123 
124     @Test
125     public void testCondensedHeader() {
126         final HeaderGroup headergroup = new HeaderGroup();
127         Assert.assertNull(headergroup.getCondensedHeader("name"));
128 
129         final Header header1 = new BasicHeader("name", "value1");
130         final Header header2 = new BasicHeader("name", "value2");
131         final Header header3 = new BasicHeader("name", "value3");
132         headergroup.setHeaders(new Header[] { header1, header2, header3 });
133 
134         Assert.assertEquals("value1, value2, value3", headergroup.getCondensedHeader("name").getValue());
135 
136         headergroup.setHeaders(new Header[] { header1 });
137         Assert.assertEquals(header1, headergroup.getCondensedHeader("name"));
138     }
139 
140     @Test
141     public void testIterator() {
142         final HeaderGroup headergroup = new HeaderGroup();
143         final HeaderIterator i = headergroup.iterator();
144         Assert.assertNotNull(i);
145         Assert.assertFalse(i.hasNext());
146     }
147 
148     @Test
149     public void testHeaderRemove() {
150         final HeaderGroup headergroup = new HeaderGroup();
151         final Header header1 = new BasicHeader("name", "value1");
152         final Header header2 = new BasicHeader("name", "value2");
153         final Header header3 = new BasicHeader("name", "value3");
154         headergroup.setHeaders(new Header[] { header1, header2, header3 });
155         final HeaderIterator i = headergroup.iterator();
156         Assert.assertNotNull(i);
157         Assert.assertTrue(i.hasNext());
158         i.next();
159         Assert.assertTrue(i.hasNext());
160         i.next();
161         i.remove();
162         Assert.assertEquals(2, headergroup.getAllHeaders().length);
163         Assert.assertTrue(i.hasNext());
164         i.next();
165         i.remove();
166         Assert.assertEquals(1, headergroup.getAllHeaders().length);
167         Assert.assertFalse(i.hasNext());
168     }
169 
170     @Test
171     public void testCloning() throws Exception {
172         final HeaderGroup orig = new HeaderGroup();
173         final Header header1 = new BasicHeader("name", "value1");
174         final Header header2 = new BasicHeader("name", "value2");
175         final Header header3 = new BasicHeader("name", "value3");
176         orig.setHeaders(new Header[] { header1, header2, header3 });
177         final HeaderGroup clone = (HeaderGroup) orig.clone();
178         final Header[] headers1 = orig.getAllHeaders();
179         final Header[] headers2 = clone.getAllHeaders();
180         Assert.assertNotNull(headers1);
181         Assert.assertNotNull(headers2);
182         Assert.assertEquals(3, headers2.length);
183         Assert.assertEquals(headers1.length, headers2.length);
184         for (int i = 0; i < headers1.length; i++) {
185             Assert.assertEquals(headers1[i].getName(), headers2[i].getName());
186             Assert.assertEquals(headers1[i].getValue(), headers2[i].getValue());
187         }
188     }
189 
190     @Test
191     public void testSerialization() throws Exception {
192         final HeaderGroup orig = new HeaderGroup();
193         final Header header1 = new BasicHeader("name", "value1");
194         final Header header2 = new BasicHeader("name", "value2");
195         final Header header3 = new BasicHeader("name", "value3");
196         orig.setHeaders(new Header[] { header1, header2, header3 });
197         final ByteArrayOutputStream outbuffer = new ByteArrayOutputStream();
198         final ObjectOutputStream outstream = new ObjectOutputStream(outbuffer);
199         outstream.writeObject(orig);
200         outstream.close();
201         final byte[] raw = outbuffer.toByteArray();
202         final ByteArrayInputStream inbuffer = new ByteArrayInputStream(raw);
203         final ObjectInputStream instream = new ObjectInputStream(inbuffer);
204         final HeaderGroup clone = (HeaderGroup) instream.readObject();
205         final Header[] headers1 = orig.getAllHeaders();
206         final Header[] headers2 = clone.getAllHeaders();
207         Assert.assertNotNull(headers1);
208         Assert.assertNotNull(headers2);
209         Assert.assertEquals(headers1.length, headers2.length);
210         for (int i = 0; i < headers1.length; i++) {
211             Assert.assertEquals(headers1[i].getName(), headers2[i].getName());
212             Assert.assertEquals(headers1[i].getValue(), headers2[i].getValue());
213         }
214     }
215 
216 }