mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-03-21 06:11:18 +01:00
java: write test programs for OptionalTemplate.java
This commit is contained in:
parent
71ae75a5bf
commit
69e32d264c
@ -6,7 +6,6 @@ import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.CLASS)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface MessagePackOptional {
|
||||
int value() default -1;
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
package org.msgpack.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.CLASS)
|
||||
public @interface MessagePackRequired {
|
||||
int value() default -1;
|
||||
}
|
@ -42,6 +42,7 @@ public class MapTemplate implements Template {
|
||||
return map;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Object convert(MessagePackObject from) throws MessageTypeException {
|
||||
Map<MessagePackObject,MessagePackObject> src = from.asMap();
|
||||
Map<Object,Object> map = new HashMap();
|
||||
|
@ -17,8 +17,6 @@
|
||||
//
|
||||
package org.msgpack.template;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
|
@ -9,96 +9,20 @@ public interface Constants {
|
||||
|
||||
String POSTFIX_TYPE_NAME_TEMPLATE = "_$$_Template";
|
||||
|
||||
String KEYWORD_MODIFIER_PUBLIC = "public";
|
||||
String STRING_NAME_COMMA_SPACE = ", ";
|
||||
|
||||
String KEYWORD_CATCH = "catch";
|
||||
|
||||
String KEYWORD_ELSE = "else";
|
||||
|
||||
String KEYWORD_ELSEIF = "else if";
|
||||
|
||||
String KEYWORD_FOR = "for";
|
||||
|
||||
String KEYWORD_IF = "if";
|
||||
|
||||
String KEYWORD_INSTANCEOF = "instanceof";
|
||||
|
||||
String KEYWORD_NEW = "new";
|
||||
|
||||
String KEYWORD_NULL = "null";
|
||||
|
||||
String KEYWORD_RETURN = "return";
|
||||
|
||||
String KEYWORD_THROW = "throw";
|
||||
|
||||
String KEYWORD_THROWS = "throws";
|
||||
|
||||
String KEYWORD_TRY = "try";
|
||||
String STRING_NAME_LEFT_RIGHT_SQUARE_BRACKET = "[]";
|
||||
|
||||
String CHAR_NAME_SPACE = " ";
|
||||
|
||||
String CHAR_NAME_COMMA = ",";
|
||||
|
||||
String CHAR_NAME_EQUAL = "=";
|
||||
|
||||
String CHAR_NAME_PLUS = "+";
|
||||
|
||||
String CHAR_NAME_MINUS = "-";
|
||||
|
||||
String CHAR_NAME_UNDERSCORE = "_";
|
||||
|
||||
String CHAR_NAME_LESSTHAN = "<";
|
||||
|
||||
String CHAR_NAME_RIGHT_PARENTHESIS = ")";
|
||||
|
||||
String CHAR_NAME_LEFT_PARENTHESIS = "(";
|
||||
|
||||
String CHAR_NAME_RIGHT_CURLY_BRACKET = "}";
|
||||
|
||||
String CHAR_NAME_LEFT_CURLY_BRACKET = "{";
|
||||
|
||||
String CHAR_NAME_RIGHT_SQUARE_BRACKET = "]";
|
||||
|
||||
String CHAR_NAME_LEFT_SQUARE_BRACKET = "[";
|
||||
|
||||
String CHAR_NAME_DOT = ".";
|
||||
|
||||
String CHAR_NAME_SEMICOLON = ";";
|
||||
|
||||
String VARIABLE_NAME_PK = "_$$_pk";
|
||||
|
||||
String VARIABLE_NAME_OBJECT = "_$$_obj";
|
||||
|
||||
String VARIABLE_NAME_TARGET = "_$$_t";
|
||||
|
||||
String VARIABLE_NAME_SIZE = "_$$_len";
|
||||
|
||||
String VARIABLE_NAME_ARRAY = "_$$_ary";
|
||||
|
||||
String VARIABLE_NAME_LIST = "_$$_list";
|
||||
|
||||
String VARIABLE_NAME_MAP = "_$$_map";
|
||||
|
||||
String VARIABLE_NAME_KEY = "_$$_key";
|
||||
|
||||
String VARIABLE_NAME_VAL = "_$$_val";
|
||||
|
||||
String VARIABLE_NAME_ITER = "_$$_iter";
|
||||
|
||||
String VARIABLE_NAME_MPO = "_$$_mpo";
|
||||
|
||||
String VARIABLE_NAME_I = "i";
|
||||
|
||||
String VARIABLE_NAME_TEMPLATES = "_$$_templates";
|
||||
|
||||
String VARIABLE_NAME_TEMPLATES0 = "_$$_tmpls";
|
||||
|
||||
String VARIABLE_NAME_CLIENT = "_$$_client";
|
||||
|
||||
String VARIABLE_NAME_CLIENT0 = "_$$_c";
|
||||
|
||||
String METHOD_NAME_VALUEOF = "valueOf";
|
||||
|
||||
String METHOD_NAME_BOOLEANVALUE = "booleanValue";
|
||||
|
||||
String METHOD_NAME_BYTEVALUE = "byteValue";
|
||||
@ -113,38 +37,14 @@ public interface Constants {
|
||||
|
||||
String METHOD_NAME_DOUBLEVALUE = "doubleValue";
|
||||
|
||||
String METHOD_NAME_ADD = "add";
|
||||
|
||||
String METHOD_NAME_PUT = "put";
|
||||
|
||||
String METHOD_NAME_GET = "get";
|
||||
|
||||
String METHOD_NAME_SIZE = "size";
|
||||
|
||||
String METHOD_NAME_KEYSET = "keySet";
|
||||
|
||||
String METHOD_NAME_ITERATOR = "iterator";
|
||||
|
||||
String METHOD_NAME_HASNEXT = "hasNext";
|
||||
|
||||
String METHOD_NAME_NEXT = "next";
|
||||
|
||||
String METHOD_NAME_ORDINAL = "ordinal";
|
||||
|
||||
String METHOD_NAME_GETENUMCONSTANTS = "getEnumConstants";
|
||||
|
||||
String METHOD_NAME_CONVERT = "convert";
|
||||
|
||||
String METHOD_NAME_MSGCONVERT = "messageConvert";
|
||||
|
||||
String METHOD_NAME_SETTEMPLATES = "setTemplates";
|
||||
|
||||
String METHOD_NAME_SETCLIENT = "setClient";
|
||||
|
||||
String METHOD_NAME_PACK = "pack";
|
||||
|
||||
String METHOD_NAME_PACKARRAY = "packArray";
|
||||
|
||||
String METHOD_NAME_UNPACK = "unpack";
|
||||
|
||||
String METHOD_NAME_UNPACKBOOLEAN = "unpackBoolean";
|
||||
@ -207,13 +107,11 @@ public interface Constants {
|
||||
|
||||
String STATEMENT_PACKER_PACKERMETHODBODY_04 = "$1.pack(((java.lang.Enum)_$$_t).ordinal()); ";
|
||||
|
||||
String STATEMENT_PACKER_PACKERMETHODBODY_05 = "if (_$$_nullCheck && $2 == null) { $1.packNil(); return; } ";
|
||||
|
||||
String STATEMENT_TMPL_UNPACKERMETHODBODY_01 = "%s _$$_t = new %s(); ";
|
||||
|
||||
String STATEMENT_TMPL_UNPACKERMETHODBODY_02 = "$1.unpackArray(); ";
|
||||
|
||||
String STATEMENT_TMPL_UNPACKERMETHODBODY_03 = "if (!_$$_nullCheck || !$1.tryUnpackNull()) { _$$_t.%s = %s(%s)_$$_templates[%d].unpack($1)%s; } ";
|
||||
String STATEMENT_TMPL_UNPACKERMETHODBODY_03 = "_$$_t.%s = %s(%s)_$$_templates[%d].unpack($1)%s; ";
|
||||
|
||||
String STATEMENT_TMPL_UNPACKERMETHODBODY_04 = "return _$$_t; ";
|
||||
|
||||
@ -221,13 +119,9 @@ public interface Constants {
|
||||
|
||||
String STATEMENT_TMPL_UNPACKERMETHODBODY_06 = "return %s.class.getEnumConstants()[i]; ";
|
||||
|
||||
String STATEMENT_TMPL_UNPACKERMETHODBODY_07 = "if (_$$_nullCheck && $1.tryUnpackNull()) { return null; } ";
|
||||
|
||||
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_02 = "_$$_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; } ";
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package org.msgpack.util.codegen;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
@ -27,7 +28,9 @@ import org.msgpack.Template;
|
||||
import org.msgpack.Unpacker;
|
||||
import org.msgpack.annotation.MessagePackDelegate;
|
||||
import org.msgpack.annotation.MessagePackMessage;
|
||||
import org.msgpack.annotation.MessagePackOptional;
|
||||
import org.msgpack.annotation.MessagePackOrdinalEnum;
|
||||
import org.msgpack.template.OptionalTemplate;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@ -61,7 +64,8 @@ class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
||||
return tmplCache.get(type.getName());
|
||||
}
|
||||
|
||||
public Class<?> generateMessagePackerClass(Class<?> origClass) {
|
||||
public Class<?> generateMessagePackerClass(Class<?> origClass,
|
||||
List<FieldOption> fieldOpts) {
|
||||
try {
|
||||
LOG.debug("start generating a packer class for "
|
||||
+ origClass.getName());
|
||||
@ -70,10 +74,12 @@ 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);
|
||||
if (fieldOpts != null) {
|
||||
fields = sortFields(fields, fieldOpts);
|
||||
}
|
||||
addPackMethod(packerCtClass, origClass, fields, false);
|
||||
Class<?> packerClass = createClass(packerCtClass);
|
||||
LOG.debug("generated a packer class for " + origClass.getName());
|
||||
@ -95,7 +101,6 @@ 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);
|
||||
@ -111,7 +116,8 @@ class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
||||
}
|
||||
}
|
||||
|
||||
public Class<?> generateTemplateClass(Class<?> origClass) {
|
||||
public Class<?> generateTemplateClass(Class<?> origClass,
|
||||
List<FieldOption> fieldOpts) {
|
||||
try {
|
||||
LOG.debug("start generating a template class for "
|
||||
+ origClass.getName());
|
||||
@ -120,11 +126,17 @@ class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
||||
checkTypeValidation(origClass);
|
||||
checkDefaultConstructorValidation(origClass);
|
||||
CtClass tmplCtClass = pool.makeClass(tmplName);
|
||||
setSuperclass(tmplCtClass, TemplateTemplate.class);
|
||||
setSuperclass(tmplCtClass, TemplateAccessorImpl.class);
|
||||
setInterface(tmplCtClass, Template.class);
|
||||
addDefaultConstructor(tmplCtClass);
|
||||
Field[] fields = getDeclaredFields(origClass);
|
||||
Template[] tmpls = createTemplates(fields);
|
||||
Template[] tmpls = null;
|
||||
if (fieldOpts != null) {
|
||||
fields = sortFields(fields, fieldOpts);
|
||||
tmpls = createTemplates(fieldOpts);
|
||||
} else {
|
||||
tmpls = createTemplates(fields);
|
||||
}
|
||||
setTemplates(origClass, tmpls);
|
||||
addUnpackMethod(tmplCtClass, origClass, fields, false);
|
||||
addConvertMethod(tmplCtClass, origClass, fields, false);
|
||||
@ -152,7 +164,7 @@ 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);
|
||||
setSuperclass(tmplCtClass, TemplateAccessorImpl.class);
|
||||
setInterface(tmplCtClass, Template.class);
|
||||
addDefaultConstructor(tmplCtClass);
|
||||
addUnpackMethod(tmplCtClass, origClass, null, true);
|
||||
@ -240,6 +252,41 @@ class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
||||
}
|
||||
}
|
||||
|
||||
Field[] sortFields(Field[] fields, List<FieldOption> fieldOpts) {
|
||||
if (fields.length != fieldOpts.size()) {
|
||||
throwFieldSortingException(String.format(
|
||||
"Mismatch: public field num: %d, option num: %d",
|
||||
new Object[] { fields.length, fieldOpts.size() }));
|
||||
}
|
||||
Field[] sorted = new Field[fields.length];
|
||||
for (int i = 0; i < sorted.length; ++i) {
|
||||
FieldOption opt = fieldOpts.get(i);
|
||||
Field match = null;
|
||||
for (Field f : fields) {
|
||||
if (opt.name.equals(f.getName())) {
|
||||
match = f;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (match != null) {
|
||||
sorted[i] = match;
|
||||
} else {
|
||||
throwFieldSortingException(String.format(
|
||||
"Mismatch: a %s field option is not declared",
|
||||
new Object[] { opt.name }));
|
||||
}
|
||||
}
|
||||
return sorted;
|
||||
}
|
||||
|
||||
Template[] createTemplates(List<FieldOption> fieldOpts) {
|
||||
Template[] tmpls = new Template[fieldOpts.size()];
|
||||
for (int i = 0; i < tmpls.length; ++i) {
|
||||
tmpls[i] = fieldOpts.get(i).tmpl;
|
||||
}
|
||||
return tmpls;
|
||||
}
|
||||
|
||||
Template[] createTemplates(Field[] fields) {
|
||||
Template[] tmpls = new Template[fields.length];
|
||||
for (int i = 0; i < tmpls.length; ++i) {
|
||||
@ -249,12 +296,23 @@ class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
||||
}
|
||||
|
||||
Template createTemplate(Field field) {
|
||||
boolean isOptional = isAnnotated(field, MessagePackOptional.class);
|
||||
Class<?> c = field.getType();
|
||||
Template tmpl = null;
|
||||
if (List.class.isAssignableFrom(c) || Map.class.isAssignableFrom(c)) {
|
||||
return createTemplate(field.getGenericType());
|
||||
tmpl = createTemplate(field.getGenericType());
|
||||
} else {
|
||||
return createTemplate(c);
|
||||
tmpl = createTemplate(c);
|
||||
}
|
||||
if (isOptional) {
|
||||
return new OptionalTemplate(tmpl);
|
||||
} else {
|
||||
return tmpl;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isAnnotated(Field field, Class<? extends Annotation> with) {
|
||||
return field.getAnnotation(with) != null;
|
||||
}
|
||||
|
||||
private void addPackMethod(CtClass packerCtClass, Class<?> c, Field[] fs,
|
||||
@ -297,8 +355,6 @@ 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,8 +413,6 @@ 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));
|
||||
@ -412,8 +466,6 @@ 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_TMPL_UNPACKERMETHODBODY_01, args0));
|
||||
// $1.unpackArray();
|
||||
@ -453,8 +505,6 @@ 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_TMPL_UNPACKERMETHODBODY_02, args0));
|
||||
@ -506,8 +556,6 @@ 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 };
|
||||
@ -533,7 +581,6 @@ 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()
|
||||
@ -550,8 +597,6 @@ 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_TMPL_CONVERTMETHODBODY_01, args0));
|
||||
|
@ -41,10 +41,6 @@ public class DynamicCodeGenBase implements Constants {
|
||||
private static Logger LOG = LoggerFactory
|
||||
.getLogger(DynamicCodeGenBase.class);
|
||||
|
||||
public static interface NullChecker {
|
||||
void setNullCheck(boolean nullCheck);
|
||||
}
|
||||
|
||||
public static class MessageUnpackableConvertableTemplate implements
|
||||
Template {
|
||||
|
||||
@ -86,23 +82,13 @@ public class DynamicCodeGenBase implements Constants {
|
||||
throw new MessageTypeException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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 TemplateTemplate extends NullCheckerImpl implements
|
||||
TemplateAccessor {
|
||||
public static class TemplateAccessorImpl implements TemplateAccessor {
|
||||
public Template[] _$$_templates;
|
||||
|
||||
public void setTemplates(Template[] _$$_tmpls) {
|
||||
@ -160,6 +146,12 @@ public class DynamicCodeGenBase implements Constants {
|
||||
throw e;
|
||||
}
|
||||
|
||||
protected void throwFieldSortingException(String message) {
|
||||
DynamicCodeGenException e = new DynamicCodeGenException(message);
|
||||
LOG.debug(e.getMessage(), e);
|
||||
throw e;
|
||||
}
|
||||
|
||||
protected static void throwMethodValidationException(Method method,
|
||||
String message) throws DynamicCodeGenException {
|
||||
DynamicCodeGenException e = new DynamicCodeGenException(String.format(
|
||||
@ -207,7 +199,7 @@ public class DynamicCodeGenBase implements Constants {
|
||||
|
||||
protected void addTemplateArrayField(CtClass newCtClass)
|
||||
throws NotFoundException, CannotCompileException {
|
||||
CtClass acsCtClass = pool.get(TemplateTemplate.class.getName());
|
||||
CtClass acsCtClass = pool.get(TemplateAccessorImpl.class.getName());
|
||||
CtField tmplsField = acsCtClass
|
||||
.getDeclaredField(VARIABLE_NAME_TEMPLATES);
|
||||
CtField tmplsField2 = new CtField(tmplsField.getType(), tmplsField
|
||||
@ -217,7 +209,7 @@ public class DynamicCodeGenBase implements Constants {
|
||||
|
||||
protected void addSetTemplatesMethod(CtClass newCtClass)
|
||||
throws NotFoundException, CannotCompileException {
|
||||
CtClass acsCtClass = pool.get(TemplateTemplate.class.getName());
|
||||
CtClass acsCtClass = pool.get(TemplateAccessorImpl.class.getName());
|
||||
CtMethod settmplsMethod = acsCtClass
|
||||
.getDeclaredMethod(METHOD_NAME_SETTEMPLATES);
|
||||
CtMethod settmplsMethod2 = CtNewMethod.copy(settmplsMethod, newCtClass,
|
||||
@ -425,8 +417,7 @@ public class DynamicCodeGenBase implements Constants {
|
||||
Class<?> t = getArrayBaseType(type);
|
||||
sb.append(t.getName());
|
||||
for (int i = 0; i < dim; ++i) {
|
||||
sb.append(CHAR_NAME_LEFT_SQUARE_BRACKET);
|
||||
sb.append(CHAR_NAME_RIGHT_SQUARE_BRACKET);
|
||||
sb.append(STRING_NAME_LEFT_RIGHT_SQUARE_BRACKET);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package org.msgpack.util.codegen;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class DynamicCodeGenException extends RuntimeException {
|
||||
|
||||
public DynamicCodeGenException(String reason) {
|
||||
|
@ -1,9 +1,16 @@
|
||||
package org.msgpack.util.codegen;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.msgpack.MessageConverter;
|
||||
|
||||
public class DynamicConverter {
|
||||
public static MessageConverter create(Class<?> c) {
|
||||
return DynamicTemplate.create(c);
|
||||
return create(c, null);
|
||||
}
|
||||
|
||||
public static MessageConverter create(Class<?> c,
|
||||
List<FieldOption> fieldOpts) {
|
||||
return DynamicTemplate.create(c, fieldOpts);
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +1,19 @@
|
||||
package org.msgpack.util.codegen;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.msgpack.MessagePacker;
|
||||
|
||||
public class DynamicPacker {
|
||||
|
||||
public static MessagePacker create(Class<?> c) {
|
||||
return create(c, null);
|
||||
}
|
||||
|
||||
public static MessagePacker create(Class<?> c, List<FieldOption> fieldOpts) {
|
||||
try {
|
||||
DynamicCodeGen gen = DynamicCodeGen.getInstance();
|
||||
Class<?> packerClass = gen.generateMessagePackerClass(c);
|
||||
Class<?> packerClass = gen.generateMessagePackerClass(c, fieldOpts);
|
||||
return (MessagePacker) packerClass.newInstance();
|
||||
} catch (InstantiationException e) {
|
||||
throw new DynamicCodeGenException(e.getMessage(), e);
|
||||
|
@ -1,13 +1,19 @@
|
||||
package org.msgpack.util.codegen;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.msgpack.Template;
|
||||
import org.msgpack.util.codegen.DynamicCodeGenBase.TemplateAccessor;
|
||||
|
||||
public class DynamicTemplate {
|
||||
public static Template create(Class<?> c) {
|
||||
return create(c, null);
|
||||
}
|
||||
|
||||
public static Template create(Class<?> c, List<FieldOption> fieldOpts) {
|
||||
try {
|
||||
DynamicCodeGen gen = DynamicCodeGen.getInstance();
|
||||
Class<?> tmplClass = gen.generateTemplateClass(c);
|
||||
Class<?> tmplClass = gen.generateTemplateClass(c, fieldOpts);
|
||||
Object obj = tmplClass.newInstance();
|
||||
((TemplateAccessor) obj).setTemplates(gen.getTemplates(c));
|
||||
return (Template) obj;
|
||||
|
@ -1,9 +1,15 @@
|
||||
package org.msgpack.util.codegen;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.msgpack.MessageUnpacker;
|
||||
|
||||
public class DynamicUnpacker {
|
||||
public static MessageUnpacker create(Class<?> c) {
|
||||
return DynamicTemplate.create(c);
|
||||
return create(c, null);
|
||||
}
|
||||
|
||||
public static MessageUnpacker create(Class<?> c, List<FieldOption> fieldOpts) {
|
||||
return DynamicTemplate.create(c, fieldOpts);
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,22 @@ import org.msgpack.Template;
|
||||
|
||||
public class FieldOption {
|
||||
|
||||
public String name;
|
||||
|
||||
public Template tmpl;
|
||||
private static final String NULL_ERR_MSG = "param is FieldOption is null.";
|
||||
|
||||
String name;
|
||||
|
||||
Template tmpl;
|
||||
|
||||
public FieldOption(final String name, final Template tmpl) {
|
||||
if (name == null) {
|
||||
throw new NullPointerException(String.format("%s %s", new Object[] {
|
||||
"1st", NULL_ERR_MSG }));
|
||||
}
|
||||
if (tmpl == null) {
|
||||
throw new NullPointerException(String.format("%s %s", new Object[] {
|
||||
"2nd", NULL_ERR_MSG }));
|
||||
}
|
||||
this.name = name;
|
||||
this.tmpl = tmpl;
|
||||
}
|
||||
}
|
||||
|
@ -9,13 +9,111 @@ import junit.framework.TestCase;
|
||||
import org.junit.Test;
|
||||
import org.msgpack.MessagePackObject;
|
||||
import org.msgpack.MessagePacker;
|
||||
import org.msgpack.MessageTypeException;
|
||||
import org.msgpack.Packer;
|
||||
import org.msgpack.Template;
|
||||
import org.msgpack.Util;
|
||||
import org.msgpack.template.BigIntegerTemplate;
|
||||
import org.msgpack.template.BooleanTemplate;
|
||||
import org.msgpack.template.ByteTemplate;
|
||||
import org.msgpack.template.DoubleTemplate;
|
||||
import org.msgpack.template.FloatTemplate;
|
||||
import org.msgpack.template.IntegerTemplate;
|
||||
import org.msgpack.template.LongTemplate;
|
||||
import org.msgpack.template.OptionalTemplate;
|
||||
import org.msgpack.template.ShortTemplate;
|
||||
import org.msgpack.template.StringTemplate;
|
||||
|
||||
public class TestPackConvert extends TestCase {
|
||||
|
||||
@Test
|
||||
public void testByte() throws Exception {
|
||||
_testByte((byte) 0);
|
||||
_testByte((byte) -1);
|
||||
_testByte((byte) 1);
|
||||
_testByte(Byte.MIN_VALUE);
|
||||
_testByte(Byte.MAX_VALUE);
|
||||
Random rand = new Random();
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
_testByte((byte) rand.nextInt());
|
||||
}
|
||||
}
|
||||
|
||||
static void _testByte(Byte src) throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = BytePacker.getInstance();
|
||||
packer.pack(new Packer(out), src);
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
assertEquals(src.byteValue(), obj.asByte());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullByte() throws Exception {
|
||||
Byte src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(BytePacker.getInstance());
|
||||
packer.pack(new Packer(out), src);
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
Template tmpl = null;
|
||||
Byte dst = null;
|
||||
try {
|
||||
tmpl = ByteTemplate.getInstance();
|
||||
dst = (Byte) tmpl.convert(obj);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
obj = Util.unpackOne(out.toByteArray());
|
||||
tmpl = new OptionalTemplate(ByteTemplate.getInstance());
|
||||
dst = (Byte) tmpl.convert(obj);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShort() throws Exception {
|
||||
_testShort((short) 0);
|
||||
_testShort((short) -1);
|
||||
_testShort((short) 1);
|
||||
_testShort(Short.MIN_VALUE);
|
||||
_testShort(Short.MAX_VALUE);
|
||||
Random rand = new Random();
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
_testShort((short) rand.nextInt());
|
||||
}
|
||||
}
|
||||
|
||||
static void _testShort(Short src) throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = ShortPacker.getInstance();
|
||||
packer.pack(new Packer(out), src);
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
assertEquals(src.shortValue(), obj.asShort());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullShort() throws Exception {
|
||||
Short src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(ShortPacker.getInstance());
|
||||
packer.pack(new Packer(out), src);
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
Template tmpl = null;
|
||||
Short dst = null;
|
||||
try {
|
||||
tmpl = ShortTemplate.getInstance();
|
||||
dst = (Short) tmpl.convert(obj);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
obj = Util.unpackOne(out.toByteArray());
|
||||
tmpl = new OptionalTemplate(ShortTemplate.getInstance());
|
||||
dst = (Short) tmpl.convert(obj);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInteger() throws Exception {
|
||||
// _testInteger(null); // FIXME
|
||||
_testInteger(0);
|
||||
_testInteger(-1);
|
||||
_testInteger(1);
|
||||
@ -35,9 +133,30 @@ public class TestPackConvert extends TestCase {
|
||||
assertEquals(src.intValue(), obj.asInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullInteger() throws Exception {
|
||||
Integer src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(IntegerPacker.getInstance());
|
||||
packer.pack(new Packer(out), src);
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
Template tmpl = null;
|
||||
Integer dst = null;
|
||||
try {
|
||||
tmpl = IntegerTemplate.getInstance();
|
||||
dst = (Integer) tmpl.convert(obj);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
obj = Util.unpackOne(out.toByteArray());
|
||||
tmpl = new OptionalTemplate(IntegerTemplate.getInstance());
|
||||
dst = (Integer) tmpl.convert(obj);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLong() throws Exception {
|
||||
// _testLong((null); // FIXME
|
||||
_testLong((long) 0);
|
||||
_testLong((long) -1);
|
||||
_testLong((long) 1);
|
||||
@ -59,9 +178,30 @@ public class TestPackConvert extends TestCase {
|
||||
assertEquals(src.longValue(), obj.asLong());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullLong() throws Exception {
|
||||
Long src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(LongPacker.getInstance());
|
||||
packer.pack(new Packer(out), src);
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
Template tmpl = null;
|
||||
Long dst = null;
|
||||
try {
|
||||
tmpl = LongTemplate.getInstance();
|
||||
dst = (Long) tmpl.convert(obj);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
obj = Util.unpackOne(out.toByteArray());
|
||||
tmpl = new OptionalTemplate(LongTemplate.getInstance());
|
||||
dst = (Long) tmpl.convert(obj);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBigInteger() throws Exception {
|
||||
// _testBigInteger(null); // FIXME
|
||||
_testBigInteger(BigInteger.valueOf(0));
|
||||
_testBigInteger(BigInteger.valueOf(-1));
|
||||
_testBigInteger(BigInteger.valueOf(1));
|
||||
@ -86,9 +226,31 @@ public class TestPackConvert extends TestCase {
|
||||
assertEquals(src, obj.asBigInteger());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullBigInteger() throws Exception {
|
||||
Long src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(BigIntegerPacker
|
||||
.getInstance());
|
||||
packer.pack(new Packer(out), src);
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
Template tmpl = null;
|
||||
BigInteger dst = null;
|
||||
try {
|
||||
tmpl = BigIntegerTemplate.getInstance();
|
||||
dst = (BigInteger) tmpl.convert(obj);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
obj = Util.unpackOne(out.toByteArray());
|
||||
tmpl = new OptionalTemplate(BigIntegerTemplate.getInstance());
|
||||
dst = (BigInteger) tmpl.convert(obj);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFloat() throws Exception {
|
||||
// _testFloat(null); // FIXME
|
||||
_testFloat((float) 0.0);
|
||||
_testFloat((float) -0.0);
|
||||
_testFloat((float) 1.0);
|
||||
@ -112,9 +274,30 @@ public class TestPackConvert extends TestCase {
|
||||
assertEquals(src.floatValue(), obj.asFloat(), 10e-10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullFloat() throws Exception {
|
||||
Long src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(FloatPacker.getInstance());
|
||||
packer.pack(new Packer(out), src);
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
Template tmpl = null;
|
||||
Float dst = null;
|
||||
try {
|
||||
tmpl = FloatTemplate.getInstance();
|
||||
dst = (Float) tmpl.convert(obj);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
obj = Util.unpackOne(out.toByteArray());
|
||||
tmpl = new OptionalTemplate(FloatTemplate.getInstance());
|
||||
dst = (Float) tmpl.convert(obj);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDouble() throws Exception {
|
||||
// _testDouble(null); // FIXME
|
||||
_testDouble((double) 0.0);
|
||||
_testDouble((double) -0.0);
|
||||
_testDouble((double) 1.0);
|
||||
@ -137,9 +320,30 @@ public class TestPackConvert extends TestCase {
|
||||
assertEquals(src.doubleValue(), obj.asDouble(), 10e-10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullDouble() throws Exception {
|
||||
Long src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(DoublePacker.getInstance());
|
||||
packer.pack(new Packer(out), src);
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
Template tmpl = null;
|
||||
Double dst = null;
|
||||
try {
|
||||
tmpl = DoubleTemplate.getInstance();
|
||||
dst = (Double) tmpl.convert(obj);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
obj = Util.unpackOne(out.toByteArray());
|
||||
tmpl = new OptionalTemplate(DoubleTemplate.getInstance());
|
||||
dst = (Double) tmpl.convert(obj);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBoolean() throws Exception {
|
||||
// _testBoolean(null); // FIXME
|
||||
_testBoolean(false);
|
||||
_testBoolean(true);
|
||||
}
|
||||
@ -152,9 +356,30 @@ public class TestPackConvert extends TestCase {
|
||||
assertEquals(src.booleanValue(), obj.asBoolean());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullBoolean() throws Exception {
|
||||
Long src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(BooleanPacker.getInstance());
|
||||
packer.pack(new Packer(out), src);
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
Template tmpl = null;
|
||||
Boolean dst = null;
|
||||
try {
|
||||
tmpl = BooleanTemplate.getInstance();
|
||||
dst = (Boolean) tmpl.convert(obj);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
obj = Util.unpackOne(out.toByteArray());
|
||||
tmpl = new OptionalTemplate(BooleanTemplate.getInstance());
|
||||
dst = (Boolean) tmpl.convert(obj);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testString() throws Exception {
|
||||
// _testString(null); // FIXME
|
||||
_testString("");
|
||||
_testString("a");
|
||||
_testString("ab");
|
||||
@ -198,4 +423,26 @@ public class TestPackConvert extends TestCase {
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
assertEquals(src, obj.asString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullString() throws Exception {
|
||||
String src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(StringPacker.getInstance());
|
||||
packer.pack(new Packer(out), src);
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
Template tmpl = null;
|
||||
String dst = null;
|
||||
try {
|
||||
tmpl = StringTemplate.getInstance();
|
||||
dst = (String) tmpl.convert(obj);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
obj = Util.unpackOne(out.toByteArray());
|
||||
tmpl = new OptionalTemplate(StringTemplate.getInstance());
|
||||
dst = (String) tmpl.convert(obj);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
}
|
||||
|
@ -9,13 +9,117 @@ import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.msgpack.MessagePacker;
|
||||
import org.msgpack.MessageTypeException;
|
||||
import org.msgpack.Packer;
|
||||
import org.msgpack.Template;
|
||||
import org.msgpack.Unpacker;
|
||||
import org.msgpack.template.BigIntegerTemplate;
|
||||
import org.msgpack.template.BooleanTemplate;
|
||||
import org.msgpack.template.ByteTemplate;
|
||||
import org.msgpack.template.DoubleTemplate;
|
||||
import org.msgpack.template.FloatTemplate;
|
||||
import org.msgpack.template.IntegerTemplate;
|
||||
import org.msgpack.template.LongTemplate;
|
||||
import org.msgpack.template.OptionalTemplate;
|
||||
import org.msgpack.template.ShortTemplate;
|
||||
import org.msgpack.template.StringTemplate;
|
||||
|
||||
public class TestPackUnpack extends TestCase {
|
||||
|
||||
@Test
|
||||
public void testByte() throws Exception {
|
||||
_testByte((byte) 0);
|
||||
_testByte((byte) -1);
|
||||
_testByte((byte) 1);
|
||||
_testByte(Byte.MIN_VALUE);
|
||||
_testByte(Byte.MAX_VALUE);
|
||||
Random rand = new Random();
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
_testByte((byte) rand.nextInt());
|
||||
}
|
||||
}
|
||||
|
||||
static void _testByte(Byte src) throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = BytePacker.getInstance();
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Unpacker unpacker = new Unpacker(in);
|
||||
assertEquals(src.byteValue(), unpacker.unpackByte());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullByte() throws Exception {
|
||||
Byte src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(BytePacker.getInstance());
|
||||
packer.pack(new Packer(out), src);
|
||||
byte[] bytes = out.toByteArray();
|
||||
Template tmpl = null;
|
||||
Unpacker unpacker = new Unpacker();
|
||||
Byte dst = null;
|
||||
try {
|
||||
tmpl = ByteTemplate.getInstance();
|
||||
unpacker.wrap(bytes);
|
||||
dst = (Byte) tmpl.unpack(unpacker);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
unpacker.wrap(bytes);
|
||||
tmpl = new OptionalTemplate(ByteTemplate.getInstance());
|
||||
dst = (Byte) tmpl.unpack(unpacker);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSort() throws Exception {
|
||||
_testShort((short) 0);
|
||||
_testShort((short) -1);
|
||||
_testShort((short) 1);
|
||||
_testShort(Short.MIN_VALUE);
|
||||
_testShort(Short.MAX_VALUE);
|
||||
Random rand = new Random();
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
_testShort((short) rand.nextInt());
|
||||
}
|
||||
}
|
||||
|
||||
static void _testShort(Short src) throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = ShortPacker.getInstance();
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Unpacker unpacker = new Unpacker(in);
|
||||
assertEquals(src.shortValue(), unpacker.unpackShort());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullShort() throws Exception {
|
||||
Short src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(ShortPacker.getInstance());
|
||||
packer.pack(new Packer(out), src);
|
||||
byte[] bytes = out.toByteArray();
|
||||
Template tmpl = null;
|
||||
Unpacker unpacker = new Unpacker();
|
||||
Short dst = null;
|
||||
try {
|
||||
tmpl = ShortTemplate.getInstance();
|
||||
unpacker.wrap(bytes);
|
||||
dst = (Short) tmpl.unpack(unpacker);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
unpacker.wrap(bytes);
|
||||
tmpl = new OptionalTemplate(ShortTemplate.getInstance());
|
||||
dst = (Short) tmpl.unpack(unpacker);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInteger() throws Exception {
|
||||
// _testInteger(null); // FIXME
|
||||
_testInteger(0);
|
||||
_testInteger(-1);
|
||||
_testInteger(1);
|
||||
@ -36,9 +140,32 @@ public class TestPackUnpack extends TestCase {
|
||||
assertEquals(src.intValue(), unpacker.unpackInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullInteger() throws Exception {
|
||||
Integer src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(IntegerPacker.getInstance());
|
||||
packer.pack(new Packer(out), src);
|
||||
byte[] bytes = out.toByteArray();
|
||||
Template tmpl = null;
|
||||
Unpacker unpacker = new Unpacker();
|
||||
Integer dst = null;
|
||||
try {
|
||||
tmpl = IntegerTemplate.getInstance();
|
||||
unpacker.wrap(bytes);
|
||||
dst = (Integer) tmpl.unpack(unpacker);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
unpacker.wrap(bytes);
|
||||
tmpl = new OptionalTemplate(IntegerTemplate.getInstance());
|
||||
dst = (Integer) tmpl.unpack(unpacker);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLong() throws Exception {
|
||||
// _testLong((null); // FIXME
|
||||
_testLong((long) 0);
|
||||
_testLong((long) -1);
|
||||
_testLong((long) 1);
|
||||
@ -61,9 +188,32 @@ public class TestPackUnpack extends TestCase {
|
||||
assertEquals(src.longValue(), unpacker.unpackLong());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullLong() throws Exception {
|
||||
Integer src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(LongPacker.getInstance());
|
||||
packer.pack(new Packer(out), src);
|
||||
byte[] bytes = out.toByteArray();
|
||||
Template tmpl = null;
|
||||
Unpacker unpacker = new Unpacker();
|
||||
Long dst = null;
|
||||
try {
|
||||
tmpl = LongTemplate.getInstance();
|
||||
unpacker.wrap(bytes);
|
||||
dst = (Long) tmpl.unpack(unpacker);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
unpacker.wrap(bytes);
|
||||
tmpl = new OptionalTemplate(LongTemplate.getInstance());
|
||||
dst = (Long) tmpl.unpack(unpacker);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBigInteger() throws Exception {
|
||||
// _testBigInteger(null); // FIXME
|
||||
_testBigInteger(BigInteger.valueOf(0));
|
||||
_testBigInteger(BigInteger.valueOf(-1));
|
||||
_testBigInteger(BigInteger.valueOf(1));
|
||||
@ -89,9 +239,33 @@ public class TestPackUnpack extends TestCase {
|
||||
assertEquals(src, unpacker.unpackBigInteger());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullBigInteger() throws Exception {
|
||||
BigInteger src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(BigIntegerPacker
|
||||
.getInstance());
|
||||
packer.pack(new Packer(out), src);
|
||||
byte[] bytes = out.toByteArray();
|
||||
Template tmpl = null;
|
||||
Unpacker unpacker = new Unpacker();
|
||||
BigInteger dst = null;
|
||||
try {
|
||||
tmpl = BigIntegerTemplate.getInstance();
|
||||
unpacker.wrap(bytes);
|
||||
dst = (BigInteger) tmpl.unpack(unpacker);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
unpacker.wrap(bytes);
|
||||
tmpl = new OptionalTemplate(BigIntegerTemplate.getInstance());
|
||||
dst = (BigInteger) tmpl.unpack(unpacker);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFloat() throws Exception {
|
||||
// _testFloat(null); // FIXME
|
||||
_testFloat((float) 0.0);
|
||||
_testFloat((float) -0.0);
|
||||
_testFloat((float) 1.0);
|
||||
@ -116,9 +290,32 @@ public class TestPackUnpack extends TestCase {
|
||||
assertEquals(src.floatValue(), unpacker.unpackFloat(), 10e-10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullFloat() throws Exception {
|
||||
Float src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(FloatPacker.getInstance());
|
||||
packer.pack(new Packer(out), src);
|
||||
byte[] bytes = out.toByteArray();
|
||||
Template tmpl = null;
|
||||
Unpacker unpacker = new Unpacker();
|
||||
Float dst = null;
|
||||
try {
|
||||
tmpl = FloatTemplate.getInstance();
|
||||
unpacker.wrap(bytes);
|
||||
dst = (Float) tmpl.unpack(unpacker);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
unpacker.wrap(bytes);
|
||||
tmpl = new OptionalTemplate(FloatTemplate.getInstance());
|
||||
dst = (Float) tmpl.unpack(unpacker);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDouble() throws Exception {
|
||||
// _testDouble(null); // FIXME
|
||||
_testDouble((double) 0.0);
|
||||
_testDouble((double) -0.0);
|
||||
_testDouble((double) 1.0);
|
||||
@ -142,9 +339,32 @@ public class TestPackUnpack extends TestCase {
|
||||
assertEquals(src.doubleValue(), unpacker.unpackDouble(), 10e-10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullDouble() throws Exception {
|
||||
Double src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(DoublePacker.getInstance());
|
||||
packer.pack(new Packer(out), src);
|
||||
byte[] bytes = out.toByteArray();
|
||||
Template tmpl = null;
|
||||
Unpacker unpacker = new Unpacker();
|
||||
Double dst = null;
|
||||
try {
|
||||
tmpl = DoubleTemplate.getInstance();
|
||||
unpacker.wrap(bytes);
|
||||
dst = (Double) tmpl.unpack(unpacker);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
unpacker.wrap(bytes);
|
||||
tmpl = new OptionalTemplate(DoubleTemplate.getInstance());
|
||||
dst = (Double) tmpl.unpack(unpacker);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBoolean() throws Exception {
|
||||
// _testBoolean(null); // FIXME
|
||||
_testBoolean(false);
|
||||
_testBoolean(true);
|
||||
}
|
||||
@ -158,9 +378,32 @@ public class TestPackUnpack extends TestCase {
|
||||
assertEquals(src.booleanValue(), unpacker.unpackBoolean());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullBoolean() throws Exception {
|
||||
Boolean src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(BooleanPacker.getInstance());
|
||||
packer.pack(new Packer(out), src);
|
||||
byte[] bytes = out.toByteArray();
|
||||
Template tmpl = null;
|
||||
Unpacker unpacker = new Unpacker();
|
||||
Boolean dst = null;
|
||||
try {
|
||||
tmpl = BooleanTemplate.getInstance();
|
||||
unpacker.wrap(bytes);
|
||||
dst = (Boolean) tmpl.unpack(unpacker);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
unpacker.wrap(bytes);
|
||||
tmpl = new OptionalTemplate(BooleanTemplate.getInstance());
|
||||
dst = (Boolean) tmpl.unpack(unpacker);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testString() throws Exception {
|
||||
// _testString(null); // FIXME
|
||||
_testString("");
|
||||
_testString("a");
|
||||
_testString("ab");
|
||||
@ -205,4 +448,28 @@ public class TestPackUnpack extends TestCase {
|
||||
Unpacker unpacker = new Unpacker(in);
|
||||
assertEquals(src, unpacker.unpackString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullString() throws Exception {
|
||||
String src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(StringPacker.getInstance());
|
||||
packer.pack(new Packer(out), src);
|
||||
byte[] bytes = out.toByteArray();
|
||||
Template tmpl = null;
|
||||
Unpacker unpacker = new Unpacker();
|
||||
String dst = null;
|
||||
try {
|
||||
tmpl = StringTemplate.getInstance();
|
||||
unpacker.wrap(bytes);
|
||||
dst = (String) tmpl.unpack(unpacker);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
unpacker.wrap(bytes);
|
||||
tmpl = new OptionalTemplate(StringTemplate.getInstance());
|
||||
dst = (String) tmpl.unpack(unpacker);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ import java.util.Random;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.msgpack.MessagePackObject;
|
||||
import org.msgpack.MessageTypeException;
|
||||
import org.msgpack.Packer;
|
||||
import org.msgpack.Template;
|
||||
import org.msgpack.Util;
|
||||
@ -20,7 +21,6 @@ public class TestPackConvert extends TestCase {
|
||||
|
||||
@Test
|
||||
public void testInteger() throws Exception {
|
||||
// _testInteger(null); // FIXME
|
||||
_testInteger(0);
|
||||
_testInteger(-1);
|
||||
_testInteger(1);
|
||||
@ -41,9 +41,28 @@ public class TestPackConvert extends TestCase {
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullInteger() throws Exception {
|
||||
Integer src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
new Packer(out).pack(src);
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
Template tmpl = IntegerTemplate.getInstance();
|
||||
Integer dst = null;
|
||||
try {
|
||||
dst = (Integer) tmpl.convert(obj);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
obj = Util.unpackOne(out.toByteArray());
|
||||
tmpl = new OptionalTemplate(IntegerTemplate.getInstance());
|
||||
dst = (Integer) tmpl.convert(obj);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLong() throws Exception {
|
||||
// _testLong(null); // FIXME
|
||||
_testLong((long) 0);
|
||||
_testLong((long) -1);
|
||||
_testLong((long) 1);
|
||||
@ -66,9 +85,28 @@ public class TestPackConvert extends TestCase {
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullLong() throws Exception {
|
||||
Long src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
new Packer(out).pack(src);
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
Template tmpl = LongTemplate.getInstance();
|
||||
Long dst = null;
|
||||
try {
|
||||
dst = (Long) tmpl.convert(obj);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
obj = Util.unpackOne(out.toByteArray());
|
||||
tmpl = new OptionalTemplate(LongTemplate.getInstance());
|
||||
dst = (Long) tmpl.convert(obj);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBiginteger() throws Exception {
|
||||
// _testBigInteger(null); // FIXME
|
||||
_testBigInteger(BigInteger.valueOf(0));
|
||||
_testBigInteger(BigInteger.valueOf(-1));
|
||||
_testBigInteger(BigInteger.valueOf(1));
|
||||
@ -94,9 +132,28 @@ public class TestPackConvert extends TestCase {
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullBigInteger() throws Exception {
|
||||
Long src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
new Packer(out).pack(src);
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
Template tmpl = BigIntegerTemplate.getInstance();
|
||||
BigInteger dst = null;
|
||||
try {
|
||||
dst = (BigInteger) tmpl.convert(obj);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
obj = Util.unpackOne(out.toByteArray());
|
||||
tmpl = new OptionalTemplate(BigIntegerTemplate.getInstance());
|
||||
dst = (BigInteger) tmpl.convert(obj);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFloat() throws Exception {
|
||||
// _testFloat(null); // FIXME
|
||||
_testFloat((float) 0.0);
|
||||
_testFloat((float) -0.0);
|
||||
_testFloat((float) 1.0);
|
||||
@ -121,9 +178,28 @@ public class TestPackConvert extends TestCase {
|
||||
assertEquals(src, dst, 10e-10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullFloat() throws Exception {
|
||||
Long src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
new Packer(out).pack(src);
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
Template tmpl = FloatTemplate.getInstance();
|
||||
Float dst = null;
|
||||
try {
|
||||
dst = (Float) tmpl.convert(obj);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
obj = Util.unpackOne(out.toByteArray());
|
||||
tmpl = new OptionalTemplate(FloatTemplate.getInstance());
|
||||
dst = (Float) tmpl.convert(obj);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDouble() throws Exception {
|
||||
// _testDouble(null); // FIXME
|
||||
_testDouble((double) 0.0);
|
||||
_testDouble((double) -0.0);
|
||||
_testDouble((double) 1.0);
|
||||
@ -148,9 +224,28 @@ public class TestPackConvert extends TestCase {
|
||||
assertEquals(src, dst, 10e-10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullDouble() throws Exception {
|
||||
Long src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
new Packer(out).pack(src);
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
Template tmpl = DoubleTemplate.getInstance();
|
||||
Double dst = null;
|
||||
try {
|
||||
dst = (Double) tmpl.convert(obj);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
obj = Util.unpackOne(out.toByteArray());
|
||||
tmpl = new OptionalTemplate(DoubleTemplate.getInstance());
|
||||
dst = (Double) tmpl.convert(obj);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBoolean() throws Exception {
|
||||
// _testBoolean(null); // FIXME
|
||||
_testBoolean(false);
|
||||
_testBoolean(true);
|
||||
}
|
||||
@ -164,9 +259,28 @@ public class TestPackConvert extends TestCase {
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullBoolean() throws Exception {
|
||||
Long src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
new Packer(out).pack(src);
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
Template tmpl = BooleanTemplate.getInstance();
|
||||
Boolean dst = null;
|
||||
try {
|
||||
dst = (Boolean) tmpl.convert(obj);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
obj = Util.unpackOne(out.toByteArray());
|
||||
tmpl = new OptionalTemplate(BooleanTemplate.getInstance());
|
||||
dst = (Boolean) tmpl.convert(obj);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testString() throws Exception {
|
||||
// _testString(null); // FIXME
|
||||
_testString("");
|
||||
_testString("a");
|
||||
_testString("ab");
|
||||
@ -212,10 +326,29 @@ public class TestPackConvert extends TestCase {
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullString() throws Exception {
|
||||
Long src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
new Packer(out).pack(src);
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
Template tmpl = StringTemplate.getInstance();
|
||||
String dst = null;
|
||||
try {
|
||||
dst = (String) tmpl.convert(obj);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
obj = Util.unpackOne(out.toByteArray());
|
||||
tmpl = new OptionalTemplate(StringTemplate.getInstance());
|
||||
dst = (String) tmpl.convert(obj);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testList() throws Exception {
|
||||
// nullList // FIXME
|
||||
List<Integer> emptyList = new ArrayList<Integer>();
|
||||
{
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
@ -261,10 +394,31 @@ public class TestPackConvert extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testNullList() throws Exception {
|
||||
List<String> src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
new Packer(out).pack(src);
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
Template tmpl = new ListTemplate(StringTemplate.getInstance());
|
||||
List<String> dst = null;
|
||||
try {
|
||||
dst = (List<String>) tmpl.convert(obj);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
obj = Util.unpackOne(out.toByteArray());
|
||||
tmpl = new OptionalTemplate(new ListTemplate(StringTemplate
|
||||
.getInstance()));
|
||||
dst = (List<String>) tmpl.convert(obj);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testMap() throws Exception {
|
||||
// nullMap // FIXME
|
||||
Map<Integer, Integer> emptyMap = new HashMap<Integer, Integer>();
|
||||
{
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
@ -317,4 +471,27 @@ public class TestPackConvert extends TestCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testNullMap() throws Exception {
|
||||
Map<String, String> src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
new Packer(out).pack(src);
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
Template tmpl = new MapTemplate(StringTemplate.getInstance(),
|
||||
StringTemplate.getInstance());
|
||||
Map<String, String> dst = null;
|
||||
try {
|
||||
dst = (Map<String, String>) tmpl.convert(obj);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
obj = Util.unpackOne(out.toByteArray());
|
||||
tmpl = new OptionalTemplate(new MapTemplate(StringTemplate
|
||||
.getInstance(), StringTemplate.getInstance()));
|
||||
dst = (Map<String, String>) tmpl.convert(obj);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.msgpack.MessageTypeException;
|
||||
import org.msgpack.Packer;
|
||||
import org.msgpack.Template;
|
||||
import org.msgpack.Unpacker;
|
||||
@ -19,7 +20,6 @@ import junit.framework.TestCase;
|
||||
public class TestPackUnpack extends TestCase {
|
||||
@Test
|
||||
public void testInteger() throws Exception {
|
||||
// _testInteger(null); // FIXME
|
||||
_testInteger(0);
|
||||
_testInteger(-1);
|
||||
_testInteger(1);
|
||||
@ -40,9 +40,31 @@ public class TestPackUnpack extends TestCase {
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullInteger() throws Exception {
|
||||
Integer src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
new Packer(out).pack(src);
|
||||
byte[] bytes = out.toByteArray();
|
||||
Template tmpl = null;
|
||||
Unpacker unpacker = new Unpacker();
|
||||
Integer dst = null;
|
||||
try {
|
||||
tmpl = IntegerTemplate.getInstance();
|
||||
unpacker.wrap(bytes);
|
||||
dst = (Integer) tmpl.unpack(unpacker);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
unpacker.wrap(bytes);
|
||||
tmpl = new OptionalTemplate(IntegerTemplate.getInstance());
|
||||
dst = (Integer) tmpl.unpack(unpacker);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLong() throws Exception {
|
||||
// _testLong(null); // FIXME
|
||||
_testLong((long) 0);
|
||||
_testLong((long) -1);
|
||||
_testLong((long) 1);
|
||||
@ -65,9 +87,31 @@ public class TestPackUnpack extends TestCase {
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullLong() throws Exception {
|
||||
Long src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
new Packer(out).pack(src);
|
||||
byte[] bytes = out.toByteArray();
|
||||
Template tmpl = null;
|
||||
Unpacker unpacker = new Unpacker();
|
||||
Long dst = null;
|
||||
try {
|
||||
tmpl = LongTemplate.getInstance();
|
||||
unpacker.wrap(bytes);
|
||||
dst = (Long) tmpl.unpack(unpacker);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
unpacker.wrap(bytes);
|
||||
tmpl = new OptionalTemplate(LongTemplate.getInstance());
|
||||
dst = (Long) tmpl.unpack(unpacker);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBigInteger() throws Exception {
|
||||
// _testBigInteger(null); // FIXME
|
||||
_testBigInteger(BigInteger.valueOf(0));
|
||||
_testBigInteger(BigInteger.valueOf(-1));
|
||||
_testBigInteger(BigInteger.valueOf(1));
|
||||
@ -93,9 +137,31 @@ public class TestPackUnpack extends TestCase {
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullBigInteger() throws Exception {
|
||||
BigInteger src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
new Packer(out).pack(src);
|
||||
byte[] bytes = out.toByteArray();
|
||||
Template tmpl = null;
|
||||
Unpacker unpacker = new Unpacker();
|
||||
BigInteger dst = null;
|
||||
try {
|
||||
tmpl = BigIntegerTemplate.getInstance();
|
||||
unpacker.wrap(bytes);
|
||||
dst = (BigInteger) tmpl.unpack(unpacker);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
unpacker.wrap(bytes);
|
||||
tmpl = new OptionalTemplate(BigIntegerTemplate.getInstance());
|
||||
dst = (BigInteger) tmpl.unpack(unpacker);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFloat() throws Exception {
|
||||
// _testFloat(null); // FIXME
|
||||
_testFloat((float) 0.0);
|
||||
_testFloat((float) -0.0);
|
||||
_testFloat((float) 1.0);
|
||||
@ -122,7 +188,6 @@ public class TestPackUnpack extends TestCase {
|
||||
|
||||
@Test
|
||||
public void testDouble() throws Exception {
|
||||
// _testDouble(null); // FIXME
|
||||
_testDouble((double) 0.0);
|
||||
_testDouble((double) -0.0);
|
||||
_testDouble((double) 1.0);
|
||||
@ -147,9 +212,31 @@ public class TestPackUnpack extends TestCase {
|
||||
assertEquals(src, dst, 10e-10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullDouble() throws Exception {
|
||||
Double src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
new Packer(out).pack(src);
|
||||
byte[] bytes = out.toByteArray();
|
||||
Template tmpl = null;
|
||||
Unpacker unpacker = new Unpacker();
|
||||
Double dst = null;
|
||||
try {
|
||||
tmpl = DoubleTemplate.getInstance();
|
||||
unpacker.wrap(bytes);
|
||||
dst = (Double) tmpl.unpack(unpacker);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
unpacker.wrap(bytes);
|
||||
tmpl = new OptionalTemplate(DoubleTemplate.getInstance());
|
||||
dst = (Double) tmpl.unpack(unpacker);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBoolean() throws Exception {
|
||||
// _testBoolean(null); // FIXME
|
||||
_testBoolean(false);
|
||||
_testBoolean(true);
|
||||
}
|
||||
@ -163,9 +250,31 @@ public class TestPackUnpack extends TestCase {
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullBoolean() throws Exception {
|
||||
Boolean src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
new Packer(out).pack(src);
|
||||
byte[] bytes = out.toByteArray();
|
||||
Template tmpl = null;
|
||||
Unpacker unpacker = new Unpacker();
|
||||
Boolean dst = null;
|
||||
try {
|
||||
tmpl = BooleanTemplate.getInstance();
|
||||
unpacker.wrap(bytes);
|
||||
dst = (Boolean) tmpl.unpack(unpacker);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
unpacker.wrap(bytes);
|
||||
tmpl = new OptionalTemplate(BooleanTemplate.getInstance());
|
||||
dst = (Boolean) tmpl.unpack(unpacker);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testString() throws Exception {
|
||||
// _testString(null); // FIXME
|
||||
_testString("");
|
||||
_testString("a");
|
||||
_testString("ab");
|
||||
@ -211,10 +320,32 @@ public class TestPackUnpack extends TestCase {
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullString() throws Exception {
|
||||
String src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
new Packer(out).pack(src);
|
||||
byte[] bytes = out.toByteArray();
|
||||
Template tmpl = null;
|
||||
Unpacker unpacker = new Unpacker();
|
||||
String dst = null;
|
||||
try {
|
||||
tmpl = StringTemplate.getInstance();
|
||||
unpacker.wrap(bytes);
|
||||
dst = (String) tmpl.unpack(unpacker);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
unpacker.wrap(bytes);
|
||||
tmpl = new OptionalTemplate(StringTemplate.getInstance());
|
||||
dst = (String) tmpl.unpack(unpacker);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testList() throws Exception {
|
||||
// nullList // FIXME
|
||||
List<Integer> emptyList = new ArrayList<Integer>();
|
||||
{
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
@ -262,10 +393,34 @@ public class TestPackUnpack extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testNullList() throws Exception {
|
||||
List<String> src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
new Packer(out).pack(src);
|
||||
byte[] bytes = out.toByteArray();
|
||||
Template tmpl = null;
|
||||
Unpacker unpacker = new Unpacker();
|
||||
List<String> dst = null;
|
||||
try {
|
||||
tmpl = new ListTemplate(StringTemplate.getInstance());
|
||||
unpacker.wrap(bytes);
|
||||
dst = (List<String>) tmpl.unpack(unpacker);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
unpacker.wrap(bytes);
|
||||
tmpl = new OptionalTemplate(new ListTemplate(StringTemplate
|
||||
.getInstance()));
|
||||
dst = (List<String>) tmpl.unpack(unpacker);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testMap() throws Exception {
|
||||
// nullMap // FIXME
|
||||
Map<Integer, Integer> emptyMap = new HashMap<Integer, Integer>();
|
||||
{
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
@ -323,4 +478,30 @@ public class TestPackUnpack extends TestCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testNullMap() throws Exception {
|
||||
Map<String, String> src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
new Packer(out).pack(src);
|
||||
byte[] bytes = out.toByteArray();
|
||||
Template tmpl = null;
|
||||
Unpacker unpacker = new Unpacker();
|
||||
Map<String, String> dst = null;
|
||||
try {
|
||||
tmpl = new MapTemplate(StringTemplate.getInstance(), StringTemplate
|
||||
.getInstance());
|
||||
unpacker.wrap(bytes);
|
||||
dst = (Map<String, String>) tmpl.unpack(unpacker);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
unpacker.wrap(bytes);
|
||||
tmpl = new OptionalTemplate(new MapTemplate(StringTemplate
|
||||
.getInstance(), StringTemplate.getInstance()));
|
||||
dst = (Map<String, String>) tmpl.unpack(unpacker);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user