Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,6 @@ local.properties
### Queue files
*.cq4t
*.cq4
.migrate/
migrate.sh
/.claude/
14 changes: 7 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>

Expand All @@ -88,12 +88,6 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>net.openhft</groupId>
<artifactId>affinity</artifactId>
Expand Down Expand Up @@ -121,6 +115,12 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<repositories>
Expand Down
41 changes: 22 additions & 19 deletions src/test/java/net/openhft/chronicle/bytes/AbstractBytesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@

import net.openhft.chronicle.core.io.ClosedIllegalStateException;
import net.openhft.chronicle.core.io.ThreadingIllegalStateException;
import org.junit.Test;
import org.junit.Before;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockitoAnnotations;

import java.nio.BufferOverflowException;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import static org.junit.Assert.*;

@SuppressWarnings("unchecked")
public class AbstractBytesTest {
Expand Down Expand Up @@ -42,7 +42,7 @@ public BytesStore<Bytes<ByteBuffer>, ByteBuffer> copy() throws IllegalStateExcep
}
}

@Before
@BeforeEach
public void setUp() {
MockitoAnnotations.openMocks(this); // Initialize mocks annotated with @Mock
mockBytesStore = mock(BytesStore.class);
Expand All @@ -63,7 +63,7 @@ public void isDirectMemory_ReturnsExpectedValue() {
when(mockBytesStore.isDirectMemory()).thenReturn(true);

ConcreteBytes bytes = new ConcreteBytes(mockBytesStore, 0, 100);
assertTrue("Expected isDirectMemory to return true", bytes.isDirectMemory());
assertTrue(bytes.isDirectMemory(), "Expected isDirectMemory to return true");
}

@Test
Expand All @@ -74,7 +74,7 @@ public void canReadDirect_WithSufficientRemaining_ReturnsTrue() throws Exception
ConcreteBytes bytes = new ConcreteBytes(mockBytesStore, 0, 100);
bytes.writePosition(50); // Simulate that we have written some data

assertTrue("Expected canReadDirect to return true for length <= remaining", bytes.canReadDirect(10));
assertTrue(bytes.canReadDirect(10), "Expected canReadDirect to return true for length <= remaining");
}

@Test
Expand All @@ -85,7 +85,7 @@ public void canReadDirect_WithInsufficientRemaining_ReturnsFalse() throws Except
ConcreteBytes bytes = new ConcreteBytes(mockBytesStore, 0, 100);
bytes.writePosition(50); // Simulate that we have written some data

assertFalse("Expected canReadDirect to return false for length > remaining", bytes.canReadDirect(51));
assertFalse(bytes.canReadDirect(51), "Expected canReadDirect to return false for length > remaining");
}

@Test
Expand All @@ -96,9 +96,9 @@ public void clear_ResetsPositionsAndLimits() throws Exception {
ConcreteBytes bytes = new ConcreteBytes(mockBytesStore, 10, 90);
bytes.clear();

assertEquals("Expected readPosition to reset", 0, bytes.readPosition());
assertEquals("Expected writePosition to reset", 0, bytes.writePosition());
assertEquals("Expected writeLimit to match capacity", 100, bytes.writeLimit());
assertEquals(0, bytes.readPosition(), "Expected readPosition to reset");
assertEquals(0, bytes.writePosition(), "Expected writePosition to reset");
assertEquals(100, bytes.writeLimit(), "Expected writeLimit to match capacity");
}

@Test
Expand All @@ -109,9 +109,9 @@ public void clearAndPad_SetsPositionsAndLimitsCorrectly() throws Exception {
ConcreteBytes bytes = new ConcreteBytes(mockBytesStore, 0, 100);
bytes.clearAndPad(20);

assertEquals("Expected readPosition to be set correctly after padding", 20, bytes.readPosition());
assertEquals("Expected writePosition to be set correctly after padding", 20, bytes.writePosition());
assertEquals("Expected writeLimit to match capacity", 100, bytes.writeLimit());
assertEquals(20, bytes.readPosition(), "Expected readPosition to be set correctly after padding");
assertEquals(20, bytes.writePosition(), "Expected writePosition to be set correctly after padding");
assertEquals(100, bytes.writeLimit(), "Expected writeLimit to match capacity");
}

@Test
Expand Down Expand Up @@ -143,11 +143,13 @@ public void performRelease_CallsReleaseOnBytesStore() {
verify(mockBytesStore).release(bytes);
}

