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