1   /*
2    * Copyright 2002-2009 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.json;
17  
18  import java.io.StringWriter;
19  import java.math.BigDecimal;
20  import java.math.BigInteger;
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
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.ArrayJSONStringBean;
31  import net.sf.json.sample.BeanA;
32  import net.sf.json.util.JSONTokener;
33  
34  import org.apache.commons.beanutils.DynaBean;
35  
36  /**
37   * @author Andres Almiray <aalmiray@users.sourceforge.net>
38   */
39  public class TestJSONArray extends TestCase {
40     public static void main( String[] args ) {
41        junit.textui.TestRunner.run( TestJSONArray.class );
42     }
43  
44     public TestJSONArray( String testName ) {
45        super( testName );
46     }
47  
48     public void testDiscard() {
49        JSONArray jsonArray = new JSONArray().element( "1" )
50              .element( "true" )
51              .element( "string" )
52              .element( "function(){ return this; }" )
53              .element( "[1,2,3]" );
54        assertEquals( 5, jsonArray.size() );
55        jsonArray.discard( "string" )
56              .discard( 0 );
57        assertEquals( 3, jsonArray.size() );
58        assertFalse( jsonArray.contains( "string" ) );
59        assertFalse( jsonArray.contains( "1" ) );
60     }
61  
62     public void testConstructor_Collection() {
63        List l = new ArrayList();
64        l.add( Boolean.TRUE );
65        l.add( new Integer( 1 ) );
66        l.add( "string" );
67        l.add( Object.class );
68        testJSONArray( l, "[true,1,\"string\",\"java.lang.Object\"]" );
69     }
70  
71     public void testConstructor_Collection_JSONArray() {
72        List l = new ArrayList();
73        l.add( JSONArray.fromObject( new int[] { 1, 2 } ) );
74        testJSONArray( l, "[[1,2]]" );
75     }
76  
77     public void testConstructor_Collection_JSONFunction() {
78        List l = new ArrayList();
79        l.add( new JSONFunction( new String[] { "a" }, "return a;" ) );
80        testJSONArray( l, "[function(a){ return a; }]" );
81     }
82  
83     public void testConstructor_Collection_JSONString() {
84        ArrayJSONStringBean bean = new ArrayJSONStringBean();
85        bean.setValue( "'json','json'" );
86        List l = new ArrayList();
87        l.add( bean );
88        testJSONArray( l, "[[\"json\",\"json\"]]" );
89     }
90  
91     public void testConstructor_Collection_nulls() {
92        List l = new ArrayList();
93        l.add( null );
94        l.add( null );
95        testJSONArray( l, "[null,null]" );
96     }
97  
98     public void testConstructor_func() {
99        JSONArray expected = JSONArray.fromObject( new String[] { "'"
100             + new JSONFunction( new String[] { "a" }, "return a;" ).toString() + "'" } );
101       JSONArray actual = JSONArray.fromObject( new String[] { "'function(a){ return a; }'" } );
102       Assertions.assertEquals( expected, actual );
103    }
104 
105    public void testConstructor_func2() {
106       JSONArray expected = JSONArray.fromObject( new String[] { "\""
107             + new JSONFunction( new String[] { "a" }, "return a;" ).toString() + "\"" } );
108       JSONArray actual = JSONArray.fromObject( new String[] { "\"function(a){ return a; }\"" } );
109       Assertions.assertEquals( expected, actual );
110    }
111 
112    public void testConstructor_JSONArray() {
113       JSONArray expected = JSONArray.fromObject( "[1,2]" );
114       JSONArray actual = JSONArray.fromObject( JSONArray.fromObject( "[1,2]" ) );
115       Assertions.assertEquals( expected, actual );
116    }
117 
118    public void testConstructor_JSONTokener_functions() {
119       testJSONArray( new JSONTokener( "[function(a){ return a; }]" ), "[function(a){ return a; }]" );
120    }
121 
122    public void testConstructor_JSONTokener_nulls() {
123       testJSONArray( new JSONTokener( "[,,]" ), "[null,null]" );
124    }
125 
126    public void testConstructor_JSONTokener_syntax_errors() {
127       try{
128          JSONArray.fromObject( "" );
129          fail( "Expected a JSONException" );
130       }catch( JSONException expected ){
131          // ok
132       }
133    }
134 
135    public void testConstructor_Object_Array() {
136       JSONArray expected = JSONArray.fromObject( "[\"json\",1]" );
137       JSONArray actual = JSONArray.fromObject( new Object[] { "json", new Integer( 1 ) } );
138       Assertions.assertEquals( expected, actual );
139    }
140 
141    public void testConstructor_Object_Array_Array() {
142       JSONArray expected = JSONArray.fromObject( "[[1,2]]" );
143       JSONArray actual = JSONArray.fromObject( new Object[] { new int[] { 1, 2 } } );
144       Assertions.assertEquals( expected, actual );
145    }
146 
147    public void testConstructor_Object_Array_BigDecimal() {
148       // bug 1596168
149 
150       // an array of BigDecimals
151       Number[] numberArray = new Number[] { BigDecimal.valueOf( 10000000000L, 10 ),
152             new BigDecimal( "-1.0" ), new BigDecimal( "99.99E-1" ) };
153 
154       assertEquals( "1.0000000000", numberArray[0].toString() );
155       assertEquals( "-1.0", numberArray[1].toString() );
156       assertEquals( "9.999", numberArray[2].toString() );
157 
158       JSONArray jsonNumArray = JSONArray.fromObject( numberArray );
159 
160       assertEquals( "1.0000000000", jsonNumArray.get( 0 )
161             .toString() );
162       assertEquals( "-1.0", jsonNumArray.get( 1 )
163             .toString() );
164       assertEquals( "9.999", jsonNumArray.get( 2 )
165             .toString() );
166    }
167 
168    public void testConstructor_Object_Array_BigInteger() {
169       // bug 1596168
170 
171       Number[] numberArray = new Number[] { new BigInteger( "18437736874454810627" ),
172             new BigInteger( "9007199254740990" ) };
173 
174       assertEquals( "18437736874454810627", numberArray[0].toString() );
175       assertEquals( "9007199254740990", numberArray[1].toString() );
176 
177       JSONArray jsonNumArray = JSONArray.fromObject( numberArray );
178 
179       assertEquals( "18437736874454810627", jsonNumArray.get( 0 )
180             .toString() );
181       assertEquals( "9007199254740990", jsonNumArray.get( 1 )
182             .toString() );
183    }
184 
185    public void testConstructor_Object_Array_Class() {
186       JSONArray expected = JSONArray.fromObject( "[\"java.lang.Object\"]" );
187       JSONArray actual = JSONArray.fromObject( new Object[] { Object.class } );
188       Assertions.assertEquals( expected, actual );
189    }
190 
191    public void testConstructor_Object_Array_functions() {
192       JSONArray expected = JSONArray.fromObject( "[function(a){ return a; }]" );
193       JSONArray actual = JSONArray.fromObject( new JSONFunction[] { new JSONFunction(
194             new String[] { "a" }, "return a;" ) } );
195       Assertions.assertEquals( expected, actual );
196    }
197 
198    public void testConstructor_Object_Array_functions_2() {
199       JSONArray expected = JSONArray.fromObject( new JSONFunction[] { new JSONFunction(
200             new String[] { "a" }, "return a;" ) } );
201       JSONArray actual = JSONArray.fromObject( "[function(a){ return a; }]" );
202       Assertions.assertEquals( expected, actual );
203    }
204 
205    public void testConstructor_Object_Array_functions_3() {
206       JSONArray expected = JSONArray.fromObject( new JSONFunction[] { new JSONFunction(
207             new String[] { "a" }, "return a;" ) } );
208       JSONArray actual = JSONArray.fromObject( new String[] { "function(a){ return a; }" } );
209       Assertions.assertEquals( expected, actual );
210    }
211 
212    public void testConstructor_Object_Array_JSONArray() {
213       JSONArray expected = JSONArray.fromObject( "[[1,2]]" );
214       JSONArray actual = JSONArray.fromObject( new Object[] { JSONArray.fromObject( "[1,2]" ) } );
215       Assertions.assertEquals( expected, actual );
216    }
217 
218    public void testConstructor_Object_Array_JSONString() {
219       ArrayJSONStringBean bean = new ArrayJSONStringBean();
220       bean.setValue( "'json','json'" );
221       JSONArray expected = JSONArray.fromObject( "[[\"json\",\"json\"]]" );
222       JSONArray actual = JSONArray.fromObject( new Object[] { bean } );
223       Assertions.assertEquals( expected, actual );
224    }
225 
226    public void testConstructor_Object_Array_nulls() {
227       JSONArray expected = JSONArray.fromObject( "[null,null]" );
228       JSONArray actual = JSONArray.fromObject( new Object[] { null, null } );
229       Assertions.assertEquals( expected, actual );
230    }
231 
232    public void testConstructor_primitive_array_boolean() {
233       testJSONArray( new boolean[] { true, false }, "[true,false]" );
234    }
235 
236    public void testConstructor_primitive_array_byte() {
237       testJSONArray( new byte[] { 1, 2, 3 }, "[1,2,3]" );
238    }
239 
240    public void testConstructor_primitive_array_char() {
241       testJSONArray( new char[] { 'a', 'b', 'c' }, "[\"a\",\"b\",\"c\"]" );
242    }
243 
244    public void testConstructor_primitive_array_double() {
245       testJSONArray( new double[] { 1.1, 2.2, 3.3 }, "[1.1,2.2,3.3]" );
246    }
247 
248    public void testConstructor_primitive_array_double_Infinity() {
249       try{
250          JSONArray.fromObject( new double[] { Double.NEGATIVE_INFINITY } );
251          fail( "Should have thrown a JSONException" );
252       }catch( JSONException expected ){
253          // OK
254       }
255 
256       try{
257          JSONArray.fromObject( new double[] { Double.POSITIVE_INFINITY } );
258          fail( "Should have thrown a JSONException" );
259       }catch( JSONException expected ){
260          // OK
261       }
262    }
263 
264    public void testConstructor_primitive_array_double_NaNs() {
265       try{
266          JSONArray.fromObject( new double[] { Double.NaN } );
267          fail( "Should have thrown a JSONException" );
268       }catch( JSONException expected ){
269          // OK
270       }
271    }
272 
273    public void testConstructor_primitive_array_float() {
274       testJSONArray( new float[] { 1.1f, 2.2f, 3.3f }, "[1.1,2.2,3.3]" );
275    }
276 
277    public void testConstructor_primitive_array_float_Infinity() {
278       try{
279          JSONArray.fromObject( new float[] { Float.NEGATIVE_INFINITY } );
280          fail( "Should have thrown a JSONException" );
281       }catch( JSONException expected ){
282          // OK
283       }
284 
285       try{
286          JSONArray.fromObject( new float[] { Float.POSITIVE_INFINITY } );
287          fail( "Should have thrown a JSONException" );
288       }catch( JSONException expected ){
289          // OK
290       }
291    }
292 
293    public void testConstructor_primitive_array_float_NaNs() {
294       try{
295          JSONArray.fromObject( new float[] { Float.NaN } );
296          fail( "Should have thrown a JSONException" );
297       }catch( JSONException expected ){
298          // OK
299       }
300    }
301 
302    public void testConstructor_primitive_array_int() {
303       testJSONArray( new int[] { 1, 2, 3 }, "[1,2,3]" );
304    }
305 
306    public void testConstructor_primitive_array_long() {
307       testJSONArray( new long[] { 1, 2, 3 }, "[1,2,3]" );
308    }
309 
310    public void testConstructor_primitive_array_short() {
311       testJSONArray( new short[] { 1, 2, 3 }, "[1,2,3]" );
312    }
313 
314    public void testConstructor_String_functions() {
315       testJSONArray( "[function(a){ return a; }]", "[function(a){ return a; }]" );
316    }
317 
318    public void testConstructor_String_functions_multi() {
319       testJSONArray( "[function(a){ return a; },[function(b){ return b; }]]",
320             "[function(a){ return a; },[function(b){ return b; }]]" );
321    }
322 
323    public void testCycleDetection_arrays() {
324       Object[] array1 = new Object[2];
325       Object[] array2 = new Object[2];
326       array1[0] = new Integer( 1 );
327       array1[1] = array2;
328       array2[0] = new Integer( 2 );
329       array2[1] = array1;
330       try{
331          JSONArray.fromObject( array1 );
332          fail( "A JSONException was expected" );
333       }catch( JSONException expected ){
334          assertTrue( expected.getMessage()
335                .endsWith( "There is a cycle in the hierarchy!" ) );
336       }
337    }
338 
339    public void testElement_Array() {
340       JSONArray array = new JSONArray();
341       int[] ints = { 1, 2 };
342       array.element( ints );
343       Assertions.assertEquals( JSONArray.fromObject( ints ), array.getJSONArray( 0 ) );
344    }
345 
346    public void testElement_boolean() {
347       JSONArray jsonArray = new JSONArray();
348       jsonArray.element( true );
349       assertEquals( 1, jsonArray.size() );
350       assertTrue( jsonArray.getBoolean( 0 ) );
351    }
352 
353    public void testElement_Boolean() {
354       JSONArray array = new JSONArray();
355       array.element( Boolean.TRUE );
356       Assertions.assertTrue( array.getBoolean( 0 ) );
357    }
358 
359    public void testElement_Collection() {
360       List l = new ArrayList();
361       l.add( Boolean.TRUE );
362       l.add( new Integer( 1 ) );
363       l.add( "string" );
364       JSONArray jsonArray = new JSONArray();
365       jsonArray.element( l );
366       assertEquals( 1, jsonArray.size() );
367       Assertions.assertEquals( JSONArray.fromObject( "[true,1,\"string\"]" ),
368             jsonArray.getJSONArray( 0 ) );
369    }
370 
371    public void testElement_double() {
372       JSONArray jsonArray = new JSONArray();
373       jsonArray.element( 2.0d );
374       assertEquals( 1, jsonArray.size() );
375       assertEquals( 2.0d, jsonArray.getDouble( 0 ), 0d );
376    }
377 
378    public void testElement_index_0_Array() {
379       JSONArray array = JSONArray.fromObject( "[null,null]" );
380       ;
381       int[] ints = { 0, 2 };
382       array.element( 0, ints );
383       Assertions.assertEquals( JSONArray.fromObject( ints ), array.getJSONArray( 0 ) );
384    }
385 
386    public void testElement_index_0_Boolean() {
387       JSONArray array = JSONArray.fromObject( "[null,null]" );
388       ;
389       array.element( 0, Boolean.TRUE );
390       Assertions.assertTrue( array.getBoolean( 0 ) );
391    }
392 
393    public void testElement_index_0_Class() {
394       JSONArray array = JSONArray.fromObject( "[null,null]" );
395       array.element( 0, Object.class );
396       Assertions.assertEquals( "java.lang.Object", array.getString( 0 ) );
397    }
398 
399    public void testElement_index_0_JSON() {
400       JSONArray array = JSONArray.fromObject( "[null,null]" );
401       array.element( 0, JSONNull.getInstance() );
402       Assertions.assertEquals( JSONNull.getInstance(), array.get( 0 ) );
403    }
404 
405    public void testElement_index_0_JSONFunction() {
406       JSONArray array = JSONArray.fromObject( "[null,null]" );
407       JSONFunction f = new JSONFunction( "return this;" );
408       array.element( 0, f );
409       Assertions.assertEquals( f, (JSONFunction) array.get( 0 ) );
410    }
411 
412    public void testElement_index_0_JSONString() {
413       JSONArray array = JSONArray.fromObject( "[null,null]" );
414       ArrayJSONStringBean bean = new ArrayJSONStringBean();
415       bean.setValue( "'json','json'" );
416       array.element( 0, bean );
417       Assertions.assertEquals( JSONArray.fromObject( bean ), array.getJSONArray( 0 ) );
418    }
419 
420    public void testElement_index_0_JSONTokener() {
421       JSONArray array = JSONArray.fromObject( "[null,null]" );
422       JSONTokener tok = new JSONTokener( "[0,2]" );
423       array.element( 0, tok );
424       tok.reset();
425       Assertions.assertEquals( JSONArray.fromObject( tok ), array.getJSONArray( 0 ) );
426    }
427 
428    public void testElement_index_0_Number() {
429       JSONArray array = JSONArray.fromObject( "[null,null]" );
430       array.element( 0, new Double( 2 ) );
431       Assertions.assertEquals( new Double( 2 ).doubleValue(), array.getDouble( 0 ), 0d );
432    }
433 
434    public void testElement_index_0_Object() {
435       JSONArray array = JSONArray.fromObject( "[null,null]" );
436       array.element( 0, new BeanA() );
437       Assertions.assertEquals( JSONObject.fromObject( new BeanA() ), array.getJSONObject( 0 ) );
438    }
439 
440    public void testElement_index_0_String() {
441       JSONArray array = JSONArray.fromObject( "[null,null]" );
442       array.element( 0, "json" );
443       Assertions.assertEquals( "json", array.getString( 0 ) );
444    }
445 
446    public void testElement_index_0_String_JSON() {
447       JSONArray array = JSONArray.fromObject( "[null,null]" );
448       array.element( 0, "[]" );
449       Assertions.assertEquals( new JSONArray().toString(), array.getString( 0 ) );
450    }
451 
452    public void testElement_index_0_String_null() {
453       JSONArray array = JSONArray.fromObject( "[null,null]" );
454       array.element( 0, (String) null );
455       Assertions.assertEquals( "", array.getString( 0 ) );
456    }
457 
458    public void testElement_index_1_Array() {
459       JSONArray array = new JSONArray();
460       int[] ints = { 1, 2 };
461       array.element( 1, ints );
462       Assertions.assertEquals( JSONArray.fromObject( ints ), array.getJSONArray( 1 ) );
463    }
464 
465    public void testElement_index_1_boolean() {
466       JSONArray jsonArray = new JSONArray();
467       jsonArray.element( 1, true );
468       assertEquals( 2, jsonArray.size() );
469       assertEquals( JSONNull.getInstance(), jsonArray.get( 0 ) );
470       assertTrue( jsonArray.getBoolean( 1 ) );
471    }
472 
473    public void testElement_index_1_Boolean() {
474       JSONArray array = new JSONArray();
475       array.element( 1, Boolean.TRUE );
476       Assertions.assertTrue( array.getBoolean( 1 ) );
477    }
478 
479    public void testElement_index_1_Class() {
480       JSONArray array = new JSONArray();
481       array.element( 1, Object.class );
482       Assertions.assertEquals( "java.lang.Object", array.getString( 1 ) );
483    }
484 
485    public void testElement_index_1_Collection() {
486       List l = new ArrayList();
487       l.add( Boolean.TRUE );
488       l.add( new Integer( 1 ) );
489       l.add( "string" );
490       JSONArray jsonArray = new JSONArray();
491       jsonArray.element( 1, l );
492       assertEquals( 2, jsonArray.size() );
493       assertEquals( JSONNull.getInstance(), jsonArray.get( 0 ) );
494       Assertions.assertEquals( JSONArray.fromObject( "[true,1,\"string\"]" ),
495             jsonArray.getJSONArray( 1 ) );
496    }
497 
498    public void testElement_index_1_double() {
499       JSONArray jsonArray = new JSONArray();
500       jsonArray.element( 1, 2.0d );
501       assertEquals( 2, jsonArray.size() );
502       assertEquals( JSONNull.getInstance(), jsonArray.get( 0 ) );
503       assertEquals( 2.0d, jsonArray.getDouble( 1 ), 0d );
504    }
505 
506    public void testElement_index_1_int() {
507       JSONArray jsonArray = new JSONArray();
508       jsonArray.element( 1, 1 );
509       assertEquals( 2, jsonArray.size() );
510       assertEquals( JSONNull.getInstance(), jsonArray.get( 0 ) );
511       assertEquals( 1, jsonArray.getInt( 1 ) );
512    }
513 
514    public void testElement_index_1_JSON() {
515       JSONArray array = new JSONArray();
516       array.element( 1, JSONNull.getInstance() );
517       Assertions.assertEquals( JSONNull.getInstance(), array.get( 1 ) );
518    }
519 
520    public void testElement_index_1_JSONFunction() {
521       JSONArray array = new JSONArray();
522       JSONFunction f = new JSONFunction( "return this;" );
523       array.element( 1, f );
524       Assertions.assertEquals( f, (JSONFunction) array.get( 1 ) );
525    }
526 
527    public void testElement_index_1_JSONString() {
528       JSONArray array = new JSONArray();
529       ArrayJSONStringBean bean = new ArrayJSONStringBean();
530       bean.setValue( "'json','json'" );
531       array.element( 1, bean );
532       Assertions.assertEquals( JSONArray.fromObject( bean ), array.getJSONArray( 1 ) );
533    }
534 
535    public void testElement_index_1_JSONTokener() {
536       JSONArray array = new JSONArray();
537       JSONTokener tok = new JSONTokener( "[1,2]" );
538       array.element( 1, tok );
539       tok.reset();
540       Assertions.assertEquals( JSONArray.fromObject( tok ), array.getJSONArray( 1 ) );
541    }
542 
543    public void testElement_index_1_long() {
544       JSONArray jsonArray = new JSONArray();
545       jsonArray.element( 1, 1L );
546       assertEquals( 2, jsonArray.size() );
547       assertEquals( JSONNull.getInstance(), jsonArray.get( 0 ) );
548       assertEquals( 1L, jsonArray.getLong( 1 ) );
549    }
550 
551    public void testElement_index_1_Map() {
552       Map map = new HashMap();
553       map.put( "name", "json" );
554       JSONArray jsonArray = new JSONArray();
555       jsonArray.element( 1, map );
556       assertEquals( 2, jsonArray.size() );
557       assertEquals( JSONNull.getInstance(), jsonArray.get( 0 ) );
558       Assertions.assertEquals( JSONObject.fromObject( map ), jsonArray.getJSONObject( 1 ) );
559    }
560 
561    public void testElement_index_1_Number() {
562       JSONArray array = new JSONArray();
563       array.element( 1, new Double( 2 ) );
564       Assertions.assertEquals( new Double( 2 ).doubleValue(), array.getDouble( 1 ), 1d );
565    }
566 
567    public void testElement_index_1_Object() {
568       JSONArray array = new JSONArray();
569       array.element( 1, new BeanA() );
570       Assertions.assertEquals( JSONObject.fromObject( new BeanA() ), array.getJSONObject( 1 ) );
571    }
572 
573    public void testElement_index_1_String() {
574       JSONArray array = new JSONArray();
575       array.element( 1, "json" );
576       Assertions.assertEquals( "json", array.getString( 1 ) );
577    }
578 
579    public void testElement_index_1_String_JSON() {
580       JSONArray array = new JSONArray();
581       array.element( 1, "[]" );
582       Assertions.assertEquals( new JSONArray().toString(), array.getString( 1 ) );
583    }
584 
585    public void testElement_index_1_String_null() {
586       JSONArray array = new JSONArray();
587       array.element( 1, (String) null );
588       Assertions.assertEquals( "", array.getString( 1 ) );
589    }
590 
591    public void testElement_int() {
592       JSONArray jsonArray = new JSONArray();
593       jsonArray.element( 1 );
594       assertEquals( 1, jsonArray.size() );
595       assertEquals( 1, jsonArray.getInt( 0 ) );
596    }
597 
598    public void testElement_JSON() {
599       JSONArray array = new JSONArray();
600       array.element( JSONNull.getInstance() );
601       Assertions.assertEquals( JSONNull.getInstance(), array.get( 0 ) );
602    }
603 
604    public void testElement_JSONFunction() {
605       JSONArray array = new JSONArray();
606       JSONFunction f = new JSONFunction( "return this;" );
607       array.element( f );
608       Assertions.assertEquals( f, (JSONFunction) array.get( 0 ) );
609    }
610 
611    public void testElement_JSONString() {
612       JSONArray array = new JSONArray();
613       ArrayJSONStringBean bean = new ArrayJSONStringBean();
614       bean.setValue( "'json','json'" );
615       array.element( bean );
616       Assertions.assertEquals( JSONArray.fromObject( bean ), array.getJSONArray( 0 ) );
617    }
618 
619    public void testElement_JSONTokener() {
620       JSONArray array = new JSONArray();
621       JSONTokener tok = new JSONTokener( "[1,2]" );
622       array.element( tok );
623       tok.reset();
624       Assertions.assertEquals( JSONArray.fromObject( tok ), array.getJSONArray( 0 ) );
625    }
626 
627    public void testElement_long() {
628       JSONArray jsonArray = new JSONArray();
629       jsonArray.element( 1L );
630       assertEquals( 1, jsonArray.size() );
631       assertEquals( 1L, jsonArray.getLong( 0 ) );
632    }
633 
634    public void testElement_Map() {
635       Map map = new HashMap();
636       map.put( "name", "json" );
637       JSONArray jsonArray = new JSONArray();
638       jsonArray.element( map );
639       assertEquals( 1, jsonArray.size() );
640       Assertions.assertEquals( JSONObject.fromObject( map ), jsonArray.getJSONObject( 0 ) );
641    }
642 
643    public void testElement_negativeIndex() {
644       try{
645          JSONArray jsonArray = new JSONArray();
646          jsonArray.element( -1, JSONNull.getInstance() );
647          fail( "Expected a JSONException" );
648       }catch( JSONException expected ){
649          // OK
650       }
651    }
652 
653    public void testElement_Number() {
654       JSONArray array = new JSONArray();
655       array.element( new Double( 2 ) );
656       Assertions.assertEquals( new Double( 2 ).doubleValue(), array.getDouble( 0 ), 0d );
657    }
658 
659    public void testElement_Object() {
660       JSONArray array = new JSONArray();
661       array.element( new BeanA() );
662       Assertions.assertEquals( JSONObject.fromObject( new BeanA() ), array.getJSONObject( 0 ) );
663    }
664 
665    public void testElement_replace() {
666       JSONArray jsonArray = new JSONArray();
667       jsonArray.element( true );
668       assertEquals( 1, jsonArray.size() );
669       assertTrue( jsonArray.getBoolean( 0 ) );
670       jsonArray.element( 0, false );
671       assertEquals( 1, jsonArray.size() );
672       assertFalse( jsonArray.getBoolean( 0 ) );
673    }
674 
675    public void testElement_String() {
676       JSONArray array = new JSONArray();
677       array.element( "json" );
678       Assertions.assertEquals( "json", array.getString( 0 ) );
679    }
680 
681    public void testElement_String_JSON() {
682       JSONArray array = new JSONArray();
683       array.element( "[]" );
684       Assertions.assertEquals( new JSONArray().toString(), array.getString( 0 ) );
685    }
686 
687    public void testElement_String_null() {
688       JSONArray array = new JSONArray();
689       array.element( (String) null );
690       Assertions.assertEquals( "", array.getString( 0 ) );
691    }
692 
693    public void testFromObject_BigDecimal() {
694       JSONArray actual = JSONArray.fromObject( new BigDecimal( "12345678901234567890.1234567890" ) );
695       assertTrue( actual.get( 0 ) instanceof BigDecimal );
696    }
697 
698    public void testFromObject_BigInteger() {
699       JSONArray actual = JSONArray.fromObject( new BigInteger( "123456789012345678901234567890" ) );
700       assertTrue( actual.get( 0 ) instanceof BigInteger );
701    }
702 
703    public void testFromObject_Boolean() {
704       JSONArray expected = JSONArray.fromObject( "[true]" );
705       JSONArray actual = JSONArray.fromObject( Boolean.TRUE );
706       Assertions.assertEquals( expected, actual );
707    }
708 
709    public void testFromObject_Byte() {
710       JSONArray expected = JSONArray.fromObject( "[1]" );
711       JSONArray actual = JSONArray.fromObject( new Byte( (byte) 1 ) );
712       Assertions.assertEquals( expected, actual );
713    }
714 
715    public void testFromObject_Double() {
716       JSONArray expected = JSONArray.fromObject( "[1.0]" );
717       JSONArray actual = JSONArray.fromObject( new Double( 1d ) );
718       Assertions.assertEquals( expected, actual );
719    }
720 
721    public void testFromObject_Float() {
722       JSONArray expected = JSONArray.fromObject( "[1.0]" );
723       JSONArray actual = JSONArray.fromObject( new Float( 1f ) );
724       Assertions.assertEquals( expected, actual );
725    }
726 
727    public void testFromObject_Integer() {
728       JSONArray expected = JSONArray.fromObject( "[1]" );
729       JSONArray actual = JSONArray.fromObject( new Integer( 1 ) );
730       Assertions.assertEquals( expected, actual );
731    }
732 
733    public void testFromObject_JSONArray() {
734       JSONArray expected = JSONArray.fromObject( "[1,2]" );
735       JSONArray actual = JSONArray.fromObject( JSONArray.fromObject( "[1,2]" ) );
736       Assertions.assertEquals( expected, actual );
737    }
738 
739    public void testFromObject_JSONFunction() {
740       JSONArray expected = JSONArray.fromObject( "[function(a){ return a; }]" );
741       JSONArray actual = JSONArray.fromObject( new JSONFunction( new String[] { "a" }, "return a;" ) );
742       Assertions.assertEquals( expected, actual );
743    }
744 
745    public void testFromObject_JSONString() {
746       ArrayJSONStringBean bean = new ArrayJSONStringBean();
747       bean.setValue( "'json','json'" );
748       JSONArray actual = JSONArray.fromObject( bean );
749       JSONArray expected = JSONArray.fromObject( "['json','json']" );
750       Assertions.assertEquals( expected, actual );
751    }
752 
753    public void testFromObject_Long() {
754       JSONArray expected = JSONArray.fromObject( "[1]" );
755       JSONArray actual = JSONArray.fromObject( new Long( 1L ) );
756       Assertions.assertEquals( expected, actual );
757    }
758 
759    public void testFromObject_Map() {
760       JSONArray expected = JSONArray.fromObject( "[{}]" );
761       JSONArray actual = JSONArray.fromObject( new HashMap() );
762       Assertions.assertEquals( expected, actual );
763    }
764 
765    public void testFromObject_Short() {
766       JSONArray expected = JSONArray.fromObject( "[1]" );
767       JSONArray actual = JSONArray.fromObject( new Short( (short) 1 ) );
768       Assertions.assertEquals( expected, actual );
769    }
770 
771    public void testGet_exception() {
772       try{
773          JSONArray jsonArray = JSONArray.fromObject( "[]" );
774          jsonArray.get( 0 );
775          fail( "Expected a IndexOutOfBoundsException" );
776       }catch( IndexOutOfBoundsException expected ){
777          // OK
778       }
779    }
780 
781    public void testGetBoolean_exception() {
782       try{
783          JSONArray jsonArray = JSONArray.fromObject( "[[]]" );
784          jsonArray.getBoolean( 0 );
785          fail( "Expected a JSONException" );
786       }catch( JSONException expected ){
787          // OK
788       }
789    }
790 
791    public void testGetBoolean_false() {
792       JSONArray jsonArray = JSONArray.fromObject( "[false]" );
793       assertFalse( jsonArray.getBoolean( 0 ) );
794    }
795 
796    public void testGetBoolean_true() {
797       JSONArray jsonArray = JSONArray.fromObject( "[true]" );
798       assertTrue( jsonArray.getBoolean( 0 ) );
799    }
800 
801    public void testGetDimensions_empty_array() {
802       int[] dims = JSONArray.getDimensions( new JSONArray() );
803       assertEquals( 1, dims.length );
804       assertEquals( 0, dims[0] );
805    }
806 
807    public void testGetDimensions_null_array() {
808       int[] dims = JSONArray.getDimensions( null );
809       assertEquals( 1, dims.length );
810       assertEquals( 0, dims[0] );
811    }
812 
813    public void testGetDimensions_one_dimension() {
814       int[] dims = JSONArray.getDimensions( JSONArray.fromObject( "[1,2,3]" ) );
815       assertEquals( 1, dims.length );
816       assertEquals( 3, dims[0] );
817    }
818 
819    public void testGetDimensions_pyramid() {
820       int[] dims = JSONArray.getDimensions( JSONArray.fromObject( "[1,[2,[3,[4]]]]" ) );
821       assertEquals( 4, dims.length );
822       assertEquals( 2, dims[0] );
823       assertEquals( 2, dims[1] );
824       assertEquals( 2, dims[2] );
825       assertEquals( 1, dims[3] );
826 
827       dims = JSONArray.getDimensions( JSONArray.fromObject( "[[[[1],2],3],4]" ) );
828       assertEquals( 4, dims.length );
829       assertEquals( 2, dims[0] );
830       assertEquals( 2, dims[1] );
831       assertEquals( 2, dims[2] );
832       assertEquals( 1, dims[3] );
833    }
834 
835    public void testGetDimensions_two_dimension() {
836       int[] dims = JSONArray.getDimensions( JSONArray.fromObject( "[[1,2,3],[4,5,6]]" ) );
837       assertEquals( 2, dims.length );
838       assertEquals( 2, dims[0] );
839       assertEquals( 3, dims[1] );
840 
841       dims = JSONArray.getDimensions( JSONArray.fromObject( "[[1,2],[4,5,6]]" ) );
842       assertEquals( 2, dims.length );
843       assertEquals( 2, dims[0] );
844       assertEquals( 3, dims[1] );
845 
846       dims = JSONArray.getDimensions( JSONArray.fromObject( "[[1,2,3],[4,5]]" ) );
847       assertEquals( 2, dims.length );
848       assertEquals( 2, dims[0] );
849       assertEquals( 3, dims[1] );
850    }
851 
852    public void testGetDouble_exception() {
853       try{
854          JSONArray jsonArray = JSONArray.fromObject( "[[]]" );
855          jsonArray.getDouble( 0 );
856          fail( "Expected a JSONException" );
857       }catch( JSONException expected ){
858          // OK
859       }
860    }
861 
862    public void testGetDouble_Number() {
863       JSONArray jsonArray = JSONArray.fromObject( "[2.0]" );
864       assertEquals( 2.0d, jsonArray.getDouble( 0 ), 0d );
865    }
866 
867    public void testGetDouble_String() {
868       JSONArray jsonArray = JSONArray.fromObject( "[\"2.0\"]" );
869       assertEquals( 2.0d, jsonArray.getDouble( 0 ), 0d );
870    }
871 
872    public void testGetInt_exception() {
873       try{
874          JSONArray jsonArray = JSONArray.fromObject( "[[]]" );
875          jsonArray.getInt( 0 );
876          fail( "Expected a JSONException" );
877       }catch( JSONException expected ){
878          // OK
879       }
880    }
881 
882    public void testGetInt_Number() {
883       JSONArray jsonArray = JSONArray.fromObject( "[2.0]" );
884       assertEquals( 2, jsonArray.getInt( 0 ) );
885    }
886 
887    public void testGetInt_String() {
888       JSONArray jsonArray = JSONArray.fromObject( "[\"2.0\"]" );
889       assertEquals( 2, jsonArray.getInt( 0 ) );
890    }
891 
892    public void testGetJSONArray() {
893       JSONArray jsonArray = JSONArray.fromObject( "[[1,2]]" );
894       Assertions.assertEquals( JSONArray.fromObject( "[1,2]" )
895             .toString(), jsonArray.getJSONArray( 0 )
896             .toString() );
897    }
898 
899    public void testGetJSONArray_exception() {
900       try{
901          JSONArray jsonArray = JSONArray.fromObject( "[1]" );
902          jsonArray.getJSONArray( 0 );
903          fail( "Expected a JSONException" );
904       }catch( JSONException expected ){
905          // OK
906       }
907    }
908 
909    public void testGetJSONObject() {
910       JSONArray jsonArray = JSONArray.fromObject( "[{\"name\":\"json\"}]" );
911       Assertions.assertEquals( JSONObject.fromObject( "{\"name\":\"json\"}" ),
912             jsonArray.getJSONObject( 0 ) );
913    }
914 
915    public void testGetJSONObject_exception() {
916       try{
917          JSONArray jsonArray = JSONArray.fromObject( "[1]" );
918          jsonArray.getJSONObject( 0 );
919          fail( "Expected a JSONException" );
920       }catch( JSONException expected ){
921          // OK
922       }
923    }
924 
925    public void testGetLong_exception() {
926       try{
927          JSONArray jsonArray = JSONArray.fromObject( "[[]]" );
928          jsonArray.getLong( 0 );
929          fail( "Expected a JSONException" );
930       }catch( JSONException expected ){
931          // OK
932       }
933    }
934 
935    public void testGetLong_Number() {
936       JSONArray jsonArray = JSONArray.fromObject( "[2.0]" );
937       assertEquals( 2, jsonArray.getLong( 0 ) );
938    }
939 
940    public void testGetLong_String() {
941       JSONArray jsonArray = JSONArray.fromObject( "[\"2.0\"]" );
942       assertEquals( 2, jsonArray.getLong( 0 ) );
943    }
944 
945    public void testGetString() {
946       JSONArray jsonArray = JSONArray.fromObject( "[\"2.0\"]" );
947       assertEquals( "2.0", jsonArray.getString( 0 ) );
948    }
949 
950    public void testGetString_exception() {
951       try{
952          JSONArray jsonArray = JSONArray.fromObject( "[]" );
953          jsonArray.getString( 0 );
954          fail( "Expected a IndexOutOfBoundsException" );
955       }catch( IndexOutOfBoundsException expected ){
956          // OK
957       }
958    }
959 
960    public void testOptionalGet() {
961       Object[] params = new Object[] { new JSONArray(), JSONObject.fromObject( "{\"int\":1}" ) };
962       JSONArray jsonArray = JSONArray.fromObject( params );
963       assertFalse( jsonArray.optBoolean( 0 ) );
964       assertTrue( jsonArray.optBoolean( 0, true ) );
965       assertTrue( Double.isNaN( jsonArray.optDouble( 0 ) ) );
966       assertEquals( 0d, jsonArray.optDouble( 0, 0d ), 0d );
967       assertEquals( 0, jsonArray.optInt( 0 ) );
968       assertEquals( 1, jsonArray.optInt( 0, 1 ) );
969       assertEquals( null, jsonArray.optJSONArray( 1 ) );
970       Assertions.assertEquals( (JSONArray) params[0], jsonArray.optJSONArray( 0 ) );
971       assertEquals( null, jsonArray.optJSONObject( 0 ) );
972       Assertions.assertEquals( (JSONObject) params[1], jsonArray.optJSONObject( 1 ) );
973       assertEquals( 0, jsonArray.optLong( 0 ) );
974       assertEquals( 1, jsonArray.optLong( 0, 1 ) );
975       assertEquals( "", jsonArray.optString( 3 ) );
976       assertEquals( "json", jsonArray.optString( 3, "json" ) );
977    }
978 
979    public void testToArray_bean_element() {
980       BeanA[] expected = new BeanA[] { new BeanA() };
981       JSONArray jsonArray = JSONArray.fromObject( expected );
982       Object actual = JSONArray.toArray( jsonArray, BeanA.class );
983       Assertions.assertEquals( expected, actual );
984    }
985 
986    public void testToArray_BigDecimal() {
987       BigDecimal[] expected = new BigDecimal[] { MorphUtils.BIGDECIMAL_ZERO,
988             MorphUtils.BIGDECIMAL_ONE };
989       JSONArray jsonArray = JSONArray.fromObject( expected );
990       Object actual = JSONArray.toArray( jsonArray );
991       Assertions.assertEquals( expected, actual );
992    }
993 
994    public void testToArray_BigInteger() {
995       BigInteger[] expected = new BigInteger[] { BigInteger.ZERO, BigInteger.ONE };
996       JSONArray jsonArray = JSONArray.fromObject( expected );
997       Object actual = JSONArray.toArray( jsonArray );
998       Assertions.assertEquals( expected, actual );
999    }
1000 
1001    public void testToArray_boolean() {
1002       boolean[] expected = new boolean[] { true, false };
1003       JSONArray jsonArray = JSONArray.fromObject( expected );
1004       Object actual = JSONArray.toArray( jsonArray );
1005       Assertions.assertEquals( expected, actual );
1006    }
1007 
1008    public void testToArray_Boolean() {
1009       Boolean[] expected = new Boolean[] { Boolean.TRUE, Boolean.FALSE };
1010       JSONArray jsonArray = JSONArray.fromObject( expected );
1011       Object actual = JSONArray.toArray( jsonArray );
1012       Assertions.assertEquals( expected, actual );
1013    }
1014 
1015    public void testToArray_boolean_multi() {
1016       boolean[][] expected = new boolean[][] { { true, false }, { false, true } };
1017       JSONArray jsonArray = JSONArray.fromObject( expected );
1018       Object actual = JSONArray.toArray( jsonArray );
1019       Assertions.assertEquals( expected, actual );
1020    }
1021 
1022    public void testToArray_byte() {
1023       byte[] input = new byte[] { 1, 2, 3, 4, 5, 6 };
1024       int[] expected = new int[] { 1, 2, 3, 4, 5, 6 };
1025       JSONArray jsonArray = JSONArray.fromObject( input );
1026       Object actual = JSONArray.toArray( jsonArray );
1027       Assertions.assertEquals( expected, actual );
1028    }
1029 
1030    public void testToArray_Byte() {
1031       Integer[] expected = new Integer[] { new Integer( 1 ), new Integer( 2 ) };
1032       Byte[] bytes = new Byte[] { new Byte( (byte) 1 ), new Byte( (byte) 2 ) };
1033       JSONArray jsonArray = JSONArray.fromObject( bytes );
1034       Object actual = JSONArray.toArray( jsonArray );
1035       Assertions.assertEquals( expected, actual );
1036    }
1037 
1038    public void testToArray_byte_multi() {
1039       byte[][] input = new byte[][] { { 1, 2, 3 }, { 4, 5, 6 } };
1040       int[][] expected = new int[][] { { 1, 2, 3 }, { 4, 5, 6 } };
1041       JSONArray jsonArray = JSONArray.fromObject( input );
1042       Object actual = JSONArray.toArray( jsonArray );
1043       Assertions.assertEquals( expected, actual );
1044    }
1045 
1046    public void testToArray_char() {
1047       String[] expected = new String[] { "A", "B" };
1048       char[] input = new char[] { 'A', 'B' };
1049       JSONArray jsonArray = JSONArray.fromObject( input );
1050       Object actual = JSONArray.toArray( jsonArray );
1051       Assertions.assertEquals( expected, actual );
1052    }
1053 
1054    public void testToArray_char_multi() {
1055       String[][] expected = new String[][] { { "a", "b" }, { "c", "d" } };
1056       char[][] input = new char[][] { { 'a', 'b' }, { 'c', 'd' } };
1057       JSONArray jsonArray = JSONArray.fromObject( input );
1058       Object actual = JSONArray.toArray( jsonArray );
1059       Assertions.assertEquals( expected, actual );
1060    }
1061 
1062    public void testToArray_Character() {
1063       String[] expected = { "A", "B" };
1064       Character[] chars = new Character[] { new Character( 'A' ), new Character( 'B' ) };
1065       JSONArray jsonArray = JSONArray.fromObject( chars );
1066       Object actual = JSONArray.toArray( jsonArray );
1067       Assertions.assertEquals( expected, actual );
1068    }
1069 
1070    public void testToArray_double() {
1071       double[] expected = new double[] { 1, 2, 3, 4, 5, 6 };
1072       JSONArray jsonArray = JSONArray.fromObject( expected );
1073       Object actual = JSONArray.toArray( jsonArray );
1074       Assertions.assertEquals( expected, actual );
1075    }
1076 
1077    public void testToArray_Double() {
1078       Double[] expected = new Double[] { new Double( 1d ), new Double( 2d ) };
1079       JSONArray jsonArray = JSONArray.fromObject( expected );
1080       Object actual = JSONArray.toArray( jsonArray );
1081       Assertions.assertEquals( expected, actual );
1082    }
1083 
1084    public void testToArray_double_multi() {
1085       double[][] expected = new double[][] { { 1, 2, 3 }, { 4, 5, 6 } };
1086       JSONArray jsonArray = JSONArray.fromObject( expected );
1087       Object actual = JSONArray.toArray( jsonArray );
1088       Assertions.assertEquals( expected, actual );
1089    }
1090 
1091    public void testToArray_dynabean_element() throws Exception {
1092       DynaBean[] expected = new DynaBean[] { createDynaBean() };
1093       JSONArray jsonArray = JSONArray.fromObject( expected );
1094       Object actual = JSONArray.toArray( jsonArray );
1095       Assertions.assertEquals( expected, actual );
1096    }
1097 
1098    public void testToArray_float() {
1099       double[] expected = new double[] { 1, 2, 3, 4, 5, 6 };
1100       float[] input = new float[] { 1, 2, 3, 4, 5, 6 };
1101       JSONArray jsonArray = JSONArray.fromObject( input );
1102       Object actual = JSONArray.toArray( jsonArray );
1103       Assertions.assertEquals( expected, actual );
1104    }
1105 
1106    public void testToArray_Float() {
1107       Double[] expected = new Double[] { new Double( 1d ), new Double( 2d ) };
1108       Float[] floats = new Float[] { new Float( 1f ), new Float( 2f ) };
1109       JSONArray jsonArray = JSONArray.fromObject( floats );
1110       Object actual = JSONArray.toArray( jsonArray );
1111       Assertions.assertEquals( expected, actual );
1112    }
1113 
1114    public void testToArray_float_multi() {
1115       double[][] expected = new double[][] { { 1, 2, 3 }, { 4, 5, 6 } };
1116       float[][] input = new float[][] { { 1, 2, 3 }, { 4, 5, 6 } };
1117       JSONArray jsonArray = JSONArray.fromObject( input );
1118       Object actual = JSONArray.toArray( jsonArray );
1119       Assertions.assertEquals( expected, actual );
1120    }
1121 
1122    public void testToArray_int() {
1123       int[] expected = new int[] { 1, 2, 3, 4, 5, 6 };
1124       JSONArray jsonArray = JSONArray.fromObject( expected );
1125       Object actual = JSONArray.toArray( jsonArray );
1126       Assertions.assertEquals( expected, actual );
1127    }
1128 
1129    public void testToArray_int_multi() {
1130       int[][] expected = new int[][] { { 1, 2, 3 }, { 4, 5, 6 } };
1131       JSONArray jsonArray = JSONArray.fromObject( expected );
1132       Object actual = JSONArray.toArray( jsonArray );
1133       Assertions.assertEquals( expected, actual );
1134    }
1135 
1136    public void testToArray_Integer() {
1137       Integer[] expected = new Integer[] { new Integer( 1 ), new Integer( 2 ) };
1138       JSONArray jsonArray = JSONArray.fromObject( expected );
1139       Object actual = JSONArray.toArray( jsonArray );
1140       Assertions.assertEquals( expected, actual );
1141    }
1142 
1143    public void testToArray_long() {
1144       long[] input = new long[] { 1, 2, 3, 4, 5, 6 };
1145       int[] expected = new int[] { 1, 2, 3, 4, 5, 6 };
1146       JSONArray jsonArray = JSONArray.fromObject( input );
1147       Object actual = JSONArray.toArray( jsonArray );
1148       Assertions.assertEquals( expected, actual );
1149    }
1150 
1151    public void testToArray_Long() {
1152       Integer[] expected = new Integer[] { new Integer( 1 ), new Integer( 2 ) };
1153       Long[] longs = new Long[] { new Long( 1L ), new Long( 2L ) };
1154       JSONArray jsonArray = JSONArray.fromObject( longs );
1155       Object actual = JSONArray.toArray( jsonArray );
1156       Assertions.assertEquals( expected, actual );
1157    }
1158 
1159    public void testToArray_long_multi() {
1160       long[][] input = new long[][] { { 1, 2, 3 }, { 4, 5, 6 } };
1161       int[][] expected = new int[][] { { 1, 2, 3 }, { 4, 5, 6 } };
1162       JSONArray jsonArray = JSONArray.fromObject( input );
1163       Object actual = JSONArray.toArray( jsonArray );
1164       Assertions.assertEquals( expected, actual );
1165    }
1166 
1167    public void testToArray_Long2() {
1168       Long[] expected = new Long[] { new Long( Integer.MAX_VALUE + 1L ),
1169             new Long( Integer.MAX_VALUE + 2L ) };
1170       JSONArray jsonArray = JSONArray.fromObject( expected );
1171       Object actual = JSONArray.toArray( jsonArray );
1172       Assertions.assertEquals( expected, actual );
1173    }
1174 
1175    public void testToArray_null_elements() {
1176       String[] expected = new String[] { null, null, null };
1177       JSONArray jsonArray = JSONArray.fromObject( expected );
1178       Object actual = JSONArray.toArray( jsonArray );
1179       Assertions.assertEquals( expected, actual );
1180    }
1181 
1182    public void testToArray_short() {
1183       short[] input = new short[] { 1, 2, 3, 4, 5, 6 };
1184       int[] expected = new int[] { 1, 2, 3, 4, 5, 6 };
1185       JSONArray jsonArray = JSONArray.fromObject( input );
1186       Object actual = JSONArray.toArray( jsonArray );
1187       Assertions.assertEquals( expected, actual );
1188    }
1189 
1190    public void testToArray_Short() {
1191       Integer[] expected = new Integer[] { new Integer( 1 ), new Integer( 2 ) };
1192       Short[] shorts = new Short[] { new Short( (short) 1 ), new Short( (short) 2 ) };
1193       JSONArray jsonArray = JSONArray.fromObject( shorts );
1194       Object actual = JSONArray.toArray( jsonArray );
1195       Assertions.assertEquals( expected, actual );
1196    }
1197 
1198    public void testToArray_short_multi() {
1199       short[][] input = new short[][] { { 1, 2, 3 }, { 4, 5, 6 } };
1200       int[][] expected = new int[][] { { 1, 2, 3 }, { 4, 5, 6 } };
1201       JSONArray jsonArray = JSONArray.fromObject( input );
1202       Object actual = JSONArray.toArray( jsonArray );
1203       Assertions.assertEquals( expected, actual );
1204    }
1205 
1206    public void testToArray_String() {
1207       String[] expected = new String[] { "1", "2", "3", "4", "5", "6" };
1208       JSONArray jsonArray = JSONArray.fromObject( expected );
1209       Object actual = JSONArray.toArray( jsonArray );
1210       Assertions.assertEquals( expected, actual );
1211    }
1212 
1213    public void testToArray_String_multi() {
1214       String[][] expected = new String[][] { { "1", "2", "3" }, { "4", "5", "6" } };
1215       JSONArray jsonArray = JSONArray.fromObject( expected );
1216       Object actual = JSONArray.toArray( jsonArray );
1217       Assertions.assertEquals( expected, actual );
1218    }
1219 
1220    public void testToArray_StringToInt() {
1221       int[] expected = new int[] { 1, 2, 3, 4, 5, 6 };
1222       String[] input = new String[] { "1", "2", "3", "4", "5", "6" };
1223       JSONArray jsonArray = JSONArray.fromObject( input );
1224       JsonConfig jsonConfig = new JsonConfig();
1225       jsonConfig.setRootClass( Integer.TYPE );
1226       Object actual = JSONArray.toArray( jsonArray, jsonConfig );
1227       Assertions.assertEquals( expected, actual );
1228    }
1229 
1230    public void testToArray_StringToInteger() {
1231       int[] expected = new int[] { 1, 2, 3, 4, 5, 6 };
1232       String[] input = new String[] { "1", "2", "3", "4", "5", "6" };
1233       JSONArray jsonArray = JSONArray.fromObject( input );
1234       JsonConfig jsonConfig = new JsonConfig();
1235       jsonConfig.setRootClass( Integer.class );
1236       Object actual = JSONArray.toArray( jsonArray, jsonConfig );
1237       Assertions.assertEquals( expected, actual );
1238    }
1239 
1240    public void testToArray_StringToInteger_empty() {
1241       int[] expected = new int[] {};
1242       String[] input = new String[] {};
1243       JSONArray jsonArray = JSONArray.fromObject( input );
1244       JsonConfig jsonConfig = new JsonConfig();
1245       jsonConfig.setRootClass( Integer.class );
1246       Object actual = JSONArray.toArray( jsonArray, jsonConfig );
1247       Assertions.assertEquals( expected, actual );
1248    }
1249 
1250    public void testToJSONObject() {
1251       JSONArray jsonArray = JSONArray.fromObject( "[\"json\",1,true]" );
1252       JSONObject expected = JSONObject.fromObject( "{\"string\":\"json\",\"int\":1,\"bool\":true}" );
1253       JSONArray names = JSONArray.fromObject( "['string','int','bool']" );
1254 
1255       Assertions.assertEquals( expected, jsonArray.toJSONObject( names ) );
1256    }
1257 
1258    public void testToJSONObject_null_object() {
1259       JSONArray jsonArray = new JSONArray();
1260       assertNull( jsonArray.toJSONObject( null ) );
1261       assertNull( jsonArray.toJSONObject( new JSONArray() ) );
1262       assertNull( jsonArray.toJSONObject( JSONArray.fromObject( "['json']" ) ) );
1263    }
1264 
1265    public void testToList_bean_elements() {
1266       List expected = new ArrayList();
1267       expected.add( new BeanA() );
1268       JSONArray jsonArray = JSONArray.fromObject( expected );
1269       List actual = JSONArray.toList( jsonArray, BeanA.class );
1270       Assertions.assertEquals( expected, actual );
1271    }
1272 
1273    public void testToList_BigDecimal() {
1274       List expected = new ArrayList();
1275       expected.add( MorphUtils.BIGDECIMAL_ZERO );
1276       expected.add( MorphUtils.BIGDECIMAL_ONE );
1277       JSONArray jsonArray = JSONArray.fromObject( expected );
1278       List actual = JSONArray.toList( jsonArray );
1279       Assertions.assertEquals( expected, actual );
1280    }
1281 
1282    public void testToList_BigInteger() {
1283       List expected = new ArrayList();
1284       expected.add( BigInteger.ZERO );
1285       expected.add( BigInteger.ONE );
1286       JSONArray jsonArray = JSONArray.fromObject( expected );
1287       List actual = JSONArray.toList( jsonArray );
1288       Assertions.assertEquals( expected, actual );
1289    }
1290 
1291    public void testToList_Boolean() {
1292       List expected = new ArrayList();
1293       expected.add( Boolean.TRUE );
1294       expected.add( Boolean.FALSE );
1295       JSONArray jsonArray = JSONArray.fromObject( expected );
1296       List actual = JSONArray.toList( jsonArray );
1297       Assertions.assertEquals( expected, actual );
1298    }
1299 
1300    public void testToList_Byte() {
1301       List expected = new ArrayList();
1302       expected.add( new Integer( 1 ) );
1303       expected.add( new Integer( 2 ) );
1304       List bytes = new ArrayList();
1305       bytes.add( new Byte( (byte) 1 ) );
1306       bytes.add( new Byte( (byte) 2 ) );
1307       JSONArray jsonArray = JSONArray.fromObject( bytes );
1308       List actual = JSONArray.toList( jsonArray );
1309       Assertions.assertEquals( expected, actual );
1310    }
1311 
1312    public void testToList_Character() {
1313       List expected = new ArrayList();
1314       expected.add( "A" );
1315       expected.add( "B" );
1316       List chars = new ArrayList();
1317       chars.add( new Character( 'A' ) );
1318       chars.add( new Character( 'B' ) );
1319       JSONArray jsonArray = JSONArray.fromObject( chars );
1320       List actual = JSONArray.toList( jsonArray );
1321       Assertions.assertEquals( expected, actual );
1322    }
1323 
1324    public void testToList_Double() {
1325       List expected = new ArrayList();
1326       expected.add( new Double( 1d ) );
1327       expected.add( new Double( 2d ) );
1328       JSONArray jsonArray = JSONArray.fromObject( expected );
1329       List actual = JSONArray.toList( jsonArray );
1330       Assertions.assertEquals( expected, actual );
1331    }
1332 
1333    public void testToList_dynaBean_elements() throws Exception {
1334       List expected = new ArrayList();
1335       expected.add( createDynaBean() );
1336       JSONArray jsonArray = JSONArray.fromObject( expected );
1337       List actual = JSONArray.toList( jsonArray );
1338       Assertions.assertEquals( expected, actual );
1339    }
1340 
1341    public void testToList_Float() {
1342       List expected = new ArrayList();
1343       expected.add( new Double( 1d ) );
1344       expected.add( new Double( 2d ) );
1345       List floats = new ArrayList();
1346       floats.add( new Float( 1f ) );
1347       floats.add( new Float( 2f ) );
1348       JSONArray jsonArray = JSONArray.fromObject( floats );
1349       List actual = JSONArray.toList( jsonArray );
1350       Assertions.assertEquals( expected, actual );
1351    }
1352 
1353    public void testToList_Integer() {
1354       List expected = new ArrayList();
1355       expected.add( new Integer( 1 ) );
1356       expected.add( new Integer( 2 ) );
1357       JSONArray jsonArray = JSONArray.fromObject( expected );
1358       List actual = JSONArray.toList( jsonArray );
1359       Assertions.assertEquals( expected, actual );
1360    }
1361 
1362    public void testToList_JSONFunction_elements() {
1363       List expected = new ArrayList();
1364       expected.add( new JSONFunction( new String[] { "a" }, "return a;" ) );
1365       JSONArray jsonArray = JSONArray.fromObject( expected );
1366       List actual = JSONArray.toList( jsonArray );
1367       Assertions.assertEquals( expected, actual );
1368    }
1369 
1370    public void testToList_JSONFunction_elements_2() {
1371       List expected = new ArrayList();
1372       expected.add( "function(a){ return a; }" );
1373       JSONArray jsonArray = JSONArray.fromObject( expected );
1374       List actual = JSONArray.toList( jsonArray );
1375       Assertions.assertEquals( expected, actual );
1376    }
1377 
1378    public void testToList_Long() {
1379       List expected = new ArrayList();
1380       expected.add( new Integer( 1 ) );
1381       expected.add( new Integer( 2 ) );
1382       List longs = new ArrayList();
1383       longs.add( new Long( 1L ) );
1384       longs.add( new Long( 2L ) );
1385       JSONArray jsonArray = JSONArray.fromObject( longs );
1386       List actual = JSONArray.toList( jsonArray );
1387       Assertions.assertEquals( expected, actual );
1388    }
1389 
1390    public void testToList_Long2() {
1391       List expected = new ArrayList();
1392       expected.add( new Long( Integer.MAX_VALUE + 1L ) );
1393       expected.add( new Long( Integer.MAX_VALUE + 2L ) );
1394       JSONArray jsonArray = JSONArray.fromObject( expected );
1395       List actual = JSONArray.toList( jsonArray );
1396       Assertions.assertEquals( expected, actual );
1397    }
1398 
1399    public void testToList_null_elements() {
1400       List expected = new ArrayList();
1401       expected.add( null );
1402       expected.add( null );
1403       expected.add( null );
1404       JSONArray jsonArray = JSONArray.fromObject( expected );
1405       List actual = JSONArray.toList( jsonArray );
1406       Assertions.assertEquals( expected, actual );
1407    }
1408 
1409    public void testToList_Short() {
1410       List expected = new ArrayList();
1411       expected.add( new Integer( 1 ) );
1412       expected.add( new Integer( 2 ) );
1413       List shorts = new ArrayList();
1414       shorts.add( new Short( (short) 1 ) );
1415       shorts.add( new Short( (short) 2 ) );
1416       JSONArray jsonArray = JSONArray.fromObject( shorts );
1417       List actual = JSONArray.toList( jsonArray );
1418       Assertions.assertEquals( expected, actual );
1419    }
1420 
1421    public void testToList_String() {
1422       List expected = new ArrayList();
1423       expected.add( "A" );
1424       expected.add( "B" );
1425       JSONArray jsonArray = JSONArray.fromObject( expected );
1426       List actual = JSONArray.toList( jsonArray );
1427       Assertions.assertEquals( expected, actual );
1428    }
1429 
1430    public void testToList_String_multi() {
1431       List a = new ArrayList();
1432       a.add( "a" );
1433       a.add( "b" );
1434       List b = new ArrayList();
1435       b.add( "1" );
1436       b.add( "2" );
1437       List expected = new ArrayList();
1438       expected.add( a );
1439       expected.add( b );
1440       JSONArray jsonArray = JSONArray.fromObject( expected );
1441       List actual = JSONArray.toList( jsonArray );
1442       Assertions.assertEquals( expected, actual );
1443    }
1444 
1445    public void testWrite() {
1446       JSONArray jsonArray = JSONArray.fromObject( "[[],{},1,true,\"json\"]" );
1447       StringWriter sw = new StringWriter();
1448       jsonArray.write( sw );
1449       assertEquals( "[[],{},1,true,\"json\"]", sw.toString() );
1450    }
1451 
1452    private MorphDynaBean createDynaBean() throws Exception {
1453       Map properties = new HashMap();
1454       properties.put( "name", String.class );
1455       MorphDynaClass dynaClass = new MorphDynaClass( properties );
1456       MorphDynaBean dynaBean = (MorphDynaBean) dynaClass.newInstance();
1457       dynaBean.setDynaBeanClass( dynaClass );
1458       dynaBean.set( "name", "json" );
1459       // JSON Strings can not be null, only empty
1460       return dynaBean;
1461    }
1462 
1463    private void testJSONArray( Object array, String expected ) {
1464       try{
1465          JSONArray jsonArray = JSONArray.fromObject( array );
1466          assertEquals( expected, jsonArray.toString() );
1467       }catch( JSONException jsone ){
1468          fail( jsone.getMessage() );
1469       }
1470    }
1471 }