@Test(expected = BufferUnderflowException.class)
@Test
public void readLong_WithInsufficientDataThrowsException() {
doThrow(new BufferUnderflowException()).when(mockBytesStore).readLong(anyLong());
bytes.lenient(false);
bytes.readLong();
assertThrows(BufferUnderflowException.class, () -> {
doThrow(new BufferUnderflowException()).when(mockBytesStore).readLong(anyLong());
bytes.lenient(false);
bytes.readLong();
});
}

@Test
Expand All @@ -158,9 +160,10 @@ public void write8bit_WithNonNullBytesStoreWritesData() {
verify(mockBytesStore).write8bit(anyLong(), eq(mockToWrite));
}

@Test(expected = BufferOverflowException.class)
@Test
public void prewriteCheckOffset_WithInvalidOffsetThrowsException() {
bytes.prewriteCheckOffset(150, 10);
assertThrows(BufferOverflowException.class, () ->
bytes.prewriteCheckOffset(150, 10));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
package net.openhft.chronicle.bytes;

import org.jetbrains.annotations.NotNull;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.nio.ByteBuffer;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* User: peter.lawrey Date: 24/12/13 Time: 19:43
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
*/
package net.openhft.chronicle.bytes;

import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.nio.BufferOverflowException;
import java.nio.BufferUnderflowException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.*;

public class AppendableUtilTest extends BytesTestCommon {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
*/
package net.openhft.chronicle.bytes;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class ByteCheckSumTest extends BytesTestCommon {
@Test
Expand Down
76 changes: 40 additions & 36 deletions src/test/java/net/openhft/chronicle/bytes/ByteStoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
import net.openhft.chronicle.core.Jvm;
import net.openhft.chronicle.core.io.IORuntimeException;
import org.jetbrains.annotations.NotNull;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -26,22 +27,23 @@

import static java.nio.charset.StandardCharsets.ISO_8859_1;
import static net.openhft.chronicle.core.io.ReferenceOwner.INIT;
import static org.junit.Assert.*;
import static org.junit.Assume.assumeFalse;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assumptions.assumeFalse;

public class ByteStoreTest extends BytesTestCommon {

private static final int SIZE = 128;
private Bytes<?> bytes;
private BytesStore<?, ?> bytesStore;

@AfterEach
@Override
public void afterChecks() {
bytes.releaseLast();
super.afterChecks();
}

@Before
@BeforeEach
public void beforeTest() {
bytesStore = BytesStore.wrap(ByteBuffer.allocate(SIZE).order(ByteOrder.nativeOrder()));
bytes = bytesStore.bytesForWrite();
Expand All @@ -65,7 +67,7 @@

@Test
public void testCAS() {
assumeFalse("TODO FIX", Jvm.isArm());
assumeFalse(Jvm.isArm(), "TODO FIX");
final BytesStore<?, ?> bytes = BytesStore.wrap(ByteBuffer.allocate(100));
bytes.compareAndSwapLong(0, 0L, 1L);
assertEquals(1L, bytes.readLong(0));
Expand All @@ -92,25 +94,25 @@
@NotNull byte[] bytes = new byte[(int) this.bytes.capacity()];
this.bytes.read(bytes);
for (int i = 0; i < this.bytes.capacity(); i++)
Assert.assertEquals((byte) i, bytes[i]);
Assertions.assertEquals((byte) i, bytes[i]);
}

@Test
public void testCompareAndSetInt() {
Assert.assertTrue(bytes.compareAndSwapInt(0, 0, 1));
Assert.assertFalse(bytes.compareAndSwapInt(0, 0, 1));
Assert.assertTrue(bytes.compareAndSwapInt(8, 0, 1));
Assert.assertTrue(bytes.compareAndSwapInt(0, 1, 2));
Assertions.assertTrue(bytes.compareAndSwapInt(0, 0, 1));
Assertions.assertFalse(bytes.compareAndSwapInt(0, 0, 1));
Assertions.assertTrue(bytes.compareAndSwapInt(8, 0, 1));
Assertions.assertTrue(bytes.compareAndSwapInt(0, 1, 2));
}

@Test
public void testCompareAndSetLong() {
assumeFalse("TODO FIX", Jvm.isArm());
assumeFalse(Jvm.isArm(), "TODO FIX");

Assert.assertTrue(bytes.compareAndSwapLong(0L, 0L, 1L));
Assert.assertFalse(bytes.compareAndSwapLong(0L, 0L, 1L));
Assert.assertTrue(bytes.compareAndSwapLong(8L, 0L, 1L));
Assert.assertTrue(bytes.compareAndSwapLong(0L, 1L, 2L));
Assertions.assertTrue(bytes.compareAndSwapLong(0L, 0L, 1L));
Assertions.assertFalse(bytes.compareAndSwapLong(0L, 0L, 1L));
Assertions.assertTrue(bytes.compareAndSwapLong(8L, 0L, 1L));
Assertions.assertTrue(bytes.compareAndSwapLong(0L, 1L, 2L));
}

@Test
Expand Down Expand Up @@ -183,13 +185,13 @@

bytes.readPosition(0);
final StringBuilder sb = new StringBuilder();
Assert.assertFalse(bytes.readUtf8(sb));
Assertions.assertFalse(bytes.readUtf8(sb));
for (String word : words) {
Assert.assertTrue(bytes.readUtf8(sb));
Assert.assertEquals(word, sb.toString());
Assertions.assertTrue(bytes.readUtf8(sb));
Assertions.assertEquals(word, sb.toString());
}
assertTrue(bytes.readUtf8(sb));
Assert.assertEquals("", sb.toString());
Assertions.assertEquals("", sb.toString());
}

@Test
Expand Down Expand Up @@ -218,9 +220,9 @@
final ByteBuffer bb2 = ByteBuffer.wrap(bytes2);
this.bytes.read(bb2);

Assert.assertEquals(bytes.length, bb2.position());
Assertions.assertEquals(bytes.length, bb2.position());
final byte[] bytes2b = Arrays.copyOf(bytes2, bytes.length);
Assert.assertArrayEquals(bytes, bytes2b);
Assertions.assertArrayEquals(bytes, bytes2b);
}

@Test
Expand Down Expand Up @@ -357,7 +359,7 @@

@Test
public void testReadWriteThreadSafeLong() {
assumeFalse("TODO FIX", Jvm.isArm());
assumeFalse(Jvm.isArm(), "TODO FIX");
for (long i = 0; i < 32; i += 8)
bytes.writeOrderedLong(i, i);
bytes.writePosition(32);
Expand Down Expand Up @@ -428,8 +430,8 @@
final byte[] bytes = new byte[12];
for (int i = 0; i < 12; i++)
bytes[i] = (byte) in.read();
Assert.assertEquals(-1, in.read());
Assert.assertEquals("Hello world\n", new String(bytes));
Assertions.assertEquals(-1, in.read());
Assertions.assertEquals("Hello world\n", new String(bytes));
}
} finally {
bytes2.releaseLast();
Expand Down Expand Up @@ -484,7 +486,7 @@

@Test
public void testAddAndGetLongNative() {
assumeFalse("TODO FIX", Jvm.isArm());
assumeFalse(Jvm.isArm(), "TODO FIX");
final BytesStore<?, ?> bytesStore2 = BytesStore.nativeStore(128);
try {
checkAddAndGetLong();
Expand All @@ -495,7 +497,7 @@

@Test
public void testAddAndGetLong() {
assumeFalse("TODO FIX", Jvm.isArm());
assumeFalse(Jvm.isArm(), "TODO FIX");
final BytesStore<?, ?> bytesStore2 = BytesStore.wrap(new byte[128]);
try {
checkAddAndGetLong();
Expand Down Expand Up @@ -640,7 +642,7 @@
bytesStoreCopy.writePosition(destOffset);
try {
long bytesCopied = bytesStoreOriginal.copyTo(bytesStoreCopy);
assertEquals("Unexpected number of bytes copied", SIZE - destOffset, bytesCopied);
assertEquals(SIZE - destOffset, bytesCopied, "Unexpected number of bytes copied");
for (int i = 0; i < bytesCopied; i++)
assertEquals(bytesStoreOriginal.readByte(i), bytesStoreCopy.readByte(i + destOffset));
} finally {
Expand All @@ -656,14 +658,16 @@
assertEquals(0, BytesStore.empty().realCapacity());
}

@Test(expected = DecoratedBufferOverflowException.class)
@Test
public void testClearAndPadTooMuch() {
final Bytes<?> b = bytesStore.bytesForWrite();
try {
b.clearAndPad(SIZE + 1);
} finally {
b.releaseLast();
}
assertThrows(DecoratedBufferOverflowException.class, () -> {

Check warning on line 663 in src/test/java/net/openhft/chronicle/bytes/ByteStoreTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor the code of the lambda to have only one invocation possibly throwing a runtime exception.

See more on https://sonarcloud.io/project/issues?id=OpenHFT_Chronicle-Bytes&issues=AZ2Q1WlpB3HP6MsbZHAL&open=AZ2Q1WlpB3HP6MsbZHAL&pullRequest=721
final Bytes<?> b = bytesStore.bytesForWrite();
try {
b.clearAndPad(SIZE + 1);
} finally {
b.releaseLast();
}
});
}

@Test
Expand Down
Loading