mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-03-21 06:11:18 +01:00
java: merge CustomMessage.java and DynamicCodeGenBase.java that can be not merged automatically
This commit is contained in:
commit
cdfac703ef
@ -24,10 +24,18 @@ public class CustomMessage {
|
||||
CustomPacker.register(target, packer);
|
||||
}
|
||||
|
||||
public static void registerTemplate(Class<?> target, Template tmpl) {
|
||||
public static void registerConverter(Class<?> target, MessageConverter converter) {
|
||||
CustomConverter.register(target, converter);
|
||||
}
|
||||
|
||||
public static void registerUnpacker(Class<?> target, MessageUnpacker unpacker) {
|
||||
CustomUnpacker.register(target, unpacker);
|
||||
}
|
||||
|
||||
public static void register(Class<?> target, Template tmpl) {
|
||||
CustomPacker.register(target, tmpl);
|
||||
CustomUnpacker.register(target, tmpl);
|
||||
CustomConverter.register(target, tmpl);
|
||||
CustomUnpacker.register(target, tmpl);
|
||||
}
|
||||
|
||||
public static boolean isAnnotated(Class<?> target, Class<? extends Annotation> with) {
|
||||
|
156
java/src/main/java/org/msgpack/MessagePack.java
Normal file
156
java/src/main/java/org/msgpack/MessagePack.java
Normal file
@ -0,0 +1,156 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack;
|
||||
|
||||
import java.io.OutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
//import org.msgpack.util.codegen.DynamicTemplate; // FIXME
|
||||
import org.msgpack.util.codegen.DynamicPacker;
|
||||
import org.msgpack.util.codegen.DynamicConverter;
|
||||
import org.msgpack.util.codegen.DynamicUnpacker;
|
||||
|
||||
public class MessagePack {
|
||||
public static byte[] pack(Object obj) {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
try {
|
||||
new Packer(out).pack(obj);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
public static void pack(OutputStream out, Object obj) throws IOException {
|
||||
new Packer(out).pack(obj);
|
||||
}
|
||||
|
||||
public static byte[] pack(Object obj, Template tmpl) throws MessageTypeException {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
try {
|
||||
new Packer(out).pack(obj, tmpl);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
public static void pack(OutputStream out, Object obj, Template tmpl) throws IOException {
|
||||
new Packer(out).pack(obj, tmpl);
|
||||
}
|
||||
|
||||
|
||||
public static MessagePackObject unpack(byte[] buffer) throws IOException {
|
||||
Unpacker pac = new Unpacker();
|
||||
pac.wrap(buffer);
|
||||
try {
|
||||
return pac.unpackObject();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static Object unpack(byte[] buffer, Template tmpl) throws MessageTypeException {
|
||||
Unpacker pac = new Unpacker();
|
||||
pac.wrap(buffer);
|
||||
try {
|
||||
return pac.unpack(tmpl);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T unpack(byte[] buffer, Class<T> klass) throws MessageTypeException {
|
||||
Unpacker pac = new Unpacker();
|
||||
pac.wrap(buffer);
|
||||
try {
|
||||
return pac.unpack(klass);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static MessagePackObject unpack(InputStream in) {
|
||||
Unpacker pac = new Unpacker(in);
|
||||
try {
|
||||
return pac.unpackObject();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static Object unpack(InputStream in, Template tmpl) throws IOException, MessageTypeException {
|
||||
Unpacker pac = new Unpacker(in);
|
||||
try {
|
||||
return pac.unpack(tmpl);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T unpack(InputStream in, Class<T> klass) throws IOException, MessageTypeException {
|
||||
Unpacker pac = new Unpacker(in);
|
||||
try {
|
||||
return pac.unpack(klass);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void register(Class<?> target) { // auto-detect
|
||||
// FIXME
|
||||
//Template tmpl;
|
||||
//if(List.isAssignableFrom(target)) {
|
||||
//} else if(Set.isAssignableFrom(target)) {
|
||||
//} else if(Map.isAssignableFrom(target)) {
|
||||
//} else if(Collection.isAssignableFrom(target)) {
|
||||
//} else if(BigInteger.isAssignableFrom(target)) {
|
||||
//} else {
|
||||
//}
|
||||
|
||||
// FIXME
|
||||
//Template tmpl = DynamicTemplate.create(target);
|
||||
//register(target, tmpl);
|
||||
|
||||
// FIXME
|
||||
CustomPacker.register(target, DynamicPacker.create(target));
|
||||
CustomConverter.register(target, DynamicConverter.create(target));
|
||||
CustomUnpacker.register(target, DynamicUnpacker.create(target));
|
||||
}
|
||||
|
||||
public static void register(Class<?> target, Template tmpl) {
|
||||
CustomPacker.register(target, tmpl);
|
||||
CustomConverter.register(target, tmpl);
|
||||
CustomUnpacker.register(target, tmpl);
|
||||
}
|
||||
|
||||
public static void registerPacker(Class<?> target, MessagePacker packer) {
|
||||
CustomPacker.register(target, packer);
|
||||
}
|
||||
|
||||
public static void registerConverter(Class<?> target, MessageConverter converter) {
|
||||
CustomConverter.register(target, converter);
|
||||
}
|
||||
|
||||
public static void registerUnpacker(Class<?> target, MessageUnpacker unpacker) {
|
||||
CustomUnpacker.register(target, unpacker);
|
||||
}
|
||||
}
|
||||
|
@ -580,8 +580,9 @@ public class Unpacker implements Iterable<MessagePackObject> {
|
||||
return tmpl.unpack(this);
|
||||
}
|
||||
|
||||
final public Object unpack(Class<?> klass) throws IOException, MessageTypeException, InstantiationException, IllegalAccessException {
|
||||
return unpack(Templates.tClass(klass));
|
||||
final public <T> T unpack(Class<T> klass) throws IOException, MessageTypeException {
|
||||
// FIXME optional?
|
||||
return (T)unpack(Templates.tOptional(Templates.tClass(klass)));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -46,7 +46,7 @@ public class AnyTemplate implements Template {
|
||||
static final AnyTemplate instance = new AnyTemplate();
|
||||
|
||||
static {
|
||||
CustomMessage.registerTemplate(MessagePackObject.class, instance);
|
||||
CustomMessage.register(MessagePackObject.class, instance);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -43,7 +43,7 @@ public class BigIntegerTemplate implements Template {
|
||||
static final BigIntegerTemplate instance = new BigIntegerTemplate();
|
||||
|
||||
static {
|
||||
CustomMessage.registerTemplate(BigInteger.class, instance);
|
||||
CustomMessage.register(BigInteger.class, instance);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -42,7 +42,7 @@ public class BooleanTemplate implements Template {
|
||||
static final BooleanTemplate instance = new BooleanTemplate();
|
||||
|
||||
static {
|
||||
CustomMessage.registerTemplate(Boolean.class, instance);
|
||||
CustomMessage.register(Boolean.class, instance);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -42,7 +42,7 @@ public class ByteArrayTemplate implements Template {
|
||||
static final ByteArrayTemplate instance = new ByteArrayTemplate();
|
||||
|
||||
static {
|
||||
CustomMessage.registerTemplate(byte[].class, instance);
|
||||
CustomMessage.register(byte[].class, instance);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -42,7 +42,7 @@ public class ByteTemplate implements Template {
|
||||
static final ByteTemplate instance = new ByteTemplate();
|
||||
|
||||
static {
|
||||
CustomMessage.registerTemplate(Byte.class, instance);
|
||||
CustomMessage.register(Byte.class, instance);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -31,10 +31,11 @@ public class CollectionTemplate implements Template {
|
||||
}
|
||||
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
if(target instanceof Collection) {
|
||||
if(!(target instanceof Collection)) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
Collection<Object> collection = (Collection<Object>)target;
|
||||
pk.packArray(collection.size());
|
||||
for(Object element : collection) {
|
||||
elementTemplate.pack(pk, element);
|
||||
}
|
||||
|
@ -42,7 +42,7 @@ public class DoubleTemplate implements Template {
|
||||
static final DoubleTemplate instance = new DoubleTemplate();
|
||||
|
||||
static {
|
||||
CustomMessage.registerTemplate(Double.class, instance);
|
||||
CustomMessage.register(Double.class, instance);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -42,7 +42,7 @@ public class FloatTemplate implements Template {
|
||||
static final FloatTemplate instance = new FloatTemplate();
|
||||
|
||||
static {
|
||||
CustomMessage.registerTemplate(Float.class, instance);
|
||||
CustomMessage.register(Float.class, instance);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -42,7 +42,7 @@ public class IntegerTemplate implements Template {
|
||||
static final IntegerTemplate instance = new IntegerTemplate();
|
||||
|
||||
static {
|
||||
CustomMessage.registerTemplate(Integer.class, instance);
|
||||
CustomMessage.register(Integer.class, instance);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -42,7 +42,7 @@ public class LongTemplate implements Template {
|
||||
static final LongTemplate instance = new LongTemplate();
|
||||
|
||||
static {
|
||||
CustomMessage.registerTemplate(Long.class, instance);
|
||||
CustomMessage.register(Long.class, instance);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -42,7 +42,7 @@ public class ShortTemplate implements Template {
|
||||
static final ShortTemplate instance = new ShortTemplate();
|
||||
|
||||
static {
|
||||
CustomMessage.registerTemplate(Short.class, instance);
|
||||
CustomMessage.register(Short.class, instance);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -42,7 +42,7 @@ public class StringTemplate implements Template {
|
||||
static final StringTemplate instance = new StringTemplate();
|
||||
|
||||
static {
|
||||
CustomMessage.registerTemplate(String.class, instance);
|
||||
CustomMessage.register(String.class, instance);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -322,7 +322,7 @@ public class DynamicCodeGenBase implements Constants {
|
||||
} else if (CustomMessage.isAnnotated(c, MessagePackMessage.class)) {
|
||||
// @MessagePackMessage
|
||||
Template tmpl = DynamicTemplate.create(c);
|
||||
CustomMessage.registerTemplate(c, tmpl);
|
||||
CustomMessage.register(c, tmpl);
|
||||
return tmpl;
|
||||
} else if (CustomMessage.isAnnotated(c, MessagePackDelegate.class)) {
|
||||
// FIXME DelegatePacker
|
||||
@ -330,20 +330,19 @@ public class DynamicCodeGenBase implements Constants {
|
||||
"not supported yet. : " + c.getName());
|
||||
LOG.error(e.getMessage(), e);
|
||||
throw e;
|
||||
} else if (CustomMessage.isAnnotated(c,
|
||||
MessagePackOrdinalEnum.class)) {
|
||||
} else if (CustomMessage.isAnnotated(c, MessagePackOrdinalEnum.class)) {
|
||||
// @MessagePackOrdinalEnum
|
||||
Template tmpl = DynamicOrdinalEnumTemplate.create(c);
|
||||
CustomMessage.registerTemplate(c, tmpl);
|
||||
CustomMessage.register(c, tmpl);
|
||||
return tmpl;
|
||||
} else if (MessageConvertable.class.isAssignableFrom(c)
|
||||
} else if (MessagePackable.class.isAssignableFrom(c)
|
||||
|| MessageConvertable.class.isAssignableFrom(c)
|
||||
|| MessageUnpackable.class.isAssignableFrom(c)) {
|
||||
Template tmpl = new MessagePackUnpackConvertableTemplate(c);
|
||||
CustomMessage.registerTemplate(c, tmpl);
|
||||
CustomMessage.register(c, tmpl);
|
||||
return tmpl;
|
||||
} else {
|
||||
throw new MessageTypeException("Type error: "
|
||||
+ ((Class<?>) t).getName());
|
||||
throw new MessageTypeException("Type error: " + ((Class<?>) t).getName());
|
||||
}
|
||||
} else if (t instanceof GenericArrayType) {
|
||||
GenericArrayType gat = (GenericArrayType) t;
|
||||
|
297
java/src/test/java/org/msgpack/TestMessagePackStaticMethods.java
Normal file
297
java/src/test/java/org/msgpack/TestMessagePackStaticMethods.java
Normal file
@ -0,0 +1,297 @@
|
||||
package org.msgpack;
|
||||
|
||||
import org.msgpack.*;
|
||||
import org.msgpack.object.*;
|
||||
import org.msgpack.annotation.*;
|
||||
import static org.msgpack.Templates.*;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
import java.math.BigInteger;
|
||||
|
||||
import org.junit.Test;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class TestMessagePackStaticMethods extends TestCase {
|
||||
public static class ProvidedClass {
|
||||
public boolean bool;
|
||||
public String str;
|
||||
public List<Integer> list;
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == this) {
|
||||
return true;
|
||||
}
|
||||
if (!(obj instanceof ProvidedClass)) {
|
||||
return false;
|
||||
}
|
||||
ProvidedClass o = (ProvidedClass)obj;
|
||||
return bool == o.bool && str.equals(o.str) && list.equals(o.list);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "ProvidedClass<bool:"+bool+" str:"+str+" list:"+list+">";
|
||||
}
|
||||
}
|
||||
|
||||
@MessagePackMessage
|
||||
public static class UserDefinedClass {
|
||||
public boolean bool;
|
||||
public String str;
|
||||
public List<Integer> list;
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == this) {
|
||||
return true;
|
||||
}
|
||||
if (!(obj instanceof UserDefinedClass)) {
|
||||
return false;
|
||||
}
|
||||
UserDefinedClass o = (UserDefinedClass)obj;
|
||||
return bool == o.bool && str.equals(o.str) && list.equals(o.list);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "UserDefinedClass<bool:"+bool+" str:"+str+" list:"+list+">";
|
||||
}
|
||||
}
|
||||
|
||||
static {
|
||||
// provided classes need registration
|
||||
MessagePack.register(ProvidedClass.class);
|
||||
// annotated classes don't need registration
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testCheckedPackToByteArray() throws Exception {
|
||||
byte[] a = MessagePack.pack("msgpack", TString);
|
||||
byte[] b = MessagePack.pack((Object)1, TInteger);
|
||||
byte[] c = MessagePack.pack((Object)null, TAny);
|
||||
byte[] d = MessagePack.pack(createStringList(), tList(TString));
|
||||
byte[] e = MessagePack.pack(createProvidedClass(), tClass(ProvidedClass.class));
|
||||
byte[] f = MessagePack.pack(createUserDefinedClass(), tClass(UserDefinedClass.class));
|
||||
|
||||
{
|
||||
Object aobj = MessagePack.unpack(a, TString);
|
||||
Object bobj = MessagePack.unpack(b, TInteger);
|
||||
Object cobj_any = MessagePack.unpack(c, TAny);
|
||||
Object cobj_obj = MessagePack.unpack(c, tOptional(TAny));
|
||||
Object dobj = MessagePack.unpack(d, tList(TString));
|
||||
Object eobj = MessagePack.unpack(e, tClass(ProvidedClass.class));
|
||||
Object fobj = MessagePack.unpack(f, tClass(UserDefinedClass.class));
|
||||
|
||||
assertEquals(aobj, "msgpack");
|
||||
assertEquals(bobj, 1);
|
||||
assertEquals(cobj_any, NilType.create());
|
||||
assertEquals(cobj_obj, null);
|
||||
assertEquals(dobj, createStringList());
|
||||
assertEquals(eobj, createProvidedClass());
|
||||
assertEquals(fobj, createUserDefinedClass());
|
||||
}
|
||||
|
||||
{
|
||||
String aobj = MessagePack.unpack(a, String.class);
|
||||
Integer bobj = MessagePack.unpack(b, Integer.class);
|
||||
Object cobj = MessagePack.unpack(c, Object.class);
|
||||
ProvidedClass eobj = MessagePack.unpack(e, ProvidedClass.class);
|
||||
UserDefinedClass fobj = MessagePack.unpack(f, UserDefinedClass.class);
|
||||
|
||||
assertEquals(aobj, "msgpack");
|
||||
assertEquals(bobj, (Integer)1);
|
||||
assertEquals(cobj, null);
|
||||
assertEquals(eobj, createProvidedClass());
|
||||
assertEquals(fobj, createUserDefinedClass());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckedPackToStream() throws Exception {
|
||||
ByteArrayOutputStream aout = new ByteArrayOutputStream();
|
||||
MessagePack.pack(aout, "msgpack");
|
||||
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
||||
MessagePack.pack(bout, (Object)1);
|
||||
ByteArrayOutputStream cout = new ByteArrayOutputStream();
|
||||
MessagePack.pack(cout, (Object)null);
|
||||
ByteArrayOutputStream dout = new ByteArrayOutputStream();
|
||||
MessagePack.pack(dout, createStringList());
|
||||
ByteArrayOutputStream eout = new ByteArrayOutputStream();
|
||||
MessagePack.pack(eout, createProvidedClass());
|
||||
ByteArrayOutputStream fout = new ByteArrayOutputStream();
|
||||
MessagePack.pack(fout, createUserDefinedClass());
|
||||
|
||||
{
|
||||
InputStream ain = new ByteArrayInputStream(aout.toByteArray());
|
||||
Object aobj = MessagePack.unpack(ain, TString);
|
||||
InputStream bin = new ByteArrayInputStream(bout.toByteArray());
|
||||
Object bobj = MessagePack.unpack(bin, TInteger);
|
||||
InputStream cin_any = new ByteArrayInputStream(cout.toByteArray());
|
||||
Object cobj_any = MessagePack.unpack(cin_any, TAny);
|
||||
InputStream cin_obj = new ByteArrayInputStream(cout.toByteArray());
|
||||
Object cobj_obj = MessagePack.unpack(cin_obj, tOptional(TAny));
|
||||
InputStream din = new ByteArrayInputStream(dout.toByteArray());
|
||||
Object dobj = MessagePack.unpack(din, tList(TString));
|
||||
InputStream ein = new ByteArrayInputStream(eout.toByteArray());
|
||||
Object eobj = MessagePack.unpack(ein, tClass(ProvidedClass.class));
|
||||
InputStream fin = new ByteArrayInputStream(fout.toByteArray());
|
||||
Object fobj = MessagePack.unpack(fin, tClass(UserDefinedClass.class));
|
||||
|
||||
assertEquals(aobj, "msgpack");
|
||||
assertEquals(bobj, 1);
|
||||
assertEquals(cobj_any, NilType.create());
|
||||
assertEquals(cobj_obj, null);
|
||||
assertEquals(dobj, createStringList());
|
||||
assertEquals(eobj, createProvidedClass());
|
||||
assertEquals(fobj, createUserDefinedClass());
|
||||
}
|
||||
|
||||
{
|
||||
InputStream ain = new ByteArrayInputStream(aout.toByteArray());
|
||||
String aobj = MessagePack.unpack(ain, String.class);
|
||||
InputStream bin = new ByteArrayInputStream(bout.toByteArray());
|
||||
Integer bobj = MessagePack.unpack(bin, Integer.class);
|
||||
InputStream cin = new ByteArrayInputStream(cout.toByteArray());
|
||||
Object cobj = MessagePack.unpack(cin, Object.class);
|
||||
InputStream ein = new ByteArrayInputStream(eout.toByteArray());
|
||||
ProvidedClass eobj = MessagePack.unpack(ein, ProvidedClass.class);
|
||||
InputStream fin = new ByteArrayInputStream(fout.toByteArray());
|
||||
UserDefinedClass fobj = MessagePack.unpack(fin, UserDefinedClass.class);
|
||||
|
||||
assertEquals(aobj, "msgpack");
|
||||
assertEquals(bobj, (Integer)1);
|
||||
assertEquals(cobj, null);
|
||||
assertEquals(eobj, createProvidedClass());
|
||||
assertEquals(fobj, createUserDefinedClass());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPackToByteArray() throws Exception {
|
||||
byte[] a = MessagePack.pack("msgpack");
|
||||
byte[] b = MessagePack.pack((Object)1);
|
||||
byte[] c = MessagePack.pack((Object)null);
|
||||
byte[] d = MessagePack.pack(createStringList());
|
||||
byte[] e = MessagePack.pack(createProvidedClass());
|
||||
byte[] f = MessagePack.pack(createUserDefinedClass_dynamic());
|
||||
|
||||
{
|
||||
MessagePackObject aobj = MessagePack.unpack(a);
|
||||
MessagePackObject bobj = MessagePack.unpack(b);
|
||||
MessagePackObject cobj = MessagePack.unpack(c);
|
||||
MessagePackObject dobj = MessagePack.unpack(d);
|
||||
MessagePackObject eobj = MessagePack.unpack(e);
|
||||
MessagePackObject fobj = MessagePack.unpack(f);
|
||||
|
||||
assertEquals(aobj, RawType.create("msgpack"));
|
||||
assertEquals(bobj, IntegerType.create(1));
|
||||
assertEquals(cobj, NilType.create());
|
||||
assertEquals(dobj, createStringList_dynamic());
|
||||
assertEquals(eobj, createProvidedClass_dynamic());
|
||||
assertEquals(fobj, createUserDefinedClass_dynamic());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPackToStream() throws Exception {
|
||||
ByteArrayOutputStream aout = new ByteArrayOutputStream();
|
||||
MessagePack.pack(aout, "msgpack");
|
||||
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
||||
MessagePack.pack(bout, (Object)1);
|
||||
ByteArrayOutputStream cout = new ByteArrayOutputStream();
|
||||
MessagePack.pack(cout, (Object)null);
|
||||
ByteArrayOutputStream dout = new ByteArrayOutputStream();
|
||||
MessagePack.pack(dout, createStringList());
|
||||
ByteArrayOutputStream eout = new ByteArrayOutputStream();
|
||||
MessagePack.pack(eout, createProvidedClass());
|
||||
ByteArrayOutputStream fout = new ByteArrayOutputStream();
|
||||
MessagePack.pack(fout, createUserDefinedClass());
|
||||
|
||||
{
|
||||
InputStream ain = new ByteArrayInputStream(aout.toByteArray());
|
||||
MessagePackObject aobj = MessagePack.unpack(ain);
|
||||
InputStream bin = new ByteArrayInputStream(bout.toByteArray());
|
||||
MessagePackObject bobj = MessagePack.unpack(bin);
|
||||
InputStream cin = new ByteArrayInputStream(cout.toByteArray());
|
||||
MessagePackObject cobj = MessagePack.unpack(cin);
|
||||
InputStream din = new ByteArrayInputStream(dout.toByteArray());
|
||||
MessagePackObject dobj = MessagePack.unpack(din);
|
||||
InputStream ein = new ByteArrayInputStream(eout.toByteArray());
|
||||
MessagePackObject eobj = MessagePack.unpack(ein);
|
||||
InputStream fin = new ByteArrayInputStream(fout.toByteArray());
|
||||
MessagePackObject fobj = MessagePack.unpack(fin);
|
||||
|
||||
assertEquals(aobj, RawType.create("msgpack"));
|
||||
assertEquals(bobj, IntegerType.create(1));
|
||||
assertEquals(cobj, NilType.create());
|
||||
assertEquals(dobj, createStringList_dynamic());
|
||||
assertEquals(eobj, createProvidedClass_dynamic());
|
||||
assertEquals(fobj, createUserDefinedClass_dynamic());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private List<String> createStringList() {
|
||||
List<String> list = new ArrayList<String>();
|
||||
list.add("frsyuki");
|
||||
list.add("kumofs");
|
||||
list.add("gem-compile");
|
||||
return list;
|
||||
}
|
||||
|
||||
private MessagePackObject createStringList_dynamic() {
|
||||
MessagePackObject[] array = new MessagePackObject[3];
|
||||
array[0] = RawType.create("frsyuki");
|
||||
array[1] = RawType.create("kumofs");
|
||||
array[2] = RawType.create("gem-compile");
|
||||
return ArrayType.create(array);
|
||||
}
|
||||
|
||||
|
||||
private ProvidedClass createProvidedClass() {
|
||||
ProvidedClass obj = new ProvidedClass();
|
||||
obj.bool = true;
|
||||
obj.str = "viver";
|
||||
obj.list = new ArrayList<Integer>();
|
||||
obj.list.add(1);
|
||||
obj.list.add(2);
|
||||
obj.list.add(3);
|
||||
return obj;
|
||||
}
|
||||
|
||||
private MessagePackObject createProvidedClass_dynamic() {
|
||||
MessagePackObject[] obj = new MessagePackObject[3];
|
||||
obj[0] = BooleanType.create(true);
|
||||
obj[1] = RawType.create("viver");
|
||||
MessagePackObject[] list = new MessagePackObject[3];
|
||||
list[0] = IntegerType.create(1);
|
||||
list[1] = IntegerType.create(2);
|
||||
list[2] = IntegerType.create(3);
|
||||
obj[2] = ArrayType.create(list);
|
||||
return ArrayType.create(obj);
|
||||
}
|
||||
|
||||
|
||||
private UserDefinedClass createUserDefinedClass() {
|
||||
UserDefinedClass obj = new UserDefinedClass();
|
||||
obj.bool = false;
|
||||
obj.str = "muga";
|
||||
obj.list = new ArrayList<Integer>();
|
||||
obj.list.add(9);
|
||||
obj.list.add(10);
|
||||
obj.list.add(11);
|
||||
return obj;
|
||||
}
|
||||
|
||||
private MessagePackObject createUserDefinedClass_dynamic() {
|
||||
MessagePackObject[] obj = new MessagePackObject[3];
|
||||
obj[0] = BooleanType.create(false);
|
||||
obj[1] = RawType.create("muga");
|
||||
MessagePackObject[] list = new MessagePackObject[3];
|
||||
list[0] = IntegerType.create(9);
|
||||
list[1] = IntegerType.create(10);
|
||||
list[2] = IntegerType.create(11);
|
||||
obj[2] = ArrayType.create(list);
|
||||
return ArrayType.create(obj);
|
||||
}
|
||||
}
|
||||
|
@ -1235,9 +1235,9 @@ public class TestPackConvert extends TestCase {
|
||||
@Test
|
||||
public void testNestedFieldClass00() throws Exception {
|
||||
Template tmpl2 = DynamicTemplate.create(NestedClass.class);
|
||||
CustomMessage.registerTemplate(NestedClass.class, tmpl2);
|
||||
CustomMessage.register(NestedClass.class, tmpl2);
|
||||
Template tmpl = DynamicTemplate.create(BaseClass.class);
|
||||
CustomMessage.registerTemplate(BaseClass.class, tmpl);
|
||||
CustomMessage.register(BaseClass.class, tmpl);
|
||||
BaseClass src = new BaseClass();
|
||||
NestedClass src2 = new NestedClass();
|
||||
src.f0 = 0;
|
||||
@ -1259,9 +1259,9 @@ public class TestPackConvert extends TestCase {
|
||||
@Test
|
||||
public void testNestedFieldClass02() throws Exception {
|
||||
Template tmpl2 = DynamicTemplate.create(NestedClass.class);
|
||||
CustomMessage.registerTemplate(NestedClass.class, tmpl2);
|
||||
CustomMessage.register(NestedClass.class, tmpl2);
|
||||
Template tmpl = new OptionalTemplate(DynamicTemplate.create(BaseClass.class));
|
||||
CustomMessage.registerTemplate(BaseClass.class, tmpl);
|
||||
CustomMessage.register(BaseClass.class, tmpl);
|
||||
BaseClass src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
tmpl.pack(new Packer(out), src);
|
||||
@ -1293,9 +1293,9 @@ public class TestPackConvert extends TestCase {
|
||||
@Test
|
||||
public void testOptionalNestedFieldClass00() throws Exception {
|
||||
Template tmpl2 = DynamicTemplate.create(OptionalNestedClass.class);
|
||||
CustomMessage.registerTemplate(OptionalNestedClass.class, tmpl2);
|
||||
CustomMessage.register(OptionalNestedClass.class, tmpl2);
|
||||
Template tmpl = DynamicTemplate.create(OptionalBaseClass.class);
|
||||
CustomMessage.registerTemplate(OptionalBaseClass.class, tmpl);
|
||||
CustomMessage.register(OptionalBaseClass.class, tmpl);
|
||||
OptionalBaseClass src = new OptionalBaseClass();
|
||||
OptionalNestedClass src2 = new OptionalNestedClass();
|
||||
src.f0 = 0;
|
||||
@ -1317,9 +1317,9 @@ public class TestPackConvert extends TestCase {
|
||||
@Test
|
||||
public void testOptionalNestedFieldClass01() throws Exception {
|
||||
Template tmpl2 = DynamicTemplate.create(OptionalNestedClass.class);
|
||||
CustomMessage.registerTemplate(OptionalNestedClass.class, tmpl2);
|
||||
CustomMessage.register(OptionalNestedClass.class, tmpl2);
|
||||
Template tmpl = DynamicTemplate.create(OptionalBaseClass.class);
|
||||
CustomMessage.registerTemplate(OptionalBaseClass.class, tmpl);
|
||||
CustomMessage.register(OptionalBaseClass.class, tmpl);
|
||||
OptionalBaseClass src = new OptionalBaseClass();
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
tmpl.pack(new Packer(out), src);
|
||||
|
@ -1116,9 +1116,9 @@ public class TestPackUnpack extends TestCase {
|
||||
@Test
|
||||
public void testNestedFieldClass00() throws Exception {
|
||||
Template tmpl2 = DynamicTemplate.create(NestedClass.class);
|
||||
CustomMessage.registerTemplate(NestedClass.class, tmpl2);
|
||||
CustomMessage.register(NestedClass.class, tmpl2);
|
||||
Template tmpl = DynamicTemplate.create(BaseClass.class);
|
||||
CustomMessage.registerTemplate(BaseClass.class, tmpl);
|
||||
CustomMessage.register(BaseClass.class, tmpl);
|
||||
BaseClass src = new BaseClass();
|
||||
NestedClass src2 = new NestedClass();
|
||||
src.f0 = 0;
|
||||
@ -1135,7 +1135,7 @@ public class TestPackUnpack extends TestCase {
|
||||
@Test
|
||||
public void testNestedFieldClass01() throws Exception {
|
||||
Template tmpl2 = DynamicTemplate.create(NestedClass.class);
|
||||
CustomMessage.registerTemplate(NestedClass.class, tmpl2);
|
||||
CustomMessage.register(NestedClass.class, tmpl2);
|
||||
Template tmpl = new OptionalTemplate(DynamicTemplate.create(BaseClass.class));
|
||||
BaseClass src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
@ -1163,7 +1163,7 @@ public class TestPackUnpack extends TestCase {
|
||||
@Test
|
||||
public void testOptionalNestedFieldClass00() throws Exception {
|
||||
Template tmpl2 = DynamicTemplate.create(OptionalNestedClass.class);
|
||||
CustomMessage.registerTemplate(OptionalNestedClass.class, tmpl2);
|
||||
CustomMessage.register(OptionalNestedClass.class, tmpl2);
|
||||
Template tmpl = DynamicTemplate.create(OptionalBaseClass.class);
|
||||
OptionalBaseClass src = new OptionalBaseClass();
|
||||
OptionalNestedClass src2 = new OptionalNestedClass();
|
||||
@ -1181,9 +1181,9 @@ public class TestPackUnpack extends TestCase {
|
||||
@Test
|
||||
public void testOptionalNestedFieldClass01() throws Exception {
|
||||
Template tmpl2 = DynamicTemplate.create(OptionalNestedClass.class);
|
||||
CustomMessage.registerTemplate(OptionalNestedClass.class, tmpl2);
|
||||
CustomMessage.register(OptionalNestedClass.class, tmpl2);
|
||||
Template tmpl = DynamicTemplate.create(OptionalBaseClass.class);
|
||||
CustomMessage.registerTemplate(OptionalBaseClass.class, tmpl);
|
||||
CustomMessage.register(OptionalBaseClass.class, tmpl);
|
||||
OptionalBaseClass src = new OptionalBaseClass();
|
||||
src.f1 = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
@ -1197,9 +1197,9 @@ public class TestPackUnpack extends TestCase {
|
||||
@Test
|
||||
public void testOptionalNestedFieldClass02() throws Exception {
|
||||
Template tmpl2 = DynamicTemplate.create(OptionalNestedClass.class);
|
||||
CustomMessage.registerTemplate(OptionalNestedClass.class, tmpl2);
|
||||
CustomMessage.register(OptionalNestedClass.class, tmpl2);
|
||||
Template tmpl = new OptionalTemplate(DynamicTemplate.create(OptionalBaseClass.class));
|
||||
CustomMessage.registerTemplate(OptionalBaseClass.class, tmpl);
|
||||
CustomMessage.register(OptionalBaseClass.class, tmpl);
|
||||
OptionalBaseClass src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
tmpl.pack(new Packer(out), src);
|
||||
|
Loading…
x
Reference in New Issue
Block a user