1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.json;
17
18 import java.math.BigInteger;
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Set;
25
26 import junit.framework.TestCase;
27 import net.sf.ezmorph.MorphUtils;
28 import net.sf.ezmorph.bean.MorphDynaBean;
29 import net.sf.ezmorph.bean.MorphDynaClass;
30 import net.sf.json.sample.BeanA;
31
32
33
34
35 public class TestJSONArrayCollections extends TestCase {
36 public static void main( String[] args ) {
37 junit.textui.TestRunner.run( TestJSONArrayCollections.class );
38 }
39
40 public TestJSONArrayCollections( String testName ) {
41 super( testName );
42 }
43
44 public void testToList_bean_elements() {
45 List expected = new ArrayList();
46 expected.add( new BeanA() );
47 JSONArray jsonArray = JSONArray.fromObject( expected );
48 List actual = (List) JSONArray.toCollection( jsonArray, BeanA.class );
49 Assertions.assertEquals( expected, actual );
50 }
51
52 public void testToList_BigDecimal() {
53 List expected = new ArrayList();
54 expected.add( MorphUtils.BIGDECIMAL_ZERO );
55 expected.add( MorphUtils.BIGDECIMAL_ONE );
56 JSONArray jsonArray = JSONArray.fromObject( expected );
57 List actual = (List) JSONArray.toCollection( jsonArray );
58 Assertions.assertEquals( expected, actual );
59 }
60
61 public void testToList_BigInteger() {
62 List expected = new ArrayList();
63 expected.add( BigInteger.ZERO );
64 expected.add( BigInteger.ONE );
65 JSONArray jsonArray = JSONArray.fromObject( expected );
66 List actual = (List) JSONArray.toCollection( jsonArray );
67 Assertions.assertEquals( expected, actual );
68 }
69
70 public void testToList_Boolean() {
71 List expected = new ArrayList();
72 expected.add( Boolean.TRUE );
73 expected.add( Boolean.FALSE );
74 JSONArray jsonArray = JSONArray.fromObject( expected );
75 List actual = (List) JSONArray.toCollection( jsonArray );
76 Assertions.assertEquals( expected, actual );
77 }
78
79 public void testToList_Byte() {
80 List expected = new ArrayList();
81 expected.add( new Integer( 1 ) );
82 expected.add( new Integer( 2 ) );
83 List bytes = new ArrayList();
84 bytes.add( new Byte( (byte) 1 ) );
85 bytes.add( new Byte( (byte) 2 ) );
86 JSONArray jsonArray = JSONArray.fromObject( bytes );
87 List actual = (List) JSONArray.toCollection( jsonArray );
88 Assertions.assertEquals( expected, actual );
89 }
90
91 public void testToList_Character() {
92 List expected = new ArrayList();
93 expected.add( "A" );
94 expected.add( "B" );
95 List chars = new ArrayList();
96 chars.add( new Character( 'A' ) );
97 chars.add( new Character( 'B' ) );
98 JSONArray jsonArray = JSONArray.fromObject( chars );
99 List actual = (List) JSONArray.toCollection( jsonArray );
100 Assertions.assertEquals( expected, actual );
101 }
102
103 public void testToList_Double() {
104 List expected = new ArrayList();
105 expected.add( new Double( 1d ) );
106 expected.add( new Double( 2d ) );
107 JSONArray jsonArray = JSONArray.fromObject( expected );
108 List actual = (List) JSONArray.toCollection( jsonArray );
109 Assertions.assertEquals( expected, actual );
110 }
111
112 public void testToList_dynaBean_elements() throws Exception {
113 List expected = new ArrayList();
114 expected.add( createDynaBean() );
115 JSONArray jsonArray = JSONArray.fromObject( expected );
116 List actual = (List) JSONArray.toCollection( jsonArray );
117 Assertions.assertEquals( expected, actual );
118 }
119
120 public void testToList_Float() {
121 List expected = new ArrayList();
122 expected.add( new Double( 1d ) );
123 expected.add( new Double( 2d ) );
124 List floats = new ArrayList();
125 floats.add( new Float( 1f ) );
126 floats.add( new Float( 2f ) );
127 JSONArray jsonArray = JSONArray.fromObject( floats );
128 List actual = (List) JSONArray.toCollection( jsonArray );
129 Assertions.assertEquals( expected, actual );
130 }
131
132 public void testToList_Integer() {
133 List expected = new ArrayList();
134 expected.add( new Integer( 1 ) );
135 expected.add( new Integer( 2 ) );
136 JSONArray jsonArray = JSONArray.fromObject( expected );
137 List actual = (List) JSONArray.toCollection( jsonArray );
138 Assertions.assertEquals( expected, actual );
139 }
140
141 public void testToList_JSONFunction_elements() {
142 List expected = new ArrayList();
143 expected.add( new JSONFunction( new String[] { "a" }, "return a;" ) );
144 JSONArray jsonArray = JSONArray.fromObject( expected );
145 List actual = (List) JSONArray.toCollection( jsonArray );
146 Assertions.assertEquals( expected, actual );
147 }
148
149 public void testToList_JSONFunction_elements_2() {
150 List expected = new ArrayList();
151 expected.add( "function(a){ return a; }" );
152 JSONArray jsonArray = JSONArray.fromObject( expected );
153 List actual = (List) JSONArray.toCollection( jsonArray );
154 Assertions.assertEquals( expected, actual );
155 }
156
157 public void testToList_Long() {
158 List expected = new ArrayList();
159 expected.add( new Integer( 1 ) );
160 expected.add( new Integer( 2 ) );
161 List longs = new ArrayList();
162 longs.add( new Long( 1L ) );
163 longs.add( new Long( 2L ) );
164 JSONArray jsonArray = JSONArray.fromObject( longs );
165 List actual = (List) JSONArray.toCollection( jsonArray );
166 Assertions.assertEquals( expected, actual );
167 }
168
169 public void testToList_Long2() {
170 List expected = new ArrayList();
171 expected.add( new Long( Integer.MAX_VALUE + 1L ) );
172 expected.add( new Long( Integer.MAX_VALUE + 2L ) );
173 JSONArray jsonArray = JSONArray.fromObject( expected );
174 List actual = (List) JSONArray.toCollection( jsonArray );
175 Assertions.assertEquals( expected, actual );
176 }
177
178 public void testToList_null_elements() {
179 List expected = new ArrayList();
180 expected.add( null );
181 expected.add( null );
182 expected.add( null );
183 JSONArray jsonArray = JSONArray.fromObject( expected );
184 List actual = (List) JSONArray.toCollection( jsonArray );
185 Assertions.assertEquals( expected, actual );
186 }
187
188 public void testToList_Short() {
189 List expected = new ArrayList();
190 expected.add( new Integer( 1 ) );
191 expected.add( new Integer( 2 ) );
192 List shorts = new ArrayList();
193 shorts.add( new Short( (short) 1 ) );
194 shorts.add( new Short( (short) 2 ) );
195 JSONArray jsonArray = JSONArray.fromObject( shorts );
196 List actual = (List) JSONArray.toCollection( jsonArray );
197 Assertions.assertEquals( expected, actual );
198 }
199
200 public void testToList_String() {
201 List expected = new ArrayList();
202 expected.add( "A" );
203 expected.add( "B" );
204 JSONArray jsonArray = JSONArray.fromObject( expected );
205 List actual = (List) JSONArray.toCollection( jsonArray );
206 Assertions.assertEquals( expected, actual );
207 }
208
209 public void testToList_String_multi() {
210 List a = new ArrayList();
211 a.add( "a" );
212 a.add( "b" );
213 List b = new ArrayList();
214 b.add( "1" );
215 b.add( "2" );
216 List expected = new ArrayList();
217 expected.add( a );
218 expected.add( b );
219 JSONArray jsonArray = JSONArray.fromObject( expected );
220 List actual = (List) JSONArray.toCollection( jsonArray );
221 Assertions.assertEquals( expected, actual );
222 }
223
224 public void testToSet_bean_elements() {
225 Set expected = new HashSet();
226 expected.add( new BeanA() );
227 JSONArray jsonArray = JSONArray.fromObject( expected );
228 JsonConfig jsonConfig = new JsonConfig();
229 jsonConfig.setCollectionType( Set.class );
230 jsonConfig.setRootClass( BeanA.class );
231 Set actual = (Set) JSONArray.toCollection( jsonArray, jsonConfig );
232 Assertions.assertEquals( expected, actual );
233 }
234
235 public void testToSet_BigDecimal() {
236 Set expected = new HashSet();
237 expected.add( MorphUtils.BIGDECIMAL_ZERO );
238 expected.add( MorphUtils.BIGDECIMAL_ONE );
239 JSONArray jsonArray = JSONArray.fromObject( expected );
240 JsonConfig jsonConfig = new JsonConfig();
241 jsonConfig.setCollectionType( Set.class );
242 Set actual = (Set) JSONArray.toCollection( jsonArray, jsonConfig );
243 Assertions.assertEquals( expected, actual );
244 }
245
246 public void testToSet_BigInteger() {
247 Set expected = new HashSet();
248 expected.add( BigInteger.ZERO );
249 expected.add( BigInteger.ONE );
250 JSONArray jsonArray = JSONArray.fromObject( expected );
251 JsonConfig jsonConfig = new JsonConfig();
252 jsonConfig.setCollectionType( Set.class );
253 Set actual = (Set) JSONArray.toCollection( jsonArray, jsonConfig );
254 Assertions.assertEquals( expected, actual );
255 }
256
257 public void testToSet_Boolean() {
258 Set expected = new HashSet();
259 expected.add( Boolean.TRUE );
260 expected.add( Boolean.FALSE );
261 JSONArray jsonArray = JSONArray.fromObject( expected );
262 JsonConfig jsonConfig = new JsonConfig();
263 jsonConfig.setCollectionType( Set.class );
264 Set actual = (Set) JSONArray.toCollection( jsonArray, jsonConfig );
265 Assertions.assertEquals( expected, actual );
266 }
267
268 public void testToSet_Byte() {
269 Set expected = new HashSet();
270 expected.add( new Integer( 1 ) );
271 expected.add( new Integer( 2 ) );
272 Set bytes = new HashSet();
273 bytes.add( new Byte( (byte) 1 ) );
274 bytes.add( new Byte( (byte) 2 ) );
275 JSONArray jsonArray = JSONArray.fromObject( bytes );
276 JsonConfig jsonConfig = new JsonConfig();
277 jsonConfig.setCollectionType( Set.class );
278 Set actual = (Set) JSONArray.toCollection( jsonArray, jsonConfig );
279 Assertions.assertEquals( expected, actual );
280 }
281
282 public void testToSet_Character() {
283 Set expected = new HashSet();
284 expected.add( "A" );
285 expected.add( "B" );
286 Set chars = new HashSet();
287 chars.add( new Character( 'A' ) );
288 chars.add( new Character( 'B' ) );
289 JSONArray jsonArray = JSONArray.fromObject( chars );
290 JsonConfig jsonConfig = new JsonConfig();
291 jsonConfig.setCollectionType( Set.class );
292 Set actual = (Set) JSONArray.toCollection( jsonArray, jsonConfig );
293 Assertions.assertEquals( expected, actual );
294 }
295
296 public void testToSet_Double() {
297 Set expected = new HashSet();
298 expected.add( new Double( 1d ) );
299 expected.add( new Double( 2d ) );
300 JSONArray jsonArray = JSONArray.fromObject( expected );
301 JsonConfig jsonConfig = new JsonConfig();
302 jsonConfig.setCollectionType( Set.class );
303 Set actual = (Set) JSONArray.toCollection( jsonArray, jsonConfig );
304 Assertions.assertEquals( expected, actual );
305 }
306
307 public void testToSet_dynaBean_elements() throws Exception {
308 Set expected = new HashSet();
309 expected.add( createDynaBean() );
310 JSONArray jsonArray = JSONArray.fromObject( expected );
311 JsonConfig jsonConfig = new JsonConfig();
312 jsonConfig.setCollectionType( Set.class );
313 Set actual = (Set) JSONArray.toCollection( jsonArray, jsonConfig );
314 Assertions.assertEquals( expected, actual );
315 }
316
317 public void testToSet_Float() {
318 Set expected = new HashSet();
319 expected.add( new Double( 1d ) );
320 expected.add( new Double( 2d ) );
321 Set floats = new HashSet();
322 floats.add( new Float( 1f ) );
323 floats.add( new Float( 2f ) );
324 JSONArray jsonArray = JSONArray.fromObject( floats );
325 JsonConfig jsonConfig = new JsonConfig();
326 jsonConfig.setCollectionType( Set.class );
327 Set actual = (Set) JSONArray.toCollection( jsonArray, jsonConfig );
328 Assertions.assertEquals( expected, actual );
329 }
330
331 public void testToSet_Integer() {
332 Set expected = new HashSet();
333 expected.add( new Integer( 1 ) );
334 expected.add( new Integer( 2 ) );
335 JSONArray jsonArray = JSONArray.fromObject( expected );
336 JsonConfig jsonConfig = new JsonConfig();
337 jsonConfig.setCollectionType( Set.class );
338 Set actual = (Set) JSONArray.toCollection( jsonArray, jsonConfig );
339 Assertions.assertEquals( expected, actual );
340 }
341
342 public void testToSet_JSONFunction_elements() {
343 Set expected = new HashSet();
344 expected.add( new JSONFunction( new String[] { "a" }, "return a;" ) );
345 JSONArray jsonArray = JSONArray.fromObject( expected );
346 JsonConfig jsonConfig = new JsonConfig();
347 jsonConfig.setCollectionType( Set.class );
348 Set actual = (Set) JSONArray.toCollection( jsonArray, jsonConfig );
349 Assertions.assertEquals( expected, actual );
350 }
351
352
353
354
355
356
357
358
359
360
361
362
363
364 public void testToSet_Long() {
365 Set expected = new HashSet();
366 expected.add( new Integer( 1 ) );
367 expected.add( new Integer( 2 ) );
368 Set longs = new HashSet();
369 longs.add( new Long( 1L ) );
370 longs.add( new Long( 2L ) );
371 JSONArray jsonArray = JSONArray.fromObject( longs );
372 JsonConfig jsonConfig = new JsonConfig();
373 jsonConfig.setCollectionType( Set.class );
374 Set actual = (Set) JSONArray.toCollection( jsonArray, jsonConfig );
375 Assertions.assertEquals( expected, actual );
376 }
377
378 public void testToSet_Long2() {
379 Set expected = new HashSet();
380 expected.add( new Long( Integer.MAX_VALUE + 1L ) );
381 expected.add( new Long( Integer.MAX_VALUE + 2L ) );
382 JSONArray jsonArray = JSONArray.fromObject( expected );
383 JsonConfig jsonConfig = new JsonConfig();
384 jsonConfig.setCollectionType( Set.class );
385 Set actual = (Set) JSONArray.toCollection( jsonArray, jsonConfig );
386 Assertions.assertEquals( expected, actual );
387 }
388
389 public void testToSet_null_elements() {
390 Set expected = new HashSet();
391 expected.add( null );
392 expected.add( null );
393 expected.add( null );
394 JSONArray jsonArray = JSONArray.fromObject( expected );
395 JsonConfig jsonConfig = new JsonConfig();
396 jsonConfig.setCollectionType( Set.class );
397 Set actual = (Set) JSONArray.toCollection( jsonArray, jsonConfig );
398 Assertions.assertEquals( expected, actual );
399 }
400
401 public void testToSet_Short() {
402 Set expected = new HashSet();
403 expected.add( new Integer( 1 ) );
404 expected.add( new Integer( 2 ) );
405 Set shorts = new HashSet();
406 shorts.add( new Short( (short) 1 ) );
407 shorts.add( new Short( (short) 2 ) );
408 JSONArray jsonArray = JSONArray.fromObject( shorts );
409 JsonConfig jsonConfig = new JsonConfig();
410 jsonConfig.setCollectionType( Set.class );
411 Set actual = (Set) JSONArray.toCollection( jsonArray, jsonConfig );
412 Assertions.assertEquals( expected, actual );
413 }
414
415 public void testToSet_String() {
416 Set expected = new HashSet();
417 expected.add( "A" );
418 expected.add( "B" );
419 JSONArray jsonArray = JSONArray.fromObject( expected );
420 JsonConfig jsonConfig = new JsonConfig();
421 jsonConfig.setCollectionType( Set.class );
422 Set actual = (Set) JSONArray.toCollection( jsonArray, jsonConfig );
423 Assertions.assertEquals( expected, actual );
424 }
425
426 public void testToSet_String_multi() {
427 Set a = new HashSet();
428 a.add( "a" );
429 a.add( "b" );
430 Set b = new HashSet();
431 b.add( "1" );
432 b.add( "2" );
433 Set expected = new HashSet();
434 expected.add( a );
435 expected.add( b );
436 JSONArray jsonArray = JSONArray.fromObject( expected );
437 JsonConfig jsonConfig = new JsonConfig();
438 jsonConfig.setCollectionType( Set.class );
439 Set actual = (Set) JSONArray.toCollection( jsonArray, jsonConfig );
440 Assertions.assertEquals( expected, actual );
441 }
442
443 private MorphDynaBean createDynaBean() throws Exception {
444 Map properties = new HashMap();
445 properties.put( "name", String.class );
446 MorphDynaClass dynaClass = new MorphDynaClass( properties );
447 MorphDynaBean dynaBean = (MorphDynaBean) dynaClass.newInstance();
448 dynaBean.setDynaBeanClass( dynaClass );
449 dynaBean.set( "name", "json" );
450
451 return dynaBean;
452 }
453 }