forked from mongodb/mongo-java-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONTest.java
More file actions
384 lines (327 loc) · 13.9 KB
/
Copy pathJSONTest.java
File metadata and controls
384 lines (327 loc) · 13.9 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
* Copyright (c) 2008-2014 MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// JSONTest.java
package com.mongodb.util;
import com.mongodb.BasicDBObject;
import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.DBObject;
import com.mongodb.DBRef;
import org.bson.BSON;
import org.bson.BasicBSONObject;
import org.bson.types.BSONTimestamp;
import org.bson.types.Code;
import org.bson.types.CodeWScope;
import org.bson.types.ObjectId;
import org.junit.Test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.SimpleTimeZone;
import java.util.UUID;
import java.util.regex.Pattern;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
public class JSONTest extends com.mongodb.util.TestCase {
@Test
public void testSerializationMethods(){
// basic test of each of JSON class' serialization methods
String json = "{ \"x\" : \"basic test\"}";
StringBuilder buf = new StringBuilder();
Object obj = JSON.parse(json);
assertEquals(JSON.serialize(obj), json);
}
@Test
public void testNumbers(){
assertEquals(JSON.serialize(JSON.parse("{'x' : 5 }")), "{ \"x\" : 5}");
assertEquals(JSON.serialize(JSON.parse("{'x' : 5.0 }")), "{ \"x\" : 5.0}");
assertEquals(JSON.serialize(JSON.parse("{'x' : 0 }")), "{ \"x\" : 0}");
assertEquals(JSON.serialize(JSON.parse("{'x' : 0.0 }")), "{ \"x\" : 0.0}");
assertEquals(JSON.serialize(JSON.parse("{'x' : 500 }")), "{ \"x\" : 500}");
assertEquals(JSON.serialize(JSON.parse("{'x' : 500.0 }")), "{ \"x\" : 500.0}");
assertEquals(JSON.serialize(JSON.parse("{'x' : 0.500 }")), "{ \"x\" : 0.5}");
assertEquals(JSON.serialize(JSON.parse("{'x' : 5. }")), "{ \"x\" : 5.0}");
assertEquals(JSON.serialize(JSON.parse("{'x' : 5.0e+1 }")), "{ \"x\" : 50.0}");
assertEquals(JSON.serialize(JSON.parse("{'x' : 5.0E-1 }")), "{ \"x\" : 0.5}");
}
@Test
public void testLongValues() {
Long bigVal = Integer.MAX_VALUE + 1L;
String test = String.format("{ \"x\" : %d}", bigVal);
assertEquals(JSON.serialize(JSON.parse(test)), test);
Long smallVal = Integer.MIN_VALUE - 1L;
String test2 = String.format("{ \"x\" : %d}", smallVal);
assertEquals(JSON.serialize(JSON.parse(test2)), test2);
try{
JSON.parse("{\"ReallyBigNumber\": 10000000000000000000 }");
fail("JSONParseException should have been thrown");
}catch(JSONParseException e) {
// fall through
}
}
@Test
public void testSimple() {
assertEquals(JSON.serialize(JSON.parse("{'csdf' : true}")), "{ \"csdf\" : true}");
assertEquals(JSON.serialize(JSON.parse("{'csdf' : false}")), "{ \"csdf\" : false}");
assertEquals(JSON.serialize(JSON.parse("{'csdf' : null}")), "{ \"csdf\" : null }");
}
@Test
public void testString() {
assertEquals(JSON.serialize(JSON.parse("{'csdf' : \"foo\"}")), "{ \"csdf\" : \"foo\"}") ;
assertEquals(JSON.serialize(JSON.parse("{'csdf' : \'foo\'}")), "{ \"csdf\" : \"foo\"}") ;
assertEquals(JSON.serialize(JSON.parse("{'csdf' : \"a\\\"b\"}")), "{ \"csdf\" : \"a\\\"b\"}");
assertEquals(JSON.serialize(JSON.parse("{\n\t\"id\":\"1689c12eb234c54a84ebd100\",\n}")),
"{ \"id\" : \"1689c12eb234c54a84ebd100\"}");
}
@Test
public void testArray() {
assertEquals(JSON.serialize(JSON.parse("{'csdf' : [\"foo\"]}")), "{ \"csdf\" : [ \"foo\"]}") ;
assertEquals(JSON.serialize(JSON.parse("{'csdf' : [3, 5, \'foo\', null]}")), "{ \"csdf\" : [ 3 , 5 , \"foo\" , null ]}") ;
assertEquals(JSON.serialize(JSON.parse("{'csdf' : [3.0, 5.0, \'foo\', null]}")), "{ \"csdf\" : [ 3.0 , 5.0 , \"foo\" , null ]}") ;
assertEquals(JSON.serialize(JSON.parse("{'csdf' : [[],[[]],false]}")), "{ \"csdf\" : [ [ ] , [ [ ]] , false]}");
}
@Test
public void testObject() {
assertEquals(JSON.serialize(JSON.parse("{'csdf' : {}}")), "{ \"csdf\" : { }}") ;
assertEquals(JSON.serialize(JSON.parse("{'csdf' : {\"foo\":\"bar\"}}")), "{ \"csdf\" : { \"foo\" : \"bar\"}}") ;
assertEquals(JSON.serialize(JSON.parse("{'csdf' : {\'hi\':{\'hi\':[{}]}}}")), "{ \"csdf\" : { \"hi\" : { \"hi\" : [ { }]}}}");
}
@Test
public void testMulti() {
assertEquals(JSON.serialize(JSON.parse("{\'\' : \"\", \"34\" : -52.5}")), "{ \"\" : \"\" , \"34\" : -52.5}") ;
}
@Test
public void testUnicode() {
assertEquals(JSON.serialize(JSON.parse("{'x' : \"hi\\u0020\"}")),"{ \"x\" : \"hi \"}") ;
assertEquals(JSON.serialize(JSON.parse("{ \"x\" : \"\\u0E01\\u2702\\uF900\"}")), "{ \"x\" : \"\u0E01\u2702\uF900\"}");
assertEquals(JSON.serialize(JSON.parse("{ \"x\" : \"foo\\u0020bar\"}")), "{ \"x\" : \"foo bar\"}");
}
@Test
public void testBin() {
byte b[] = {'a', 'b', 0, 'd'};
DBObject obj = BasicDBObjectBuilder.start().add("b", b).get();
assertEquals(JSON.serialize(obj), "{ \"b\" : <Binary Data>}");
}
@Test
public void testErrors(){
boolean threw = false;
try {
JSON.parse("{\"x\" : \"");
}
catch(JSONParseException e) {
threw = true;
}
assertEquals(threw, true);
threw = false;
try {
JSON.parse("{\"x\" : \"\\");
}
catch(JSONParseException e) {
threw = true;
}
assertEquals(threw, true);
threw = false;
try {
JSON.parse("{\"x\" : 5.2");
}
catch(JSONParseException e) {
threw = true;
}
assertEquals(threw, true);
threw = false;
try {
JSON.parse("{\"x\" : 5");
}
catch(JSONParseException e) {
threw = true;
}
assertEquals(threw, true);
threw = false;
try {
JSON.parse("{\"x\" : 5,");
}
catch(JSONParseException e) {
threw = true;
}
assertEquals(threw, true);
threw = false;
}
@Test
public void testBasic(){
assertEquals( JSON.serialize(JSON.parse("{}")), "{ }");
assertEquals( JSON.parse(""), null );
assertEquals( JSON.parse(" "), null);
assertEquals( JSON.parse(null), null);
boolean threw = false;
try {
JSON.parse("{");
}
catch(JSONParseException e) {
threw = true;
}
assertEquals(threw, true);
threw = false;
try {
JSON.parse("}");
}
catch(JSONParseException e) {
threw = true;
}
assertEquals(threw, true);
threw = false;
try {
JSON.parse("{{}");
}
catch(JSONParseException e) {
threw = true;
}
assertEquals(threw, true);
threw = false;
try {
JSON.parse("4");
}
catch(JSONParseException e) {
threw = true;
}
assertEquals(threw, false);
threw = false;
assertEquals( 4 , JSON.parse( "4" ) );
}
@Test
public void testNumbers2(){
DBObject x = new BasicDBObject( "x" , 123 );
assertEquals( x , JSON.parse( x.toString() ) );
x = new BasicDBObject( "x" , 123123123123L );
assertEquals( x , JSON.parse( x.toString() ) );
x = new BasicDBObject( "x" , 123123123 );
assertEquals( x , JSON.parse( x.toString() ) );
}
void _escapeChar( String s ){
String thingy = "va" + s + "lue";
DBObject x = new BasicDBObject( "name" , thingy );
x = (DBObject)JSON.parse( x.toString() );
assertEquals( thingy , x.get( "name" ) );
thingy = "va" + s + s + s + "lue" + s;
x = new BasicDBObject( "name" , thingy );
x = (DBObject)JSON.parse( x.toString() );
assertEquals( thingy , x.get( "name" ) );
}
@Test
public void testEscape1(){
String raw = "a\nb";
DBObject x = new BasicDBObject( "x" , raw );
assertEquals( "\"a\\nb\"" , JSON.serialize( raw ) );
assertEquals( x , JSON.parse( x.toString() ) );
assertEquals( raw , ((DBObject)JSON.parse( x.toString() ) ).get( "x" ) );
x = new BasicDBObject( "x" , "a\nb\bc\td\re" );
assertEquals( x , JSON.parse( x.toString() ) );
String thingy = "va\"lue";
x = new BasicDBObject( "name" , thingy );
x = (DBObject)JSON.parse( x.toString() );
assertEquals( thingy , x.get( "name" ) );
thingy = "va\\lue";
x = new BasicDBObject( "name" , thingy );
x = (DBObject)JSON.parse( x.toString() );
assertEquals( thingy , x.get( "name" ) );
assertEquals( "va/lue" , (String)JSON.parse("\"va\\/lue\"") );
assertEquals( "value" , (String)JSON.parse("\"va\\lue\"") );
assertEquals( "va\\lue" , (String)JSON.parse("\"va\\\\lue\"") );
_escapeChar( "\t" );
_escapeChar( "\b" );
_escapeChar( "\n" );
_escapeChar( "\r" );
_escapeChar( "\'" );
_escapeChar( "\"" );
_escapeChar( "\\" );
}
@Test
public void testPattern() {
String x = "^Hello$";
String serializedPattern =
"{ \"$regex\" : \"" + x + "\" , \"$options\" : \"" + "i\"}";
Pattern pattern = Pattern.compile( x , Pattern.CASE_INSENSITIVE);
assertEquals( serializedPattern, JSON.serialize(pattern));
BasicDBObject a = new BasicDBObject( "x" , pattern );
assertEquals( "{ \"x\" : " + serializedPattern + "}" , a.toString() );
DBObject b = (DBObject)JSON.parse( a.toString() );
assertEquals( b.get("x").getClass(), Pattern.class );
assertEquals( a.toString() , b.toString() );
}
@Test
public void testRegexNoOptions() {
String x = "^Hello$";
String serializedPattern =
"{ \"$regex\" : \"" + x + "\"}";
Pattern pattern = Pattern.compile( x );
assertEquals( serializedPattern, JSON.serialize(pattern));
BasicDBObject a = new BasicDBObject( "x" , pattern );
assertEquals( "{ \"x\" : " + serializedPattern + "}" , a.toString() );
DBObject b = (DBObject)JSON.parse( a.toString() );
assertEquals( b.get("x").getClass(), Pattern.class );
assertEquals( a.toString() , b.toString() );
}
@Test
public void testObjectId() {
ObjectId oid = new ObjectId(new Date());
String serialized = JSON.serialize(oid);
assertEquals("{ \"$oid\" : \"" + oid + "\"}", serialized);
ObjectId oid2 = (ObjectId)JSON.parse(serialized);
assertEquals(oid, oid2);
}
@Test
public void testDate() {
Date d = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
String formattedDate = format.format(d);
String serialized = JSON.serialize(d);
assertEquals("{ \"$date\" : \"" + formattedDate + "\"}", serialized);
Date d2 = (Date)JSON.parse(serialized);
assertEquals(d.toString(), d2.toString());
assertTrue(d.equals(d2));
}
@Test
public void testJSONEncoding() throws ParseException {
String json = "{ 'str' : 'asdfasd' , 'long' : 123123123123 , 'int' : 5 , 'float' : 0.4 , 'bool' : false , 'date' : { '$date' : '2011-05-18T18:56:00Z'} , 'pat' : { '$regex' : '.*' , '$options' : ''} , 'oid' : { '$oid' : '4d83ab3ea39562db9c1ae2ae'} , 'ref' : { '$ref' : 'test.test' , '$id' : { '$oid' : '4d83ab59a39562db9c1ae2af'}} , 'code' : { '$code' : 'asdfdsa'} , 'codews' : { '$code' : 'ggggg' , '$scope' : { }} , 'ts' : { '$ts' : 1300474885 , '$inc' : 10} , 'null' : null, 'uuid' : { '$uuid' : '60f65152-6d4a-4f11-9c9b-590b575da7b5' }}";
BasicDBObject a = (BasicDBObject) JSON.parse(json);
assert (a.get("str").equals("asdfasd"));
assert (a.get("int").equals(5));
assert (a.get("long").equals(123123123123L));
assert (a.get("float").equals(0.4d));
assert (a.get("bool").equals(false));
SimpleDateFormat format =
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
assert (a.get("date").equals(format.parse("2011-05-18T18:56:00Z")));
Pattern pat = (Pattern) a.get("pat");
Pattern pat2 = Pattern.compile(".*", BSON.regexFlags(""));
assert (pat.pattern().equals(pat2.pattern()));
assert (pat.flags() == (pat2.flags()));
ObjectId oid = (ObjectId) a.get("oid");
assert (oid.equals(new ObjectId("4d83ab3ea39562db9c1ae2ae")));
DBRef ref = (DBRef) a.get("ref");
assert (ref.equals(new DBRef(null, "test.test", new ObjectId("4d83ab59a39562db9c1ae2af"))));
assert (a.get("code").equals(new Code("asdfdsa")));
assert (a.get("codews").equals(new CodeWScope("ggggg", new BasicBSONObject())));
assert (a.get("ts").equals(new BSONTimestamp(1300474885, 10)));
assert (a.get("uuid").equals(UUID.fromString("60f65152-6d4a-4f11-9c9b-590b575da7b5")));
String json2 = JSON.serialize(a);
BasicDBObject b = (BasicDBObject) JSON.parse(json2);
a.equals(b);
assert (a.equals(b));
}
}