mirror of
https://github.com/alibaba/fastjson.git
synced 2026-10-25 02:57:41 +08:00
addtestcase & remove unused code
This commit is contained in:
@@ -1,78 +0,0 @@
|
||||
package com.alibaba.json.bvt.parser;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.util.Map;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.fastjson.util.AntiCollisionHashMap;
|
||||
|
||||
public class AnticollisionHashMapTest extends TestCase {
|
||||
|
||||
public void testHash() {
|
||||
try {
|
||||
InputStream in = AnticollisionHashMapTest.class
|
||||
.getResourceAsStream("/hashcollide.txt");
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(in));
|
||||
String line = br.readLine();
|
||||
br.close();
|
||||
String[] kvs = line.split("&");
|
||||
long start = System.currentTimeMillis();
|
||||
Map<String, String> m = new AntiCollisionHashMap<String, String>();
|
||||
for (String kv : kvs) {
|
||||
String[] cols = kv.split("=");
|
||||
m.put(cols[0], "test");
|
||||
}
|
||||
System.out.println("map size: " + m.size());
|
||||
System.out.println("take time:"
|
||||
+ (System.currentTimeMillis() - start));
|
||||
|
||||
|
||||
m = new AntiCollisionHashMap<String, String>();
|
||||
m.put("axmantest", "12345");
|
||||
m.put("axmantest1", "123451");
|
||||
m.put("axmantest2", "123452");
|
||||
m.put("axmantest3", "123453");
|
||||
System.out.println(m.get("axmantest"));
|
||||
System.out.println(m.get("axmantest1"));
|
||||
System.out.println(m.get("axmantest2"));
|
||||
System.out.println(m.get("axmantest3"));
|
||||
ByteArrayOutputStream buf = new ByteArrayOutputStream();
|
||||
ObjectOutputStream out = new ObjectOutputStream(buf);
|
||||
out.writeObject(m);
|
||||
out.flush();
|
||||
byte[] data = buf.toByteArray();
|
||||
out.close();
|
||||
|
||||
ByteArrayInputStream inbuf = new ByteArrayInputStream(data);
|
||||
ObjectInputStream oin = new ObjectInputStream(inbuf);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
AntiCollisionHashMap<String, String> m1 = (AntiCollisionHashMap<String, String>)oin.readObject();
|
||||
oin.close();
|
||||
System.out.println(m1.get("axmantest"));
|
||||
System.out.println(m1.get("axmantest1"));
|
||||
System.out.println(m1.get("axmantest2"));
|
||||
System.out.println(m1.get("axmantest3"));
|
||||
|
||||
// start = System.currentTimeMillis();
|
||||
// Map<String, String> m1 = new HashMap<String, String>();
|
||||
// for (String kv : kvs) {
|
||||
// String[] cols = kv.split("=");
|
||||
// m1.put(cols[0], "test");
|
||||
// }
|
||||
// System.out.println("map size: " + m.size());
|
||||
// System.out.println("take time:"
|
||||
// + (System.currentTimeMillis() - start));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
package com.alibaba.json.bvt.parser;
|
||||
|
||||
import org.junit.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.fastjson.parser.JSONToken;
|
||||
import com.alibaba.fastjson.parser.deserializer.ArrayDeserializer;
|
||||
import com.alibaba.fastjson.parser.deserializer.ArrayListStringDeserializer;
|
||||
import com.alibaba.fastjson.parser.deserializer.AtomicIntegerArrayDeserializer;
|
||||
import com.alibaba.fastjson.parser.deserializer.AtomicLongArrayDeserializer;
|
||||
import com.alibaba.fastjson.parser.deserializer.CharacterDeserializer;
|
||||
@@ -22,7 +22,6 @@ import com.alibaba.fastjson.parser.deserializer.TimestampDeserializer;
|
||||
public class FastMatchCheckTest extends TestCase {
|
||||
public void test_match() throws Exception {
|
||||
Assert.assertEquals(JSONToken.LBRACKET, AtomicIntegerArrayDeserializer.instance.getFastMatchToken());
|
||||
Assert.assertEquals(JSONToken.LBRACKET, ArrayListStringDeserializer.instance.getFastMatchToken());
|
||||
Assert.assertEquals(JSONToken.LBRACKET, AtomicLongArrayDeserializer.instance.getFastMatchToken());
|
||||
Assert.assertEquals(JSONToken.LITERAL_STRING, InetAddressDeserializer.instance.getFastMatchToken());
|
||||
Assert.assertEquals(JSONToken.LITERAL_STRING, LocaleDeserializer.instance.getFastMatchToken());
|
||||
|
||||
@@ -20,20 +20,7 @@ public class JSONScannerTest_symbol extends TestCase {
|
||||
JSONScanner lexer = new JSONScanner("\"name\"");
|
||||
String symbol = lexer.scanSymbol(symbolTable, '"');
|
||||
Assert.assertTrue("name".equals(symbol));
|
||||
|
||||
String symbol2 = lexer.symbol(symbolTable);
|
||||
Assert.assertTrue("name".equals(symbol2));
|
||||
}
|
||||
|
||||
public void test_0_1() throws Exception {
|
||||
SymbolTable symbolTable = new SymbolTable();
|
||||
|
||||
JSONScanner lexer = new JSONScanner("\"name\"");
|
||||
lexer.scanString();
|
||||
String symbol = lexer.symbol(symbolTable);
|
||||
Assert.assertTrue("name".equals(symbol));
|
||||
|
||||
Assert.assertTrue("name" != lexer.symbol(null));
|
||||
lexer.close();
|
||||
}
|
||||
|
||||
public void test_1() throws Exception {
|
||||
@@ -42,6 +29,7 @@ public class JSONScannerTest_symbol extends TestCase {
|
||||
JSONScanner lexer = new JSONScanner("\"nick name\"");
|
||||
String symbol = lexer.scanSymbol(symbolTable, '"');
|
||||
Assert.assertTrue("nick name".equals(symbol));
|
||||
lexer.close();
|
||||
}
|
||||
|
||||
public void test_2() throws Exception {
|
||||
@@ -50,17 +38,7 @@ public class JSONScannerTest_symbol extends TestCase {
|
||||
JSONScanner lexer = new JSONScanner("\"nick \\\"name\"");
|
||||
String symbol = lexer.scanSymbol(symbolTable, '"');
|
||||
Assert.assertTrue("nick \"name" == symbol);
|
||||
}
|
||||
|
||||
public void test_2_1() throws Exception {
|
||||
SymbolTable symbolTable = new SymbolTable();
|
||||
|
||||
JSONScanner lexer = new JSONScanner("\"nick \\\"name\"");
|
||||
lexer.scanString();
|
||||
String symbol = lexer.symbol(symbolTable);
|
||||
Assert.assertTrue("nick \"name" == symbol);
|
||||
|
||||
Assert.assertTrue("nick \"name" != lexer.symbol(null));
|
||||
lexer.close();
|
||||
}
|
||||
|
||||
public void test_3() throws Exception {
|
||||
@@ -69,6 +47,7 @@ public class JSONScannerTest_symbol extends TestCase {
|
||||
JSONScanner lexer = new JSONScanner("\"nick \\\\name\"");
|
||||
String symbol = lexer.scanSymbol(symbolTable, '"');
|
||||
Assert.assertTrue("nick \\name" == symbol);
|
||||
lexer.close();
|
||||
}
|
||||
|
||||
public void test_4() throws Exception {
|
||||
@@ -77,6 +56,7 @@ public class JSONScannerTest_symbol extends TestCase {
|
||||
JSONScanner lexer = new JSONScanner("\"nick \\/name\"");
|
||||
String symbol = lexer.scanSymbol(symbolTable, '"');
|
||||
Assert.assertTrue("nick /name" == symbol);
|
||||
lexer.close();
|
||||
}
|
||||
|
||||
public void test_5() throws Exception {
|
||||
@@ -85,6 +65,7 @@ public class JSONScannerTest_symbol extends TestCase {
|
||||
JSONScanner lexer = new JSONScanner("\"nick \\bname\"");
|
||||
String symbol = lexer.scanSymbol(symbolTable, '"');
|
||||
Assert.assertTrue("nick \bname" == symbol);
|
||||
lexer.close();
|
||||
}
|
||||
|
||||
public void test_6() throws Exception {
|
||||
@@ -93,6 +74,7 @@ public class JSONScannerTest_symbol extends TestCase {
|
||||
JSONScanner lexer = new JSONScanner("\"nick \\f name\"");
|
||||
String symbol = lexer.scanSymbol(symbolTable, '"');
|
||||
Assert.assertTrue("nick \f name" == symbol);
|
||||
lexer.close();
|
||||
}
|
||||
|
||||
public void test_7() throws Exception {
|
||||
@@ -101,6 +83,7 @@ public class JSONScannerTest_symbol extends TestCase {
|
||||
JSONScanner lexer = new JSONScanner("\"nick \\F name\"");
|
||||
String symbol = lexer.scanSymbol(symbolTable, '"');
|
||||
Assert.assertTrue("nick \f name" == symbol);
|
||||
lexer.close();
|
||||
}
|
||||
|
||||
public void test_8() throws Exception {
|
||||
@@ -109,6 +92,7 @@ public class JSONScannerTest_symbol extends TestCase {
|
||||
JSONScanner lexer = new JSONScanner("\"nick \\n name\"");
|
||||
String symbol = lexer.scanSymbol(symbolTable, '"');
|
||||
Assert.assertTrue("nick \n name" == symbol);
|
||||
lexer.close();
|
||||
}
|
||||
|
||||
public void test_9() throws Exception {
|
||||
@@ -117,6 +101,7 @@ public class JSONScannerTest_symbol extends TestCase {
|
||||
JSONScanner lexer = new JSONScanner("\"nick \\r name\"");
|
||||
String symbol = lexer.scanSymbol(symbolTable, '"');
|
||||
Assert.assertTrue("nick \r name" == symbol);
|
||||
lexer.close();
|
||||
}
|
||||
|
||||
public void test_10() throws Exception {
|
||||
@@ -125,6 +110,7 @@ public class JSONScannerTest_symbol extends TestCase {
|
||||
JSONScanner lexer = new JSONScanner("\"nick \\t name\"");
|
||||
String symbol = lexer.scanSymbol(symbolTable, '"');
|
||||
Assert.assertTrue("nick \t name" == symbol);
|
||||
lexer.close();
|
||||
}
|
||||
|
||||
public void test_11() throws Exception {
|
||||
@@ -133,6 +119,7 @@ public class JSONScannerTest_symbol extends TestCase {
|
||||
JSONScanner lexer = new JSONScanner("\"nick \\u4e2d name\"");
|
||||
String symbol = lexer.scanSymbol(symbolTable, '"');
|
||||
Assert.assertTrue("nick 中 name" == symbol);
|
||||
lexer.close();
|
||||
}
|
||||
|
||||
public void test_12() throws Exception {
|
||||
@@ -142,6 +129,7 @@ public class JSONScannerTest_symbol extends TestCase {
|
||||
"\"\\tabcdefghijklmnopqrstuvwxyz01234567890abcdefghijklmnopqrstuvwxyz01234567890abcdefghijklmnopqrstuvwxyz01234567890abcdefghijklmnopqrstuvwxyz01234567890\"");
|
||||
String symbol = lexer.scanSymbol(symbolTable, '"');
|
||||
Assert.assertTrue("\tabcdefghijklmnopqrstuvwxyz01234567890abcdefghijklmnopqrstuvwxyz01234567890abcdefghijklmnopqrstuvwxyz01234567890abcdefghijklmnopqrstuvwxyz01234567890" == symbol);
|
||||
lexer.close();
|
||||
}
|
||||
|
||||
public void test_error() throws Exception {
|
||||
@@ -151,6 +139,7 @@ public class JSONScannerTest_symbol extends TestCase {
|
||||
|
||||
JSONScanner lexer = new JSONScanner("\"nick \\a name\"");
|
||||
lexer.scanSymbol(symbolTable, '"');
|
||||
lexer.close();
|
||||
} catch (JSONException e) {
|
||||
error = e;
|
||||
}
|
||||
@@ -164,6 +153,7 @@ public class JSONScannerTest_symbol extends TestCase {
|
||||
|
||||
JSONScanner lexer = new JSONScanner("\"name");
|
||||
lexer.scanSymbol(symbolTable, '"');
|
||||
lexer.close();
|
||||
} catch (JSONException e) {
|
||||
error = e;
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
@@ -65,6 +66,7 @@ public class ArrayListTypeDeserializerTest extends TestCase {
|
||||
return value;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public void setValue(ArrayList<Integer> value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.alibaba.json.bvt.parser.deser;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
|
||||
public class DateFormatDeserializerTest extends TestCase {
|
||||
|
||||
public void test_dateFormat_empty() throws Exception {
|
||||
VO vo = JSON.parseObject("{\"format\":\"\"}", VO.class);
|
||||
Assert.assertEquals(null, vo.getFormat());
|
||||
}
|
||||
|
||||
public void test_dateFormat_null() throws Exception {
|
||||
VO vo = JSON.parseObject("{\"format\":null}", VO.class);
|
||||
Assert.assertEquals(null, vo.getFormat());
|
||||
}
|
||||
|
||||
public void test_dateFormat_yyyy() throws Exception {
|
||||
VO vo = JSON.parseObject("{\"format\":\"yyyy\"}", VO.class);
|
||||
Assert.assertEquals(new SimpleDateFormat("yyyy"), vo.getFormat());
|
||||
}
|
||||
|
||||
public void test_dateFormat_error() throws Exception {
|
||||
Exception error = null;
|
||||
try {
|
||||
JSON.parseObject("{\"format\":123}", VO.class);
|
||||
} catch (Exception e) {
|
||||
error = e;
|
||||
}
|
||||
Assert.assertNotNull(error);
|
||||
}
|
||||
|
||||
public static class VO {
|
||||
|
||||
private SimpleDateFormat format;
|
||||
|
||||
public VO(){
|
||||
|
||||
}
|
||||
|
||||
public VO(SimpleDateFormat format){
|
||||
this.format = format;
|
||||
}
|
||||
|
||||
public SimpleDateFormat getFormat() {
|
||||
return format;
|
||||
}
|
||||
|
||||
public void setFormat(SimpleDateFormat format) {
|
||||
this.format = format;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -7,17 +7,18 @@ import java.util.TreeMap;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
import org.junit.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.fastjson.TypeReference;
|
||||
import com.alibaba.fastjson.parser.DefaultExtJSONParser;
|
||||
import com.alibaba.fastjson.parser.ParserConfig;
|
||||
import com.alibaba.fastjson.parser.deserializer.DefaultObjectDeserializer;
|
||||
import com.alibaba.fastjson.util.AntiCollisionHashMap;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public class DefaultObjectDeserializerTest2 extends TestCase {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
@@ -52,7 +53,7 @@ public class DefaultObjectDeserializerTest2 extends TestCase {
|
||||
SortedMap<String, Map> map = JSON.parseObject(input, new TypeReference<SortedMap<String, Map>>() {
|
||||
}.getType());
|
||||
|
||||
Assert.assertEquals(AntiCollisionHashMap.class, map.get("map").getClass());
|
||||
Assert.assertEquals(HashMap.class, map.get("map").getClass());
|
||||
}
|
||||
|
||||
public void test_3() throws Exception {
|
||||
|
||||
@@ -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_chars 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,57 @@
|
||||
package com.alibaba.json.bvt.parser.stream;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.fastjson.parser.DefaultJSONParser;
|
||||
import com.alibaba.fastjson.parser.JSONReaderScanner;
|
||||
|
||||
public class JSONReaderScannerTest_enum extends TestCase {
|
||||
|
||||
public void test_a() throws Exception {
|
||||
DefaultJSONParser parser = new DefaultJSONParser(new JSONReaderScanner("{\"type\":\"A\"}"));
|
||||
VO vo = parser.parseObject(VO.class);
|
||||
Assert.assertEquals(Type.A, vo.getType());
|
||||
parser.close();
|
||||
}
|
||||
|
||||
public void test_b() throws Exception {
|
||||
DefaultJSONParser parser = new DefaultJSONParser(new JSONReaderScanner("{\"type\":\"B\"}"));
|
||||
VO vo = parser.parseObject(VO.class);
|
||||
Assert.assertEquals(Type.B, vo.getType());
|
||||
parser.close();
|
||||
}
|
||||
|
||||
public void test_c() throws Exception {
|
||||
DefaultJSONParser parser = new DefaultJSONParser(new JSONReaderScanner("{\"type\":\"C\"}"));
|
||||
VO vo = parser.parseObject(VO.class);
|
||||
Assert.assertEquals(Type.C, vo.getType());
|
||||
parser.close();
|
||||
}
|
||||
|
||||
public void test_x() throws Exception {
|
||||
DefaultJSONParser parser = new DefaultJSONParser(new JSONReaderScanner("{\"type\":\"XXXXXXXXXXXXXXXXXXXXXXXX\"}"));
|
||||
VO vo = parser.parseObject(VO.class);
|
||||
Assert.assertEquals(Type.XXXXXXXXXXXXXXXXXXXXXXXX, vo.getType());
|
||||
parser.close();
|
||||
}
|
||||
|
||||
public static class VO {
|
||||
|
||||
private Type type;
|
||||
|
||||
public Type getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(Type type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static enum Type {
|
||||
A, B, C, D, XXXXXXXXXXXXXXXXXXXXXXXX
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.alibaba.json.bvt.parser.stream;
|
||||
|
||||
import java.io.StringReader;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONReader;
|
||||
|
||||
public class JSONReader_array extends TestCase {
|
||||
|
||||
public void test_array() throws Exception {
|
||||
JSONReader reader = new JSONReader(new StringReader("[[],[],3,null,{\"name\":\"jobs\"},{\"id\":123},{\"id\":1},{\"id\":2}]"));
|
||||
reader.startArray();
|
||||
|
||||
JSONArray first = (JSONArray) reader.readObject();
|
||||
JSONArray second = (JSONArray) reader.readObject();
|
||||
|
||||
Assert.assertNotNull(first);
|
||||
Assert.assertNotNull(second);
|
||||
|
||||
Assert.assertEquals(new Integer(3), reader.readInteger());
|
||||
Assert.assertNull(reader.readString());
|
||||
|
||||
{
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
reader.readObject(map);
|
||||
Assert.assertEquals("jobs", map.get("name"));
|
||||
}
|
||||
|
||||
{
|
||||
VO vo = new VO();
|
||||
reader.readObject(vo);
|
||||
Assert.assertEquals(123, vo.getId());
|
||||
}
|
||||
|
||||
while (reader.hasNext()) {
|
||||
VO vo = reader.readObject(VO.class);
|
||||
Assert.assertNotNull(vo);
|
||||
}
|
||||
reader.endArray();
|
||||
reader.close();
|
||||
}
|
||||
|
||||
public static class VO {
|
||||
|
||||
private int id;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.alibaba.json.bvt.parser.stream;
|
||||
|
||||
import java.io.StringReader;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.fastjson.JSONReader;
|
||||
|
||||
public class JSONReader_map extends TestCase {
|
||||
|
||||
public void test_array() throws Exception {
|
||||
JSONReader reader = new JSONReader(new StringReader("[{\"id\":123}]"));
|
||||
|
||||
reader.startArray();
|
||||
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
reader.readObject(map);
|
||||
|
||||
Assert.assertEquals(123, map.get("id"));
|
||||
|
||||
reader.endArray();
|
||||
|
||||
reader.close();
|
||||
}
|
||||
|
||||
public void test_map() throws Exception {
|
||||
JSONReader reader = new JSONReader(new StringReader("{\"id\":123}"));
|
||||
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
reader.readObject(map);
|
||||
|
||||
Assert.assertEquals(123, map.get("id"));
|
||||
|
||||
reader.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.alibaba.json.bvt.parser.stream;
|
||||
|
||||
import java.io.StringReader;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.fastjson.JSONReader;
|
||||
|
||||
public class JSONReader_obj extends TestCase {
|
||||
|
||||
public void test_array() throws Exception {
|
||||
JSONReader reader = new JSONReader(new StringReader("[{\"id\":123}]"));
|
||||
|
||||
reader.startArray();
|
||||
|
||||
VO vo = new VO();
|
||||
reader.readObject(vo);
|
||||
|
||||
Assert.assertEquals(123, vo.getId());
|
||||
|
||||
reader.endArray();
|
||||
|
||||
reader.close();
|
||||
}
|
||||
|
||||
public void test_obj() throws Exception {
|
||||
JSONReader reader = new JSONReader(new StringReader("{\"id\":123}"));
|
||||
|
||||
VO vo = new VO();
|
||||
reader.readObject(vo);
|
||||
|
||||
Assert.assertEquals(123, vo.getId());
|
||||
|
||||
reader.close();
|
||||
}
|
||||
|
||||
public static class VO {
|
||||
|
||||
private int id;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.alibaba.json.bvt.parser.stream;
|
||||
|
||||
import java.io.StringReader;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.fastjson.JSONReader;
|
||||
|
||||
public class JSONReader_obj_2 extends TestCase {
|
||||
|
||||
public void test_array() throws Exception {
|
||||
JSONReader reader = new JSONReader(new StringReader("[{\"id\":123}]"));
|
||||
|
||||
reader.startArray();
|
||||
|
||||
VO vo = reader.readObject(VO.class);
|
||||
|
||||
Assert.assertEquals(123, vo.getId());
|
||||
|
||||
reader.endArray();
|
||||
|
||||
reader.close();
|
||||
}
|
||||
|
||||
public void test_obj() throws Exception {
|
||||
JSONReader reader = new JSONReader(new StringReader("{\"id\":123}"));
|
||||
|
||||
VO vo = reader.readObject(VO.class);
|
||||
|
||||
Assert.assertEquals(123, vo.getId());
|
||||
|
||||
reader.close();
|
||||
}
|
||||
|
||||
public static class VO {
|
||||
|
||||
private int id;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.alibaba.json.bvt.parser.stream;
|
||||
|
||||
import java.io.StringReader;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.fastjson.JSONReader;
|
||||
|
||||
public class JSONReader_obj_3 extends TestCase {
|
||||
|
||||
public void test_obj() throws Exception {
|
||||
JSONReader reader = new JSONReader(new StringReader("{\"id\":123}"));
|
||||
|
||||
reader.startObject();
|
||||
Assert.assertEquals("id", reader.readString());
|
||||
Assert.assertEquals(Integer.valueOf(123), reader.readInteger());
|
||||
reader.endObject();
|
||||
|
||||
reader.close();
|
||||
}
|
||||
|
||||
public void test_obj_2() throws Exception {
|
||||
JSONReader reader = new JSONReader(new StringReader("{\"val\":{\"id\":123}}"));
|
||||
|
||||
reader.startObject();
|
||||
|
||||
Assert.assertEquals("val", reader.readString());
|
||||
|
||||
reader.startObject();
|
||||
Assert.assertEquals("id", reader.readString());
|
||||
Assert.assertEquals(Integer.valueOf(123), reader.readInteger());
|
||||
reader.endObject();
|
||||
|
||||
reader.endObject();
|
||||
|
||||
reader.close();
|
||||
}
|
||||
|
||||
public void test_obj_3() throws Exception {
|
||||
JSONReader reader = new JSONReader(new StringReader("{\"val\":{\"val\":{\"id\":123}}}"));
|
||||
|
||||
reader.startObject();
|
||||
Assert.assertEquals("val", reader.readString());
|
||||
|
||||
reader.startObject();
|
||||
Assert.assertEquals("val", reader.readString());
|
||||
|
||||
reader.startObject();
|
||||
Assert.assertEquals("id", reader.readString());
|
||||
Assert.assertEquals(Long.valueOf(123), reader.readLong());
|
||||
reader.endObject();
|
||||
|
||||
reader.endObject();
|
||||
reader.endObject();
|
||||
|
||||
reader.close();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.alibaba.json.bvt.parser.stream;
|
||||
|
||||
import java.io.StringReader;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.fastjson.JSONReader;
|
||||
|
||||
public class JSONReader_string extends TestCase {
|
||||
|
||||
public void test_array() throws Exception {
|
||||
JSONReader reader = new JSONReader(new StringReader("[\"abc\"]"));
|
||||
|
||||
reader.startArray();
|
||||
|
||||
Assert.assertEquals("abc", reader.readString());
|
||||
|
||||
reader.endArray();
|
||||
|
||||
reader.close();
|
||||
}
|
||||
|
||||
public void test_array_2() throws Exception {
|
||||
JSONReader reader = new JSONReader(new StringReader("[[\"abc\"]]"));
|
||||
|
||||
reader.startArray();
|
||||
reader.startArray();
|
||||
|
||||
Assert.assertEquals("abc", reader.readString());
|
||||
|
||||
reader.endArray();
|
||||
reader.endArray();
|
||||
|
||||
reader.close();
|
||||
}
|
||||
|
||||
public void test_array_3() throws Exception {
|
||||
JSONReader reader = new JSONReader(new StringReader("[[[\"abc\"]]]"));
|
||||
|
||||
reader.startArray();
|
||||
reader.startArray();
|
||||
reader.startArray();
|
||||
|
||||
Assert.assertEquals("abc", reader.readString());
|
||||
|
||||
reader.endArray();
|
||||
reader.endArray();
|
||||
reader.endArray();
|
||||
|
||||
reader.close();
|
||||
}
|
||||
|
||||
public void test_obj() throws Exception {
|
||||
JSONReader reader = new JSONReader(new StringReader("\"abc\""));
|
||||
|
||||
Assert.assertEquals("abc", reader.readString());
|
||||
|
||||
reader.close();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.alibaba.json.bvt.parser.stream;
|
||||
|
||||
import java.io.StringReader;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.fastjson.JSONReader;
|
||||
import com.alibaba.fastjson.TypeReference;
|
||||
|
||||
public class JSONReader_typeRef extends TestCase {
|
||||
public void test_array() throws Exception {
|
||||
JSONReader reader = new JSONReader(new StringReader("[{\"id\":123}]"));
|
||||
|
||||
List<VO> list = reader.readObject(new TypeReference<List<VO>>() {}.getType());
|
||||
|
||||
Assert.assertEquals(123, list.get(0).getId());
|
||||
|
||||
reader.close();
|
||||
}
|
||||
|
||||
public void test_array_1() throws Exception {
|
||||
JSONReader reader = new JSONReader(new StringReader("[[{\"id\":123}]]"));
|
||||
|
||||
reader.startArray();
|
||||
List<VO> list = reader.readObject(new TypeReference<List<VO>>() {}.getType());
|
||||
|
||||
Assert.assertEquals(123, list.get(0).getId());
|
||||
|
||||
reader.endArray();
|
||||
|
||||
reader.close();
|
||||
}
|
||||
|
||||
public static class VO {
|
||||
|
||||
private int id;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,37 +1,50 @@
|
||||
package com.alibaba.json.bvt.serializer;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Rectangle;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.serializer.ColorSerializer;
|
||||
import com.alibaba.fastjson.serializer.JSONSerializer;
|
||||
import com.alibaba.fastjson.serializer.SerializerFeature;
|
||||
|
||||
|
||||
public class ColorSerializerTest extends TestCase {
|
||||
|
||||
|
||||
public void test_null() throws Exception {
|
||||
JSONSerializer serializer = new JSONSerializer();
|
||||
Assert.assertEquals(ColorSerializer.class, serializer.getObjectWriter(Color.class).getClass());
|
||||
|
||||
|
||||
VO vo = new VO();
|
||||
|
||||
|
||||
Assert.assertEquals("{\"value\":null}", JSON.toJSONString(vo, SerializerFeature.WriteMapNullValue));
|
||||
}
|
||||
|
||||
public void test_rgb() throws Exception {
|
||||
JSONSerializer serializer = new JSONSerializer();
|
||||
Assert.assertEquals(ColorSerializer.class, serializer.getObjectWriter(Color.class).getClass());
|
||||
|
||||
VO vo = new VO();
|
||||
vo.setValue(new Color(1,1,1,0));
|
||||
|
||||
Assert.assertEquals("{\"value\":{\"r\":1,\"g\":1,\"b\":1}}", JSON.toJSONString(vo, SerializerFeature.WriteMapNullValue));
|
||||
}
|
||||
|
||||
public void test_rgb_getAutowiredFor() throws Exception {
|
||||
|
||||
}
|
||||
|
||||
private static class VO {
|
||||
|
||||
private Rectangle value;
|
||||
private Color value;
|
||||
|
||||
public Rectangle getValue() {
|
||||
public Color getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(Rectangle value) {
|
||||
public void setValue(Color value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
package com.alibaba.json.bvt.serializer;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.serializer.DateFormatSerializer;
|
||||
import com.alibaba.fastjson.serializer.JSONSerializer;
|
||||
import com.alibaba.fastjson.serializer.SerializeConfig;
|
||||
import com.alibaba.fastjson.serializer.SerializeWriter;
|
||||
import com.alibaba.fastjson.serializer.SerializerFeature;
|
||||
|
||||
public class DateFormatSerializerTest extends TestCase {
|
||||
|
||||
public void test_date() throws Exception {
|
||||
Assert.assertEquals("{\"format\":null}", JSON.toJSONString(new VO(), SerializerFeature.WriteMapNullValue));
|
||||
}
|
||||
|
||||
public void test_date_2() throws Exception {
|
||||
SerializeWriter out = new SerializeWriter();
|
||||
SerializeConfig config = new SerializeConfig();
|
||||
config.put(Date.class, DateFormatSerializer.instance);
|
||||
JSONSerializer serializer = new JSONSerializer(out, config);
|
||||
|
||||
serializer.config(SerializerFeature.WriteMapNullValue, true);
|
||||
serializer.write(new VO());
|
||||
|
||||
Assert.assertEquals("{\"format\":null}", out.toString());
|
||||
}
|
||||
|
||||
public void test_date_3() throws Exception {
|
||||
SerializeWriter out = new SerializeWriter();
|
||||
SerializeConfig config = new SerializeConfig();
|
||||
config.put(Date.class, DateFormatSerializer.instance);
|
||||
JSONSerializer serializer = new JSONSerializer(out, config);
|
||||
|
||||
serializer.config(SerializerFeature.WriteClassName, true);
|
||||
serializer.write(new VO());
|
||||
|
||||
Assert.assertEquals("{\"@type\":\"com.alibaba.json.bvt.serializer.DateFormatSerializerTest$VO\"}",
|
||||
out.toString());
|
||||
}
|
||||
|
||||
public void test_date_4() throws Exception {
|
||||
SerializeWriter out = new SerializeWriter();
|
||||
SerializeConfig config = new SerializeConfig();
|
||||
config.put(Date.class, DateFormatSerializer.instance);
|
||||
JSONSerializer serializer = new JSONSerializer(out, config);
|
||||
|
||||
serializer.write(new VO(new SimpleDateFormat("yyyy")));
|
||||
|
||||
Assert.assertEquals("{\"format\":\"yyyy\"}", out.toString());
|
||||
|
||||
JSON.parseObject(out.toString(), VO.class);
|
||||
}
|
||||
|
||||
public static class VO {
|
||||
|
||||
private SimpleDateFormat format;
|
||||
|
||||
public VO(){
|
||||
|
||||
}
|
||||
|
||||
public VO(SimpleDateFormat format){
|
||||
this.format = format;
|
||||
}
|
||||
|
||||
public SimpleDateFormat getFormat() {
|
||||
return format;
|
||||
}
|
||||
|
||||
public void setFormat(SimpleDateFormat format) {
|
||||
this.format = format;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.alibaba.json.bvt.serializer;
|
||||
|
||||
import java.util.Enumeration;
|
||||
import java.util.Vector;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.serializer.SerializerFeature;
|
||||
|
||||
public class EnumerationSeriliazerTest extends TestCase {
|
||||
|
||||
public void test_nullAsEmtpyList() throws Exception {
|
||||
VO e = new VO();
|
||||
Assert.assertEquals("{\"elements\":[]}", JSON.toJSONString(e, SerializerFeature.WriteMapNullValue,
|
||||
SerializerFeature.WriteNullListAsEmpty));
|
||||
}
|
||||
|
||||
public void test_null() throws Exception {
|
||||
VO e = new VO();
|
||||
Assert.assertEquals("{\"elements\":null}", JSON.toJSONString(e, SerializerFeature.WriteMapNullValue));
|
||||
}
|
||||
|
||||
public void test_1() throws Exception {
|
||||
VO e = new VO(new Entity(),new Entity());
|
||||
Assert.assertEquals("{\"elements\":[{},{}]}", JSON.toJSONString(e, SerializerFeature.WriteMapNullValue));
|
||||
}
|
||||
|
||||
private static class VO {
|
||||
|
||||
private Enumeration<Entity> elements;
|
||||
|
||||
public VO(Entity... array){
|
||||
if (array.length > 0) {
|
||||
Vector<Entity> vector = new Vector<Entity>();
|
||||
for (Entity item : array) {
|
||||
vector.add(item);
|
||||
}
|
||||
this.elements = vector.elements();
|
||||
}
|
||||
}
|
||||
|
||||
public Enumeration<Entity> getElements() {
|
||||
return elements;
|
||||
}
|
||||
|
||||
public void setElements(Enumeration<Entity> elements) {
|
||||
this.elements = elements;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class Entity {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.alibaba.json.bvt.serializer;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.fastjson.serializer.JSONSerializer;
|
||||
import com.alibaba.fastjson.serializer.PascalNameFilter;
|
||||
|
||||
public class PascalNameFilterTest_1 extends TestCase {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
JSONSerializer serializer = new JSONSerializer();
|
||||
serializer.getNameFilters().add(new PascalNameFilter());
|
||||
|
||||
Map<String, Object> vo = new LinkedHashMap<String, Object>();
|
||||
vo.put("", 123);
|
||||
vo.put(null, "wenshao");
|
||||
|
||||
serializer.write(vo);
|
||||
|
||||
Assert.assertEquals("{\"\":123,null:\"wenshao\"}", serializer.toString());
|
||||
|
||||
serializer.close();
|
||||
}
|
||||
|
||||
}
|
||||
+62
-7
@@ -1,19 +1,74 @@
|
||||
package com.alibaba.json.bvt.support.spring;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpInputMessage;
|
||||
import org.springframework.http.HttpOutputMessage;
|
||||
import org.springframework.http.MediaType;
|
||||
|
||||
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
|
||||
|
||||
public class FastJsonHttpMessageConverterTest extends TestCase {
|
||||
public void test_() throws Exception {
|
||||
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
|
||||
converter.setCharset(Charset.forName("UTF-8"));
|
||||
Assert.assertEquals(Charset.forName("UTF-8"), converter.getCharset());
|
||||
|
||||
Assert.assertEquals(0, converter.getFeatures().length);
|
||||
}
|
||||
|
||||
public void test_read() throws Exception {
|
||||
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
|
||||
converter.setCharset(Charset.forName("UTF-8"));
|
||||
Assert.assertEquals(Charset.forName("UTF-8"), converter.getCharset());
|
||||
|
||||
Assert.assertEquals(0, converter.getFeatures().length);
|
||||
|
||||
HttpInputMessage input = new HttpInputMessage() {
|
||||
|
||||
public HttpHeaders getHeaders() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
public InputStream getBody() throws IOException {
|
||||
return new ByteArrayInputStream("{\"id\":123}".getBytes(Charset.forName("UTF-8")));
|
||||
}
|
||||
|
||||
};
|
||||
VO vo = (VO) converter.read(VO.class, input);
|
||||
Assert.assertEquals(123, vo.getId());
|
||||
|
||||
final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
|
||||
HttpOutputMessage out = new HttpOutputMessage() {
|
||||
|
||||
public HttpHeaders getHeaders() {
|
||||
return new HttpHeaders();
|
||||
}
|
||||
|
||||
public OutputStream getBody() throws IOException {
|
||||
return byteOut;
|
||||
}
|
||||
};
|
||||
converter.write(vo, MediaType.TEXT_PLAIN, out);
|
||||
|
||||
byte[] bytes = byteOut.toByteArray();
|
||||
Assert.assertEquals("{\"id\":123}", new String(bytes, "UTF-8"));
|
||||
}
|
||||
|
||||
public static class VO {
|
||||
|
||||
private int id;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.alibaba.json.bvt.support.spring;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
|
||||
import com.alibaba.fastjson.serializer.SerializerFeature;
|
||||
import com.alibaba.fastjson.support.spring.FastJsonJsonView;
|
||||
|
||||
public class FastJsonJsonViewTest extends TestCase {
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public void test_0() throws Exception {
|
||||
FastJsonJsonView view = new FastJsonJsonView();
|
||||
|
||||
Assert.assertEquals(Charset.forName("UTF-8"), view.getCharset());
|
||||
view.setCharset(Charset.forName("GBK"));
|
||||
Assert.assertEquals(Charset.forName("GBK"), view.getCharset());
|
||||
|
||||
Assert.assertNotNull(view.getFeatures());
|
||||
Assert.assertEquals(0, view.getFeatures().length);
|
||||
|
||||
view.setSerializerFeature(SerializerFeature.BrowserCompatible);
|
||||
Assert.assertEquals(1, view.getFeatures().length);
|
||||
Assert.assertEquals(SerializerFeature.BrowserCompatible, view.getFeatures()[0]);
|
||||
|
||||
view.setFeatures(SerializerFeature.DisableCheckSpecialChar, SerializerFeature.SortField);
|
||||
Assert.assertEquals(2, view.getFeatures().length);
|
||||
Assert.assertEquals(SerializerFeature.DisableCheckSpecialChar, view.getFeatures()[0]);
|
||||
Assert.assertEquals(SerializerFeature.SortField, view.getFeatures()[1]);
|
||||
|
||||
Map<String, Object> model = new HashMap<String, Object>();
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
view.render(model, request, response);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user