-
Notifications
You must be signed in to change notification settings - Fork 2
/
AtdjTest.java
89 lines (77 loc) · 2.43 KB
/
AtdjTest.java
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.io.*;
import java.util.*;
import org.json.*;
import org.junit.Test;
import static org.junit.Assert.*;
import com.mylife.test.*;
public class AtdjTest {
@Test
public void testSum() throws JSONException {
SampleSum s = new SampleSum();
boolean errorDetected;
try {
s.toJson();
errorDetected = false;
} catch (JSONException e) {
errorDetected = true;
}
assertTrue(errorDetected);
s.setSimpleTag();
assertEquals(SampleSum.Tag.SIMPLE_TAG, s.tag());
SimpleRecord r = new SimpleRecord();
assertEquals(null, r.o);
r.o = true;
assertEquals(true, r.o);
s.setSimpleRecord(r);
assertEquals(SampleSum.Tag.SIMPLE_RECORD, s.tag());
assertTrue(s.getSimpleRecord() != null);
assertTrue(s.getComplexRecord() == null);
}
@Test
public void testMissingField() throws JSONException {
ComplexRecord x = new ComplexRecord();
boolean errorDetected;
try {
x.toJson();
errorDetected = false;
} catch (JSONException e) {
errorDetected = true;
}
assertTrue(errorDetected);
}
@Test
public void testRecordSerialization() throws JSONException {
ComplexRecord x = new ComplexRecord();
x.b = true;
x.i = -123;
x.s = "\u0000 Hello!\n\r\t\u007f";
x.l = new ArrayList<Boolean>();
x.l.add(true);
x.l.add(false);
x.sample_sum = new SampleSum();
x.sample_sum.setS("Hippopotamus");
x.class_ = 99;
/* omitting optional x.final_ */
x.l2 = new ArrayList<RecordWithDefaults>();
String json = x.toJson();
System.out.println("This is x: " + json);
ComplexRecord x2 = new ComplexRecord(json);
String json2 = x2.toJson();
assertEquals(json, json2);
}
@Test
public void testComplexRecord() throws JSONException {
ComplexRecord v = new ComplexRecord("{\"b\": true, \"i\": 42, \"s\": \"foo\", \"o\": [\"Some\", true], \"l\": [true, false], \"sample_sum\": \"Simple_tag\", \"l2\":[]}");
ComplexRecord v2 = new ComplexRecord("{\"b\": true, \"i\": 42, \"s\": \"foo\", \"o\": [\"Some\", true], \"l\": [true, false], \"sample_sum\": [\"Simple_record\",{\"o\":true}], \"l2\":[]}");
assertEquals(true, v.b);
v.b = false;
assertEquals(42, (int)v.i);
assertEquals("foo", v.s);
assertEquals(2, v.l.size());
assertEquals(true, v.l.get(0));
assertEquals(false, v.l.get(1));
}
public static void main(String[] args) {
org.junit.runner.JUnitCore.main("AtdjTest");
}
}