mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-04-15 22:50:35 +02:00
java: add the code for checking a null pointer to DynamicCodeGen.java
This commit is contained in:
parent
3f5ac54bf5
commit
c10eb2c17b
@ -207,21 +207,27 @@ public interface Constants {
|
||||
|
||||
String STATEMENT_PACKER_PACKERMETHODBODY_04 = "$1.pack(((java.lang.Enum)_$$_t).ordinal()); ";
|
||||
|
||||
String STATEMENT_PACKER_UNPACKERMETHODBODY_01 = "%s _$$_t = new %s(); ";
|
||||
String STATEMENT_PACKER_PACKERMETHODBODY_05 = "if (_$$_nullCheck && $2 == null) { $1.packNil(); return; } ";
|
||||
|
||||
String STATEMENT_PACKER_UNPACKERMETHODBODY_02 = "$1.unpackArray(); ";
|
||||
String STATEMENT_TMPL_UNPACKERMETHODBODY_01 = "%s _$$_t = new %s(); ";
|
||||
|
||||
String STATEMENT_PACKER_UNPACKERMETHODBODY_03 = "_$$_t.%s = %s(%s)_$$_templates[%d].unpack($1)%s; ";
|
||||
String STATEMENT_TMPL_UNPACKERMETHODBODY_02 = "$1.unpackArray(); ";
|
||||
|
||||
String STATEMENT_PACKER_UNPACKERMETHODBODY_04 = "return _$$_t; ";
|
||||
String STATEMENT_TMPL_UNPACKERMETHODBODY_03 = "if (!_$$_nullCheck || !$1.tryUnpackNull()) { _$$_t.%s = %s(%s)_$$_templates[%d].unpack($1)%s; } ";
|
||||
|
||||
String STATEMENT_PACKER_UNPACKERMETHODBODY_05 = "int i = $1.unpackInt(); ";
|
||||
String STATEMENT_TMPL_UNPACKERMETHODBODY_04 = "return _$$_t; ";
|
||||
|
||||
String STATEMENT_PACKER_UNPACKERMETHODBODY_06 = "return %s.class.getEnumConstants()[i]; ";
|
||||
String STATEMENT_TMPL_UNPACKERMETHODBODY_05 = "int i = $1.unpackInt(); ";
|
||||
|
||||
String STATEMENT_PACKER_CONVERTMETHODBODY_01 = "%s _$$_ary = $1.asArray(); ";
|
||||
String STATEMENT_TMPL_UNPACKERMETHODBODY_06 = "return %s.class.getEnumConstants()[i]; ";
|
||||
|
||||
String STATEMENT_PACKER_CONVERTMETHODBODY_02 = "_$$_t.%s = %s(%s)_$$_templates[%d].convert(_$$_ary[%d])%s; ";
|
||||
String STATEMENT_TMPL_UNPACKERMETHODBODY_07 = "if (_$$_nullCheck && $1.tryUnpackNull()) { return null; } ";
|
||||
|
||||
String STATEMENT_PACKER_CONVERTMETHODBODY_03 = "int i = _$$_ary[0].asInt(); ";
|
||||
String STATEMENT_TMPL_CONVERTMETHODBODY_01 = "%s _$$_ary = $1.asArray(); ";
|
||||
|
||||
String STATEMENT_TMPL_CONVERTMETHODBODY_02 = "if (!_$$_nullCheck || !_$$_ary[%d].isNil()) { _$$_t.%s = %s(%s)_$$_templates[%d].convert(_$$_ary[%d])%s; } ";
|
||||
|
||||
String STATEMENT_TMPL_CONVERTMETHODBODY_03 = "int i = _$$_ary[0].asInt(); ";
|
||||
|
||||
String STATEMENT_TMPL_CONVERTMETHODBODY_04 = "if (_$$_nullCheck && $1.isNil()) { return null; } ";
|
||||
}
|
||||
|
@ -72,6 +72,7 @@ class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
||||
checkTypeValidation(origClass);
|
||||
checkDefaultConstructorValidation(origClass);
|
||||
CtClass packerCtClass = pool.makeClass(packerName);
|
||||
setSuperclass(packerCtClass, NullCheckerImpl.class);
|
||||
setInterface(packerCtClass, MessagePacker.class);
|
||||
addDefaultConstructor(packerCtClass);
|
||||
Field[] fields = getDeclaredFields(origClass);
|
||||
@ -96,6 +97,7 @@ class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
||||
String packerName = origName + POSTFIX_TYPE_NAME_PACKER + inc();
|
||||
checkTypeValidation(origClass);
|
||||
CtClass packerCtClass = pool.makeClass(packerName);
|
||||
setSuperclass(packerCtClass, NullCheckerImpl.class);
|
||||
setInterface(packerCtClass, MessagePacker.class);
|
||||
addDefaultConstructor(packerCtClass);
|
||||
addPackMethod(packerCtClass, origClass, null, true);
|
||||
@ -120,14 +122,12 @@ class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
||||
checkTypeValidation(origClass);
|
||||
checkDefaultConstructorValidation(origClass);
|
||||
CtClass tmplCtClass = pool.makeClass(tmplName);
|
||||
setSuperclass(tmplCtClass, TemplateTemplate.class);
|
||||
setInterface(tmplCtClass, Template.class);
|
||||
setInterface(tmplCtClass, DynamicCodeGenBase.TemplateAccessor.class);
|
||||
addDefaultConstructor(tmplCtClass);
|
||||
Field[] fields = getDeclaredFields(origClass);
|
||||
Template[] tmpls = createTemplates(fields);
|
||||
setTemplates(origClass, tmpls);
|
||||
addTemplateArrayField(tmplCtClass);
|
||||
addSetTemplatesMethod(tmplCtClass);
|
||||
addUnpackMethod(tmplCtClass, origClass, fields, false);
|
||||
addConvertMethod(tmplCtClass, origClass, fields, false);
|
||||
Class<?> tmplClass = createClass(tmplCtClass);
|
||||
@ -154,11 +154,11 @@ class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
||||
checkTypeValidation(origClass);
|
||||
String tmplName = origName + POSTFIX_TYPE_NAME_TEMPLATE + inc();
|
||||
CtClass tmplCtClass = pool.makeClass(tmplName);
|
||||
setSuperclass(tmplCtClass, TemplateTemplate.class);
|
||||
setInterface(tmplCtClass, Template.class);
|
||||
addDefaultConstructor(tmplCtClass);
|
||||
addUnpackMethod(tmplCtClass, origClass, null, true);
|
||||
addConvertMethod(tmplCtClass, origClass, null, true);
|
||||
// addConvertMethodForOrdinalEnumTypes(tmplCtClass, origClass);
|
||||
Class<?> tmplClass = createClass(tmplCtClass);
|
||||
LOG.debug("generated an enum template class for "
|
||||
+ origClass.getName());
|
||||
@ -218,7 +218,7 @@ class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
||||
checkFieldValidation(field, allFields);
|
||||
allFields.add(field);
|
||||
} catch (DynamicCodeGenException e) { // ignore
|
||||
LOG.error(e.getMessage(), e);
|
||||
LOG.trace(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
nextClass = nextClass.getSuperclass();
|
||||
@ -299,6 +299,8 @@ class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
||||
// void pack(Packer packer, Object target) throws IOException;
|
||||
sb.append(CHAR_NAME_LEFT_CURLY_BRACKET);
|
||||
sb.append(CHAR_NAME_SPACE);
|
||||
Object[] args2 = new Object[0];
|
||||
sb.append(String.format(STATEMENT_PACKER_PACKERMETHODBODY_05, args2));
|
||||
String typeName = classToString(type);
|
||||
Object[] args0 = new Object[] { typeName, typeName };
|
||||
sb.append(String.format(STATEMENT_PACKER_PACKERMETHODBODY_01, args0));
|
||||
@ -357,6 +359,8 @@ class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
||||
// void pack(Packer packer, Object target) throws IOException;
|
||||
sb.append(CHAR_NAME_LEFT_CURLY_BRACKET);
|
||||
sb.append(CHAR_NAME_SPACE);
|
||||
Object[] args3 = new Object[0];
|
||||
sb.append(String.format(STATEMENT_PACKER_PACKERMETHODBODY_05, args3));
|
||||
String typeName = classToString(c);
|
||||
Object[] args0 = new Object[] { typeName, typeName };
|
||||
sb.append(String.format(STATEMENT_PACKER_PACKERMETHODBODY_01, args0));
|
||||
@ -410,15 +414,17 @@ class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
||||
sb.append(CHAR_NAME_SPACE);
|
||||
// Foo _$$_t = new Foo();
|
||||
String typeName = classToString(type);
|
||||
Object[] args3 = new Object[0];
|
||||
sb.append(String.format(STATEMENT_TMPL_UNPACKERMETHODBODY_07, args3));
|
||||
Object[] args0 = new Object[] { typeName, typeName };
|
||||
sb.append(String.format(STATEMENT_PACKER_UNPACKERMETHODBODY_01, args0));
|
||||
sb.append(String.format(STATEMENT_TMPL_UNPACKERMETHODBODY_01, args0));
|
||||
// $1.unpackArray();
|
||||
Object[] args1 = new Object[0];
|
||||
sb.append(String.format(STATEMENT_PACKER_UNPACKERMETHODBODY_02, args1));
|
||||
sb.append(String.format(STATEMENT_TMPL_UNPACKERMETHODBODY_02, args1));
|
||||
insertCodeOfUnpackMethodCalls(sb, fields);
|
||||
// return _$$_t;
|
||||
Object[] args2 = new Object[0];
|
||||
sb.append(String.format(STATEMENT_PACKER_UNPACKERMETHODBODY_04, args2));
|
||||
sb.append(String.format(STATEMENT_TMPL_UNPACKERMETHODBODY_04, args2));
|
||||
sb.append(CHAR_NAME_RIGHT_CURLY_BRACKET);
|
||||
}
|
||||
|
||||
@ -441,7 +447,7 @@ class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
||||
i,
|
||||
isPrim ? ")." + getPrimTypeValueMethodName(returnType) + "()"
|
||||
: "" };
|
||||
sb.append(String.format(STATEMENT_PACKER_UNPACKERMETHODBODY_03, args));
|
||||
sb.append(String.format(STATEMENT_TMPL_UNPACKERMETHODBODY_03, args));
|
||||
}
|
||||
|
||||
private void insertCodeOfUnpackMethodCallForMsgUnpackableType(
|
||||
@ -499,15 +505,17 @@ class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
||||
// Object unpack(Unpacker u) throws IOException, MessageTypeException;
|
||||
sb.append(CHAR_NAME_LEFT_CURLY_BRACKET);
|
||||
sb.append(CHAR_NAME_SPACE);
|
||||
Object[] args3 = new Object[0];
|
||||
sb.append(String.format(STATEMENT_TMPL_UNPACKERMETHODBODY_07, args3));
|
||||
// $1.unpackArray();
|
||||
Object[] args0 = new Object[0];
|
||||
sb.append(String.format(STATEMENT_PACKER_UNPACKERMETHODBODY_02, args0));
|
||||
sb.append(String.format(STATEMENT_TMPL_UNPACKERMETHODBODY_02, args0));
|
||||
// int i = $1.unapckInt();
|
||||
Object[] args1 = new Object[0];
|
||||
sb.append(String.format(STATEMENT_PACKER_UNPACKERMETHODBODY_05, args1));
|
||||
sb.append(String.format(STATEMENT_TMPL_UNPACKERMETHODBODY_05, args1));
|
||||
// return Foo.class.getEnumConstants()[i];
|
||||
Object[] args2 = new Object[] { classToString(type) };
|
||||
sb.append(String.format(STATEMENT_PACKER_UNPACKERMETHODBODY_06, args2));
|
||||
sb.append(String.format(STATEMENT_TMPL_UNPACKERMETHODBODY_06, args2));
|
||||
sb.append(CHAR_NAME_RIGHT_CURLY_BRACKET);
|
||||
}
|
||||
|
||||
@ -550,17 +558,19 @@ class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
||||
// Object convert(MessagePackObject mpo) throws MessageTypeException;
|
||||
sb.append(CHAR_NAME_LEFT_CURLY_BRACKET);
|
||||
sb.append(CHAR_NAME_SPACE);
|
||||
Object[] args3 = new Object[0];
|
||||
sb.append(String.format(STATEMENT_TMPL_CONVERTMETHODBODY_04, args3));
|
||||
// Foo _$$_t = new Foo();
|
||||
String typeName = classToString(type);
|
||||
Object[] args0 = new Object[] { typeName, typeName };
|
||||
sb.append(String.format(STATEMENT_PACKER_UNPACKERMETHODBODY_01, args0));
|
||||
sb.append(String.format(STATEMENT_TMPL_UNPACKERMETHODBODY_01, args0));
|
||||
// MessagePackObject[] _$$_ary = $1.asArray();
|
||||
Object[] args1 = new Object[] { classToString(MessagePackObject[].class) };
|
||||
sb.append(String.format(STATEMENT_PACKER_CONVERTMETHODBODY_01, args1));
|
||||
sb.append(String.format(STATEMENT_TMPL_CONVERTMETHODBODY_01, args1));
|
||||
insertCodeOfConvertMethodCalls(sb, fields);
|
||||
// return _$$_t;
|
||||
Object[] args2 = new Object[0];
|
||||
sb.append(String.format(STATEMENT_PACKER_UNPACKERMETHODBODY_04, args2));
|
||||
sb.append(String.format(STATEMENT_TMPL_UNPACKERMETHODBODY_04, args2));
|
||||
sb.append(CHAR_NAME_RIGHT_CURLY_BRACKET);
|
||||
}
|
||||
|
||||
@ -575,6 +585,7 @@ class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
||||
Class<?> returnType = field.getType();
|
||||
boolean isPrim = returnType.isPrimitive();
|
||||
Object[] args = new Object[] {
|
||||
i,
|
||||
field.getName(),
|
||||
isPrim ? "(" : "",
|
||||
isPrim ? getPrimToWrapperType(returnType).getName()
|
||||
@ -583,7 +594,7 @@ class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
||||
i,
|
||||
isPrim ? ")." + getPrimTypeValueMethodName(returnType) + "()"
|
||||
: "" };
|
||||
sb.append(String.format(STATEMENT_PACKER_CONVERTMETHODBODY_02, args));
|
||||
sb.append(String.format(STATEMENT_TMPL_CONVERTMETHODBODY_02, args));
|
||||
}
|
||||
|
||||
private void insertCodeOfMessageConvertCallForMsgConvtblType(
|
||||
@ -640,15 +651,17 @@ class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
||||
// Object convert(MessagePackObject mpo) throws MessageTypeException;
|
||||
sb.append(CHAR_NAME_LEFT_CURLY_BRACKET);
|
||||
sb.append(CHAR_NAME_SPACE);
|
||||
Object[] args3 = new Object[0];
|
||||
sb.append(String.format(STATEMENT_TMPL_CONVERTMETHODBODY_04, args3));
|
||||
// MessagePackObject[] _$$_ary = $1.asArray();
|
||||
Object[] args0 = new Object[] { classToString(MessagePackObject[].class) };
|
||||
sb.append(String.format(STATEMENT_PACKER_CONVERTMETHODBODY_01, args0));
|
||||
sb.append(String.format(STATEMENT_TMPL_CONVERTMETHODBODY_01, args0));
|
||||
// int i = _$$_ary[0].asInt();
|
||||
Object[] args1 = new Object[0];
|
||||
sb.append(String.format(STATEMENT_PACKER_CONVERTMETHODBODY_03, args1));
|
||||
sb.append(String.format(STATEMENT_TMPL_CONVERTMETHODBODY_03, args1));
|
||||
// return Foo.class.getEnumConstants()[i];
|
||||
Object[] args2 = new Object[] { classToString(type) };
|
||||
sb.append(String.format(STATEMENT_PACKER_UNPACKERMETHODBODY_06, args2));
|
||||
sb.append(String.format(STATEMENT_TMPL_UNPACKERMETHODBODY_06, args2));
|
||||
sb.append(CHAR_NAME_RIGHT_CURLY_BRACKET);
|
||||
}
|
||||
}
|
||||
|
@ -32,11 +32,24 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class DynamicCodeGenBase implements Constants {
|
||||
|
||||
public static interface NullChecker {
|
||||
void setNullCheck(boolean nullCheck);
|
||||
}
|
||||
|
||||
public static class NullCheckerImpl implements NullChecker {
|
||||
public boolean _$$_nullCheck = true;
|
||||
|
||||
public void setNullCheck(boolean _$$_check) {
|
||||
_$$_nullCheck = _$$_check;
|
||||
}
|
||||
}
|
||||
|
||||
public static interface TemplateAccessor {
|
||||
void setTemplates(Template[] templates);
|
||||
}
|
||||
|
||||
public static class TemplateAccessorImpl implements TemplateAccessor {
|
||||
|
||||
public static class TemplateTemplate extends NullCheckerImpl implements TemplateAccessor {
|
||||
public Template[] _$$_templates;
|
||||
|
||||
public void setTemplates(Template[] _$$_tmpls) {
|
||||
@ -111,10 +124,50 @@ public class DynamicCodeGenBase implements Constants {
|
||||
throw e;
|
||||
}
|
||||
|
||||
protected void setInterface(CtClass packerCtClass, Class<?> infClass)
|
||||
enum Color {
|
||||
// TODO
|
||||
RED, BLUE
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
// TODO
|
||||
class Foo {
|
||||
}
|
||||
class Bar extends Foo {
|
||||
}
|
||||
Color c = Color.RED;
|
||||
Color c1 = null;
|
||||
|
||||
ClassPool pool = ClassPool.getDefault();
|
||||
CtClass barCtClass = pool.get(Bar.class.getName());
|
||||
CtClass sbarCtClass = barCtClass.getSuperclass();
|
||||
System.out.println("bar: " + sbarCtClass.getName());
|
||||
CtClass fooCtClass = pool.get(Foo.class.getName());
|
||||
CtClass sfooCtClass = fooCtClass.getSuperclass();
|
||||
System.out.println("foo: " + sfooCtClass.getName());
|
||||
}
|
||||
|
||||
protected void setSuperclass(CtClass newCtClass, Class<?> superClass)
|
||||
throws NotFoundException, CannotCompileException {
|
||||
// check the specified super class
|
||||
if (superClass.isInterface() || superClass.isEnum()
|
||||
|| superClass.isAnnotation() || superClass.isArray()
|
||||
|| superClass.isPrimitive()) {
|
||||
throwTypeValidationException(superClass, "Fatal error");
|
||||
}
|
||||
|
||||
// check the base class
|
||||
if (!newCtClass.getSuperclass().equals(classToCtClass(Object.class))) {
|
||||
throwTypeValidationException(superClass, "Fatal error");
|
||||
}
|
||||
CtClass superCtClass = pool.get(superClass.getName());
|
||||
newCtClass.setSuperclass(superCtClass);
|
||||
}
|
||||
|
||||
protected void setInterface(CtClass newCtClass, Class<?> infClass)
|
||||
throws NotFoundException {
|
||||
CtClass infCtClass = pool.get(infClass.getName());
|
||||
packerCtClass.addInterface(infCtClass);
|
||||
newCtClass.addInterface(infCtClass);
|
||||
}
|
||||
|
||||
protected void addDefaultConstructor(CtClass enhancedCtClass)
|
||||
@ -126,7 +179,7 @@ public class DynamicCodeGenBase implements Constants {
|
||||
|
||||
protected void addTemplateArrayField(CtClass newCtClass)
|
||||
throws NotFoundException, CannotCompileException {
|
||||
CtClass acsCtClass = pool.get(TemplateAccessorImpl.class.getName());
|
||||
CtClass acsCtClass = pool.get(TemplateTemplate.class.getName());
|
||||
CtField tmplsField = acsCtClass
|
||||
.getDeclaredField(VARIABLE_NAME_TEMPLATES);
|
||||
CtField tmplsField2 = new CtField(tmplsField.getType(), tmplsField
|
||||
@ -136,7 +189,7 @@ public class DynamicCodeGenBase implements Constants {
|
||||
|
||||
protected void addSetTemplatesMethod(CtClass newCtClass)
|
||||
throws NotFoundException, CannotCompileException {
|
||||
CtClass acsCtClass = pool.get(TemplateAccessorImpl.class.getName());
|
||||
CtClass acsCtClass = pool.get(TemplateTemplate.class.getName());
|
||||
CtMethod settmplsMethod = acsCtClass
|
||||
.getDeclaredMethod(METHOD_NAME_SETTEMPLATES);
|
||||
CtMethod settmplsMethod2 = CtNewMethod.copy(settmplsMethod, newCtClass,
|
||||
|
@ -26,7 +26,7 @@ import org.msgpack.annotation.MessagePackOrdinalEnum;
|
||||
public class TestDynamicCodeGenPackerConverter extends TestCase {
|
||||
|
||||
@Test
|
||||
public void testPrimitiveTypeFields() throws Exception {
|
||||
public void testPrimitiveTypeField00s() throws Exception {
|
||||
PrimitiveTypeFieldsClass src = new PrimitiveTypeFieldsClass();
|
||||
src.f0 = (byte) 0;
|
||||
src.f1 = 1;
|
||||
@ -40,8 +40,7 @@ public class TestDynamicCodeGenPackerConverter extends TestCase {
|
||||
.create(PrimitiveTypeFieldsClass.class);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate
|
||||
.create(PrimitiveTypeFieldsClass.class);
|
||||
Template tmpl = DynamicTemplate.create(PrimitiveTypeFieldsClass.class);
|
||||
Unpacker pac = new Unpacker(in);
|
||||
Iterator<MessagePackObject> it = pac.iterator();
|
||||
assertTrue(it.hasNext());
|
||||
@ -58,6 +57,50 @@ public class TestDynamicCodeGenPackerConverter extends TestCase {
|
||||
assertFalse(it.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrimitiveTypeFields01() throws Exception {
|
||||
PrimitiveTypeFieldsClass src = new PrimitiveTypeFieldsClass();
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker
|
||||
.create(PrimitiveTypeFieldsClass.class);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate.create(PrimitiveTypeFieldsClass.class);
|
||||
Unpacker pac = new Unpacker(in);
|
||||
Iterator<MessagePackObject> it = pac.iterator();
|
||||
assertTrue(it.hasNext());
|
||||
MessagePackObject mpo = it.next();
|
||||
PrimitiveTypeFieldsClass dst = (PrimitiveTypeFieldsClass) tmpl
|
||||
.convert(mpo);
|
||||
assertEquals(src.f0, dst.f0);
|
||||
assertEquals(src.f1, dst.f1);
|
||||
assertEquals(src.f2, dst.f2);
|
||||
assertEquals(src.f3, dst.f3);
|
||||
assertEquals(src.f4, dst.f4);
|
||||
assertEquals(src.f5, dst.f5);
|
||||
assertEquals(src.f6, dst.f6);
|
||||
assertFalse(it.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrimitiveTypeFields02() throws Exception {
|
||||
PrimitiveTypeFieldsClass src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker
|
||||
.create(PrimitiveTypeFieldsClass.class);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate.create(PrimitiveTypeFieldsClass.class);
|
||||
Unpacker pac = new Unpacker(in);
|
||||
Iterator<MessagePackObject> it = pac.iterator();
|
||||
assertTrue(it.hasNext());
|
||||
MessagePackObject mpo = it.next();
|
||||
PrimitiveTypeFieldsClass dst = (PrimitiveTypeFieldsClass) tmpl
|
||||
.convert(mpo);
|
||||
assertEquals(src, dst);
|
||||
assertFalse(it.hasNext());
|
||||
}
|
||||
|
||||
public static class PrimitiveTypeFieldsClass {
|
||||
public byte f0;
|
||||
public short f1;
|
||||
@ -72,7 +115,7 @@ public class TestDynamicCodeGenPackerConverter extends TestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGeneralReferenceTypeFieldsClass() throws Exception {
|
||||
public void testGeneralReferenceTypeFieldsClass00() throws Exception {
|
||||
GeneralReferenceTypeFieldsClass src = new GeneralReferenceTypeFieldsClass();
|
||||
src.f0 = 0;
|
||||
src.f1 = 1;
|
||||
@ -111,6 +154,65 @@ public class TestDynamicCodeGenPackerConverter extends TestCase {
|
||||
assertFalse(it.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void XtestGeneralReferenceTypeFieldsClass01() throws Exception {
|
||||
GeneralReferenceTypeFieldsClass src = new GeneralReferenceTypeFieldsClass();
|
||||
src.f0 = null;
|
||||
src.f1 = null;
|
||||
src.f2 = null;
|
||||
src.f3 = null;
|
||||
src.f4 = null;
|
||||
src.f5 = null;
|
||||
src.f6 = null;
|
||||
src.f7 = null;
|
||||
src.f8 = null;
|
||||
src.f9 = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker
|
||||
.create(GeneralReferenceTypeFieldsClass.class);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate
|
||||
.create(GeneralReferenceTypeFieldsClass.class);
|
||||
Unpacker pac = new Unpacker(in);
|
||||
Iterator<MessagePackObject> it = pac.iterator();
|
||||
assertTrue(it.hasNext());
|
||||
MessagePackObject mpo = it.next();
|
||||
GeneralReferenceTypeFieldsClass dst = (GeneralReferenceTypeFieldsClass) tmpl
|
||||
.convert(mpo);
|
||||
assertEquals(src.f0, dst.f0);
|
||||
assertEquals(src.f1, dst.f1);
|
||||
assertEquals(src.f2, dst.f2);
|
||||
assertEquals(src.f3, dst.f3);
|
||||
assertEquals(src.f4, dst.f4);
|
||||
assertEquals(src.f5, dst.f5);
|
||||
assertEquals(src.f6, dst.f6);
|
||||
assertEquals(src.f7, dst.f7);
|
||||
assertEquals(src.f8, dst.f8);
|
||||
assertEquals(src.f9, dst.f9);
|
||||
assertFalse(it.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void XtestGeneralReferenceTypeFieldsClass02() throws Exception {
|
||||
GeneralReferenceTypeFieldsClass src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker
|
||||
.create(GeneralReferenceTypeFieldsClass.class);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate
|
||||
.create(GeneralReferenceTypeFieldsClass.class);
|
||||
Unpacker pac = new Unpacker(in);
|
||||
Iterator<MessagePackObject> it = pac.iterator();
|
||||
assertTrue(it.hasNext());
|
||||
MessagePackObject mpo = it.next();
|
||||
GeneralReferenceTypeFieldsClass dst = (GeneralReferenceTypeFieldsClass) tmpl
|
||||
.convert(mpo);
|
||||
assertEquals(src, dst);
|
||||
assertFalse(it.hasNext());
|
||||
}
|
||||
|
||||
public static class GeneralReferenceTypeFieldsClass {
|
||||
public Byte f0;
|
||||
public Short f1;
|
||||
@ -127,7 +229,8 @@ public class TestDynamicCodeGenPackerConverter extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
public void testListTypes() throws Exception {
|
||||
@Test
|
||||
public void testListTypes00() throws Exception {
|
||||
SampleListTypes src = new SampleListTypes();
|
||||
src.f0 = new ArrayList<Integer>();
|
||||
src.f1 = new ArrayList<Integer>();
|
||||
@ -146,8 +249,7 @@ public class TestDynamicCodeGenPackerConverter extends TestCase {
|
||||
slnt.f1 = "muga";
|
||||
src.f4.add(slnt);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker
|
||||
.create(SampleListTypes.class);
|
||||
MessagePacker packer = DynamicPacker.create(SampleListTypes.class);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate.create(SampleListTypes.class);
|
||||
@ -185,6 +287,49 @@ public class TestDynamicCodeGenPackerConverter extends TestCase {
|
||||
assertFalse(it.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListTypes01() throws Exception {
|
||||
SampleListTypes src = new SampleListTypes();
|
||||
src.f0 = new ArrayList<Integer>();
|
||||
src.f1 = null;
|
||||
src.f2 = new ArrayList<String>();
|
||||
src.f3 = null;
|
||||
src.f4 = new ArrayList<SampleListNestedType>();
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker.create(SampleListTypes.class);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate.create(SampleListTypes.class);
|
||||
Unpacker pac = new Unpacker(in);
|
||||
Iterator<MessagePackObject> it = pac.iterator();
|
||||
assertTrue(it.hasNext());
|
||||
MessagePackObject mpo = it.next();
|
||||
SampleListTypes dst = (SampleListTypes) tmpl.convert(mpo);
|
||||
assertEquals(src.f0.size(), dst.f0.size());
|
||||
assertEquals(src.f1, dst.f1);
|
||||
assertEquals(src.f2.size(), dst.f2.size());
|
||||
assertEquals(src.f3, dst.f3);
|
||||
assertEquals(src.f4.size(), dst.f4.size());
|
||||
assertFalse(it.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListTypes02() throws Exception {
|
||||
SampleListTypes src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker.create(SampleListTypes.class);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate.create(SampleListTypes.class);
|
||||
Unpacker pac = new Unpacker(in);
|
||||
Iterator<MessagePackObject> it = pac.iterator();
|
||||
assertTrue(it.hasNext());
|
||||
MessagePackObject mpo = it.next();
|
||||
SampleListTypes dst = (SampleListTypes) tmpl.convert(mpo);
|
||||
assertEquals(src, dst);
|
||||
assertFalse(it.hasNext());
|
||||
}
|
||||
|
||||
public static class SampleListTypes {
|
||||
public List<Integer> f0;
|
||||
public List<Integer> f1;
|
||||
@ -205,7 +350,8 @@ public class TestDynamicCodeGenPackerConverter extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
public void testMapTypes() throws Exception {
|
||||
@Test
|
||||
public void testMapTypes00() throws Exception {
|
||||
SampleMapTypes src = new SampleMapTypes();
|
||||
src.f0 = new HashMap<Integer, Integer>();
|
||||
src.f1 = new HashMap<Integer, Integer>();
|
||||
@ -217,8 +363,7 @@ public class TestDynamicCodeGenPackerConverter extends TestCase {
|
||||
src.f2.put("k2", 2);
|
||||
src.f2.put("k3", 3);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker
|
||||
.create(SampleMapTypes.class);
|
||||
MessagePacker packer = DynamicPacker.create(SampleMapTypes.class);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate.create(SampleMapTypes.class);
|
||||
@ -249,6 +394,45 @@ public class TestDynamicCodeGenPackerConverter extends TestCase {
|
||||
assertFalse(it.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapTypes01() throws Exception {
|
||||
SampleMapTypes src = new SampleMapTypes();
|
||||
src.f0 = new HashMap<Integer, Integer>();
|
||||
src.f1 = null;
|
||||
src.f2 = new HashMap<String, Integer>();
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker.create(SampleMapTypes.class);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate.create(SampleMapTypes.class);
|
||||
Unpacker pac = new Unpacker(in);
|
||||
Iterator<MessagePackObject> it = pac.iterator();
|
||||
assertTrue(it.hasNext());
|
||||
MessagePackObject mpo = it.next();
|
||||
SampleMapTypes dst = (SampleMapTypes) tmpl.convert(mpo);
|
||||
assertEquals(src.f0.size(), dst.f0.size());
|
||||
assertEquals(src.f1, dst.f1);
|
||||
assertEquals(src.f2.size(), dst.f2.size());
|
||||
assertFalse(it.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapTypes02() throws Exception {
|
||||
SampleMapTypes src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker.create(SampleMapTypes.class);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate.create(SampleMapTypes.class);
|
||||
Unpacker pac = new Unpacker(in);
|
||||
Iterator<MessagePackObject> it = pac.iterator();
|
||||
assertTrue(it.hasNext());
|
||||
MessagePackObject mpo = it.next();
|
||||
SampleMapTypes dst = (SampleMapTypes) tmpl.convert(mpo);
|
||||
assertEquals(src, dst);
|
||||
assertFalse(it.hasNext());
|
||||
}
|
||||
|
||||
public static class SampleMapTypes {
|
||||
public Map<Integer, Integer> f0;
|
||||
public Map<Integer, Integer> f1;
|
||||
@ -307,8 +491,7 @@ public class TestDynamicCodeGenPackerConverter extends TestCase {
|
||||
}
|
||||
assertTrue(true);
|
||||
try {
|
||||
DynamicTemplate
|
||||
.create(ProtectedDefaultConstructorClass.class);
|
||||
DynamicTemplate.create(ProtectedDefaultConstructorClass.class);
|
||||
fail();
|
||||
} catch (DynamicCodeGenException e) {
|
||||
assertTrue(true);
|
||||
@ -479,17 +662,15 @@ public class TestDynamicCodeGenPackerConverter extends TestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnumTypeForOrdinal() throws Exception {
|
||||
public void testEnumTypeForOrdinal00() throws Exception {
|
||||
SampleEnumFieldClass src = new SampleEnumFieldClass();
|
||||
src.f0 = 0;
|
||||
src.f1 = SampleEnum.ONE;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker
|
||||
.create(SampleEnumFieldClass.class);
|
||||
MessagePacker packer = DynamicPacker.create(SampleEnumFieldClass.class);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate
|
||||
.create(SampleEnumFieldClass.class);
|
||||
Template tmpl = DynamicTemplate.create(SampleEnumFieldClass.class);
|
||||
Unpacker pac = new Unpacker(in);
|
||||
Iterator<MessagePackObject> it = pac.iterator();
|
||||
assertTrue(it.hasNext());
|
||||
@ -497,6 +678,43 @@ public class TestDynamicCodeGenPackerConverter extends TestCase {
|
||||
SampleEnumFieldClass dst = (SampleEnumFieldClass) tmpl.convert(mpo);
|
||||
assertTrue(src.f0 == dst.f0);
|
||||
assertTrue(src.f1 == dst.f1);
|
||||
assertFalse(it.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnumTypeForOrdinal01() throws Exception {
|
||||
SampleEnumFieldClass src = new SampleEnumFieldClass();
|
||||
src.f1 = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker.create(SampleEnumFieldClass.class);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate.create(SampleEnumFieldClass.class);
|
||||
Unpacker pac = new Unpacker(in);
|
||||
Iterator<MessagePackObject> it = pac.iterator();
|
||||
assertTrue(it.hasNext());
|
||||
MessagePackObject mpo = it.next();
|
||||
SampleEnumFieldClass dst = (SampleEnumFieldClass) tmpl.convert(mpo);
|
||||
assertEquals(src.f0, dst.f0);
|
||||
assertEquals(src.f1, dst.f1);
|
||||
assertFalse(it.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnumTypeForOrdinal02() throws Exception {
|
||||
SampleEnumFieldClass src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker.create(SampleEnumFieldClass.class);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate.create(SampleEnumFieldClass.class);
|
||||
Unpacker pac = new Unpacker(in);
|
||||
Iterator<MessagePackObject> it = pac.iterator();
|
||||
assertTrue(it.hasNext());
|
||||
MessagePackObject mpo = it.next();
|
||||
SampleEnumFieldClass dst = (SampleEnumFieldClass) tmpl.convert(mpo);
|
||||
assertEquals(src, dst);
|
||||
assertFalse(it.hasNext());
|
||||
}
|
||||
|
||||
public static class SampleEnumFieldClass {
|
||||
@ -521,12 +739,10 @@ public class TestDynamicCodeGenPackerConverter extends TestCase {
|
||||
src.f3 = 3;
|
||||
src.f4 = 4;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker
|
||||
.create(FieldModifiersClass.class);
|
||||
MessagePacker packer = DynamicPacker.create(FieldModifiersClass.class);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate
|
||||
.create(FieldModifiersClass.class);
|
||||
Template tmpl = DynamicTemplate.create(FieldModifiersClass.class);
|
||||
Unpacker pac = new Unpacker(in);
|
||||
Iterator<MessagePackObject> it = pac.iterator();
|
||||
assertTrue(it.hasNext());
|
||||
@ -552,7 +768,7 @@ public class TestDynamicCodeGenPackerConverter extends TestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNestedFieldClass() throws Exception {
|
||||
public void testNestedFieldClass00() throws Exception {
|
||||
MessagePacker packer2 = DynamicPacker.create(NestedClass.class);
|
||||
CustomPacker.register(NestedClass.class, packer2);
|
||||
MessagePacker packer1 = DynamicPacker.create(BaseClass.class);
|
||||
@ -581,6 +797,57 @@ public class TestDynamicCodeGenPackerConverter extends TestCase {
|
||||
assertFalse(it.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNestedFieldClass01() throws Exception {
|
||||
MessagePacker packer2 = DynamicPacker.create(NestedClass.class);
|
||||
CustomPacker.register(NestedClass.class, packer2);
|
||||
MessagePacker packer1 = DynamicPacker.create(BaseClass.class);
|
||||
CustomPacker.register(BaseClass.class, packer1);
|
||||
Template tmpl2 = DynamicTemplate.create(NestedClass.class);
|
||||
CustomUnpacker.register(NestedClass.class, tmpl2);
|
||||
CustomConverter.register(NestedClass.class, tmpl2);
|
||||
Template tmpl1 = DynamicTemplate.create(BaseClass.class);
|
||||
CustomUnpacker.register(BaseClass.class, tmpl1);
|
||||
CustomConverter.register(BaseClass.class, tmpl1);
|
||||
BaseClass src = new BaseClass();
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
packer1.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Unpacker pac = new Unpacker(in);
|
||||
Iterator<MessagePackObject> it = pac.iterator();
|
||||
assertTrue(it.hasNext());
|
||||
MessagePackObject mpo = it.next();
|
||||
BaseClass dst = (BaseClass) tmpl1.convert(mpo);
|
||||
assertTrue(src.f0 == dst.f0);
|
||||
assertEquals(src.f1, dst.f1);
|
||||
assertFalse(it.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNestedFieldClass02() throws Exception {
|
||||
MessagePacker packer2 = DynamicPacker.create(NestedClass.class);
|
||||
CustomPacker.register(NestedClass.class, packer2);
|
||||
MessagePacker packer1 = DynamicPacker.create(BaseClass.class);
|
||||
CustomPacker.register(BaseClass.class, packer1);
|
||||
Template tmpl2 = DynamicTemplate.create(NestedClass.class);
|
||||
CustomUnpacker.register(NestedClass.class, tmpl2);
|
||||
CustomConverter.register(NestedClass.class, tmpl2);
|
||||
Template tmpl1 = DynamicTemplate.create(BaseClass.class);
|
||||
CustomUnpacker.register(BaseClass.class, tmpl1);
|
||||
CustomConverter.register(BaseClass.class, tmpl1);
|
||||
BaseClass src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
packer1.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Unpacker pac = new Unpacker(in);
|
||||
Iterator<MessagePackObject> it = pac.iterator();
|
||||
assertTrue(it.hasNext());
|
||||
MessagePackObject mpo = it.next();
|
||||
BaseClass dst = (BaseClass) tmpl1.convert(mpo);
|
||||
assertEquals(src, dst);
|
||||
assertFalse(it.hasNext());
|
||||
}
|
||||
|
||||
public static class BaseClass {
|
||||
public int f0;
|
||||
public NestedClass f1;
|
||||
@ -597,7 +864,7 @@ public class TestDynamicCodeGenPackerConverter extends TestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMessagePackMessageFieldClass() throws Exception {
|
||||
public void testMessagePackMessageFieldClass00() throws Exception {
|
||||
BaseClass2 src = new BaseClass2();
|
||||
MessagePackMessageClass2 src2 = new MessagePackMessageClass2();
|
||||
src.f0 = 0;
|
||||
@ -618,6 +885,41 @@ public class TestDynamicCodeGenPackerConverter extends TestCase {
|
||||
assertFalse(it.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMessagePackMessageFieldClass01() throws Exception {
|
||||
BaseClass2 src = new BaseClass2();
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker.create(BaseClass2.class);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Unpacker pac = new Unpacker(in);
|
||||
Iterator<MessagePackObject> it = pac.iterator();
|
||||
assertTrue(it.hasNext());
|
||||
MessagePackObject mpo = it.next();
|
||||
Template tmpl = DynamicTemplate.create(BaseClass2.class);
|
||||
BaseClass2 dst = (BaseClass2) tmpl.convert(mpo);
|
||||
assertTrue(src.f0 == dst.f0);
|
||||
assertEquals(src.f1, dst.f1);
|
||||
assertFalse(it.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMessagePackMessageFieldClass02() throws Exception {
|
||||
BaseClass2 src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker.create(BaseClass2.class);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Unpacker pac = new Unpacker(in);
|
||||
Iterator<MessagePackObject> it = pac.iterator();
|
||||
assertTrue(it.hasNext());
|
||||
MessagePackObject mpo = it.next();
|
||||
Template tmpl = DynamicTemplate.create(BaseClass2.class);
|
||||
BaseClass2 dst = (BaseClass2) tmpl.convert(mpo);
|
||||
assertEquals(src, dst);
|
||||
assertFalse(it.hasNext());
|
||||
}
|
||||
|
||||
public static class BaseClass2 {
|
||||
public int f0;
|
||||
public MessagePackMessageClass2 f1;
|
||||
@ -635,7 +937,7 @@ public class TestDynamicCodeGenPackerConverter extends TestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExtendedClass() throws Exception {
|
||||
public void testExtendedClass00() throws Exception {
|
||||
SampleSubClass src = new SampleSubClass();
|
||||
src.f0 = 0;
|
||||
src.f2 = 2;
|
||||
@ -645,8 +947,7 @@ public class TestDynamicCodeGenPackerConverter extends TestCase {
|
||||
src.f8 = 8;
|
||||
src.f9 = 9;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker
|
||||
.create(SampleSubClass.class);
|
||||
MessagePacker packer = DynamicPacker.create(SampleSubClass.class);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate.create(SampleSubClass.class);
|
||||
@ -667,6 +968,23 @@ public class TestDynamicCodeGenPackerConverter extends TestCase {
|
||||
assertFalse(it.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExtendedClass01() throws Exception {
|
||||
SampleSubClass src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker.create(SampleSubClass.class);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate.create(SampleSubClass.class);
|
||||
Unpacker pac = new Unpacker(in);
|
||||
Iterator<MessagePackObject> it = pac.iterator();
|
||||
assertTrue(it.hasNext());
|
||||
MessagePackObject mpo = it.next();
|
||||
SampleSubClass dst = (SampleSubClass) tmpl.convert(mpo);
|
||||
assertEquals(src, dst);
|
||||
assertFalse(it.hasNext());
|
||||
}
|
||||
|
||||
public static class SampleSubClass extends SampleSuperClass {
|
||||
public int f0;
|
||||
public final int f1 = 1;
|
||||
|
@ -25,7 +25,7 @@ import junit.framework.TestCase;
|
||||
public class TestDynamicCodeGenPackerUnpacker extends TestCase {
|
||||
|
||||
@Test
|
||||
public void testPrimitiveTypeFields() throws Exception {
|
||||
public void testPrimitiveTypeFields00() throws Exception {
|
||||
PrimitiveTypeFieldsClass src = new PrimitiveTypeFieldsClass();
|
||||
src.f0 = (byte) 0;
|
||||
src.f1 = 1;
|
||||
@ -39,8 +39,7 @@ public class TestDynamicCodeGenPackerUnpacker extends TestCase {
|
||||
.create(PrimitiveTypeFieldsClass.class);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate
|
||||
.create(PrimitiveTypeFieldsClass.class);
|
||||
Template tmpl = DynamicTemplate.create(PrimitiveTypeFieldsClass.class);
|
||||
PrimitiveTypeFieldsClass dst = (PrimitiveTypeFieldsClass) tmpl
|
||||
.unpack(new Unpacker(in));
|
||||
assertEquals(src.f0, dst.f0);
|
||||
@ -52,6 +51,40 @@ public class TestDynamicCodeGenPackerUnpacker extends TestCase {
|
||||
assertEquals(src.f6, dst.f6);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrimitiveTypeFields01() throws Exception {
|
||||
PrimitiveTypeFieldsClass src = new PrimitiveTypeFieldsClass();
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker
|
||||
.create(PrimitiveTypeFieldsClass.class);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate.create(PrimitiveTypeFieldsClass.class);
|
||||
PrimitiveTypeFieldsClass dst = (PrimitiveTypeFieldsClass) tmpl
|
||||
.unpack(new Unpacker(in));
|
||||
assertEquals(src.f0, dst.f0);
|
||||
assertEquals(src.f1, dst.f1);
|
||||
assertEquals(src.f2, dst.f2);
|
||||
assertEquals(src.f3, dst.f3);
|
||||
assertEquals(src.f4, dst.f4);
|
||||
assertEquals(src.f5, dst.f5);
|
||||
assertEquals(src.f6, dst.f6);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrimitiveTypeFields02() throws Exception {
|
||||
PrimitiveTypeFieldsClass src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker
|
||||
.create(PrimitiveTypeFieldsClass.class);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate.create(PrimitiveTypeFieldsClass.class);
|
||||
PrimitiveTypeFieldsClass dst = (PrimitiveTypeFieldsClass) tmpl
|
||||
.unpack(new Unpacker(in));
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
public static class PrimitiveTypeFieldsClass {
|
||||
public byte f0;
|
||||
public short f1;
|
||||
@ -66,7 +99,7 @@ public class TestDynamicCodeGenPackerUnpacker extends TestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGeneralReferenceTypeFieldsClass() throws Exception {
|
||||
public void testGeneralReferenceTypeFieldsClass00() throws Exception {
|
||||
GeneralReferenceTypeFieldsClass src = new GeneralReferenceTypeFieldsClass();
|
||||
src.f0 = 0;
|
||||
src.f1 = 1;
|
||||
@ -100,6 +133,55 @@ public class TestDynamicCodeGenPackerUnpacker extends TestCase {
|
||||
assertEquals(src.f9[1], dst.f9[1]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGeneralReferenceTypeFieldsClass01() throws Exception {
|
||||
GeneralReferenceTypeFieldsClass src = new GeneralReferenceTypeFieldsClass();
|
||||
src.f0 = null;
|
||||
src.f1 = null;
|
||||
src.f2 = null;
|
||||
src.f3 = null;
|
||||
src.f4 = null;
|
||||
src.f5 = null;
|
||||
src.f6 = null;
|
||||
src.f7 = null;
|
||||
src.f8 = null;
|
||||
src.f9 = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker
|
||||
.create(GeneralReferenceTypeFieldsClass.class);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate
|
||||
.create(GeneralReferenceTypeFieldsClass.class);
|
||||
GeneralReferenceTypeFieldsClass dst = (GeneralReferenceTypeFieldsClass) tmpl
|
||||
.unpack(new Unpacker(in));
|
||||
assertEquals(src.f0, dst.f0);
|
||||
assertEquals(src.f1, dst.f1);
|
||||
assertEquals(src.f2, dst.f2);
|
||||
assertEquals(src.f3, dst.f3);
|
||||
assertEquals(src.f4, dst.f4);
|
||||
assertEquals(src.f5, dst.f5);
|
||||
assertEquals(src.f6, dst.f6);
|
||||
assertEquals(src.f7, dst.f7);
|
||||
assertEquals(src.f8, dst.f8);
|
||||
assertEquals(src.f9, dst.f9);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGeneralReferenceTypeFieldsClass02() throws Exception {
|
||||
GeneralReferenceTypeFieldsClass src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker
|
||||
.create(GeneralReferenceTypeFieldsClass.class);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate
|
||||
.create(GeneralReferenceTypeFieldsClass.class);
|
||||
GeneralReferenceTypeFieldsClass dst = (GeneralReferenceTypeFieldsClass) tmpl
|
||||
.unpack(new Unpacker(in));
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
public static class GeneralReferenceTypeFieldsClass {
|
||||
public Byte f0;
|
||||
public Short f1;
|
||||
@ -116,7 +198,8 @@ public class TestDynamicCodeGenPackerUnpacker extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
public void testListTypes() throws Exception {
|
||||
@Test
|
||||
public void testListTypes00() throws Exception {
|
||||
SampleListTypes src = new SampleListTypes();
|
||||
src.f0 = new ArrayList<Integer>();
|
||||
src.f1 = new ArrayList<Integer>();
|
||||
@ -135,8 +218,7 @@ public class TestDynamicCodeGenPackerUnpacker extends TestCase {
|
||||
slnt.f1 = "muga";
|
||||
src.f4.add(slnt);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker
|
||||
.create(SampleListTypes.class);
|
||||
MessagePacker packer = DynamicPacker.create(SampleListTypes.class);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate.create(SampleListTypes.class);
|
||||
@ -169,6 +251,39 @@ public class TestDynamicCodeGenPackerUnpacker extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListTypes01() throws Exception {
|
||||
SampleListTypes src = new SampleListTypes();
|
||||
src.f0 = new ArrayList<Integer>();
|
||||
src.f1 = null;
|
||||
src.f2 = new ArrayList<String>();
|
||||
src.f3 = new ArrayList<List<String>>();
|
||||
src.f4 = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker.create(SampleListTypes.class);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate.create(SampleListTypes.class);
|
||||
SampleListTypes dst = (SampleListTypes) tmpl.unpack(new Unpacker(in));
|
||||
assertEquals(src.f0.size(), dst.f0.size());
|
||||
assertEquals(src.f1, dst.f1);
|
||||
assertEquals(src.f2.size(), dst.f2.size());
|
||||
assertEquals(src.f3.size(), dst.f3.size());
|
||||
assertEquals(src.f4, dst.f4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListTypes02() throws Exception {
|
||||
SampleListTypes src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker.create(SampleListTypes.class);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate.create(SampleListTypes.class);
|
||||
SampleListTypes dst = (SampleListTypes) tmpl.unpack(new Unpacker(in));
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
public static class SampleListTypes {
|
||||
public List<Integer> f0;
|
||||
public List<Integer> f1;
|
||||
@ -189,7 +304,8 @@ public class TestDynamicCodeGenPackerUnpacker extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
public void testMapTypes() throws Exception {
|
||||
@Test
|
||||
public void testMapTypes00() throws Exception {
|
||||
SampleMapTypes src = new SampleMapTypes();
|
||||
src.f0 = new HashMap<Integer, Integer>();
|
||||
src.f1 = new HashMap<Integer, Integer>();
|
||||
@ -201,8 +317,7 @@ public class TestDynamicCodeGenPackerUnpacker extends TestCase {
|
||||
src.f2.put("k2", 2);
|
||||
src.f2.put("k3", 3);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker
|
||||
.create(SampleMapTypes.class);
|
||||
MessagePacker packer = DynamicPacker.create(SampleMapTypes.class);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate.create(SampleMapTypes.class);
|
||||
@ -228,6 +343,35 @@ public class TestDynamicCodeGenPackerUnpacker extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapTypes01() throws Exception {
|
||||
SampleMapTypes src = new SampleMapTypes();
|
||||
src.f0 = new HashMap<Integer, Integer>();
|
||||
src.f1 = null;
|
||||
src.f2 = new HashMap<String, Integer>();
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker.create(SampleMapTypes.class);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate.create(SampleMapTypes.class);
|
||||
SampleMapTypes dst = (SampleMapTypes) tmpl.unpack(new Unpacker(in));
|
||||
assertEquals(src.f0.size(), dst.f0.size());
|
||||
assertEquals(src.f1, dst.f1);
|
||||
assertEquals(src.f2.size(), dst.f2.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapTypes02() throws Exception {
|
||||
SampleMapTypes src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker.create(SampleMapTypes.class);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate.create(SampleMapTypes.class);
|
||||
SampleMapTypes dst = (SampleMapTypes) tmpl.unpack(new Unpacker(in));
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
public static class SampleMapTypes {
|
||||
public Map<Integer, Integer> f0;
|
||||
public Map<Integer, Integer> f1;
|
||||
@ -238,7 +382,7 @@ public class TestDynamicCodeGenPackerUnpacker extends TestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultConstructorModifiers01() throws Exception {
|
||||
public void testDefaultConstructorModifiers00() throws Exception {
|
||||
try {
|
||||
DynamicPacker.create(NoDefaultConstructorClass.class);
|
||||
fail();
|
||||
@ -270,7 +414,7 @@ public class TestDynamicCodeGenPackerUnpacker extends TestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultConstructorModifiers02() throws Exception {
|
||||
public void testDefaultConstructorModifiers01() throws Exception {
|
||||
try {
|
||||
DynamicUnpacker.create(NoDefaultConstructorClass.class);
|
||||
fail();
|
||||
@ -286,8 +430,7 @@ public class TestDynamicCodeGenPackerUnpacker extends TestCase {
|
||||
}
|
||||
assertTrue(true);
|
||||
try {
|
||||
DynamicUnpacker
|
||||
.create(ProtectedDefaultConstructorClass.class);
|
||||
DynamicUnpacker.create(ProtectedDefaultConstructorClass.class);
|
||||
fail();
|
||||
} catch (DynamicCodeGenException e) {
|
||||
assertTrue(true);
|
||||
@ -323,7 +466,7 @@ public class TestDynamicCodeGenPackerUnpacker extends TestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClassModifiers01() throws Exception {
|
||||
public void testClassModifiers00() throws Exception {
|
||||
try {
|
||||
DynamicPacker.create(PrivateModifierClass.class);
|
||||
fail();
|
||||
@ -348,7 +491,7 @@ public class TestDynamicCodeGenPackerUnpacker extends TestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClassModifiers02() throws Exception {
|
||||
public void testClassModifiers01() throws Exception {
|
||||
try {
|
||||
DynamicUnpacker.create(PrivateModifierClass.class);
|
||||
fail();
|
||||
@ -384,7 +527,7 @@ public class TestDynamicCodeGenPackerUnpacker extends TestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFinalClassAndAbstractClass01() throws Exception {
|
||||
public void testFinalClassAndAbstractClass00() throws Exception {
|
||||
try {
|
||||
DynamicPacker.create(FinalModifierClass.class);
|
||||
assertTrue(true);
|
||||
@ -402,7 +545,7 @@ public class TestDynamicCodeGenPackerUnpacker extends TestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFinalClassAndAbstractClass02() throws Exception {
|
||||
public void testFinalClassAndAbstractClass01() throws Exception {
|
||||
try {
|
||||
DynamicUnpacker.create(FinalModifierClass.class);
|
||||
assertTrue(true);
|
||||
@ -426,7 +569,7 @@ public class TestDynamicCodeGenPackerUnpacker extends TestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInterfaceType01() throws Exception {
|
||||
public void testInterfaceType00() throws Exception {
|
||||
try {
|
||||
DynamicPacker.create(SampleInterface.class);
|
||||
fail();
|
||||
@ -437,7 +580,7 @@ public class TestDynamicCodeGenPackerUnpacker extends TestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInterfaceType02() throws Exception {
|
||||
public void testInterfaceType01() throws Exception {
|
||||
try {
|
||||
DynamicUnpacker.create(SampleInterface.class);
|
||||
fail();
|
||||
@ -451,23 +594,49 @@ public class TestDynamicCodeGenPackerUnpacker extends TestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnumTypeForOrdinal() throws Exception {
|
||||
public void testEnumTypeForOrdinal00() throws Exception {
|
||||
SampleEnumFieldClass src = new SampleEnumFieldClass();
|
||||
src.f0 = 0;
|
||||
src.f1 = SampleEnum.ONE;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker
|
||||
.create(SampleEnumFieldClass.class);
|
||||
MessagePacker packer = DynamicPacker.create(SampleEnumFieldClass.class);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate
|
||||
.create(SampleEnumFieldClass.class);
|
||||
Template tmpl = DynamicTemplate.create(SampleEnumFieldClass.class);
|
||||
SampleEnumFieldClass dst = (SampleEnumFieldClass) tmpl
|
||||
.unpack(new Unpacker(in));
|
||||
assertTrue(src.f0 == dst.f0);
|
||||
assertTrue(src.f1 == dst.f1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnumTypeForOrdinal01() throws Exception {
|
||||
SampleEnumFieldClass src = new SampleEnumFieldClass();
|
||||
src.f1 = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker.create(SampleEnumFieldClass.class);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate.create(SampleEnumFieldClass.class);
|
||||
SampleEnumFieldClass dst = (SampleEnumFieldClass) tmpl
|
||||
.unpack(new Unpacker(in));
|
||||
assertTrue(src.f0 == dst.f0);
|
||||
assertEquals(src.f1, dst.f1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnumTypeForOrdinal02() throws Exception {
|
||||
SampleEnumFieldClass src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker.create(SampleEnumFieldClass.class);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate.create(SampleEnumFieldClass.class);
|
||||
SampleEnumFieldClass dst = (SampleEnumFieldClass) tmpl
|
||||
.unpack(new Unpacker(in));
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
public static class SampleEnumFieldClass {
|
||||
public int f0;
|
||||
|
||||
@ -490,12 +659,10 @@ public class TestDynamicCodeGenPackerUnpacker extends TestCase {
|
||||
src.f3 = 3;
|
||||
src.f4 = 4;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker
|
||||
.create(FieldModifiersClass.class);
|
||||
MessagePacker packer = DynamicPacker.create(FieldModifiersClass.class);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate
|
||||
.create(FieldModifiersClass.class);
|
||||
Template tmpl = DynamicTemplate.create(FieldModifiersClass.class);
|
||||
FieldModifiersClass dst = (FieldModifiersClass) tmpl
|
||||
.unpack(new Unpacker(in));
|
||||
assertTrue(src.f0 == dst.f0);
|
||||
@ -517,7 +684,7 @@ public class TestDynamicCodeGenPackerUnpacker extends TestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNestedFieldClass() throws Exception {
|
||||
public void testNestedFieldClass00() throws Exception {
|
||||
MessagePacker packer2 = DynamicPacker.create(NestedClass.class);
|
||||
CustomPacker.register(NestedClass.class, packer2);
|
||||
MessagePacker packer1 = DynamicPacker.create(BaseClass.class);
|
||||
@ -541,6 +708,48 @@ public class TestDynamicCodeGenPackerUnpacker extends TestCase {
|
||||
assertTrue(src.f1.f2 == dst.f1.f2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNestedFieldClass01() throws Exception {
|
||||
MessagePacker packer2 = DynamicPacker.create(NestedClass.class);
|
||||
CustomPacker.register(NestedClass.class, packer2);
|
||||
MessagePacker packer1 = DynamicPacker.create(BaseClass.class);
|
||||
CustomPacker.register(BaseClass.class, packer1);
|
||||
Template tmpl2 = DynamicTemplate.create(NestedClass.class);
|
||||
CustomUnpacker.register(NestedClass.class, tmpl2);
|
||||
CustomConverter.register(NestedClass.class, tmpl2);
|
||||
Template tmpl1 = DynamicTemplate.create(BaseClass.class);
|
||||
CustomUnpacker.register(BaseClass.class, tmpl1);
|
||||
CustomConverter.register(BaseClass.class, tmpl1);
|
||||
BaseClass src = new BaseClass();
|
||||
src.f1 = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
packer1.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
BaseClass dst = (BaseClass) tmpl1.unpack(new Unpacker(in));
|
||||
assertTrue(src.f0 == dst.f0);
|
||||
assertTrue(src.f1 == dst.f1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNestedFieldClass02() throws Exception {
|
||||
MessagePacker packer2 = DynamicPacker.create(NestedClass.class);
|
||||
CustomPacker.register(NestedClass.class, packer2);
|
||||
MessagePacker packer1 = DynamicPacker.create(BaseClass.class);
|
||||
CustomPacker.register(BaseClass.class, packer1);
|
||||
Template tmpl2 = DynamicTemplate.create(NestedClass.class);
|
||||
CustomUnpacker.register(NestedClass.class, tmpl2);
|
||||
CustomConverter.register(NestedClass.class, tmpl2);
|
||||
Template tmpl1 = DynamicTemplate.create(BaseClass.class);
|
||||
CustomUnpacker.register(BaseClass.class, tmpl1);
|
||||
CustomConverter.register(BaseClass.class, tmpl1);
|
||||
BaseClass src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
packer1.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
BaseClass dst = (BaseClass) tmpl1.unpack(new Unpacker(in));
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
public static class BaseClass {
|
||||
public int f0;
|
||||
public NestedClass f1;
|
||||
@ -557,7 +766,7 @@ public class TestDynamicCodeGenPackerUnpacker extends TestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMessagePackMessageFieldClass() throws Exception {
|
||||
public void testMessagePackMessageFieldClass00() throws Exception {
|
||||
BaseClass2 src = new BaseClass2();
|
||||
MessagePackMessageClass2 src2 = new MessagePackMessageClass2();
|
||||
src.f0 = 0;
|
||||
@ -573,6 +782,32 @@ public class TestDynamicCodeGenPackerUnpacker extends TestCase {
|
||||
assertTrue(src.f1.f2 == dst.f1.f2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMessagePackMessageFieldClass01() throws Exception {
|
||||
BaseClass2 src = new BaseClass2();
|
||||
src.f1 = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker.create(BaseClass2.class);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate.create(BaseClass2.class);
|
||||
BaseClass2 dst = (BaseClass2) tmpl.unpack(new Unpacker(in));
|
||||
assertTrue(src.f0 == dst.f0);
|
||||
assertEquals(src.f1, dst.f1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMessagePackMessageFieldClass02() throws Exception {
|
||||
BaseClass2 src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker.create(BaseClass2.class);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate.create(BaseClass2.class);
|
||||
BaseClass2 dst = (BaseClass2) tmpl.unpack(new Unpacker(in));
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
public static class BaseClass2 {
|
||||
public int f0;
|
||||
public MessagePackMessageClass2 f1;
|
||||
@ -590,7 +825,7 @@ public class TestDynamicCodeGenPackerUnpacker extends TestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExtendedClass() throws Exception {
|
||||
public void testExtendedClass00() throws Exception {
|
||||
SampleSubClass src = new SampleSubClass();
|
||||
src.f0 = 0;
|
||||
src.f2 = 2;
|
||||
@ -600,8 +835,7 @@ public class TestDynamicCodeGenPackerUnpacker extends TestCase {
|
||||
src.f8 = 8;
|
||||
src.f9 = 9;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker
|
||||
.create(SampleSubClass.class);
|
||||
MessagePacker packer = DynamicPacker.create(SampleSubClass.class);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate.create(SampleSubClass.class);
|
||||
@ -617,6 +851,18 @@ public class TestDynamicCodeGenPackerUnpacker extends TestCase {
|
||||
assertTrue(src.f9 != dst.f9);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExtendedClass01() throws Exception {
|
||||
SampleSubClass src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker.create(SampleSubClass.class);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate.create(SampleSubClass.class);
|
||||
SampleSubClass dst = (SampleSubClass) tmpl.unpack(new Unpacker(in));
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
public static class SampleSubClass extends SampleSuperClass {
|
||||
public int f0;
|
||||
public final int f1 = 1;
|
||||
|
Loading…
x
Reference in New Issue
Block a user