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.io.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 testBasics() 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, null);
61  
62          Assert.assertEquals(-1, httpentity.getContentLength());
63          Assert.assertNotNull(httpentity.getContent());
64          Assert.assertTrue(httpentity.isRepeatable());
65          Assert.assertFalse(httpentity.isStreaming());
66      }
67  
68      @Test
69      public void testWriteTo() throws Exception {
70          final Serializable serializableObj = new SerializableObject();
71          final SerializableEntity httpentity = new SerializableEntity(serializableObj, null);
72  
73          final ByteArrayOutputStream out = new ByteArrayOutputStream();
74          httpentity.writeTo(out);
75          final byte[] bytes = out.toByteArray();
76          Assert.assertNotNull(bytes);
77          final ObjectInputStream oin = new ObjectInputStream(new ByteArrayInputStream(
78                  bytes));
79          final SerializableObject serIn = (SerializableObject) oin.readObject();
80          Assert.assertEquals(4, serIn.intValue);
81          Assert.assertEquals("Hello", serIn.stringValue);
82  
83          try {
84              httpentity.writeTo(null);
85              Assert.fail("NullPointerException should have been thrown");
86          } catch (final NullPointerException ex) {
87              // expected
88          }
89      }
90  
91  }