From 54f88101ae91b2bcc800a376c8382af4ec6f4bfe Mon Sep 17 00:00:00 2001 From: vasiliy-mikhailov Date: Sat, 18 Jul 2026 04:32:51 +0300 Subject: [PATCH] [#720] Add unit tests for ProtobufUtil --- .../protostream/ProtobufUtilTest.java | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/core/src/test/java/org/infinispan/protostream/ProtobufUtilTest.java b/core/src/test/java/org/infinispan/protostream/ProtobufUtilTest.java index fb02bd62e..314263577 100644 --- a/core/src/test/java/org/infinispan/protostream/ProtobufUtilTest.java +++ b/core/src/test/java/org/infinispan/protostream/ProtobufUtilTest.java @@ -1127,4 +1127,54 @@ public void testStructuredObject() throws IOException { String converted = ProtobufUtil.toCanonicalJSON(ctx, protobuf); assertValid(converted); } + + // ---- Additional mutation-coverage tests ---- + + @Test + public void testWriteToNullObject() throws Exception { + ImmutableSerializationContext ctx = createContext(); + assertThrows(NullPointerException.class, () -> ProtobufUtil.toByteArray(ctx, null)); + } + + @Test + public void testToByteBuffer() throws Exception { + ImmutableSerializationContext ctx = createContext(); + Account account = createAccount(); + + java.nio.ByteBuffer buffer = ProtobufUtil.toByteBuffer(ctx, account); + byte[] bytes = new byte[buffer.remaining()]; + buffer.get(bytes); + + Account result = ProtobufUtil.fromByteArray(ctx, bytes, Account.class); + assertEquals(account, result); + } + + @Test + public void testFromByteBuffer() throws Exception { + ImmutableSerializationContext ctx = createContext(); + Account account = createAccount(); + + byte[] bytes = ProtobufUtil.toByteArray(ctx, account); + java.nio.ByteBuffer buffer = java.nio.ByteBuffer.wrap(bytes); + + Account result = ProtobufUtil.fromByteBuffer(ctx, buffer, Account.class); + assertEquals(account, result); + } + + @Test + public void testFromEnumClass() throws Exception { + ImmutableSerializationContext ctx = createContext(); + byte[] bytes = ProtobufUtil.toByteArray(ctx, createAccount()); + assertThrows(IllegalArgumentException.class, () -> ProtobufUtil.fromByteArray(ctx, bytes, Account.Currency.class)); + } + + @Test + public void testEstimateSize() throws Exception { + ImmutableSerializationContext ctx = createContext(); + Account account = createAccount(); + + int estimated = ProtobufUtil.estimateSize(ctx, account); + int actual = ProtobufUtil.toByteArray(ctx, account).length; + assertTrue(estimated >= actual, "Estimated size " + estimated + " should be >= actual " + actual); + } }