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