java: adds TestMessageUnpackable test

This commit is contained in:
frsyuki 2010-08-21 03:36:44 +09:00
parent b4c98584db
commit c8e351b31e
3 changed files with 85 additions and 1 deletions

View File

@ -212,7 +212,7 @@ public class Packer {
out.write(castBytes); out.write(castBytes);
return this; return this;
} else { } else {
throw new MessageTypeException("can't BigInteger larger than 0xffffffffffffffff"); throw new MessageTypeException("can't pack BigInteger larger than 0xffffffffffffffff");
} }
} }

View File

@ -0,0 +1,50 @@
package org.msgpack;
import org.msgpack.*;
import java.io.*;
import java.util.*;
public class Image implements MessagePackable, MessageUnpackable {
public String uri = "";
public String title = "";
public int width = 0;
public int height = 0;
public int size = 0;
public void messagePack(Packer pk) throws IOException {
pk.packArray(5);
pk.pack(uri);
pk.pack(title);
pk.pack(width);
pk.pack(height);
pk.pack(size);
}
public void messageUnpack(Unpacker pac) throws IOException, MessageTypeException {
int length = pac.unpackArray();
if(length != 5) {
throw new MessageTypeException();
}
uri = pac.unpackString();
title = pac.unpackString();
width = pac.unpackInt();
height = pac.unpackInt();
size = pac.unpackInt();
}
public boolean equals(Image obj) {
return uri.equals(obj.uri) &&
title.equals(obj.title) &&
width == obj.width &&
height == obj.height &&
size == obj.size;
}
public boolean equals(Object obj) {
if(obj.getClass() != Image.class) {
return false;
}
return equals((Image)obj);
}
}

View File

@ -0,0 +1,34 @@
package org.msgpack;
import org.msgpack.*;
import java.io.*;
import java.util.*;
import java.math.BigInteger;
import org.junit.Test;
import static org.junit.Assert.*;
public class TestMessageUnpackable {
@Test
public void testImage() throws Exception {
Image src = new Image();
src.title = "msgpack";
src.uri = "http://msgpack.org/";
src.width = 2560;
src.height = 1600;
src.size = 4096000;
ByteArrayOutputStream out = new ByteArrayOutputStream();
src.messagePack(new Packer(out));
Image dst = new Image();
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Unpacker pac = new Unpacker(in);
dst.messageUnpack(pac);
assertEquals(src, dst);
}
}