Skip to content

Commit 4c3d460

Browse files
committed
Fix code style and test cases
1 parent 9467f61 commit 4c3d460

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

msgpack-core/src/main/java/org/msgpack/core/buffer/SequenceMessageBufferInput.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private void nextInput() throws IOException
6363
if (sequence.hasMoreElements()) {
6464
input = sequence.nextElement();
6565
if (input == null) {
66-
throw new NullPointerException();
66+
throw new NullPointerException("An element in the MessageBufferInput sequence is null");
6767
}
6868
}
6969
else {

msgpack-core/src/test/scala/org/msgpack/core/MessageUnpackerTest.scala

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -207,21 +207,21 @@ class MessageUnpackerTest extends MessagePackSpec {
207207
builder.result()
208208
}
209209

210-
def sequenceUnpackers(data: Array[Byte], size: Int) : Seq[MessageUnpacker] = {
210+
def unpackerCollectionWithVariousBuffers(data: Array[Byte], chunkSize: Int) : Seq[MessageUnpacker] = {
211211
val seqBytes = Seq.newBuilder[MessageBufferInput]
212212
val seqByteBuffers = Seq.newBuilder[MessageBufferInput]
213213
val seqDirectBuffers = Seq.newBuilder[MessageBufferInput]
214214
var left = data.length
215215
var position = 0
216216
while (left > 0) {
217-
val length = Math.min(size, left)
218-
seqBytes += new ArrayBufferInput(data, position, length);
217+
val length = Math.min(chunkSize, left)
218+
seqBytes += new ArrayBufferInput(data, position, length)
219219
val bb = ByteBuffer.allocate(length)
220220
val db = ByteBuffer.allocateDirect(length)
221221
bb.put(data, position, length).flip()
222222
db.put(data, position, length).flip()
223-
seqByteBuffers += new ByteBufferInput(bb);
224-
seqDirectBuffers += new ByteBufferInput(db);
223+
seqByteBuffers += new ByteBufferInput(bb)
224+
seqDirectBuffers += new ByteBufferInput(db)
225225
left -= length
226226
position += length
227227
}
@@ -360,25 +360,47 @@ class MessageUnpackerTest extends MessagePackSpec {
360360
new SplitTest {val data = testData3(30)}.run
361361
}
362362

363-
"read data at buffer boundary" taggedAs("boundary2") in {
363+
"read integer at MessageBuffer boundaries" taggedAs("integer-buffer-boundary") in {
364364
val packer = MessagePack.newDefaultBufferPacker()
365365
(0 until 1170).foreach{i =>
366366
packer.packLong(0x0011223344556677L)
367-
packer.packString("hello world")
368367
}
369368
packer.close
370369
val data = packer.toByteArray
371370

372-
var unpacker = MessagePack.newDefaultUnpacker(new InputStreamBufferInput(new ByteArrayInputStream(data), 8192))
373-
(0 until 1170).foreach { i =>
374-
unpacker.unpackLong() shouldBe 0x0011223344556677L
375-
unpacker.unpackString() shouldBe "hello world"
371+
// Boundary test
372+
withResource(MessagePack.newDefaultUnpacker(new InputStreamBufferInput(new ByteArrayInputStream(data), 8192))) { unpacker =>
373+
(0 until 1170).foreach { i =>
374+
unpacker.unpackLong() shouldBe 0x0011223344556677L
375+
}
376376
}
377-
unpacker.close()
378377

379-
for (unpacker <- sequenceUnpackers(data, 32)) {
378+
// Boundary test for sequences of ByteBuffer, DirectByteBuffer backed MessageInput.
379+
for (unpacker <- unpackerCollectionWithVariousBuffers(data, 32)) {
380380
(0 until 1170).foreach { i =>
381381
unpacker.unpackLong() shouldBe 0x0011223344556677L
382+
}
383+
}
384+
}
385+
386+
"read string at MessageBuffer boundaries" taggedAs("string-buffer-boundary") in {
387+
val packer = MessagePack.newDefaultBufferPacker()
388+
(0 until 1170).foreach{i =>
389+
packer.packString("hello world")
390+
}
391+
packer.close
392+
val data = packer.toByteArray
393+
394+
// Boundary test
395+
withResource(MessagePack.newDefaultUnpacker(new InputStreamBufferInput(new ByteArrayInputStream(data), 8192))) { unpacker =>
396+
(0 until 1170).foreach { i =>
397+
unpacker.unpackString() shouldBe "hello world"
398+
}
399+
}
400+
401+
// Boundary test for sequences of ByteBuffer, DirectByteBuffer backed MessageInput.
402+
for (unpacker <- unpackerCollectionWithVariousBuffers(data, 32)) {
403+
(0 until 1170).foreach { i =>
382404
unpacker.unpackString() shouldBe "hello world"
383405
}
384406
}

0 commit comments

Comments
 (0)