Skip to content

Commit 0169c77

Browse files
committed
added src/test/java/org/msgpack/simple
1 parent ddd1cb1 commit 0169c77

File tree

3 files changed

+152
-3
lines changed

3 files changed

+152
-3
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package org.msgpack.simple;
2+
3+
import org.msgpack.MessagePack;
4+
import org.msgpack.type.Value;
5+
import org.msgpack.type.IntegerValue;
6+
import org.msgpack.type.RawValue;
7+
import org.msgpack.type.ArrayValue;
8+
9+
import java.io.IOException;
10+
11+
import static org.junit.Assert.assertEquals;
12+
import static org.junit.Assert.assertArrayEquals;
13+
import org.junit.Test;
14+
15+
16+
public class TestSimpleDynamicTyping {
17+
@Test
18+
@SuppressWarnings("unused")
19+
public void testTypes() throws IOException {
20+
MessagePack msgpack = new MessagePack();
21+
22+
byte[] raw = msgpack.write(new int[] {1,2,3});
23+
Value v = msgpack.read(raw);
24+
25+
if(v.isArrayValue()) {
26+
// ArrayValue extends List<Value>
27+
ArrayValue array = v.asArrayValue();
28+
int n0 = array.get(0).asIntegerValue().intValue();
29+
assertEquals(1, n0);
30+
int n1 = array.get(1).asIntegerValue().intValue();
31+
assertEquals(2, n1);
32+
int n2 = array.get(2).asIntegerValue().intValue();
33+
assertEquals(3, n2);
34+
35+
} else if(v.isIntegerValue()) {
36+
// IntegerValue extends Number
37+
int num = v.asIntegerValue().intValue();
38+
39+
} else if(v.isRawValue()) {
40+
// getString() or getByteArray()
41+
String str = v.asRawValue().getString();
42+
}
43+
// other types:
44+
// NilValue asNilValue() / isNilValue()
45+
// BooleanValue asBooleanValue() / isBooleanValue()
46+
// IntegerValue asIntegerValue() / isIntegerValue()
47+
// FloatValue asFloatValue() / isFloatValue()
48+
// ArrayValue asArrayValue() / isArrayValue()
49+
// MapValue asMapValue() / isMapValue()
50+
// RawValue asRawValue() / isRawValue
51+
}
52+
53+
@Test
54+
public void testSimpleConvert() throws IOException {
55+
MessagePack msgpack = new MessagePack();
56+
57+
byte[] raw = msgpack.write(new int[] {1,2,3});
58+
Value v = msgpack.read(raw);
59+
60+
// convert from dynamic type (Value) to static type (int[])
61+
int[] array = msgpack.convert(v, new int[3]);
62+
assertArrayEquals(new int[] {1,2,3}, array);
63+
64+
// unconvert from static type (int[]) to dynamic type (Value)
65+
Value v2 = msgpack.unconvert(array);
66+
assertEquals(v, v2);
67+
}
68+
}
69+

src/test/java/org/msgpack/TestSimplePackUnpack.java renamed to src/test/java/org/msgpack/simple/TestSimplePackUnpack.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,33 @@
33
import java.nio.ByteBuffer;
44
import java.io.IOException;
55

6-
import org.junit.Test;
76
import org.msgpack.MessagePack;
87
import org.msgpack.type.Value;
98

9+
import static org.junit.Assert.assertEquals;
10+
import static org.junit.Assert.assertArrayEquals;
11+
import org.junit.Test;
12+
1013

1114
public class TestSimplePackUnpack {
1215
@SuppressWarnings("unused")
1316
@Test
1417
public void testSimplePackUnpack() throws IOException {
1518
MessagePack msgpack = new MessagePack();
19+
20+
// serialize
1621
byte[] raw = msgpack.write(new int[] {1,2,3});
1722

18-
Value v = msgpack.read(raw);
23+
// deserialize to static type
1924
int[] a = msgpack.read(raw, new int[3]);
25+
assertArrayEquals(new int[] {1,2,3}, a);
26+
27+
// deserialize to dynamic type (see TestSimpleDynamicTyping.java)
28+
Value v = msgpack.read(raw);
2029

21-
Value vb = msgpack.read(ByteBuffer.wrap(raw));
30+
// ByteBuffer is also supported
2231
int[] ab = msgpack.read(ByteBuffer.wrap(raw), new int[3]);
32+
assertArrayEquals(new int[] {1,2,3}, ab);
2333
}
2434
}
2535

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package org.msgpack;
2+
3+
import org.msgpack.MessagePack;
4+
import org.msgpack.packer.Packer;
5+
import org.msgpack.unpacker.Unpacker;
6+
import org.msgpack.packer.BufferPacker;
7+
import org.msgpack.unpacker.BufferUnpacker;
8+
9+
import java.io.ByteArrayOutputStream;
10+
import java.io.ByteArrayInputStream;
11+
import java.io.IOException;
12+
13+
import static org.junit.Assert.assertEquals;
14+
import static org.junit.Assert.assertArrayEquals;
15+
import org.junit.Test;
16+
17+
18+
public class TestSimpleStreaming {
19+
@SuppressWarnings("unused")
20+
@Test
21+
public void testSimpleStreamingSerialize() throws IOException {
22+
MessagePack msgpack = new MessagePack();
23+
24+
// streaming serialize
25+
ByteArrayOutputStream out = new ByteArrayOutputStream();
26+
Packer packer = msgpack.createPacker(out);
27+
28+
packer.write(new String[] {"a", "b", "c"});
29+
packer.write(new int[] {1, 2, 3});
30+
packer.write(9.1);
31+
32+
// streaming deserialize
33+
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
34+
Unpacker unpacker = msgpack.createUnpacker(in);
35+
36+
String[] msg1 = unpacker.read(new String[3]);
37+
int[] msg2 = unpacker.read(new int[3]);
38+
double msg3 = unpacker.read(double.class);
39+
40+
assertArrayEquals(new String[] {"a", "b", "c"}, msg1);
41+
assertArrayEquals(new int[] {1, 2, 3}, msg2);
42+
assertEquals(9.1, msg3, 0.001);
43+
}
44+
45+
@SuppressWarnings("unused")
46+
@Test
47+
public void testBufferPackUnpacker() throws IOException {
48+
MessagePack msgpack = new MessagePack();
49+
50+
// streaming serialize into efficient buffer
51+
BufferPacker packer = msgpack.createBufferPacker();
52+
53+
packer.write(new String[] {"a", "b", "c"});
54+
packer.write(new int[] {1, 2, 3});
55+
packer.write(9.1);
56+
57+
// streaming deserialize from the buffer
58+
// BufferUnpacker reduces copies
59+
BufferUnpacker unpacker = msgpack.createBufferUnpacker(packer.toByteArray());
60+
61+
String[] msg1 = unpacker.read(new String[3]);
62+
int[] msg2 = unpacker.read(new int[3]);
63+
double msg3 = unpacker.read(double.class);
64+
65+
assertArrayEquals(new String[] {"a", "b", "c"}, msg1);
66+
assertArrayEquals(new int[] {1, 2, 3}, msg2);
67+
assertEquals(9.1, msg3, 0.01);
68+
}
69+
}
70+

0 commit comments

Comments
 (0)