Skip to content

Commit

Permalink
Handle NPE (#2906)
Browse files Browse the repository at this point in the history
* Handle NPE

* Add Unit Test
  • Loading branch information
thachlp authored Jul 3, 2024
1 parent e5b75da commit e37e3ab
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/main/java/io/lettuce/core/codec/ByteArrayCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,11 @@ public ByteBuffer encodeValue(byte[] value) {
}

private static byte[] getBytes(ByteBuffer buffer) {
if (buffer == null) {
return EMPTY;
}

int remaining = buffer.remaining();

if (remaining == 0) {
return EMPTY;
}
Expand Down
35 changes: 35 additions & 0 deletions src/test/java/io/lettuce/core/codec/ByteArrayCodecUnitTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.lettuce.core.codec;

import org.junit.jupiter.api.Test;

import java.nio.ByteBuffer;

import static org.assertj.core.api.Assertions.assertThat;

class ByteArrayCodecUnitTests {

@Test
void testDecodeValue_withNonEmptyByteBuffer() {
final ByteArrayCodec byteArrayCodec = ByteArrayCodec.INSTANCE;
final byte[] expectedBytes = { 1, 2, 3, 4, 5 };
final ByteBuffer buffer = ByteBuffer.wrap(expectedBytes);
final byte[] result = byteArrayCodec.decodeValue(buffer);
assertThat(result).isEqualTo(expectedBytes);
}

@Test
void testDecodeValue_withEmptyByteBuffer() {
final ByteArrayCodec byteArrayCodec = ByteArrayCodec.INSTANCE;
final ByteBuffer buffer = ByteBuffer.allocate(0);
final byte[] result = byteArrayCodec.decodeValue(buffer);
assertThat(result).isEmpty();
}

@Test
void testDecodeValue_withNullByteBuffer() {
final ByteArrayCodec byteArrayCodec = ByteArrayCodec.INSTANCE;
final byte[] result = byteArrayCodec.decodeValue(null);
assertThat(result).isEmpty();
}

}

0 comments on commit e37e3ab

Please sign in to comment.