2021-04-14 11:44:58 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2021 The Netty Project
|
|
|
|
*
|
|
|
|
* The Netty Project licenses this file to you under the Apache License,
|
|
|
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
package io.netty.buffer.api;
|
|
|
|
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import org.junit.jupiter.params.ParameterizedTest;
|
|
|
|
import org.junit.jupiter.params.provider.MethodSource;
|
|
|
|
|
|
|
|
import java.nio.ByteOrder;
|
|
|
|
|
|
|
|
import static java.nio.ByteOrder.BIG_ENDIAN;
|
|
|
|
import static java.nio.ByteOrder.LITTLE_ENDIAN;
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
|
|
|
|
|
public class BufferCompositionTest extends BufferTestSupport {
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void compositeBufferCanOnlyBeOwnedWhenAllConstituentBuffersAreOwned() {
|
|
|
|
try (BufferAllocator allocator = BufferAllocator.heap()) {
|
|
|
|
Buffer composite;
|
|
|
|
try (Buffer a = allocator.allocate(8)) {
|
|
|
|
assertTrue(a.isOwned());
|
|
|
|
Buffer leakB;
|
|
|
|
try (Buffer b = allocator.allocate(8)) {
|
|
|
|
assertTrue(a.isOwned());
|
|
|
|
assertTrue(b.isOwned());
|
2021-04-27 12:10:44 +02:00
|
|
|
composite = CompositeBuffer.compose(allocator, a, b);
|
2021-04-14 11:44:58 +02:00
|
|
|
assertFalse(composite.isOwned());
|
|
|
|
assertFalse(a.isOwned());
|
|
|
|
assertFalse(b.isOwned());
|
|
|
|
leakB = b;
|
|
|
|
}
|
|
|
|
assertFalse(composite.isOwned());
|
|
|
|
assertFalse(a.isOwned());
|
|
|
|
assertTrue(leakB.isOwned());
|
|
|
|
}
|
|
|
|
assertTrue(composite.isOwned());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void compositeBuffersCannotHaveDuplicateComponents() {
|
|
|
|
try (BufferAllocator allocator = BufferAllocator.heap();
|
|
|
|
Buffer a = allocator.allocate(4)) {
|
2021-04-27 12:10:44 +02:00
|
|
|
var e = assertThrows(IllegalArgumentException.class, () -> CompositeBuffer.compose(allocator, a, a));
|
2021-04-14 11:44:58 +02:00
|
|
|
assertThat(e).hasMessageContaining("duplicate");
|
|
|
|
|
2021-04-27 12:10:44 +02:00
|
|
|
try (Buffer composite = CompositeBuffer.compose(allocator, a)) {
|
2021-04-14 11:44:58 +02:00
|
|
|
a.close();
|
|
|
|
try {
|
2021-04-27 12:10:44 +02:00
|
|
|
e = assertThrows(IllegalArgumentException.class,
|
2021-04-27 12:19:53 +02:00
|
|
|
() -> ((CompositeBuffer) composite).extendWith(a));
|
2021-04-14 11:44:58 +02:00
|
|
|
assertThat(e).hasMessageContaining("duplicate");
|
|
|
|
} finally {
|
|
|
|
a.acquire();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void compositeBufferFromSends() {
|
|
|
|
try (BufferAllocator allocator = BufferAllocator.heap();
|
2021-04-27 12:10:44 +02:00
|
|
|
Buffer composite = CompositeBuffer.compose(allocator,
|
2021-04-14 11:44:58 +02:00
|
|
|
allocator.allocate(8).send(),
|
|
|
|
allocator.allocate(8).send(),
|
|
|
|
allocator.allocate(8).send())) {
|
|
|
|
assertEquals(24, composite.capacity());
|
|
|
|
assertTrue(composite.isOwned());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void compositeBufferMustNotBeAllowedToContainThemselves() {
|
|
|
|
try (BufferAllocator allocator = BufferAllocator.heap()) {
|
|
|
|
Buffer a = allocator.allocate(4);
|
2021-04-27 12:10:44 +02:00
|
|
|
Buffer buf = CompositeBuffer.compose(allocator, a);
|
2021-04-14 11:44:58 +02:00
|
|
|
try (buf; a) {
|
|
|
|
a.close();
|
|
|
|
try {
|
2021-04-27 12:19:53 +02:00
|
|
|
assertThrows(IllegalArgumentException.class, () -> ((CompositeBuffer) buf).extendWith(buf));
|
2021-04-14 11:44:58 +02:00
|
|
|
assertTrue(buf.isOwned());
|
2021-04-27 12:10:44 +02:00
|
|
|
try (Buffer composite = CompositeBuffer.compose(allocator, buf)) {
|
2021-04-14 11:44:58 +02:00
|
|
|
// the composing increments the reference count of constituent buffers...
|
|
|
|
// counter-act this so it can be extended:
|
|
|
|
a.close(); // buf is now owned so it can be extended.
|
|
|
|
try {
|
2021-04-27 12:19:53 +02:00
|
|
|
assertThrows(ClassCastException.class,
|
|
|
|
() -> ((CompositeBuffer) buf).extendWith(composite));
|
2021-04-14 11:44:58 +02:00
|
|
|
} finally {
|
|
|
|
a.acquire(); // restore the reference count to align with our try-with-resources structure.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assertTrue(buf.isOwned());
|
|
|
|
} finally {
|
|
|
|
a.acquire();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
public void ensureWritableOnCompositeBuffersMustRespectExistingBigEndianByteOrder(Fixture fixture) {
|
|
|
|
try (BufferAllocator allocator = fixture.createAllocator()) {
|
|
|
|
Buffer composite;
|
|
|
|
try (Buffer a = allocator.allocate(4, BIG_ENDIAN)) {
|
2021-04-27 12:10:44 +02:00
|
|
|
composite = CompositeBuffer.compose(allocator, a);
|
2021-04-14 11:44:58 +02:00
|
|
|
}
|
|
|
|
try (composite) {
|
|
|
|
composite.writeInt(0x01020304);
|
|
|
|
composite.ensureWritable(4);
|
|
|
|
composite.writeInt(0x05060708);
|
|
|
|
assertEquals(0x0102030405060708L, composite.readLong());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
public void ensureWritableOnCompositeBuffersMustRespectExistingLittleEndianByteOrder(Fixture fixture) {
|
|
|
|
try (BufferAllocator allocator = fixture.createAllocator()) {
|
|
|
|
Buffer composite;
|
|
|
|
try (Buffer a = allocator.allocate(4, LITTLE_ENDIAN)) {
|
2021-04-27 12:10:44 +02:00
|
|
|
composite = CompositeBuffer.compose(allocator, a);
|
2021-04-14 11:44:58 +02:00
|
|
|
}
|
|
|
|
try (composite) {
|
|
|
|
composite.writeInt(0x05060708);
|
|
|
|
composite.ensureWritable(4);
|
|
|
|
composite.writeInt(0x01020304);
|
|
|
|
assertEquals(0x0102030405060708L, composite.readLong());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void emptyCompositeBufferMustUseNativeByteOrder() {
|
|
|
|
try (BufferAllocator allocator = BufferAllocator.heap();
|
2021-04-27 12:10:44 +02:00
|
|
|
Buffer composite = CompositeBuffer.compose(allocator)) {
|
2021-04-14 11:44:58 +02:00
|
|
|
assertThat(composite.order()).isEqualTo(ByteOrder.nativeOrder());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void extendOnNonCompositeBufferMustThrow() {
|
|
|
|
try (BufferAllocator allocator = BufferAllocator.heap();
|
|
|
|
Buffer a = allocator.allocate(8);
|
|
|
|
Buffer b = allocator.allocate(8)) {
|
2021-04-27 12:19:53 +02:00
|
|
|
var exc = assertThrows(IllegalArgumentException.class, () -> ((CompositeBuffer) a).extendWith(b));
|
2021-04-14 11:44:58 +02:00
|
|
|
assertThat(exc).hasMessageContaining("Expected").hasMessageContaining("composite");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void extendingNonOwnedCompositeBufferMustThrow() {
|
|
|
|
try (BufferAllocator allocator = BufferAllocator.heap();
|
|
|
|
Buffer a = allocator.allocate(8);
|
|
|
|
Buffer b = allocator.allocate(8);
|
2021-04-27 12:10:44 +02:00
|
|
|
Buffer composed = CompositeBuffer.compose(allocator, a)) {
|
2021-04-14 11:44:58 +02:00
|
|
|
try (Buffer ignore = composed.acquire()) {
|
2021-04-27 12:19:53 +02:00
|
|
|
var exc = assertThrows(IllegalStateException.class, () -> ((CompositeBuffer) composed).extendWith(b));
|
2021-04-14 11:44:58 +02:00
|
|
|
assertThat(exc).hasMessageContaining("owned");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void extendingCompositeBufferWithItselfMustThrow() {
|
|
|
|
try (BufferAllocator allocator = BufferAllocator.heap()) {
|
|
|
|
Buffer composite;
|
|
|
|
try (Buffer a = allocator.allocate(8)) {
|
2021-04-27 12:10:44 +02:00
|
|
|
composite = CompositeBuffer.compose(allocator, a);
|
2021-04-14 11:44:58 +02:00
|
|
|
}
|
|
|
|
try (composite) {
|
|
|
|
var exc = assertThrows(IllegalArgumentException.class,
|
2021-04-27 12:19:53 +02:00
|
|
|
() -> ((CompositeBuffer) composite).extendWith(composite));
|
2021-04-14 11:44:58 +02:00
|
|
|
assertThat(exc).hasMessageContaining("cannot be extended");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void extendingWithZeroCapacityBufferHasNoEffect() {
|
|
|
|
try (BufferAllocator allocator = BufferAllocator.heap();
|
2021-04-27 12:10:44 +02:00
|
|
|
Buffer composite = CompositeBuffer.compose(allocator)) {
|
2021-04-27 12:19:53 +02:00
|
|
|
((CompositeBuffer) composite).extendWith(composite);
|
2021-04-14 11:44:58 +02:00
|
|
|
assertThat(composite.capacity()).isZero();
|
|
|
|
assertThat(composite.countComponents()).isZero();
|
|
|
|
}
|
|
|
|
try (BufferAllocator allocator = BufferAllocator.heap()) {
|
|
|
|
Buffer a = allocator.allocate(1);
|
2021-04-27 12:10:44 +02:00
|
|
|
Buffer composite = CompositeBuffer.compose(allocator, a);
|
2021-04-14 11:44:58 +02:00
|
|
|
a.close();
|
|
|
|
assertTrue(composite.isOwned());
|
|
|
|
assertThat(composite.capacity()).isOne();
|
|
|
|
assertThat(composite.countComponents()).isOne();
|
2021-04-27 12:10:44 +02:00
|
|
|
try (Buffer b = CompositeBuffer.compose(allocator)) {
|
2021-04-27 12:19:53 +02:00
|
|
|
((CompositeBuffer) composite).extendWith(b);
|
2021-04-14 11:44:58 +02:00
|
|
|
}
|
|
|
|
assertTrue(composite.isOwned());
|
|
|
|
assertThat(composite.capacity()).isOne();
|
|
|
|
assertThat(composite.countComponents()).isOne();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void extendingCompositeBufferWithNullMustThrow() {
|
|
|
|
try (BufferAllocator allocator = BufferAllocator.heap();
|
2021-04-27 12:10:44 +02:00
|
|
|
Buffer composite = CompositeBuffer.compose(allocator)) {
|
2021-04-27 12:19:53 +02:00
|
|
|
assertThrows(NullPointerException.class, () -> ((CompositeBuffer) composite).extendWith(null));
|
2021-04-14 11:44:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void extendingCompositeBufferMustIncreaseCapacityByGivenBigEndianBuffer() {
|
|
|
|
try (BufferAllocator allocator = BufferAllocator.heap();
|
2021-04-27 12:10:44 +02:00
|
|
|
Buffer composite = CompositeBuffer.compose(allocator)) {
|
2021-04-14 11:44:58 +02:00
|
|
|
assertThat(composite.capacity()).isZero();
|
|
|
|
try (Buffer buf = allocator.allocate(8, BIG_ENDIAN)) {
|
2021-04-27 12:19:53 +02:00
|
|
|
((CompositeBuffer) composite).extendWith(buf);
|
2021-04-14 11:44:58 +02:00
|
|
|
}
|
|
|
|
assertThat(composite.capacity()).isEqualTo(8);
|
|
|
|
composite.writeLong(0x0102030405060708L);
|
|
|
|
assertThat(composite.readLong()).isEqualTo(0x0102030405060708L);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void extendingCompositeBufferMustIncreaseCapacityByGivenLittleEndianBuffer() {
|
|
|
|
try (BufferAllocator allocator = BufferAllocator.heap();
|
2021-04-27 12:10:44 +02:00
|
|
|
Buffer composite = CompositeBuffer.compose(allocator)) {
|
2021-04-14 11:44:58 +02:00
|
|
|
assertThat(composite.capacity()).isZero();
|
|
|
|
try (Buffer buf = allocator.allocate(8, LITTLE_ENDIAN)) {
|
2021-04-27 12:19:53 +02:00
|
|
|
((CompositeBuffer) composite).extendWith(buf);
|
2021-04-14 11:44:58 +02:00
|
|
|
}
|
|
|
|
assertThat(composite.capacity()).isEqualTo(8);
|
|
|
|
composite.writeLong(0x0102030405060708L);
|
|
|
|
assertThat(composite.readLong()).isEqualTo(0x0102030405060708L);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void extendingBigEndianCompositeBufferMustThrowIfExtensionIsLittleEndian() {
|
|
|
|
try (BufferAllocator allocator = BufferAllocator.heap()) {
|
|
|
|
Buffer composite;
|
|
|
|
try (Buffer a = allocator.allocate(8, BIG_ENDIAN)) {
|
2021-04-27 12:10:44 +02:00
|
|
|
composite = CompositeBuffer.compose(allocator, a);
|
2021-04-14 11:44:58 +02:00
|
|
|
}
|
|
|
|
try (composite) {
|
|
|
|
try (Buffer b = allocator.allocate(8, LITTLE_ENDIAN)) {
|
2021-04-27 12:10:44 +02:00
|
|
|
var exc = assertThrows(IllegalArgumentException.class,
|
2021-04-27 12:19:53 +02:00
|
|
|
() -> ((CompositeBuffer) composite).extendWith(b));
|
2021-04-14 11:44:58 +02:00
|
|
|
assertThat(exc).hasMessageContaining("byte order");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void extendingLittleEndianCompositeBufferMustThrowIfExtensionIsBigEndian() {
|
|
|
|
try (BufferAllocator allocator = BufferAllocator.heap()) {
|
|
|
|
Buffer composite;
|
|
|
|
try (Buffer a = allocator.allocate(8, LITTLE_ENDIAN)) {
|
2021-04-27 12:10:44 +02:00
|
|
|
composite = CompositeBuffer.compose(allocator, a);
|
2021-04-14 11:44:58 +02:00
|
|
|
}
|
|
|
|
try (composite) {
|
|
|
|
try (Buffer b = allocator.allocate(8, BIG_ENDIAN)) {
|
2021-04-27 12:10:44 +02:00
|
|
|
var exc = assertThrows(IllegalArgumentException.class,
|
2021-04-27 12:19:53 +02:00
|
|
|
() -> ((CompositeBuffer) composite).extendWith(b));
|
2021-04-14 11:44:58 +02:00
|
|
|
assertThat(exc).hasMessageContaining("byte order");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void emptyCompositeBufferMustAllowExtendingWithBufferWithBigEndianByteOrder() {
|
|
|
|
try (BufferAllocator allocator = BufferAllocator.heap()) {
|
2021-04-27 12:10:44 +02:00
|
|
|
try (Buffer composite = CompositeBuffer.compose(allocator)) {
|
2021-04-14 11:44:58 +02:00
|
|
|
try (Buffer b = allocator.allocate(8, BIG_ENDIAN)) {
|
2021-04-27 12:19:53 +02:00
|
|
|
((CompositeBuffer) composite).extendWith(b);
|
2021-04-14 11:44:58 +02:00
|
|
|
assertThat(composite.order()).isEqualTo(BIG_ENDIAN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void emptyCompositeBufferMustAllowExtendingWithBufferWithLittleEndianByteOrder() {
|
|
|
|
try (BufferAllocator allocator = BufferAllocator.heap()) {
|
2021-04-27 12:10:44 +02:00
|
|
|
try (Buffer composite = CompositeBuffer.compose(allocator)) {
|
2021-04-14 11:44:58 +02:00
|
|
|
try (Buffer b = allocator.allocate(8, LITTLE_ENDIAN)) {
|
2021-04-27 12:19:53 +02:00
|
|
|
((CompositeBuffer) composite).extendWith(b);
|
2021-04-14 11:44:58 +02:00
|
|
|
assertThat(composite.order()).isEqualTo(LITTLE_ENDIAN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void emptyCompositeBufferMustAllowExtendingWithReadOnlyBuffer() {
|
|
|
|
try (BufferAllocator allocator = BufferAllocator.heap()) {
|
2021-04-27 12:10:44 +02:00
|
|
|
try (Buffer composite = CompositeBuffer.compose(allocator)) {
|
2021-04-14 11:44:58 +02:00
|
|
|
try (Buffer b = allocator.allocate(8).readOnly(true)) {
|
2021-04-27 12:19:53 +02:00
|
|
|
((CompositeBuffer) composite).extendWith(b);
|
2021-04-14 11:44:58 +02:00
|
|
|
assertTrue(composite.readOnly());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void whenExtendingCompositeBufferWithWriteOffsetAtCapacityExtensionWriteOffsetCanBeNonZero() {
|
|
|
|
try (BufferAllocator allocator = BufferAllocator.heap()) {
|
|
|
|
Buffer composite;
|
|
|
|
try (Buffer a = allocator.allocate(8)) {
|
2021-04-27 12:10:44 +02:00
|
|
|
composite = CompositeBuffer.compose(allocator, a);
|
2021-04-14 11:44:58 +02:00
|
|
|
}
|
|
|
|
try (composite) {
|
|
|
|
composite.writeLong(0);
|
|
|
|
try (Buffer b = allocator.allocate(8)) {
|
|
|
|
b.writeInt(1);
|
2021-04-27 12:19:53 +02:00
|
|
|
((CompositeBuffer) composite).extendWith(b);
|
2021-04-14 11:44:58 +02:00
|
|
|
assertThat(composite.capacity()).isEqualTo(16);
|
|
|
|
assertThat(composite.writerOffset()).isEqualTo(12);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void whenExtendingCompositeBufferWithWriteOffsetLessThanCapacityExtensionWriteOffsetMustZero() {
|
|
|
|
try (BufferAllocator allocator = BufferAllocator.heap()) {
|
|
|
|
Buffer composite;
|
|
|
|
try (Buffer a = allocator.allocate(8)) {
|
2021-04-27 12:10:44 +02:00
|
|
|
composite = CompositeBuffer.compose(allocator, a);
|
2021-04-14 11:44:58 +02:00
|
|
|
}
|
|
|
|
try (composite) {
|
|
|
|
composite.writeInt(0);
|
|
|
|
try (Buffer b = allocator.allocate(8)) {
|
|
|
|
b.writeInt(1);
|
2021-04-27 12:10:44 +02:00
|
|
|
var exc = assertThrows(IllegalArgumentException.class,
|
2021-04-27 12:19:53 +02:00
|
|
|
() -> ((CompositeBuffer) composite).extendWith(b));
|
2021-04-14 11:44:58 +02:00
|
|
|
assertThat(exc).hasMessageContaining("unwritten gap");
|
|
|
|
b.writerOffset(0);
|
2021-04-27 12:19:53 +02:00
|
|
|
((CompositeBuffer) composite).extendWith(b);
|
2021-04-14 11:44:58 +02:00
|
|
|
assertThat(composite.capacity()).isEqualTo(16);
|
|
|
|
assertThat(composite.writerOffset()).isEqualTo(4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void whenExtendingCompositeBufferWithReadOffsetAtCapacityExtensionReadOffsetCanBeNonZero() {
|
|
|
|
try (BufferAllocator allocator = BufferAllocator.heap()) {
|
|
|
|
Buffer composite;
|
|
|
|
try (Buffer a = allocator.allocate(8)) {
|
2021-04-27 12:10:44 +02:00
|
|
|
composite = CompositeBuffer.compose(allocator, a);
|
2021-04-14 11:44:58 +02:00
|
|
|
}
|
|
|
|
try (composite) {
|
|
|
|
composite.writeLong(0);
|
|
|
|
composite.readLong();
|
|
|
|
try (Buffer b = allocator.allocate(8)) {
|
|
|
|
b.writeInt(1);
|
|
|
|
b.readInt();
|
2021-04-27 12:19:53 +02:00
|
|
|
((CompositeBuffer) composite).extendWith(b);
|
2021-04-14 11:44:58 +02:00
|
|
|
assertThat(composite.capacity()).isEqualTo(16);
|
|
|
|
assertThat(composite.writerOffset()).isEqualTo(12);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void whenExtendingCompositeBufferWithReadOffsetLessThanCapacityExtensionReadOffsetMustZero() {
|
|
|
|
try (BufferAllocator allocator = BufferAllocator.heap()) {
|
|
|
|
Buffer composite;
|
|
|
|
try (Buffer a = allocator.allocate(8)) {
|
2021-04-27 12:10:44 +02:00
|
|
|
composite = CompositeBuffer.compose(allocator, a);
|
2021-04-14 11:44:58 +02:00
|
|
|
}
|
|
|
|
try (composite) {
|
|
|
|
composite.writeLong(0);
|
|
|
|
composite.readInt();
|
|
|
|
try (Buffer b = allocator.allocate(8)) {
|
|
|
|
b.writeInt(1);
|
|
|
|
b.readInt();
|
2021-04-27 12:10:44 +02:00
|
|
|
var exc = assertThrows(IllegalArgumentException.class,
|
2021-04-27 12:19:53 +02:00
|
|
|
() -> ((CompositeBuffer) composite).extendWith(b));
|
2021-04-14 11:44:58 +02:00
|
|
|
assertThat(exc).hasMessageContaining("unread gap");
|
|
|
|
b.readerOffset(0);
|
2021-04-27 12:19:53 +02:00
|
|
|
((CompositeBuffer) composite).extendWith(b);
|
2021-04-14 11:44:58 +02:00
|
|
|
assertThat(composite.capacity()).isEqualTo(16);
|
|
|
|
assertThat(composite.writerOffset()).isEqualTo(12);
|
|
|
|
assertThat(composite.readerOffset()).isEqualTo(4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void composeMustThrowWhenBuffersHaveMismatchedByteOrder() {
|
|
|
|
try (BufferAllocator allocator = BufferAllocator.heap();
|
|
|
|
Buffer a = allocator.allocate(4, BIG_ENDIAN);
|
|
|
|
Buffer b = allocator.allocate(4, LITTLE_ENDIAN)) {
|
2021-04-27 12:10:44 +02:00
|
|
|
assertThrows(IllegalArgumentException.class, () -> CompositeBuffer.compose(allocator, a, b));
|
2021-04-14 11:44:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void composingReadOnlyBuffersMustCreateReadOnlyCompositeBuffer() {
|
|
|
|
try (BufferAllocator allocator = BufferAllocator.heap();
|
|
|
|
Buffer a = allocator.allocate(4).readOnly(true);
|
|
|
|
Buffer b = allocator.allocate(4).readOnly(true);
|
2021-04-27 12:10:44 +02:00
|
|
|
Buffer composite = CompositeBuffer.compose(allocator, a, b)) {
|
2021-04-14 11:44:58 +02:00
|
|
|
assertTrue(composite.readOnly());
|
|
|
|
verifyWriteInaccessible(composite);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void composingReadOnlyAndWritableBuffersMustThrow() {
|
|
|
|
try (BufferAllocator allocator = BufferAllocator.heap();
|
|
|
|
Buffer a = allocator.allocate(8).readOnly(true);
|
|
|
|
Buffer b = allocator.allocate(8)) {
|
2021-04-27 12:10:44 +02:00
|
|
|
assertThrows(IllegalArgumentException.class, () -> CompositeBuffer.compose(allocator, a, b));
|
|
|
|
assertThrows(IllegalArgumentException.class, () -> CompositeBuffer.compose(allocator, b, a));
|
|
|
|
assertThrows(IllegalArgumentException.class, () -> CompositeBuffer.compose(allocator, a, b, a));
|
|
|
|
assertThrows(IllegalArgumentException.class, () -> CompositeBuffer.compose(allocator, b, a, b));
|
2021-04-14 11:44:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void compositeWritableBufferCannotBeExtendedWithReadOnlyBuffer() {
|
|
|
|
try (BufferAllocator allocator = BufferAllocator.heap()) {
|
|
|
|
Buffer composite;
|
|
|
|
try (Buffer a = allocator.allocate(8)) {
|
2021-04-27 12:10:44 +02:00
|
|
|
composite = CompositeBuffer.compose(allocator, a);
|
2021-04-14 11:44:58 +02:00
|
|
|
}
|
|
|
|
try (composite; Buffer b = allocator.allocate(8).readOnly(true)) {
|
2021-04-27 12:19:53 +02:00
|
|
|
assertThrows(IllegalArgumentException.class, () -> ((CompositeBuffer) composite).extendWith(b));
|
2021-04-14 11:44:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void compositeReadOnlyBufferCannotBeExtendedWithWritableBuffer() {
|
|
|
|
try (BufferAllocator allocator = BufferAllocator.heap()) {
|
|
|
|
Buffer composite;
|
|
|
|
try (Buffer a = allocator.allocate(8).readOnly(true)) {
|
2021-04-27 12:10:44 +02:00
|
|
|
composite = CompositeBuffer.compose(allocator, a);
|
2021-04-14 11:44:58 +02:00
|
|
|
}
|
|
|
|
try (composite; Buffer b = allocator.allocate(8)) {
|
2021-04-27 12:19:53 +02:00
|
|
|
assertThrows(IllegalArgumentException.class, () -> ((CompositeBuffer) composite).extendWith(b));
|
2021-04-14 11:44:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|