forked from stleary/JSON-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONObjectAsyncTest.java
More file actions
157 lines (133 loc) · 6.25 KB
/
JSONObjectAsyncTest.java
File metadata and controls
157 lines (133 loc) · 6.25 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package org.json.junit.milestone5;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.XML;
import org.junit.Test;
import java.io.Reader;
import java.io.StringReader;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.*;
public class JSONObjectAsyncTest {
private final XML.AsyncRunner runner = new XML.AsyncRunner();
public JSONObjectAsyncTest() {
// Default constructor
}
private final Reader medReader = new StringReader(
"<catalog>"
+ " <book id=\"bk101\">"
+ " <author>Gambardella, Matthew</author>"
+ " <title>XML Developer's Guide</title>"
+ " <genre>Computer</genre>"
+ " <price>44.95</price>"
+ " <publish_date>2000-10-01</publish_date>"
+ " <description>An in-depth look at creating applications with XML.</description>"
+ " </book>"
+ " <book id=\"bk102\">"
+ " <author>Ralls, Kim</author>"
+ " <title>Midnight Rain</title>"
+ " <genre>Fantasy</genre>"
+ " <price>5.95</price>"
+ " <publish_date>2000-12-16</publish_date>"
+ " <description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description>"
+ " </book>"
+ "</catalog>"
);
private final Reader smallReader = new StringReader(
"<root>"
+ " <message>Hello</message>"
+ "</root>"
);
@Test
public void testAsyncParsing() throws Exception {
CountDownLatch latch = new CountDownLatch(2);
JSONObject[] results = new JSONObject[2];
Exception[] errors = new Exception[2];
long[] timeElapsed = new long[2];
final long startTime = System.nanoTime();
Future<JSONObject> task1 = XML.toJSONObject(
medReader,
jo -> {
results[0] = jo;
long endNano = System.nanoTime();
timeElapsed[0] = TimeUnit.NANOSECONDS.toMillis(endNano - startTime);
latch.countDown();
},
e -> {
errors[0] = e;
latch.countDown();
}
);
Future<JSONObject> task2 = XML.toJSONObject(
smallReader,
jo -> {
results[1] = jo;
long endNano = System.nanoTime();
timeElapsed[1] = TimeUnit.NANOSECONDS.toMillis(endNano - startTime);
latch.countDown();
},
e -> {
errors[1] = e;
latch.countDown();
}
);
boolean completed = latch.await(10, TimeUnit.SECONDS);
assertTrue("Both callbacks should complete within 10 seconds", completed);
assertNull("No error expected for medReader", errors[0]);
assertNull("No error expected for smallReader", errors[1]);
JSONObject expectedMed = new JSONObject()
.put("catalog", new JSONObject()
.put("book", new JSONArray()
.put(new JSONObject()
.put("author", "Gambardella, Matthew")
.put("title", "XML Developer's Guide")
.put("genre", "Computer")
.put("price", 44.95)
.put("publish_date", "2000-10-01")
.put("description", "An in-depth look at creating applications with XML.")
.put("id", "bk101")
)
.put(new JSONObject()
.put("author", "Ralls, Kim")
.put("title", "Midnight Rain")
.put("genre", "Fantasy")
.put("price", 5.95)
.put("publish_date", "2000-12-16")
.put("description", "A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.")
.put("id", "bk102")
)
)
);
JSONObject expectedSmall = new JSONObject()
.put("root", new JSONObject()
.put("message", "Hello")
);
assertTrue("medReader JSON should match expected", expectedMed.similar(results[0]));
assertTrue("smallReader JSON should match expected", expectedSmall.similar(results[1]));
assertTrue("smallReader must be faster than medReader", timeElapsed[1] < timeElapsed[0]);
// Optional debug output
System.out.println("medReader elapsed: " + timeElapsed[0] + " ms");
System.out.println("smallReader elapsed: " + timeElapsed[1] + " ms");
}
@Test
public void testAsyncParsingWithInvalidXML() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
Exception[] errors = new Exception[1];
Reader invalidReader = new StringReader(
"<root><tag>Unclosed tag</root>"
);
Future<JSONObject> task = org.json.XML.toJSONObject(
invalidReader,
jo -> fail("Should not succeed with invalid XML"),
e -> {
errors[0] = e;
latch.countDown();
}
);
boolean completed = latch.await(5, TimeUnit.SECONDS);
assertTrue("Callback should complete within 5 seconds", completed);
assertNotNull("Error should be captured for invalid XML", errors[0]);
assertTrue("Error should be a JSONException", errors[0] instanceof org.json.JSONException);
}
}