net.buffalo.protocal.io.json.JsonStandardStreamReaderTest.java
第一个版本的测试用例,很长,但对buffalo-call具有较好的覆盖度:
package net.buffalo.protocal.io.json;
import java.io.StringReader;
import junit.framework.TestCase;
import net.buffalo.protocal.io.StreamReader;
/**
*
* for json unit test
*
* @author zhengxq
* @see FastStreamReaderTest.java
*/
public class JsonStandardStreamReaderTest extends TestCase {
private String simpleCall = "[{'buffalo-call':''},{'method':'divide'},{'double':1},{'double':2}]";
private String callWithMap = "[{'buffalo-call':''},{'method':'sum'}," +
"{'int':1}," +
"{'map':[{'type':java.util.HashMap},{'string':'key1'},{'string':'value1'},{'string':'key2'},{'string':'value2'}]}," +
"{'string':test}" +
"]";
private String hasMoreChildren = "[{'buffalo-call':''},{'method':'sum'}," +
"{'int':1}," +
"{'map':[" +
" {'type':java.util.HashMap}," +
" {'string':'type'}," +
" {'string':'name'}," +
" {'list':[{'type':'list'},{'length':3}," +
" {'string':a}," +
" {'int':2}," +
" {'double':8.88}]}," +
"]}]";
private String complexTest = "[{'buffalo-call':''},{'method':'fullTypeTest'}," +
"{'boolean':1}," +
"{'date':20100118T154212Z}," +
"{'int':1}," +
"{'double':2.22},"+
"{'string':'Hello world'},"+
"{'list':["+
" {'type':''},"+
" {'length':2},"+
" {'int':100},"+
" {'string':hi}"+
" ]},"+
"{'map':[" +
" {'type':java.util.HashMap}," +
" {'string':key1}," +
" {'string':value1}," +
" {'string':key2}," +
" {'string':value2}," +
" {'string':java.util.ArrayList}," +
" {'list':[" +
" {'type':''}," +
" {'length':3}," +
" {'int':1}," +
" {'string':good}," +
" {'string':baby}" +
" ]}" +
"]}," +
"{'double':3.33}" +
"]";
/* simpleCall */
public void testSimpleCall() throws Exception {
StringReader reader = new StringReader(simpleCall);
StreamReader streamReader = new JsonStandardStreamReader(reader);
assertEquals("buffalo-call",streamReader.getNodeName());
streamReader.moveDown();
assertEquals("method",streamReader.getNodeName());
assertEquals("divide", streamReader.getValue());
streamReader.moveUp();
streamReader.moveDown();
assertEquals("double", streamReader.getNodeName());
assertEquals("1", streamReader.getValue());
streamReader.moveUp();
streamReader.moveDown();
assertEquals("double", streamReader.getNodeName());
assertEquals("2", streamReader.getValue());
}
/* callWithMap */
public void testShoudReadNested() throws Exception {
StringReader reader = new StringReader(callWithMap);
StreamReader streamReader = new JsonStandardStreamReader(reader);
assertEquals("buffalo-call", streamReader.getNodeName());
streamReader.moveDown();
assertEquals("method", streamReader.getNodeName());
assertEquals("sum", streamReader.getValue());
streamReader.moveUp();
streamReader.moveDown();
assertEquals("int", streamReader.getNodeName());
assertEquals("1", streamReader.getValue());
streamReader.moveUp();
streamReader.moveDown();
assertEquals("map", streamReader.getNodeName());
streamReader.moveDown();
assertEquals("type", streamReader.getNodeName());
assertEquals("java.util.HashMap", streamReader.getValue());
streamReader.moveUp();
streamReader.moveDown();
assertEquals("string", streamReader.getNodeName());
assertEquals("key1", streamReader.getValue());
streamReader.moveUp();
streamReader.moveDown();
assertEquals("string", streamReader.getNodeName());
assertEquals("value1", streamReader.getValue());
streamReader.moveUp();
streamReader.moveDown();
assertEquals("string", streamReader.getNodeName());
assertEquals("key2", streamReader.getValue());
streamReader.moveUp();
streamReader.moveDown();
assertEquals("string", streamReader.getNodeName());
assertEquals("value2", streamReader.getValue());
streamReader.moveUp();
streamReader.moveUp();
}
/* hasMoreChildren */
public void testHasMoreChildren() throws Exception {
StringReader reader = new StringReader(hasMoreChildren);
StreamReader streamReader = new JsonStandardStreamReader(reader);
assertTrue(streamReader.hasMoreChildren());
streamReader.moveDown();
//then {'method':'sum'}
assertFalse(streamReader.hasMoreChildren());
streamReader.moveUp();
streamReader.moveDown();
streamReader.moveUp();
streamReader.moveDown();
//then {'map':[...]}
assertEquals("map",streamReader.getNodeName());
assertTrue(streamReader.hasMoreChildren());
streamReader.moveDown();
//then {'type':java.util.HashMap}
streamReader.moveUp();
streamReader.moveDown();
//then {'string':'type'}
streamReader.moveUp();
streamReader.moveDown();
//then {'string':'name'}
streamReader.moveUp();
streamReader.moveDown();
//then {'list':[...]}
assertTrue(streamReader.hasMoreChildren());
assertEquals("list",streamReader.getNodeName());
while(streamReader.hasMoreChildren()) {
streamReader.moveDown();
streamReader.moveUp();
}
streamReader.moveUp();
assertEquals("map",streamReader.getNodeName());
assertFalse(streamReader.hasMoreChildren());
}
/**
* more complex test.
* //boolean、date、int、long、null、string、list、map、double
* var t1 = true;//
* var t2 = new Date();
* var t3 = 1;//int
* var t4 = parseFloat("2.22");
* //var t5 = null;
* var t6 = "Hello world";
* var t7 = [100,"hi"];//list
* var t8 = {'key1':'value1','key2':'value2','java.util.ArrayList':[1,"good","baby"]};//map
* var t9 = parseFloat("3.33");
*
* buffalo.remoteCall("fullTypeService.fullTypeTest",[t1,t2,t3,t4,t6,t7,t8,t9], function(reply) {
* $("result").value = reply.getResult();
* },protocol)
* @author zhengxq
*/
public void testComplexTest() throws Exception {
StringReader reader = new StringReader(complexTest);
StreamReader streamReader = new JsonStandardStreamReader(reader);
assertEquals("buffalo-call",streamReader.getNodeName());
assertTrue(streamReader.hasMoreChildren());
streamReader.moveDown();
//<method>fullTypeTest</method>
assertEquals("method",streamReader.getNodeName());
assertEquals("fullTypeTest",streamReader.getValue());
assertFalse(streamReader.hasMoreChildren());
streamReader.moveUp();
streamReader.moveDown();
//<boolean>1</boolean>
assertEquals("boolean",streamReader.getNodeName());
assertEquals("1",streamReader.getValue());
streamReader.moveUp();
streamReader.moveDown();
//<date>20100118T154212Z</date>
assertEquals("date",streamReader.getNodeName());
assertEquals("20100118T154212Z",streamReader.getValue());
streamReader.moveUp();
streamReader.moveDown();
//<int>1</int>
assertEquals("int",streamReader.getNodeName());
assertEquals("1",streamReader.getValue());
streamReader.moveUp();
streamReader.moveDown();
//<double>2.22</double>
assertEquals("double",streamReader.getNodeName());
assertEquals("2.22",streamReader.getValue());
streamReader.moveUp();
streamReader.moveDown();
//<string>Hello world</string>
assertEquals("string",streamReader.getNodeName());
assertEquals("Hello world",streamReader.getValue());
streamReader.moveUp();
streamReader.moveDown();
//<list>...</list>
assertEquals("list",streamReader.getNodeName());
assertTrue(streamReader.hasMoreChildren());
streamReader.moveDown();
//<type></type>
assertEquals("type",streamReader.getNodeName());
streamReader.moveUp();
streamReader.moveDown();
//<length>2</length>
assertEquals("length",streamReader.getNodeName());
assertEquals("2",streamReader.getValue());
streamReader.moveUp();
streamReader.moveDown();
//<int>100</int>
assertEquals("int",streamReader.getNodeName());
assertEquals("100",streamReader.getValue());
streamReader.moveUp();
streamReader.moveDown();
//<string>hi</string>
assertEquals("string",streamReader.getNodeName());
assertEquals("hi",streamReader.getValue());
streamReader.moveUp();
streamReader.moveUp();//</list>
streamReader.moveDown();
//<map>...</map>
assertEquals("map",streamReader.getNodeName());
assertTrue(streamReader.hasMoreChildren());
streamReader.moveDown();
//<type>java.util.HashMap</type>
assertEquals("type",streamReader.getNodeName());
assertEquals("java.util.HashMap",streamReader.getValue());
streamReader.moveUp();
streamReader.moveDown();
//<string>key1</string>
assertEquals("string",streamReader.getNodeName());
assertEquals("key1",streamReader.getValue());
streamReader.moveUp();
streamReader.moveDown();
//<string>value1</string>
assertEquals("string",streamReader.getNodeName());
assertEquals("value1",streamReader.getValue());
streamReader.moveUp();
streamReader.moveDown();
//<string>key2</string>
assertEquals("string",streamReader.getNodeName());
assertEquals("key2",streamReader.getValue());
streamReader.moveUp();
streamReader.moveDown();
//<string>value2</string>
assertEquals("string",streamReader.getNodeName());
assertEquals("value2",streamReader.getValue());
streamReader.moveUp();
streamReader.moveDown();
//<string>java.util.ArrayList</string>
assertEquals("string",streamReader.getNodeName());
assertEquals("java.util.ArrayList",streamReader.getValue());
streamReader.moveUp();
streamReader.moveDown();
//<list>...</list>
assertEquals("list",streamReader.getNodeName());
assertTrue(streamReader.hasMoreChildren());
streamReader.moveDown();
//<type></type>
assertEquals("type",streamReader.getNodeName());
streamReader.moveUp();
streamReader.moveDown();
//<length>3</length>
assertEquals("length",streamReader.getNodeName());
assertEquals("3",streamReader.getValue());
streamReader.moveUp();
streamReader.moveDown();
//<int>1</int>
assertEquals("int",streamReader.getNodeName());
assertEquals("1",streamReader.getValue());
streamReader.moveUp();
streamReader.moveDown();
//<string>good</string>
assertEquals("string",streamReader.getNodeName());
assertEquals("good",streamReader.getValue());
streamReader.moveUp();
streamReader.moveDown();
//<string>baby</string>
assertEquals("string",streamReader.getNodeName());
assertEquals("baby",streamReader.getValue());
streamReader.moveUp();
//assertEquals("list",streamReader.getNodeName());
streamReader.moveUp();//</list>
assertEquals("map",streamReader.getNodeName());
streamReader.moveUp();//</map>
assertEquals("buffalo-call",streamReader.getNodeName());
streamReader.moveDown();
//<double>3.33</double>
assertEquals("double",streamReader.getNodeName());
assertEquals("3.33",streamReader.getValue());
streamReader.moveUp();
//assertEquals("buffalo-call",streamReader.getNodeName());
assertFalse(streamReader.hasMoreChildren());
}
}