11package org .json .junit ;
22
3+ import static org .junit .Assert .assertEquals ;
34import static org .junit .Assert .assertNull ;
45import static org .junit .Assert .assertTrue ;
5- import static org .junit .Assert .assertEquals ;
66
7+ import java .io .IOException ;
78import java .io .StringWriter ;
8- import java .io .Writer ;
99import java .math .BigDecimal ;
1010import java .math .BigInteger ;
1111import java .util .ArrayList ;
@@ -61,7 +61,7 @@ public class JSONArrayTest {
6161 @ Test (expected =NullPointerException .class )
6262 public void nullException () {
6363 String str = null ;
64- new JSONArray (str );
64+ assertNull ( "Should throw an exception" , new JSONArray (str ) );
6565 }
6666
6767 /**
@@ -72,8 +72,7 @@ public void nullException() {
7272 public void emptStr () {
7373 String str = "" ;
7474 try {
75- new JSONArray (str );
76- assertTrue ("Should throw an exception" , false );
75+ assertNull ("Should throw an exception" , new JSONArray (str ));
7776 } catch (JSONException e ) {
7877 assertTrue ("Expected an exception message" ,
7978 "A JSONArray text must start with '[' at 1 [character 2 line 1]" .
@@ -90,8 +89,7 @@ public void emptStr() {
9089 public void badObject () {
9190 String str = "abc" ;
9291 try {
93- new JSONArray ((Object )str );
94- assertTrue ("Should throw an exception" , false );
92+ assertNull ("Should throw an exception" , new JSONArray ((Object )str ));
9593 } catch (JSONException e ) {
9694 assertTrue ("Expected an exception message" ,
9795 "JSONArray initial value should be a string or collection or array." .
@@ -100,7 +98,7 @@ public void badObject() {
10098 }
10199
102100 /**
103- * Verifies that the constructor has backwards compatability with RAW types pre-java5.
101+ * Verifies that the constructor has backwards compatibility with RAW types pre-java5.
104102 */
105103 @ Test
106104 public void verifyConstructor () {
@@ -130,7 +128,7 @@ public void verifyConstructor() {
130128 }
131129
132130 /**
133- * Verifies that the put Collection has backwards compatability with RAW types pre-java5.
131+ * Verifies that the put Collection has backwards compatibility with RAW types pre-java5.
134132 */
135133 @ Test
136134 public void verifyPutCollection () {
@@ -164,7 +162,7 @@ public void verifyPutCollection() {
164162
165163
166164 /**
167- * Verifies that the put Map has backwards compatability with RAW types pre-java5.
165+ * Verifies that the put Map has backwards compatibility with RAW types pre-java5.
168166 */
169167 @ Test
170168 public void verifyPutMap () {
@@ -209,9 +207,10 @@ public void verifyPutMap() {
209207 * Create a JSONArray doc with a variety of different elements.
210208 * Confirm that the values can be accessed via the get[type]() API methods
211209 */
210+ @ SuppressWarnings ("boxing" )
212211 @ Test
213212 public void getArrayValues () {
214- JSONArray jsonArray = new JSONArray (arrayStr );
213+ JSONArray jsonArray = new JSONArray (this . arrayStr );
215214 // booleans
216215 assertTrue ("Array true" ,
217216 true == jsonArray .getBoolean (0 ));
@@ -255,7 +254,7 @@ public void getArrayValues() {
255254 */
256255 @ Test
257256 public void failedGetArrayValues () {
258- JSONArray jsonArray = new JSONArray (arrayStr );
257+ JSONArray jsonArray = new JSONArray (this . arrayStr );
259258 try {
260259 jsonArray .getBoolean (4 );
261260 assertTrue ("expected getBoolean to fail" , false );
@@ -321,7 +320,7 @@ public void failedGetArrayValues() {
321320 */
322321 @ Test
323322 public void join () {
324- JSONArray jsonArray = new JSONArray (arrayStr );
323+ JSONArray jsonArray = new JSONArray (this . arrayStr );
325324 String joinStr = jsonArray .join ("," );
326325
327326 // validate JSON
@@ -357,7 +356,7 @@ public void join() {
357356 public void length () {
358357 assertTrue ("expected empty JSONArray length 0" ,
359358 new JSONArray ().length () == 0 );
360- JSONArray jsonArray = new JSONArray (arrayStr );
359+ JSONArray jsonArray = new JSONArray (this . arrayStr );
361360 assertTrue ("expected JSONArray length 13" , jsonArray .length () == 13 );
362361 JSONArray nestedJsonArray = jsonArray .getJSONArray (9 );
363362 assertTrue ("expected JSONArray length 1" , nestedJsonArray .length () == 1 );
@@ -368,9 +367,10 @@ public void length() {
368367 * Confirm that the values can be accessed via the opt[type](index)
369368 * and opt[type](index, default) API methods.
370369 */
370+ @ SuppressWarnings ("boxing" )
371371 @ Test
372372 public void opt () {
373- JSONArray jsonArray = new JSONArray (arrayStr );
373+ JSONArray jsonArray = new JSONArray (this . arrayStr );
374374 assertTrue ("Array opt value true" ,
375375 Boolean .TRUE == jsonArray .opt (0 ));
376376 assertTrue ("Array opt value out of range" ,
@@ -441,6 +441,7 @@ public void optStringConversion(){
441441 * Exercise the JSONArray.put(value) method with various parameters
442442 * and confirm the resulting JSONArray.
443443 */
444+ @ SuppressWarnings ("boxing" )
444445 @ Test
445446 public void put () {
446447 JSONArray jsonArray = new JSONArray ();
@@ -516,6 +517,7 @@ public void put() {
516517 * Exercise the JSONArray.put(index, value) method with various parameters
517518 * and confirm the resulting JSONArray.
518519 */
520+ @ SuppressWarnings ("boxing" )
519521 @ Test
520522 public void putIndex () {
521523 JSONArray jsonArray = new JSONArray ();
@@ -596,11 +598,11 @@ public void putIndex() {
596598 */
597599 @ Test
598600 public void remove () {
599- String arrayStr =
601+ String arrayStr1 =
600602 "[" +
601603 "1" +
602604 "]" ;
603- JSONArray jsonArray = new JSONArray (arrayStr );
605+ JSONArray jsonArray = new JSONArray (arrayStr1 );
604606 jsonArray .remove (0 );
605607 assertTrue ("array should be empty" , null == jsonArray .remove (5 ));
606608 assertTrue ("jsonArray should be empty" , jsonArray .length () == 0 );
@@ -612,11 +614,11 @@ public void remove() {
612614 */
613615 @ Test
614616 public void notSimilar () {
615- String arrayStr =
617+ String arrayStr1 =
616618 "[" +
617619 "1" +
618620 "]" ;
619- JSONArray jsonArray = new JSONArray (arrayStr );
621+ JSONArray jsonArray = new JSONArray (arrayStr1 );
620622 JSONArray otherJsonArray = new JSONArray ();
621623 assertTrue ("arrays lengths differ" , !jsonArray .similar (otherJsonArray ));
622624
@@ -745,9 +747,10 @@ public void objectArrayVsIsArray() {
745747 /**
746748 * Exercise the JSONArray iterator.
747749 */
750+ @ SuppressWarnings ("boxing" )
748751 @ Test
749752 public void iterator () {
750- JSONArray jsonArray = new JSONArray (arrayStr );
753+ JSONArray jsonArray = new JSONArray (this . arrayStr );
751754 Iterator <Object > it = jsonArray .iterator ();
752755 assertTrue ("Array true" ,
753756 Boolean .TRUE .equals (it .next ()));
@@ -803,16 +806,20 @@ public void optQueryWithSyntaxError() {
803806 * Exercise the JSONArray write() method
804807 */
805808 @ Test
806- public void write () {
809+ public void write () throws IOException {
807810 String str = "[\" value1\" ,\" value2\" ,{\" key1\" :1,\" key2\" :2,\" key3\" :3}]" ;
808811 JSONArray jsonArray = new JSONArray (str );
809812 String expectedStr = str ;
810813 StringWriter stringWriter = new StringWriter ();
811- Writer writer = jsonArray .write (stringWriter );
812- String actualStr = writer .toString ();
813- assertTrue ("write() expected " + expectedStr +
814- " but found " + actualStr ,
815- expectedStr .equals (actualStr ));
814+ try {
815+ jsonArray .write (stringWriter );
816+ String actualStr = stringWriter .toString ();
817+ assertTrue ("write() expected " + expectedStr +
818+ " but found " + actualStr ,
819+ expectedStr .equals (actualStr ));
820+ } finally {
821+ stringWriter .close ();
822+ }
816823 }
817824
818825 /**
@@ -837,7 +844,7 @@ public void writeAppendable() {
837844 * Exercise the JSONArray write(Writer, int, int) method
838845 */
839846 @ Test
840- public void write3Param () {
847+ public void write3Param () throws IOException {
841848 String str0 = "[\" value1\" ,\" value2\" ,{\" key1\" :1,\" key2\" :false,\" key3\" :3.14}]" ;
842849 String str2 =
843850 "[\n " +
@@ -852,15 +859,20 @@ public void write3Param() {
852859 JSONArray jsonArray = new JSONArray (str0 );
853860 String expectedStr = str0 ;
854861 StringWriter stringWriter = new StringWriter ();
855- Writer writer = jsonArray .write (stringWriter , 0 , 0 );
856- String actualStr = writer .toString ();
857- assertEquals (expectedStr , actualStr );
858-
859- expectedStr = str2 ;
862+ try {
863+ String actualStr = jsonArray .write (stringWriter , 0 , 0 ).toString ();
864+ assertEquals (expectedStr , actualStr );
865+ } finally {
866+ stringWriter .close ();
867+ }
860868 stringWriter = new StringWriter ();
861- writer = jsonArray .write (stringWriter , 2 , 1 );
862- actualStr = writer .toString ();
863- assertEquals (expectedStr , actualStr );
869+ try {
870+ expectedStr = str2 ;
871+ String actualStr = jsonArray .write (stringWriter , 2 , 1 ).toString ();
872+ assertEquals (expectedStr , actualStr );
873+ } finally {
874+ stringWriter .close ();
875+ }
864876 }
865877
866878 /**
@@ -917,49 +929,49 @@ public void toList() {
917929 "]" ;
918930
919931 JSONArray jsonArray = new JSONArray (jsonArrayStr );
920- List list = jsonArray .toList ();
932+ List <?> list = jsonArray .toList ();
921933
922934 assertTrue ("List should not be null" , list != null );
923935 assertTrue ("List should have 3 elements" , list .size () == 3 );
924936
925- List val1List = (List ) list .get (0 );
937+ List <?> val1List = (List <?> ) list .get (0 );
926938 assertTrue ("val1 should not be null" , val1List != null );
927939 assertTrue ("val1 should have 3 elements" , val1List .size () == 3 );
928940
929941 assertTrue ("val1 value 1 should be 1" , val1List .get (0 ).equals (Integer .valueOf (1 )));
930942 assertTrue ("val1 value 2 should be 2" , val1List .get (1 ).equals (Integer .valueOf (2 )));
931943
932- Map key1Value3Map = (Map )val1List .get (2 );
944+ Map <?,?> key1Value3Map = (Map <?,?> )val1List .get (2 );
933945 assertTrue ("Map should not be null" , key1Value3Map != null );
934946 assertTrue ("Map should have 1 element" , key1Value3Map .size () == 1 );
935947 assertTrue ("Map key3 should be true" , key1Value3Map .get ("key3" ).equals (Boolean .TRUE ));
936948
937- Map val2Map = (Map ) list .get (1 );
949+ Map <?,?> val2Map = (Map <?,?> ) list .get (1 );
938950 assertTrue ("val2 should not be null" , val2Map != null );
939951 assertTrue ("val2 should have 4 elements" , val2Map .size () == 4 );
940952 assertTrue ("val2 map key 1 should be val1" , val2Map .get ("key1" ).equals ("val1" ));
941953 assertTrue ("val2 map key 3 should be 42" , val2Map .get ("key3" ).equals (Integer .valueOf (42 )));
942954
943- Map val2Key2Map = (Map )val2Map .get ("key2" );
955+ Map <?,?> val2Key2Map = (Map <?,?> )val2Map .get ("key2" );
944956 assertTrue ("val2 map key 2 should not be null" , val2Key2Map != null );
945957 assertTrue ("val2 map key 2 should have an entry" , val2Key2Map .containsKey ("key2" ));
946958 assertTrue ("val2 map key 2 value should be null" , val2Key2Map .get ("key2" ) == null );
947959
948- List val2Key4List = (List )val2Map .get ("key4" );
960+ List <?> val2Key4List = (List <?> )val2Map .get ("key4" );
949961 assertTrue ("val2 map key 4 should not be null" , val2Key4List != null );
950962 assertTrue ("val2 map key 4 should be empty" , val2Key4List .isEmpty ());
951963
952- List val3List = (List ) list .get (2 );
964+ List <?> val3List = (List <?> ) list .get (2 );
953965 assertTrue ("val3 should not be null" , val3List != null );
954966 assertTrue ("val3 should have 2 elements" , val3List .size () == 2 );
955967
956- List val3Val1List = (List )val3List .get (0 );
968+ List <?> val3Val1List = (List <?> )val3List .get (0 );
957969 assertTrue ("val3 list val 1 should not be null" , val3Val1List != null );
958970 assertTrue ("val3 list val 1 should have 2 elements" , val3Val1List .size () == 2 );
959971 assertTrue ("val3 list val 1 list element 1 should be value1" , val3Val1List .get (0 ).equals ("value1" ));
960972 assertTrue ("val3 list val 1 list element 2 should be 2.1" , val3Val1List .get (1 ).equals (Double .valueOf ("2.1" )));
961973
962- List val3Val2List = (List )val3List .get (1 );
974+ List <?> val3Val2List = (List <?> )val3List .get (1 );
963975 assertTrue ("val3 list val 2 should not be null" , val3Val2List != null );
964976 assertTrue ("val3 list val 2 should have 1 element" , val3Val2List .size () == 1 );
965977 assertTrue ("val3 list val 2 list element 1 should be null" , val3Val2List .get (0 ) == null );
0 commit comments