java: write test programs for org.msgpack.util.codegen.*.java

This commit is contained in:
Muga Nishizawa
2010-10-20 21:04:37 +09:00
parent 2065affd45
commit 64711e615e
4 changed files with 257 additions and 44 deletions

View File

@@ -33,6 +33,9 @@ import org.slf4j.LoggerFactory;
public class DynamicCodeGenBase implements Constants { public class DynamicCodeGenBase implements Constants {
private static Logger LOG = LoggerFactory
.getLogger(DynamicCodeGenBase.class);
public static interface NullChecker { public static interface NullChecker {
void setNullCheck(boolean nullCheck); void setNullCheck(boolean nullCheck);
} }
@@ -58,9 +61,6 @@ public class DynamicCodeGenBase implements Constants {
} }
} }
private static Logger LOG = LoggerFactory
.getLogger(DynamicCodeGenBase.class);
private static AtomicInteger COUNTER = new AtomicInteger(0); private static AtomicInteger COUNTER = new AtomicInteger(0);
protected static int inc() { protected static int inc() {
@@ -74,80 +74,58 @@ public class DynamicCodeGenBase implements Constants {
} }
protected void checkTypeValidation(Class<?> type) { protected void checkTypeValidation(Class<?> type) {
DynamicCodeGenException e = new DynamicCodeGenException("Fatal error: " DynamicCodeGenException e = new DynamicCodeGenException(String.format(
+ type.getName()); "Fatal error: %s", new Object[] { type.getName() }));
LOG.error(e.getMessage(), e); LOG.error(e.getMessage(), e);
throw e; throw e;
} }
protected void throwTypeValidationException(Class<?> origClass, protected void throwTypeValidationException(Class<?> type, String message)
String message) throws DynamicCodeGenException { throws DynamicCodeGenException {
DynamicCodeGenException e = new DynamicCodeGenException(message + ": " DynamicCodeGenException e = new DynamicCodeGenException(String.format(
+ origClass.getName()); "%s: %s", new Object[] { message, type.getName() }));
LOG.error(e.getMessage(), e); LOG.error(e.getMessage(), e);
throw e; throw e;
} }
protected void checkDefaultConstructorValidation(Class<?> type) { protected void checkDefaultConstructorValidation(Class<?> type) {
DynamicCodeGenException e = new DynamicCodeGenException("Fatal error: " DynamicCodeGenException e = new DynamicCodeGenException(String.format(
+ type.getName()); "Fatal error: %s", new Object[] { type.getName() }));
LOG.error(e.getMessage(), e); LOG.error(e.getMessage(), e);
throw e; throw e;
} }
protected void throwConstructorValidationException(Class<?> origClass) { protected void throwConstructorValidationException(Class<?> origClass) {
DynamicCodeGenException e = new DynamicCodeGenException( DynamicCodeGenException e = new DynamicCodeGenException(String.format(
"it must have a public zero-argument constructor: " "it must have a public zero-argument constructor: %s",
+ origClass.getName()); new Object[] { origClass.getName() }));
LOG.error(e.getMessage(), e); LOG.error(e.getMessage(), e);
throw e; throw e;
} }
protected void throwFieldValidationException(Field f) { protected void throwFieldValidationException(Field field) {
DynamicCodeGenException e = new DynamicCodeGenException( DynamicCodeGenException e = new DynamicCodeGenException(String.format(
"it must be a public field: " + f.getName()); "it must be a public field: %s",
new Object[] { field.getName() }));
LOG.debug(e.getMessage(), e); LOG.debug(e.getMessage(), e);
throw e; throw e;
} }
protected static void throwMethodValidationException(Method method, protected static void throwMethodValidationException(Method method,
String message) throws DynamicCodeGenException { String message) throws DynamicCodeGenException {
DynamicCodeGenException e = new DynamicCodeGenException(message + ": " DynamicCodeGenException e = new DynamicCodeGenException(String.format(
+ method.getName()); "%s: %s", new Object[] { message, method.getName() }));
LOG.error(e.getMessage(), e); LOG.error(e.getMessage(), e);
throw e; throw e;
} }
protected CtClass makeClass(String name) throws NotFoundException { protected CtClass makeClass(String name) throws NotFoundException {
DynamicCodeGenException e = new DynamicCodeGenException("Fatal error: " DynamicCodeGenException e = new DynamicCodeGenException(String.format(
+ name); "Fatal error: %s", new Object[] { name }));
LOG.error(e.getMessage(), e); LOG.error(e.getMessage(), e);
throw e; throw e;
} }
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) protected void setSuperclass(CtClass newCtClass, Class<?> superClass)
throws NotFoundException, CannotCompileException { throws NotFoundException, CannotCompileException {
// check the specified super class // check the specified super class

View File

@@ -0,0 +1,10 @@
package org.msgpack.util.codegen;
import org.msgpack.Template;
public class FieldOption {
public String name;
public Template tmpl;
}

View File

@@ -2,6 +2,7 @@ package org.msgpack.util.codegen;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigInteger; import java.math.BigInteger;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@@ -15,8 +16,11 @@ import org.junit.Test;
import org.msgpack.CustomConverter; import org.msgpack.CustomConverter;
import org.msgpack.CustomPacker; import org.msgpack.CustomPacker;
import org.msgpack.CustomUnpacker; import org.msgpack.CustomUnpacker;
import org.msgpack.MessageConvertable;
import org.msgpack.MessagePackObject; import org.msgpack.MessagePackObject;
import org.msgpack.MessagePackable;
import org.msgpack.MessagePacker; import org.msgpack.MessagePacker;
import org.msgpack.MessageTypeException;
import org.msgpack.Packer; import org.msgpack.Packer;
import org.msgpack.Template; import org.msgpack.Template;
import org.msgpack.Unpacker; import org.msgpack.Unpacker;
@@ -1007,4 +1011,121 @@ public class TestPackConvert extends TestCase {
public SampleSuperClass() { public SampleSuperClass() {
} }
} }
//@Test
public void XtestMessagePackableUnpackableClass00() throws Exception {
BaseMessagePackableConvertableClass src = new BaseMessagePackableConvertableClass();
MessagePackableConvertableClass src1 = new MessagePackableConvertableClass();
List<MessagePackableConvertableClass> src2 = new ArrayList<MessagePackableConvertableClass>();
src1.f0 = 0;
src1.f1 = 1;
src.f0 = src1;
src.f1 = 1;
src2.add(src1);
src.f2 = src2;
ByteArrayOutputStream out = new ByteArrayOutputStream();
MessagePacker packer = DynamicPacker
.create(BaseMessagePackableConvertableClass.class);
packer.pack(new Packer(out), src);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Template tmpl = DynamicTemplate
.create(BaseMessagePackableConvertableClass.class);
Unpacker pac = new Unpacker(in);
Iterator<MessagePackObject> it = pac.iterator();
assertTrue(it.hasNext());
MessagePackObject mpo = it.next();
BaseMessagePackableConvertableClass dst = (BaseMessagePackableConvertableClass) tmpl
.convert(mpo);
assertEquals(src.f0.f0, dst.f0.f0);
assertEquals(src.f0.f1, dst.f0.f1);
assertEquals(src.f1, dst.f1);
assertEquals(src.f2.size(), dst.f2.size());
assertEquals(src.f2.get(0).f0, dst.f2.get(0).f0);
assertEquals(src.f2.get(0).f1, dst.f2.get(0).f1);
assertFalse(it.hasNext());
}
//@Test
public void XtestMessagePackableUnpackableClass01() throws Exception {
BaseMessagePackableConvertableClass src = new BaseMessagePackableConvertableClass();
src.f0 = null;
src.f1 = 1;
src.f2 = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
MessagePacker packer = DynamicPacker
.create(BaseMessagePackableConvertableClass.class);
packer.pack(new Packer(out), src);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Template tmpl = DynamicTemplate
.create(BaseMessagePackableConvertableClass.class);
Unpacker pac = new Unpacker(in);
Iterator<MessagePackObject> it = pac.iterator();
assertTrue(it.hasNext());
MessagePackObject mpo = it.next();
BaseMessagePackableConvertableClass dst = (BaseMessagePackableConvertableClass) tmpl
.convert(mpo);
assertEquals(src.f0, dst.f0);
assertEquals(src.f1, dst.f1);
assertEquals(src.f2, dst.f2);
assertFalse(it.hasNext());
}
//@Test
public void XtestMessagePackableUnpackableClass02() throws Exception {
BaseMessagePackableConvertableClass src = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
MessagePacker packer = DynamicPacker
.create(BaseMessagePackableConvertableClass.class);
packer.pack(new Packer(out), src);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Template tmpl = DynamicTemplate
.create(BaseMessagePackableConvertableClass.class);
Unpacker pac = new Unpacker(in);
Iterator<MessagePackObject> it = pac.iterator();
assertTrue(it.hasNext());
MessagePackObject mpo = it.next();
BaseMessagePackableConvertableClass dst = (BaseMessagePackableConvertableClass) tmpl
.convert(mpo);
assertEquals(src, dst);
assertFalse(it.hasNext());
}
public static class BaseMessagePackableConvertableClass {
public MessagePackableConvertableClass f0;
public int f1;
public List<MessagePackableConvertableClass> f2;
public BaseMessagePackableConvertableClass() {
}
}
public static class MessagePackableConvertableClass implements
MessagePackable, MessageConvertable {
public int f0;
public int f1;
public MessagePackableConvertableClass() {
}
@Override
public void messagePack(Packer packer) throws IOException {
packer.pack(f0);
packer.pack(f1);
}
@Override
public void messageConvert(MessagePackObject obj)
throws MessageTypeException {
if (obj.isNil()) {
return;
}
MessagePackObject[] objs = obj.asArray();
f0 = objs[0].asInt();
f1 = objs[1].asInt();
}
}
} }

