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.HeaderElement;
36  import org.apache.http.util.CharArrayBuffer;
37  import org.junit.Assert;
38  import org.junit.Test;
39  
40  /**
41   * Unit tests for {@link BufferedHeader}.
42   *
43   */
44  public class TestBufferedHeader {
45  
46      @Test
47      public void testBasicConstructor() {
48          final CharArrayBuffer buf = new CharArrayBuffer(32);
49          buf.append("name: value");
50          final BufferedHeader header = new BufferedHeader(buf);
51          Assert.assertEquals("name", header.getName());
52          Assert.assertEquals("value", header.getValue());
53          Assert.assertSame(buf, header.getBuffer());
54          Assert.assertEquals(5, header.getValuePos());
55      }
56  
57      @Test
58      public void testInvalidName() {
59          try {
60              new BufferedHeader(null);
61              Assert.fail("IllegalArgumentException should have been thrown");
62          } catch (final IllegalArgumentException ex) {
63              //expected
64          }
65      }
66  
67      @Test
68      public void testHeaderElements() {
69          final CharArrayBuffer buf = new CharArrayBuffer(32);
70          buf.append("name: element1 = value1, element2; param1 = value1, element3");
71          final BufferedHeader header = new BufferedHeader(buf);
72          final HeaderElement[] elements = header.getElements();
73          Assert.assertNotNull(elements);
74          Assert.assertEquals(3, elements.length);
75          Assert.assertEquals("element1", elements[0].getName());
76          Assert.assertEquals("value1", elements[0].getValue());
77          Assert.assertEquals("element2", elements[1].getName());
78          Assert.assertEquals(null, elements[1].getValue());
79          Assert.assertEquals("element3", elements[2].getName());
80          Assert.assertEquals(null, elements[2].getValue());
81          Assert.assertEquals(1, elements[1].getParameters().length);
82      }
83  
84      @Test
85      public void testCloning() throws Exception {
86          final CharArrayBuffer buf = new CharArrayBuffer(32);
87          buf.append("name: value");
88          final BufferedHeader orig = new BufferedHeader(buf);
89          final BufferedHeader clone = (BufferedHeader) orig.clone();
90          Assert.assertEquals(orig.getName(), clone.getName());
91          Assert.assertEquals(orig.getValue(), clone.getValue());
92      }
93  
94      @Test
95      public void testSerialization() throws Exception {
96          final CharArrayBuffer buf = new CharArrayBuffer(32);
97          buf.append("name: value");
98          final BufferedHeader orig = new BufferedHeader(buf);
99          final ByteArrayOutputStream outbuffer = new ByteArrayOutputStream();
100         final ObjectOutputStream outstream = new ObjectOutputStream(outbuffer);
101         outstream.writeObject(orig);
102         outstream.close();
103         final byte[] raw = outbuffer.toByteArray();
104         final ByteArrayInputStream inbuffer = new ByteArrayInputStream(raw);
105         final ObjectInputStream instream = new ObjectInputStream(inbuffer);
106         final BufferedHeader clone = (BufferedHeader) instream.readObject();
107         Assert.assertEquals(orig.getName(), clone.getName());
108         Assert.assertEquals(orig.getValue(), clone.getValue());
109     }
110 
111 }
112