add testcase & remove unused code

This commit is contained in:
wenshao
2013-06-30 03:46:13 +08:00
parent bbda3c9336
commit 74141bd7ed
31 changed files with 679 additions and 642 deletions
@@ -42,4 +42,20 @@ public class JSONReaderTest_array_object extends TestCase {
reader.endArray();
reader.close();
}
public void test_read_3() throws Exception {
JSONReader reader = new JSONReader(new JSONScanner(text));
reader.startArray();
int count = 0;
while (reader.hasNext()) {
reader.startObject();
reader.endObject();
count++;
}
Assert.assertEquals(10, count);
reader.endArray();
reader.close();
}
}
@@ -22,6 +22,8 @@ public class JSONReaderTest_object_object extends TestCase {
while (reader.hasNext()) {
String key = (String) reader.readObject();
Object value = reader.readObject();
Assert.assertNotNull(key);
Assert.assertNotNull(value);
count++;
}
Assert.assertEquals(10, count);
@@ -38,6 +40,10 @@ public class JSONReaderTest_object_object extends TestCase {
while (reader.hasNext()) {
String key = (String) reader.readObject();
Object value = reader.readObject();
Assert.assertNotNull(key);
Assert.assertNotNull(value);
count++;
}
Assert.assertEquals(10, count);
@@ -45,4 +51,37 @@ public class JSONReaderTest_object_object extends TestCase {
reader.endObject();
reader.close();
}
public void test_read_2() throws Exception {
JSONReader reader = new JSONReader(new JSONScanner("{{}:{},{}:{}}"));
reader.startObject();
Assert.assertTrue(reader.hasNext());
reader.startObject();
reader.endObject();
reader.startObject();
reader.endObject();
Assert.assertTrue(reader.hasNext());
reader.startObject();
reader.endObject();
reader.startObject();
reader.endObject();
Assert.assertFalse(reader.hasNext());
reader.endObject();
Exception error = null;
try {
reader.hasNext();
} catch (Exception ex) {
error = ex;
}
Assert.assertNotNull(error);
reader.close();
}
}
@@ -0,0 +1,40 @@
package com.alibaba.fastjson;
import java.io.StringReader;
import junit.framework.TestCase;
import org.junit.Assert;
public class JSONReader_error extends TestCase {
public void test_0() throws Exception {
JSONReader reader = new JSONReader(new StringReader("[]"));
Exception error = null;
try {
reader.hasNext();
} catch (Exception e) {
error = e;
}
Assert.assertNotNull(error);
reader.close();
}
public void test_1() throws Exception {
JSONReader reader = new JSONReader(new StringReader("{\"id\":123}"));
reader.startObject();
reader.readObject();
Exception error = null;
try {
reader.hasNext();
} catch (Exception e) {
error = e;
}
Assert.assertNotNull(error);
reader.close();
}
}
@@ -0,0 +1,26 @@
package com.alibaba.fastjson;
import java.io.StringReader;
import junit.framework.TestCase;
import org.junit.Assert;
public class JSONReader_top extends TestCase {
public void test_int() throws Exception {
JSONReader reader = new JSONReader(new StringReader("123"));
Assert.assertEquals(new Integer(123), reader.readInteger());
reader.close();
}
public void test_long() throws Exception {
JSONReader reader = new JSONReader(new StringReader("123"));
Assert.assertEquals(new Long(123), reader.readLong());
reader.close();
}
}
@@ -0,0 +1,24 @@
package com.alibaba.json.bvt;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
public class JSONObjectTest4 extends TestCase {
public void test_interface() throws Exception {
VO vo = JSON.parseObject("{id:123}", VO.class);
Assert.assertEquals(123, vo.getId());
}
public static interface VO {
@JSONField()
int getId();
@JSONField()
void setId(int val);
}
}
@@ -2,43 +2,65 @@ package com.alibaba.json.bvt.parser;
import java.util.Date;
import org.junit.Assert;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.DefaultExtJSONParser;
import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.parser.ParserConfig;
public class DateParserTest extends TestCase {
public void test_date_0() throws Exception {
DefaultExtJSONParser parser = new DefaultExtJSONParser("1294552193254");
public void test_date_new() throws Exception {
DefaultJSONParser parser = new DefaultJSONParser("new Date(1294552193254)");
java.util.Date date = parser.parseObject(java.util.Date.class);
Assert.assertEquals(new java.util.Date(1294552193254L), date);
parser.close();
}
public void test_date_new_1() throws Exception {
DefaultJSONParser parser = new DefaultJSONParser("new Date(1294552193254)");
java.util.Date date = (java.util.Date) parser.parse();
Assert.assertEquals(new java.util.Date(1294552193254L), date);
parser.close();
}
public void test_date_0() throws Exception {
DefaultJSONParser parser = new DefaultJSONParser("1294552193254");
java.util.Date date = parser.parseObject(java.util.Date.class);
Assert.assertEquals(new java.util.Date(1294552193254L), date);
parser.close();
}
public void test_date_1() throws Exception {
int featrues = JSON.DEFAULT_PARSER_FEATURE;
featrues = Feature.config(featrues, Feature.AllowISO8601DateFormat, true);
DefaultExtJSONParser parser = new DefaultExtJSONParser("\"2011-01-09T13:49:53.254\"", ParserConfig.getGlobalInstance(), featrues);
DefaultJSONParser parser = new DefaultJSONParser("\"2011-01-09T13:49:53.254\"", ParserConfig.getGlobalInstance(), featrues);
java.util.Date date = parser.parseObject(java.util.Date.class);
Assert.assertEquals(new java.util.Date(1294552193254L), date);
parser.close();
}
public void test_date_2() throws Exception {
int featrues = JSON.DEFAULT_PARSER_FEATURE;
DefaultExtJSONParser parser = new DefaultExtJSONParser("new Date(1294552193254)", ParserConfig.getGlobalInstance(), featrues);
DefaultJSONParser parser = new DefaultJSONParser("new Date(1294552193254)", ParserConfig.getGlobalInstance(), featrues);
java.util.Date date = parser.parseObject(java.util.Date.class);
Assert.assertEquals(new java.util.Date(1294552193254L), date);
parser.close();
}
public void test_date_3() throws Exception {
@@ -50,11 +72,12 @@ public class DateParserTest extends TestCase {
public void test_date_4() throws Exception {
int featrues = JSON.DEFAULT_PARSER_FEATURE;
featrues = Feature.config(featrues, Feature.AllowISO8601DateFormat, true);
DefaultExtJSONParser parser = new DefaultExtJSONParser("\"2011-01-09\"", ParserConfig.getGlobalInstance(), featrues);
DefaultJSONParser parser = new DefaultJSONParser("\"2011-01-09\"", ParserConfig.getGlobalInstance(), featrues);
java.util.Date date = parser.parseObject(java.util.Date.class);
Assert.assertEquals(new java.util.Date(1294502400000L), date);
parser.close();
}
public void test_date_5() throws Exception {
@@ -82,9 +105,10 @@ public class DateParserTest extends TestCase {
JSONException error = null;
try {
DefaultExtJSONParser parser = new DefaultExtJSONParser("true");
DefaultJSONParser parser = new DefaultJSONParser("true");
parser.parseObject(java.util.Date.class);
parser.close();
} catch (JSONException e) {
error = e;
}
@@ -0,0 +1,25 @@
package com.alibaba.json.bvt.parser;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
public class JSONLexerTest extends TestCase {
public void test_0 () throws Exception {
StringBuilder buf = new StringBuilder("{\"value\":\"");
for (int i = 0; i < 256; ++i) {
buf.append('a');
}
buf.append("\\n");
buf.append("\"}");
JSONObject json = JSON.parseObject(buf.toString());
Assert.assertEquals(257, json.getString("value").length());
Assert.assertEquals('a', json.getString("value").charAt(255));
Assert.assertEquals('\n', json.getString("value").charAt(256));
}
}
@@ -0,0 +1,115 @@
package com.alibaba.json.bvt.parser;
import java.util.List;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
public class JSONLexerTest_2 extends TestCase {
public void test_0() throws Exception {
VO vo = (VO) JSON.parseObject("{\"@type\":\"com.alibaba.json.bvt.parser.JSONLexerTest_2$VO\"}", VO.class);
Assert.assertNotNull(vo);
}
public void test_1() throws Exception {
JSON.parseObject("{\"@type\":\"com.alibaba.json.bvt.parser.JSONLexerTest_2$VO1\"}", VO.class);
}
public void test_2() throws Exception {
JSON.parseObject("{\"@type\":\"com.alibaba.json.bvt.parser.JSONLexerTest_2$A\"}", VO.class);
}
public void test_a() throws Exception {
P a = JSON.parseObject("{\"vo\":{\"@type\":\"com.alibaba.json.bvt.parser.JSONLexerTest_2$VO\"}}", P.class);
Assert.assertNotNull(a);
}
public void test_list() throws Exception {
List<VO> list = JSON.parseObject("[{\"@type\":\"com.alibaba.json.bvt.parser.JSONLexerTest_2$VO\"}]",
new TypeReference<List<VO>>() {
});
Assert.assertNotNull(list);
Assert.assertNotNull(list.get(0));
}
public void test_list_2() throws Exception {
List<VO> list = JSON.parseObject("[{\"@type\":\"com.alibaba.json.bvt.parser.JSONLexerTest_2$VO\"},{}]",
new TypeReference<List<VO>>() {
});
Assert.assertNotNull(list);
Assert.assertEquals(2, list.size());
Assert.assertNotNull(list.get(0));
Assert.assertNotNull(list.get(1));
}
public void test_error() throws Exception {
Exception error = null;
try {
JSON.parseObject("[{\"@type\":\"com.alibaba.json.bvt.parser.JSONLexerTest_2$VO\"}[]",
new TypeReference<List<VO>>() {
});
} catch (Exception ex) {
error = ex;
}
Assert.assertNotNull(error);
}
public static class P {
private VO vo;
public VO getVo() {
return vo;
}
public void setVo(VO vo) {
this.vo = vo;
}
}
public static class VO {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
public static class VO1 {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
public static class A {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
}
@@ -0,0 +1,30 @@
package com.alibaba.json.bvt.parser;
import com.alibaba.fastjson.JSON;
import junit.framework.TestCase;
public class JSONLexerTest_3 extends TestCase {
public void test_matchField() throws Exception {
JSON.parseObject("{\"val\":{}}", VO.class);
}
public static class VO {
private A value;
public A getValue() {
return value;
}
public void setValue(A value) {
this.value = value;
}
}
public static class A {
}
}
@@ -0,0 +1,29 @@
package com.alibaba.json.bvt.parser;
import org.junit.Assert;
import com.alibaba.fastjson.JSON;
import junit.framework.TestCase;
public class JSONLexerTest_4 extends TestCase {
public void test_scanFieldString() throws Exception {
VO vo = JSON.parseObject("{\"value\":\"abc\"}", VO.class);
Assert.assertEquals("abc", vo.getValue());
}
public static class VO {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
}
@@ -1,29 +1,73 @@
package com.alibaba.json.bvt.parser.deser;
import java.math.BigInteger;
import java.util.List;
import org.junit.Assert;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.DefaultExtJSONParser;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.parser.deserializer.BigIntegerDeserializer;
public class BigIntegerDeserializerTest extends TestCase {
public void test_null() throws Exception {
String input = "null";
DefaultExtJSONParser parser = new DefaultExtJSONParser(input, ParserConfig.getGlobalInstance(), JSON.DEFAULT_PARSER_FEATURE);
BigIntegerDeserializer deser = new BigIntegerDeserializer();
Assert.assertNull(deser.deserialze(parser, null, null));
}
public void test_1() throws Exception {
BigInteger value = JSON.parseObject("'123'", BigInteger.class);
Assert.assertEquals(new BigInteger("123"), value);
}
public void test_vo() throws Exception {
VO vo = JSON.parseObject("{\"value\":123}", VO.class);
Assert.assertEquals(new BigInteger("123"), vo.getValue());
}
public void test_vo_null() throws Exception {
VO vo = JSON.parseObject("{\"value\":null}", VO.class);
Assert.assertEquals(null, vo.getValue());
}
public void test_vo2() throws Exception {
VO2 vo = JSON.parseObject("{\"value\":123}", VO2.class);
Assert.assertEquals(new BigInteger("123"), vo.getValue());
}
public void test_array() throws Exception {
List<BigInteger> list = JSON.parseArray("[123,345]", BigInteger.class);
Assert.assertEquals(new BigInteger("123"), list.get(0));
Assert.assertEquals(new BigInteger("345"), list.get(1));
}
public static class VO {
private BigInteger value;
public BigInteger getValue() {
return value;
}
public void setValue(BigInteger value) {
this.value = value;
}
}
private static class VO2 {
private BigInteger value;
public BigInteger getValue() {
return value;
}
public void setValue(BigInteger value) {
this.value = value;
}
}
}
@@ -1,6 +1,7 @@
package com.alibaba.json.bvt.parser.deser;
import java.text.SimpleDateFormat;
import java.util.List;
import junit.framework.TestCase;
@@ -14,6 +15,13 @@ public class DateFormatDeserializerTest extends TestCase {
VO vo = JSON.parseObject("{\"format\":\"\"}", VO.class);
Assert.assertEquals(null, vo.getFormat());
}
public void test_dateFormat_array() throws Exception {
List<SimpleDateFormat> list = JSON.parseArray("[\"\",null,\"yyyy\"]", SimpleDateFormat.class);
Assert.assertEquals(null, list.get(0));
Assert.assertEquals(null, list.get(1));
Assert.assertEquals(new SimpleDateFormat("yyyy"), list.get(2));
}
public void test_dateFormat_null() throws Exception {
VO vo = JSON.parseObject("{\"format\":null}", VO.class);
@@ -0,0 +1,51 @@
package com.alibaba.json.bvt.parser.deser;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONCreator;
import com.alibaba.fastjson.annotation.JSONField;
public class IntegerFieldDeserializerTest3 extends TestCase {
public void test_0() throws Exception {
Entity a = JSON.parseObject("{f1:123, f2:null}", Entity.class);
Assert.assertEquals(123, a.getF1());
Assert.assertEquals(null, a.getF2());
}
public void test_1() throws Exception {
Entity a = JSON.parseObject("{f1:22, f2:'33'}", Entity.class);
Assert.assertEquals(22, a.getF1());
Assert.assertEquals(33, a.getF2().intValue());
}
public void test_2() throws Exception {
Entity a = JSON.parseObject("{f1:'22', f2:33}", Entity.class);
Assert.assertEquals(22, a.getF1());
Assert.assertEquals(33, a.getF2().intValue());
}
public static class Entity {
private int f1 = 124;
private Integer f2 = 123;
@JSONCreator
public Entity(@JSONField(name="f1") int f1, @JSONField(name="f2") Integer f2){
this.f1 = f1;
this.f2 = f2;
}
public int getF1() {
return f1;
}
public Integer getF2() {
return f2;
}
}
}
@@ -0,0 +1,51 @@
package com.alibaba.json.bvt.parser.deser;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONCreator;
import com.alibaba.fastjson.annotation.JSONField;
public class LongFieldDeserializerTest3 extends TestCase {
public void test_0() throws Exception {
Entity a = JSON.parseObject("{f1:123, f2:null}", Entity.class);
Assert.assertEquals(123L, a.getF1());
Assert.assertEquals(null, a.getF2());
}
public void test_1() throws Exception {
Entity a = JSON.parseObject("{f1:22, f2:'33'}", Entity.class);
Assert.assertEquals(22L, a.getF1());
Assert.assertEquals(33L, a.getF2().intValue());
}
public void test_2() throws Exception {
Entity a = JSON.parseObject("{f1:'22', f2:33}", Entity.class);
Assert.assertEquals(22L, a.getF1());
Assert.assertEquals(33L, a.getF2().intValue());
}
public static class Entity {
private long f1 = 124;
private Long f2 = 123L;
@JSONCreator
public Entity(@JSONField(name = "f1") long f1, @JSONField(name = "f2") Long f2){
this.f1 = f1;
this.f2 = f2;
}
public long getF1() {
return f1;
}
public Long getF2() {
return f2;
}
}
}
@@ -1,23 +1,24 @@
package com.alibaba.json.bvt.parser.deser;
import java.awt.Color;
import java.sql.Time;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import org.junit.Assert;
import junit.framework.TestCase;
public class TimeDeserializerTest2 extends TestCase {
public void test_0 () throws Exception {
public void test_0() throws Exception {
long millis = System.currentTimeMillis();
JSON.parse("{\"@type\":\"java.sql.Time\",\"value\":" + millis + "}");
}
public void test_error() throws Exception {
long millis = System.currentTimeMillis();
Exception error = null;
try {
JSON.parse("{\"@type\":\"java.sql.Time\",33:" + millis + "}");
@@ -26,7 +27,7 @@ public class TimeDeserializerTest2 extends TestCase {
}
Assert.assertNotNull(error);
}
public void test_error_1() throws Exception {
Exception error = null;
try {
@@ -36,10 +37,10 @@ public class TimeDeserializerTest2 extends TestCase {
}
Assert.assertNotNull(error);
}
public void test_error_2() throws Exception {
long millis = System.currentTimeMillis();
Exception error = null;
try {
JSON.parse("{\"@type\":\"java.sql.Time\",\"value\":" + millis + ",}");
@@ -48,4 +49,28 @@ public class TimeDeserializerTest2 extends TestCase {
}
Assert.assertNotNull(error);
}
public void test_error_3() throws Exception {
Exception error = null;
try {
JSON.parseObject("{\"time\":{}}", VO.class);
} catch (JSONException ex) {
error = ex;
}
Assert.assertNotNull(error);
}
public static class VO {
private Time time;
public Time getTime() {
return time;
}
public void setTime(Time time) {
this.time = time;
}
}
}
@@ -0,0 +1,46 @@
package com.alibaba.json.bvt.parser.stream;
import java.math.BigDecimal;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.JSONReaderScanner;
public class JSONReaderScannerTest_negative extends TestCase {
public void test_double() throws Exception {
char[] chars = "{\"value\":-3.5D}".toCharArray();
DefaultJSONParser parser = new DefaultJSONParser(new JSONReaderScanner(chars, chars.length));
JSONObject json = parser.parseObject();
Assert.assertTrue(-3.5D == ((Double) json.get("value")).doubleValue());
parser.close();
}
public void test_float() throws Exception {
char[] chars = "{\"value\":-3.5F}".toCharArray();
DefaultJSONParser parser = new DefaultJSONParser(new JSONReaderScanner(chars, chars.length));
JSONObject json = parser.parseObject();
Assert.assertTrue(-3.5F == ((Float) json.get("value")).doubleValue());
parser.close();
}
public void test_decimal() throws Exception {
char[] chars = "{\"value\":-3.5}".toCharArray();
DefaultJSONParser parser = new DefaultJSONParser(new JSONReaderScanner(chars, chars.length));
JSONObject json = parser.parseObject();
Assert.assertEquals(new BigDecimal("-3.5"), json.get("value"));
parser.close();
}
public void test_long() throws Exception {
char[] chars = "{\"value\":-3L}".toCharArray();
DefaultJSONParser parser = new DefaultJSONParser(new JSONReaderScanner(chars, chars.length));
JSONObject json = parser.parseObject();
Assert.assertTrue(-3L == ((Long) json.get("value")).longValue());
parser.close();
}
}
@@ -0,0 +1,33 @@
package com.alibaba.json.bvt.serializer;
import java.io.StringWriter;
import junit.framework.TestCase;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
public class SerializeWriterTest_8 extends TestCase {
public void test_BrowserCompatible() throws Exception {
StringBuilder buf = new StringBuilder();
for (int i = 0; i < 1024; ++i) {
buf.append('a');
}
buf.append("中国");
buf.append("\0");
JSON.toJSONString(buf.toString(), SerializerFeature.BrowserCompatible);
}
public void test_writer() throws Exception {
StringBuilder buf = new StringBuilder();
for (int i = 0; i < 1024; ++i) {
buf.append('a');
}
buf.append("中国");
buf.append("\0");
StringWriter out = new StringWriter();
JSON.writeJSONStringTo(buf.toString(), out, SerializerFeature.BrowserCompatible);
}
}