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.entity;
29  
30  import java.io.ByteArrayInputStream;
31  import java.io.ByteArrayOutputStream;
32  import java.io.ObjectInputStream;
33  import java.io.ObjectOutputStream;
34  import java.io.Serializable;
35  
36  import org.junit.Assert;
37  import org.junit.Test;
38  
39  public class TestSerializableEntity {
40  
41      public static class SerializableObject implements Serializable {
42  
43          private static final long serialVersionUID = 1833335861188359573L;
44  
45          public int intValue = 4;
46  
47          public String stringValue = "Hello";
48  
49          public SerializableObject() {}
50      }
51  
52      @Test
53      public void testBasicsBuff() throws Exception {
54          final ByteArrayOutputStream baos = new ByteArrayOutputStream();
55          final ObjectOutputStream out = new ObjectOutputStream(baos);
56  
57          final Serializable serializableObj = new SerializableObject();
58          out.writeObject(serializableObj);
59  
60          final SerializableEntity httpentity = new SerializableEntity(serializableObj, true);
61  
62          Assert.assertEquals(baos.toByteArray().length, httpentity.getContentLength());
63          Assert.assertNotNull(httpentity.getContent());
64          Assert.assertTrue(httpentity.isRepeatable());
65          Assert.assertFalse(httpentity.isStreaming());
66      }
67  
68      @Test
69      public void testBasicsDirect() throws Exception {
70          final ByteArrayOutputStream baos = new ByteArrayOutputStream();
71          final ObjectOutputStream out = new ObjectOutputStream(baos);
72  
73          final Serializable serializableObj = new SerializableObject();
74          out.writeObject(serializableObj);
75  
76          final SerializableEntity httpentity = new SerializableEntity(serializableObj, false);
77  
78          Assert.assertEquals(-1, httpentity.getContentLength());
79          Assert.assertNotNull(httpentity.getContent());
80          Assert.assertTrue(httpentity.isRepeatable());
81          Assert.assertFalse(httpentity.isStreaming());
82      }
83  
84      @Test
85      public void testIllegalConstructor() throws Exception {
86          try {
87              new SerializableEntity(null, false);
88              Assert.fail("IllegalArgumentException should have been thrown");
89          } catch (final IllegalArgumentException ex) {
90              // expected
91          }
92      }
93  
94      @Test
95      public void testWriteToBuff() throws Exception {
96          final Serializable serializableObj = new SerializableObject();
97          final SerializableEntity httpentity = new SerializableEntity(serializableObj, true);
98  
99          final ByteArrayOutputStream out = new ByteArrayOutputStream();
100         httpentity.writeTo(out);
101         final byte[] bytes = out.toByteArray();
102         Assert.assertNotNull(bytes);
103         final ObjectInputStream oin = new ObjectInputStream(new ByteArrayInputStream(
104                 bytes));
105         final SerializableObject serIn = (SerializableObject) oin.readObject();
106         Assert.assertEquals(4, serIn.intValue);
107         Assert.assertEquals("Hello", serIn.stringValue);
108 
109         try {
110             httpentity.writeTo(null);
111             Assert.fail("IllegalArgumentException should have been thrown");
112         } catch (final IllegalArgumentException ex) {
113             // expected
114         }
115     }
116 
117     @Test
118     public void testWriteToDirect() throws Exception {
119         final Serializable serializableObj = new SerializableObject();
120         final SerializableEntity httpentity = new SerializableEntity(serializableObj, false);
121 
122         final ByteArrayOutputStream out = new ByteArrayOutputStream();
123         httpentity.writeTo(out);
124         final byte[] bytes = out.toByteArray();
125         Assert.assertNotNull(bytes);
126         final ObjectInputStream oin = new ObjectInputStream(new ByteArrayInputStream(
127                 bytes));
128         final SerializableObject serIn = (SerializableObject) oin.readObject();
129         Assert.assertEquals(4, serIn.intValue);
130         Assert.assertEquals("Hello", serIn.stringValue);
131 
132         try {
133             httpentity.writeTo(null);
134             Assert.fail("IllegalArgumentException should have been thrown");
135         } catch (final IllegalArgumentException ex) {
136             // expected
137         }
138     }
139 
140 }