forked from json-iterator/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestExisting.java
More file actions
71 lines (60 loc) · 2.52 KB
/
TestExisting.java
File metadata and controls
71 lines (60 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.jsoniter;
import com.jsoniter.spi.TypeLiteral;
import junit.framework.TestCase;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
public class TestExisting extends TestCase {
static {
// JsonIterator.setMode(DecodingMode.REFLECTION_MODE);
}
public static class TestObj1 {
public String field1;
public String field2;
}
public void test_direct_reuse() throws IOException {
TestObj1 testObj = new TestObj1();
testObj.field2 = "world";
JsonIterator iter = JsonIterator.parse("{ 'field1' : 'hello' }".replace('\'', '"'));
TestObj1 oldObj = testObj;
testObj = iter.read(testObj);
assertEquals("hello", testObj.field1);
assertEquals(System.identityHashCode(oldObj), System.identityHashCode(testObj));
}
public static class TestObj2 {
public String field3;
public TestObj1 field4;
}
public void test_indirect_reuse() throws IOException {
TestObj2 testObj = new TestObj2();
testObj.field4 = new TestObj1();
testObj.field4.field1 = "world";
JsonIterator iter = JsonIterator.parse("{ 'field3' : 'hello', 'field4': {'field2': 'hello'} }".replace('\'', '"'));
TestObj2 oldObj = testObj;
testObj = iter.read(testObj);
assertEquals("hello", testObj.field3);
assertEquals("hello", testObj.field4.field2);
assertEquals(System.identityHashCode(oldObj), System.identityHashCode(testObj));
}
public void test_reuse_list() throws IOException {
List list1 = new ArrayList();
JsonIterator iter = JsonIterator.parse("[1]");
List list2= iter.read(new TypeLiteral<List<Integer>>(){}, list1);
assertEquals(System.identityHashCode(list2), System.identityHashCode(list1));
}
public void test_reuse_linked_list() throws IOException {
LinkedList list1 = new LinkedList();
JsonIterator iter = JsonIterator.parse("[1]");
List list2= iter.read(new TypeLiteral<LinkedList<Integer>>(){}, list1);
assertEquals(System.identityHashCode(list2), System.identityHashCode(list1));
}
public void test_reuse_map() throws IOException {
JsonIterator iter = JsonIterator.parse("{ 'field1' : 'hello' }".replace('\'', '"'));
HashMap<String, Object> map1 = new HashMap<String, Object>();
map1.put("a", "b");
HashMap<String, Object> map2 = iter.read(map1);
assertEquals("b", map2.get("a"));
}
}