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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
package org.apache.thrift.transport;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import org.apache.thrift.TConfiguration;
import org.junit.jupiter.api.Test;
public class TestTByteBuffer {
@Test
public void testReadWrite() throws Exception {
final TByteBuffer byteBuffer = new TByteBuffer(ByteBuffer.allocate(16));
byteBuffer.write("Hello World".getBytes(StandardCharsets.UTF_8));
assertEquals(
"Hello World", new String(byteBuffer.flip().toByteArray(), StandardCharsets.UTF_8));
}
@Test
public void testReuseReadWrite() throws Exception {
final TByteBuffer byteBuffer = new TByteBuffer(ByteBuffer.allocate(16));
byteBuffer.write("Hello World".getBytes(StandardCharsets.UTF_8));
assertEquals(
"Hello World", new String(byteBuffer.flip().toByteArray(), StandardCharsets.UTF_8));
byteBuffer.clear();
byteBuffer.write("Goodbye Horses".getBytes(StandardCharsets.UTF_8));
assertEquals(
"Goodbye Horses", new String(byteBuffer.flip().toByteArray(), StandardCharsets.UTF_8));
}
@Test
public void testOverflow() throws Exception {
final TByteBuffer byteBuffer = new TByteBuffer(ByteBuffer.allocate(4));
TTransportException e =
assertThrows(
TTransportException.class,
() -> byteBuffer.write("Hello World".getBytes(StandardCharsets.UTF_8)));
assertEquals("Not enough room in output buffer", e.getMessage());
}
@Test
public void testSmallTConfiguration() throws Exception {
// Test that TByteBuffer init fail with small max message size.
final TConfiguration configSmall =
new TConfiguration(
4, TConfiguration.DEFAULT_MAX_FRAME_SIZE, TConfiguration.DEFAULT_RECURSION_DEPTH);
TTransportException e =
assertThrows(
TTransportException.class,
() -> new TByteBuffer(configSmall, ByteBuffer.allocate(100)));
assertEquals("MaxMessageSize reached", e.getMessage());
}
@Test
public void testLargeTConfiguration() throws Exception {
// Test that TByteBuffer init pass with large max message size beyond
// TConfiguration.DEFAULT_MAX_MESSAGE_SIZE.
int maxSize = 101 * 1024 * 1024;
int bufferSize = (100 * 1024 + 512) * 1024;
final TConfiguration configLarge =
new TConfiguration(
maxSize, TConfiguration.DEFAULT_MAX_FRAME_SIZE, TConfiguration.DEFAULT_RECURSION_DEPTH);
assertDoesNotThrow(() -> new TByteBuffer(configLarge, ByteBuffer.allocate(bufferSize)));
}
}
|