mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-03-19 13:02:13 +01:00
java: adds TestMessageUnpackable test
This commit is contained in:
parent
b4c98584db
commit
c8e351b31e
@ -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");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
50
java/src/test/java/org/msgpack/Image.java
Normal file
50
java/src/test/java/org/msgpack/Image.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
34
java/src/test/java/org/msgpack/TestMessageUnpackable.java
Normal file
34
java/src/test/java/org/msgpack/TestMessageUnpackable.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user