Skip to content

Commit c63e78b

Browse files
committed
initial commit
1 parent 6ddaa13 commit c63e78b

File tree

8 files changed

+179
-56
lines changed

8 files changed

+179
-56
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@
118118
<artifactId>maven-compiler-plugin</artifactId>
119119
<version>2.3.2</version>
120120
<configuration>
121-
<source>1.7</source>
122-
<target>1.7</target>
121+
<source>1.6</source>
122+
<target>1.6</target>
123123
</configuration>
124124
</plugin>
125125
<plugin>

src/test/java/org/json/junit/JSONArrayTest.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -940,18 +940,21 @@ public void write() throws IOException {
940940
String str = "[\"value1\",\"value2\",{\"key1\":1,\"key2\":2,\"key3\":3}]";
941941
JSONArray jsonArray = new JSONArray(str);
942942
String expectedStr = str;
943-
try (StringWriter stringWriter = new StringWriter();) {
944-
jsonArray.write(stringWriter);
945-
String actualStr = stringWriter.toString();
943+
StringWriter stringWriter = new StringWriter();
944+
jsonArray.write(stringWriter);
945+
String actualStr = stringWriter.toString();
946+
try {
946947
JSONArray finalArray = new JSONArray(actualStr);
947948
Util.compareActualVsExpectedJsonArrays(jsonArray, finalArray);
948949
assertTrue("write() expected " + expectedStr +
949-
" but found " + actualStr,
950-
actualStr.startsWith("[\"value1\",\"value2\",{")
951-
&& actualStr.contains("\"key1\":1")
952-
&& actualStr.contains("\"key2\":2")
953-
&& actualStr.contains("\"key3\":3")
954-
);
950+
" but found " + actualStr,
951+
actualStr.startsWith("[\"value1\",\"value2\",{")
952+
&& actualStr.contains("\"key1\":1")
953+
&& actualStr.contains("\"key2\":2")
954+
&& actualStr.contains("\"key3\":3")
955+
);
956+
} finally {
957+
stringWriter.close();
955958
}
956959
}
957960

