mirror of
https://github.com/msgpack/msgpack-python.git
synced 2026-02-08 18:59:59 +00:00
java: move ByteBufferTemplate.java to org.msgpack.util.codegen package
This commit is contained in:
parent
a078d2360c
commit
e3553b87fe
6 changed files with 17 additions and 170 deletions
|
|
@ -2,7 +2,6 @@ package org.msgpack.template;
|
|||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.math.BigInteger;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
|
@ -280,73 +279,6 @@ public class TestPackConvert extends TestCase {
|
|||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testByteBuffer() throws Exception {
|
||||
_testByteBuffer(ByteBuffer.wrap(("".getBytes())));
|
||||
_testByteBuffer(ByteBuffer.wrap(("a".getBytes())));
|
||||
_testByteBuffer(ByteBuffer.wrap(("ab".getBytes())));
|
||||
_testByteBuffer(ByteBuffer.wrap(("abc".getBytes())));
|
||||
|
||||
// small size string
|
||||
for (int i = 0; i < 100; i++) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int len = (int) Math.random() % 31 + 1;
|
||||
for (int j = 0; j < len; j++) {
|
||||
sb.append('a' + ((int) Math.random()) & 26);
|
||||
}
|
||||
_testByteBuffer(ByteBuffer.wrap(sb.toString().getBytes()));
|
||||
}
|
||||
|
||||
// medium size string
|
||||
for (int i = 0; i < 100; i++) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int len = (int) Math.random() % 100 + (1 << 15);
|
||||
for (int j = 0; j < len; j++) {
|
||||
sb.append('a' + ((int) Math.random()) & 26);
|
||||
}
|
||||
_testByteBuffer(ByteBuffer.wrap(sb.toString().getBytes()));
|
||||
}
|
||||
|
||||
// large size string
|
||||
for (int i = 0; i < 10; i++) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int len = (int) Math.random() % 100 + (1 << 31);
|
||||
for (int j = 0; j < len; j++) {
|
||||
sb.append('a' + ((int) Math.random()) & 26);
|
||||
}
|
||||
_testByteBuffer(ByteBuffer.wrap(sb.toString().getBytes()));
|
||||
}
|
||||
}
|
||||
|
||||
static void _testByteBuffer(ByteBuffer src) throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
new Packer(out).pack(src);
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
Template tmpl = ByteBufferTemplate.getInstance();
|
||||
ByteBuffer dst = (ByteBuffer) tmpl.convert(obj);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullByteBuffer() throws Exception {
|
||||
ByteBuffer src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
new Packer(out).pack(src);
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
Template tmpl = ByteBufferTemplate.getInstance();
|
||||
ByteBuffer dst = null;
|
||||
try {
|
||||
dst = (ByteBuffer) tmpl.convert(obj);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
obj = Util.unpackOne(out.toByteArray());
|
||||
tmpl = new OptionalTemplate(ByteBufferTemplate.getInstance());
|
||||
dst = (ByteBuffer) tmpl.convert(obj);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testString() throws Exception {
|
||||
_testString("");
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ package org.msgpack.template;
|
|||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.math.BigInteger;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
|
@ -274,76 +273,6 @@ public class TestPackUnpack extends TestCase {
|
|||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testByteBuffer() throws Exception {
|
||||
_testByteBuffer(ByteBuffer.wrap("".getBytes()));
|
||||
_testByteBuffer(ByteBuffer.wrap("a".getBytes()));
|
||||
_testByteBuffer(ByteBuffer.wrap("ab".getBytes()));
|
||||
_testByteBuffer(ByteBuffer.wrap("abc".getBytes()));
|
||||
|
||||
// small size string
|
||||
for (int i = 0; i < 100; i++) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int len = (int) Math.random() % 31 + 1;
|
||||
for (int j = 0; j < len; j++) {
|
||||
sb.append('a' + ((int) Math.random()) & 26);
|
||||
}
|
||||
_testByteBuffer(ByteBuffer.wrap(sb.toString().getBytes()));
|
||||
}
|
||||
|
||||
// medium size string
|
||||
for (int i = 0; i < 100; i++) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int len = (int) Math.random() % 100 + (1 << 15);
|
||||
for (int j = 0; j < len; j++) {
|
||||
sb.append('a' + ((int) Math.random()) & 26);
|
||||
}
|
||||
_testByteBuffer(ByteBuffer.wrap(sb.toString().getBytes()));
|
||||
}
|
||||
|
||||
// large size string
|
||||
for (int i = 0; i < 10; i++) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int len = (int) Math.random() % 100 + (1 << 31);
|
||||
for (int j = 0; j < len; j++) {
|
||||
sb.append('a' + ((int) Math.random()) & 26);
|
||||
}
|
||||
_testByteBuffer(ByteBuffer.wrap(sb.toString().getBytes()));
|
||||
}
|
||||
}
|
||||
|
||||
static void _testByteBuffer(ByteBuffer src) throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
new Packer(out).pack(src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = ByteBufferTemplate.getInstance();
|
||||
ByteBuffer dst = (ByteBuffer) tmpl.unpack(new Unpacker(in));
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullByteBuffer() throws Exception {
|
||||
ByteBuffer src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
new Packer(out).pack(src);
|
||||
byte[] bytes = out.toByteArray();
|
||||
Template tmpl = null;
|
||||
Unpacker unpacker = new Unpacker();
|
||||
ByteBuffer dst = null;
|
||||
try {
|
||||
tmpl = ByteBufferTemplate.getInstance();
|
||||
unpacker.wrap(bytes);
|
||||
dst = (ByteBuffer) tmpl.unpack(unpacker);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
unpacker.wrap(bytes);
|
||||
tmpl = new OptionalTemplate(ByteBufferTemplate.getInstance());
|
||||
dst = (ByteBuffer) tmpl.unpack(unpacker);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testString() throws Exception {
|
||||
_testString("");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue