From 96ae542588a5b99fe2b600d4f324aa778cefccd3 Mon Sep 17 00:00:00 2001 From: fredoboulo Date: Tue, 5 Mar 2019 22:37:10 +0100 Subject: [PATCH] Problem: Msg.getBytes does not return the bytes of the message. Solution: Fix it and test it. Fixes #682 --- src/main/java/zmq/Msg.java | 4 +- src/test/java/zmq/TestMsg.java | 73 ++++++++++++++++++++++++++++ src/test/java/zmq/TestMsgDirect.java | 11 +++++ 3 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 src/test/java/zmq/TestMsgDirect.java diff --git a/src/main/java/zmq/Msg.java b/src/main/java/zmq/Msg.java index e06b63c94..056c10584 100644 --- a/src/main/java/zmq/Msg.java +++ b/src/main/java/zmq/Msg.java @@ -88,7 +88,7 @@ enum Type // the file descriptor where this message originated, needs to be 64bit due to alignment private SocketChannel fileDesc; - private int size; + private final int size; private byte[] data; private final ByteBuffer buf; // keep track of relative write position @@ -330,7 +330,7 @@ public int getBytes(int index, byte[] dst, int off, int len) if (data == null) { ByteBuffer dup = buf.duplicate(); dup.position(index); - dup.put(dst, off, count); + dup.get(dst, off, count); } else { System.arraycopy(data, index, dst, off, count); diff --git a/src/test/java/zmq/TestMsg.java b/src/test/java/zmq/TestMsg.java index 083efb579..a610536e8 100644 --- a/src/test/java/zmq/TestMsg.java +++ b/src/test/java/zmq/TestMsg.java @@ -1,11 +1,27 @@ package zmq; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + import java.nio.ByteBuffer; +import java.util.function.Function; import org.junit.Test; public class TestMsg { + private final Function allocator; + + public TestMsg() + { + this(ByteBuffer::allocateDirect); + } + + protected TestMsg(Function allocator) + { + this.allocator = allocator; + } + @Test(expected = IllegalArgumentException.class) public void shouldThrowForNullByteBuffer() { @@ -22,4 +38,61 @@ public void shouldWorkForFlippedBuffers() buffer.flip(); new Msg(buffer); } + + @Test + public void testGetBytes() + { + final Msg msg = initMsg(); + + final byte[] dst = new byte[3]; + msg.getBytes(0, dst, 0, 3); + assertThat(dst, is(new byte[] { 0, 1, 2 })); + } + + @Test + public void testGetBytesIndex() + { + final Msg msg = initMsg(); + + final byte[] dst = new byte[4]; + msg.getBytes(1, dst, 0, 4); + assertThat(dst, is(new byte[] { 1, 2, 3, 4 })); + } + + @Test + public void testGetBytesLength() + { + final Msg msg = initMsg(); + + final byte[] dst = new byte[5]; + msg.getBytes(2, dst, 0, 2); + assertThat(dst, is(new byte[] { 2, 3, 0, 0, 0 })); + } + + @Test + public void testGetBytesOffset() + { + final Msg msg = initMsg(); + + final byte[] dst = new byte[6]; + msg.getBytes(3, dst, 1, 2); + assertThat(dst, is(new byte[] { 0, 3, 4, 0, 0, 0 })); + } + + protected Msg initMsg() + { + return initDirectMsg(allocator); + } + + Msg initDirectMsg(Function allocator) + { + int size = 30; + final ByteBuffer buffer = allocator.apply(size); + for (int idx = 0; idx < size; ++idx) { + buffer.put((byte) idx); + } + buffer.position(0); + final Msg msg = new Msg(buffer); + return msg; + } } diff --git a/src/test/java/zmq/TestMsgDirect.java b/src/test/java/zmq/TestMsgDirect.java new file mode 100644 index 000000000..c9d79fcf3 --- /dev/null +++ b/src/test/java/zmq/TestMsgDirect.java @@ -0,0 +1,11 @@ +package zmq; + +import java.nio.ByteBuffer; + +public class TestMsgDirect extends TestMsg +{ + public TestMsgDirect() + { + super(ByteBuffer::allocate); + } +}