@@ -981,7 +984,8 @@ public void write3Param() throws IOException {
981984
String str0 = "[\"value1\",\"value2\",{\"key1\":1,\"key2\":false,\"key3\":3.14}]";
982985
JSONArray jsonArray = new JSONArray(str0);
983986
String expectedStr = str0;
984-
try (StringWriter stringWriter = new StringWriter();) {
987+
StringWriter stringWriter = new StringWriter();
988+
try {
985989
String actualStr = jsonArray.write(stringWriter, 0, 0).toString();
986990
JSONArray finalArray = new JSONArray(actualStr);
987991
Util.compareActualVsExpectedJsonArrays(jsonArray, finalArray);
@@ -992,9 +996,12 @@ public void write3Param() throws IOException {
992996
&& actualStr.contains("\"key2\":false")
993997
&& actualStr.contains("\"key3\":3.14")
994998
);
999+
} finally {
1000+
stringWriter.close();
9951001
}
9961002

997-
try (StringWriter stringWriter = new StringWriter();) {
1003+
stringWriter = new StringWriter();
1004+
try {
9981005
String actualStr = jsonArray.write(stringWriter, 2, 1).toString();
9991006
JSONArray finalArray = new JSONArray(actualStr);
10001007
Util.compareActualVsExpectedJsonArrays(jsonArray, finalArray);
@@ -1008,6 +1015,8 @@ public void write3Param() throws IOException {
10081015
&& actualStr.contains("\"key2\": false")
10091016
&& actualStr.contains("\"key3\": 3.14")
10101017
);
1018+
} finally {
1019+
stringWriter.close();
10111020
}
10121021
}
10131022

src/test/java/org/json/junit/JSONObjectTest.java

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1879,7 +1879,7 @@ public void jsonObjectToStringIndent() {
18791879
@Test
18801880
public void jsonObjectToStringSuppressWarningOnCastToMap() {
18811881
JSONObject jsonObject = new JSONObject();
1882-
Map<String, String> map = new HashMap<>();
1882+
Map<String, String> map = new HashMap();
18831883
map.put("abc", "def");
18841884
jsonObject.put("key", map);
18851885

@@ -2632,12 +2632,15 @@ public void write() throws IOException {
26322632
String str = "{\"key1\":\"value1\",\"key2\":[1,2,3]}";
26332633
String expectedStr = str;
26342634
JSONObject jsonObject = new JSONObject(str);
2635-
try (StringWriter stringWriter = new StringWriter()) {
2635+
StringWriter stringWriter = new StringWriter();
2636+
try {
26362637
String actualStr = jsonObject.write(stringWriter).toString();
26372638
// key order may change. verify length and individual key content
26382639
assertEquals("length", expectedStr.length(), actualStr.length());
26392640
assertTrue("key1", actualStr.contains("\"key1\":\"value1\""));
26402641
assertTrue("key2", actualStr.contains("\"key2\":[1,2,3]"));
2642+
} finally {
2643+
stringWriter.close();
26412644
}
26422645
}
26432646

@@ -2651,29 +2654,40 @@ public void testJSONWriterException() {
26512654
jsonObject.put("someKey",new BrokenToString());
26522655

26532656
// test single element JSONObject
2654-
try(StringWriter writer = new StringWriter();) {
2657+
StringWriter writer = new StringWriter();
2658+
try {
26552659
jsonObject.write(writer).toString();
26562660
fail("Expected an exception, got a String value");
26572661
} catch (JSONException e) {
26582662
assertEquals("Unable to write JSONObject value for key: someKey", e.getMessage());
26592663
} catch(Exception e) {
26602664
fail("Expected JSONException");
2665+
} finally {
2666+
try {
2667+
writer.close();
2668+
} catch (Exception e) {}
26612669
}
26622670

26632671
//test multiElement
26642672
jsonObject.put("somethingElse", "a value");
26652673

2666-
try (StringWriter writer = new StringWriter()) {
2674+
writer = new StringWriter();
2675+
try {
26672676
jsonObject.write(writer).toString();
26682677
fail("Expected an exception, got a String value");
26692678
} catch (JSONException e) {
26702679
assertEquals("Unable to write JSONObject value for key: someKey", e.getMessage());
26712680
} catch(Exception e) {
26722681
fail("Expected JSONException");
2682+
} finally {
2683+
try {
2684+
writer.close();
2685+
} catch (Exception e) {}
26732686
}
26742687

26752688
// test a more complex object
2676-
try (StringWriter writer = new StringWriter()) {
2689+
writer = new StringWriter();
2690+
try {
26772691
new JSONObject()
26782692
.put("somethingElse", "a value")
26792693
.put("someKey", new JSONArray()
@@ -2684,10 +2698,15 @@ public void testJSONWriterException() {
26842698
assertEquals("Unable to write JSONObject value for key: someKey", e.getMessage());
26852699
} catch(Exception e) {
26862700
fail("Expected JSONException");
2701+
} finally {
2702+
try {
2703+
writer.close();
2704+
} catch (Exception e) {}
26872705
}
26882706

26892707
// test a more slightly complex object
2690-
try (StringWriter writer = new StringWriter()) {
2708+
writer = new StringWriter();
2709+
try {
26912710
new JSONObject()
26922711
.put("somethingElse", "a value")
26932712
.put("someKey", new JSONArray()
@@ -2700,6 +2719,10 @@ public void testJSONWriterException() {
27002719
assertEquals("Unable to write JSONObject value for key: someKey", e.getMessage());
27012720
} catch(Exception e) {
27022721
fail("Expected JSONException");
2722+
} finally {
2723+
try {
2724+
writer.close();
2725+
} catch (Exception e) {}
27032726
}
27042727

27052728
}
@@ -2739,15 +2762,21 @@ public void write3Param() throws IOException {
27392762
" ]\n" +
27402763
" }";
27412764
JSONObject jsonObject = new JSONObject(str0);
2742-
try (StringWriter stringWriter = new StringWriter();) {
2765+
StringWriter stringWriter = new StringWriter();
2766+
try {
27432767
String actualStr = jsonObject.write(stringWriter,0,0).toString();
27442768

27452769
assertEquals("length", str0.length(), actualStr.length());
27462770
assertTrue("key1", actualStr.contains("\"key1\":\"value1\""));
27472771
assertTrue("key2", actualStr.contains("\"key2\":[1,false,3.14]"));
2772+
} finally {
2773+
try {
2774+
stringWriter.close();
2775+
} catch (Exception e) {}
27482776
}
27492777

2750-
try (StringWriter stringWriter = new StringWriter();) {
2778+
stringWriter = new StringWriter();
2779+
try {
27512780
String actualStr = jsonObject.write(stringWriter,2,1).toString();
27522781

27532782
assertEquals("length", str2.length(), actualStr.length());
@@ -2758,6 +2787,10 @@ public void write3Param() throws IOException {
27582787
" 3.14\n" +
27592788
" ]")
27602789
);
2790+
} finally {
2791+
try {
2792+
stringWriter.close();
2793+
} catch (Exception e) {}
27612794
}
27622795
}
27632796

@@ -3039,7 +3072,7 @@ public void testSingletonEnumBean() {
30393072
@SuppressWarnings("boxing")
30403073
@Test
30413074
public void testGenericBean() {
3042-
GenericBean<Integer> bean = new GenericBean<>(42);
3075+
GenericBean<Integer> bean = new GenericBean(42);
30433076
final JSONObject jo = new JSONObject(bean);
30443077
assertEquals(jo.keySet().toString(), 8, jo.length());
30453078
assertEquals(42, jo.get("genericValue"));

0 commit comments

Comments
 (0)