1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
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
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
137 }
138 }
139
140 }