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.ByteBuffer;
32 import java.nio.charset.StandardCharsets;
33
34 import org.junit.Assert;
35 import org.junit.Test;
36
37
38
39
40
41 public class TestByteBufferEntity {
42
43 @Test
44 public void testBasics() throws Exception {
45 final ByteBuffer bytes = ByteBuffer.wrap("Message content".getBytes(StandardCharsets.US_ASCII));
46 final ByteBufferEntity httpentity = new ByteBufferEntity(bytes, null);
47
48 Assert.assertEquals(bytes.capacity(), httpentity.getContentLength());
49 Assert.assertNotNull(httpentity.getContent());
50 Assert.assertFalse(httpentity.isRepeatable());
51 Assert.assertFalse(httpentity.isStreaming());
52 }
53
54
55 @Test
56 public void testWriteTo() throws Exception {
57 final ByteBuffer bytes = ByteBuffer.wrap("Message content".getBytes(StandardCharsets.US_ASCII));
58 final ByteBufferEntity httpentity = new ByteBufferEntity(bytes, null);
59
60 ByteArrayOutputStream out = new ByteArrayOutputStream();
61 httpentity.writeTo(out);
62 byte[] bytes2 = out.toByteArray();
63 Assert.assertNotNull(bytes2);
64 Assert.assertEquals(bytes.capacity(), bytes2.length);
65 bytes.position(0);
66 for (int i = 0; i < bytes2.length; i++) {
67 Assert.assertEquals(bytes.get(i), bytes2[i]);
68 }
69
70 out = new ByteArrayOutputStream();
71 httpentity.writeTo(out);
72 bytes2 = out.toByteArray();
73 Assert.assertNotNull(bytes2);
74 Assert.assertEquals(bytes.capacity(), bytes2.length);
75 bytes.position(0);
76 for (int i = 0; i < bytes.capacity(); i++) {
77 Assert.assertEquals(bytes.get(i), bytes2[i]);
78 }
79
80 try {
81 httpentity.writeTo(null);
82 Assert.fail("NullPointerException should have been thrown");
83 } catch (final NullPointerException ex) {
84
85 }
86 }
87 }