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.hc.core5.http.io.entity;
29
30 import java.io.ByteArrayOutputStream;
31 import java.nio.charset.StandardCharsets;
32
33 import org.junit.Assert;
34 import org.junit.Test;
35
36
37
38
39
40 public class TestByteArrayEntity {
41
42 @Test
43 public void testBasics() throws Exception {
44 final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
45 final ByteArrayEntity entity = new ByteArrayEntity(bytes, null);
46
47 Assert.assertEquals(bytes.length, entity.getContentLength());
48 Assert.assertNotNull(entity.getContent());
49 Assert.assertTrue(entity.isRepeatable());
50 Assert.assertFalse(entity.isStreaming());
51 }
52
53 @Test
54 public void testBasicOffLen() throws Exception {
55 final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
56 final ByteArrayEntity entity = new ByteArrayEntity(bytes, 8, 7, null);
57
58 Assert.assertEquals(7, entity.getContentLength());
59 Assert.assertNotNull(entity.getContent());
60 Assert.assertTrue(entity.isRepeatable());
61 Assert.assertFalse(entity.isStreaming());
62 }
63
64 @Test(expected=NullPointerException.class)
65 public void testIllegalConstructorNullByteArray() throws Exception {
66 new ByteArrayEntity(null, null);
67 }
68
69 @Test(expected=IndexOutOfBoundsException.class)
70 public void testIllegalConstructorBadLen() throws Exception {
71 final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
72 new ByteArrayEntity(bytes, 0, bytes.length + 1, null);
73 }
74
75 @Test(expected=IndexOutOfBoundsException.class)
76 public void testIllegalConstructorBadOff1() throws Exception {
77 final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
78 new ByteArrayEntity(bytes, -1, bytes.length, null);
79 }
80
81 @Test(expected=IndexOutOfBoundsException.class)
82 public void testIllegalConstructorBadOff2() throws Exception {
83 final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
84 new ByteArrayEntity(bytes, bytes.length + 1, bytes.length, null);
85 }
86
87 @Test
88 public void testWriteTo() throws Exception {
89 final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
90 final ByteArrayEntity entity = new ByteArrayEntity(bytes, null);
91
92 ByteArrayOutputStream out = new ByteArrayOutputStream();
93 entity.writeTo(out);
94 byte[] bytes2 = out.toByteArray();
95 Assert.assertNotNull(bytes2);
96 Assert.assertEquals(bytes.length, bytes2.length);
97 for (int i = 0; i < bytes.length; i++) {
98 Assert.assertEquals(bytes[i], bytes2[i]);
99 }
100
101 out = new ByteArrayOutputStream();
102 entity.writeTo(out);
103 bytes2 = out.toByteArray();
104 Assert.assertNotNull(bytes2);
105 Assert.assertEquals(bytes.length, bytes2.length);
106 for (int i = 0; i < bytes.length; i++) {
107 Assert.assertEquals(bytes[i], bytes2[i]);
108 }
109
110 try {
111 entity.writeTo(null);
112 Assert.fail("NullPointerException should have been thrown");
113 } catch (final NullPointerException ex) {
114
115 }
116 }
117
118 @Test
119 public void testWriteToOffLen() throws Exception {
120 final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
121 final int off = 8;
122 final int len = 7;
123 final ByteArrayEntity entity = new ByteArrayEntity(bytes, off, len, null);
124
125 ByteArrayOutputStream out = new ByteArrayOutputStream();
126 entity.writeTo(out);
127 byte[] bytes2 = out.toByteArray();
128 Assert.assertNotNull(bytes2);
129 Assert.assertEquals(len, bytes2.length);
130 for (int i = 0; i < len; i++) {
131 Assert.assertEquals(bytes[i+off], bytes2[i]);
132 }
133
134 out = new ByteArrayOutputStream();
135 entity.writeTo(out);
136 bytes2 = out.toByteArray();
137 Assert.assertNotNull(bytes2);
138 Assert.assertEquals(len, bytes2.length);
139 for (int i = 0; i < len; i++) {
140 Assert.assertEquals(bytes[i+off], bytes2[i]);
141 }
142
143 try {
144 entity.writeTo(null);
145 Assert.fail("NullPointerException should have been thrown");
146 } catch (final NullPointerException ex) {
147
148 }
149 }
150
151 }