View File

@@ -2,6 +2,7 @@ package org.msgpack.util.codegen;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigInteger; import java.math.BigInteger;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@@ -13,7 +14,10 @@ import org.junit.Test;
import org.msgpack.CustomConverter; import org.msgpack.CustomConverter;
import org.msgpack.CustomPacker; import org.msgpack.CustomPacker;
import org.msgpack.CustomUnpacker; import org.msgpack.CustomUnpacker;
import org.msgpack.MessagePackable;
import org.msgpack.MessagePacker; import org.msgpack.MessagePacker;
import org.msgpack.MessageTypeException;
import org.msgpack.MessageUnpackable;
import org.msgpack.Packer; import org.msgpack.Packer;
import org.msgpack.Template; import org.msgpack.Template;
import org.msgpack.Unpacker; import org.msgpack.Unpacker;
@@ -885,4 +889,104 @@ public class TestPackUnpack extends TestCase {
public SampleSuperClass() { public SampleSuperClass() {
} }
} }
//@Test
public void XtestMessagePackableUnpackableClass00() throws Exception {
BaseMessagePackableUnpackableClass src = new BaseMessagePackableUnpackableClass();
MessagePackableUnpackableClass src1 = new MessagePackableUnpackableClass();
List<MessagePackableUnpackableClass> src2 = new ArrayList<MessagePackableUnpackableClass>();
src1.f0 = 0;
src1.f1 = 1;
src.f0 = src1;
src.f1 = 1;
src2.add(src1);
ByteArrayOutputStream out = new ByteArrayOutputStream();
MessagePacker packer = DynamicPacker
.create(BaseMessagePackableUnpackableClass.class);
packer.pack(new Packer(out), src);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Template tmpl = DynamicTemplate
.create(BaseMessagePackableUnpackableClass.class);
BaseMessagePackableUnpackableClass dst = (BaseMessagePackableUnpackableClass) tmpl
.unpack(new Unpacker(in));
assertEquals(src.f0.f0, dst.f0.f0);
assertEquals(src.f0.f1, dst.f0.f1);
assertEquals(src.f1, dst.f1);
assertEquals(src.f2.size(), dst.f2.size());
assertEquals(src.f2.get(0).f0, dst.f2.get(0).f0);
assertEquals(src.f2.get(0).f1, dst.f2.get(0).f1);
}
//@Test
public void XtestMessagePackableUnpackableClass01() throws Exception {
BaseMessagePackableUnpackableClass src = new BaseMessagePackableUnpackableClass();
src.f0 = null;
src.f1 = 1;
src.f2 = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
MessagePacker packer = DynamicPacker
.create(BaseMessagePackableUnpackableClass.class);
packer.pack(new Packer(out), src);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Template tmpl = DynamicTemplate
.create(BaseMessagePackableUnpackableClass.class);
BaseMessagePackableUnpackableClass dst = (BaseMessagePackableUnpackableClass) tmpl
.unpack(new Unpacker(in));
assertEquals(src.f0, dst.f0);
assertEquals(src.f1, dst.f1);
assertEquals(src.f2, dst.f2);
}
//@Test
public void XtestMessagePackableUnpackableClass02() throws Exception {
BaseMessagePackableUnpackableClass src = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
MessagePacker packer = DynamicPacker
.create(BaseMessagePackableUnpackableClass.class);
packer.pack(new Packer(out), src);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Template tmpl = DynamicTemplate
.create(BaseMessagePackableUnpackableClass.class);
BaseMessagePackableUnpackableClass dst = (BaseMessagePackableUnpackableClass) tmpl
.unpack(new Unpacker(in));
assertEquals(src, dst);
}
public static class BaseMessagePackableUnpackableClass {
public MessagePackableUnpackableClass f0;
public int f1;
public List<MessagePackableUnpackableClass> f2;
public BaseMessagePackableUnpackableClass() {
}
}
public static class MessagePackableUnpackableClass implements
MessagePackable, MessageUnpackable {
public int f0;
public int f1;
public MessagePackableUnpackableClass() {
}
@Override
public void messagePack(Packer packer) throws IOException {
packer.pack(f0);
packer.pack(f1);
}
@Override
public void messageUnpack(Unpacker unpacker) throws IOException,
MessageTypeException {
if (unpacker.tryUnpackNull()) {
return;
}
f0 = unpacker.unpackInt();
f1 = unpacker.unpackInt();
}
}
} }