@@ -1856,10 +1856,12 @@ public void jsonObjectToStringIndent() {
18561856 " ]\n " +
18571857 "}" ;
18581858 JSONObject jsonObject = new JSONObject (jsonObject0Str );
1859- assertEquals ("toString()" ,jsonObject0Str , jsonObject .toString ());
1860- assertEquals ("toString(0)" ,jsonObject0Str , jsonObject .toString (0 ));
1861- assertEquals ("toString(1)" ,jsonObject1Str , jsonObject .toString (1 ));
1862- assertEquals ("toString(4)" ,jsonObject4Str , jsonObject .toString (4 ));
1859+ // contents are tested in other methods, in this case just validate the spacing by
1860+ // checking length
1861+ assertEquals ("toString() length" ,jsonObject0Str .length (), jsonObject .toString ().length ());
1862+ assertEquals ("toString(0) length" ,jsonObject0Str .length (), jsonObject .toString (0 ).length ());
1863+ assertEquals ("toString(1) length" ,jsonObject1Str .length (), jsonObject .toString (1 ).length ());
1864+ assertEquals ("toString(4) length" ,jsonObject4Str .length (), jsonObject .toString (4 ).length ());
18631865
18641866 JSONObject jo = new JSONObject ().put ("TABLE" , new JSONObject ().put ("yhoo" , new JSONObject ()));
18651867 assertEquals ("toString(2)" ,"{\" TABLE\" : {\" yhoo\" : {}}}" , jo .toString (2 ));
@@ -2630,9 +2632,10 @@ public void write() throws IOException {
26302632 JSONObject jsonObject = new JSONObject (str );
26312633 try (StringWriter stringWriter = new StringWriter ()) {
26322634 String actualStr = jsonObject .write (stringWriter ).toString ();
2633- assertTrue ("write() expected " +expectedStr +
2634- " but found " +actualStr ,
2635- expectedStr .equals (actualStr ));
2635+ // key order may change. verify length and individual key content
2636+ assertEquals ("length" , expectedStr .length (), actualStr .length ());
2637+ assertTrue ("key1" , actualStr .contains ("\" key1\" :\" value1\" " ));
2638+ assertTrue ("key2" , actualStr .contains ("\" key2\" :[1,2,3]" ));
26362639 }
26372640 }
26382641
@@ -2734,16 +2737,25 @@ public void write3Param() throws IOException {
27342737 " ]\n " +
27352738 " }" ;
27362739 JSONObject jsonObject = new JSONObject (str0 );
2737- String expectedStr = str0 ;
27382740 try (StringWriter stringWriter = new StringWriter ();) {
27392741 String actualStr = jsonObject .write (stringWriter ,0 ,0 ).toString ();
2740- assertEquals (expectedStr , actualStr );
2742+
2743+ assertEquals ("length" , str0 .length (), actualStr .length ());
2744+ assertTrue ("key1" , actualStr .contains ("\" key1\" :\" value1\" " ));
2745+ assertTrue ("key2" , actualStr .contains ("\" key2\" :[1,false,3.14]" ));
27412746 }
2742-
2743- expectedStr = str2 ;
2747+
27442748 try (StringWriter stringWriter = new StringWriter ();) {
27452749 String actualStr = jsonObject .write (stringWriter ,2 ,1 ).toString ();
2746- assertEquals (expectedStr , actualStr );
2750+
2751+ assertEquals ("length" , str2 .length (), actualStr .length ());
2752+ assertTrue ("key1" , actualStr .contains (" \" key1\" : \" value1\" " ));
2753+ assertTrue ("key2" , actualStr .contains (" \" key2\" : [\n " +
2754+ " 1,\n " +
2755+ " false,\n " +
2756+ " 3.14\n " +
2757+ " ]" )
2758+ );
27472759 }
27482760 }
27492761
@@ -2978,9 +2990,9 @@ public void toMap() {
29782990 @ Test
29792991 public void testSingletonBean () {
29802992 final JSONObject jo = new JSONObject (Singleton .getInstance ());
2981- // assertEquals(jo.keySet().toString(), 1, jo.length());
2982- // assertEquals(0, jo.get("someInt"));
2983- // assertEquals(null, jo.opt("someString"));
2993+ assertEquals (jo .keySet ().toString (), 1 , jo .length ());
2994+ assertEquals (0 , jo .get ("someInt" ));
2995+ assertEquals (null , jo .opt ("someString" ));
29842996
29852997 // Update the singleton values
29862998 Singleton .getInstance ().setSomeInt (42 );
@@ -2991,8 +3003,8 @@ public void testSingletonBean() {
29913003 assertEquals ("Something" , jo2 .get ("someString" ));
29923004
29933005 // ensure our original jo hasn't changed.
2994- // assertEquals(0, jo.get("someInt"));
2995- // assertEquals(null, jo.opt("someString"));
3006+ assertEquals (0 , jo .get ("someInt" ));
3007+ assertEquals (null , jo .opt ("someString" ));
29963008 }
29973009
29983010 /**
@@ -3002,9 +3014,9 @@ public void testSingletonBean() {
30023014 @ Test
30033015 public void testSingletonEnumBean () {
30043016 final JSONObject jo = new JSONObject (SingletonEnum .getInstance ());
3005- // assertEquals(jo.keySet().toString(), 1, jo.length());
3006- // assertEquals(0, jo.get("someInt"));
3007- // assertEquals(null, jo.opt("someString"));
3017+ assertEquals (jo .keySet ().toString (), 1 , jo .length ());
3018+ assertEquals (0 , jo .get ("someInt" ));
3019+ assertEquals (null , jo .opt ("someString" ));
30083020
30093021 // Update the singleton values
30103022 SingletonEnum .getInstance ().setSomeInt (42 );
@@ -3015,8 +3027,8 @@ public void testSingletonEnumBean() {
30153027 assertEquals ("Something" , jo2 .get ("someString" ));
30163028
30173029 // ensure our original jo hasn't changed.
3018- // assertEquals(0, jo.get("someInt"));
3019- // assertEquals(null, jo.opt("someString"));
3030+ assertEquals (0 , jo .get ("someInt" ));
3031+ assertEquals (null , jo .opt ("someString" ));
30203032 }
30213033
30223034 /**
0 commit comments