From c63e78bbc7b518ee51eb6c45a492551935d92d37 Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 18 Jul 2020 17:14:39 -0500 Subject: [PATCH] initial commit --- pom.xml | 4 +- .../java/org/json/junit/JSONArrayTest.java | 31 +++++--- .../java/org/json/junit/JSONObjectTest.java | 51 +++++++++--- .../java/org/json/junit/JSONStringTest.java | 79 +++++++++++++++---- .../java/org/json/junit/JSONTokenerTest.java | 27 +++++-- .../org/json/junit/XMLConfigurationTest.java | 24 ++++-- src/test/java/org/json/junit/XMLTest.java | 13 ++- .../java/org/json/junit/data/WeirdList.java | 6 +- 8 files changed, 179 insertions(+), 56 deletions(-) diff --git a/pom.xml b/pom.xml index 1dde059d7..e70c487bd 100644 --- a/pom.xml +++ b/pom.xml @@ -118,8 +118,8 @@ maven-compiler-plugin 2.3.2 - 1.7 - 1.7 + 1.6 + 1.6 diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index eda3c06bd..cfda344d1 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -940,18 +940,21 @@ public void write() throws IOException { String str = "[\"value1\",\"value2\",{\"key1\":1,\"key2\":2,\"key3\":3}]"; JSONArray jsonArray = new JSONArray(str); String expectedStr = str; - try (StringWriter stringWriter = new StringWriter();) { - jsonArray.write(stringWriter); - String actualStr = stringWriter.toString(); + StringWriter stringWriter = new StringWriter(); + jsonArray.write(stringWriter); + String actualStr = stringWriter.toString(); + try { JSONArray finalArray = new JSONArray(actualStr); Util.compareActualVsExpectedJsonArrays(jsonArray, finalArray); assertTrue("write() expected " + expectedStr + - " but found " + actualStr, - actualStr.startsWith("[\"value1\",\"value2\",{") - && actualStr.contains("\"key1\":1") - && actualStr.contains("\"key2\":2") - && actualStr.contains("\"key3\":3") - ); + " but found " + actualStr, + actualStr.startsWith("[\"value1\",\"value2\",{") + && actualStr.contains("\"key1\":1") + && actualStr.contains("\"key2\":2") + && actualStr.contains("\"key3\":3") + ); + } finally { + stringWriter.close(); } } @@ -981,7 +984,8 @@ public void write3Param() throws IOException { String str0 = "[\"value1\",\"value2\",{\"key1\":1,\"key2\":false,\"key3\":3.14}]"; JSONArray jsonArray = new JSONArray(str0); String expectedStr = str0; - try (StringWriter stringWriter = new StringWriter();) { + StringWriter stringWriter = new StringWriter(); + try { String actualStr = jsonArray.write(stringWriter, 0, 0).toString(); JSONArray finalArray = new JSONArray(actualStr); Util.compareActualVsExpectedJsonArrays(jsonArray, finalArray); @@ -992,9 +996,12 @@ public void write3Param() throws IOException { && actualStr.contains("\"key2\":false") && actualStr.contains("\"key3\":3.14") ); + } finally { + stringWriter.close(); } - try (StringWriter stringWriter = new StringWriter();) { + stringWriter = new StringWriter(); + try { String actualStr = jsonArray.write(stringWriter, 2, 1).toString(); JSONArray finalArray = new JSONArray(actualStr); Util.compareActualVsExpectedJsonArrays(jsonArray, finalArray); @@ -1008,6 +1015,8 @@ public void write3Param() throws IOException { && actualStr.contains("\"key2\": false") && actualStr.contains("\"key3\": 3.14") ); + } finally { + stringWriter.close(); } } diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 56e110edd..2b4321261 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -1879,7 +1879,7 @@ public void jsonObjectToStringIndent() { @Test public void jsonObjectToStringSuppressWarningOnCastToMap() { JSONObject jsonObject = new JSONObject(); - Map map = new HashMap<>(); + Map map = new HashMap(); map.put("abc", "def"); jsonObject.put("key", map); @@ -2632,12 +2632,15 @@ public void write() throws IOException { String str = "{\"key1\":\"value1\",\"key2\":[1,2,3]}"; String expectedStr = str; JSONObject jsonObject = new JSONObject(str); - try (StringWriter stringWriter = new StringWriter()) { + StringWriter stringWriter = new StringWriter(); + try { String actualStr = jsonObject.write(stringWriter).toString(); // key order may change. verify length and individual key content assertEquals("length", expectedStr.length(), actualStr.length()); assertTrue("key1", actualStr.contains("\"key1\":\"value1\"")); assertTrue("key2", actualStr.contains("\"key2\":[1,2,3]")); + } finally { + stringWriter.close(); } } @@ -2651,29 +2654,40 @@ public void testJSONWriterException() { jsonObject.put("someKey",new BrokenToString()); // test single element JSONObject - try(StringWriter writer = new StringWriter();) { + StringWriter writer = new StringWriter(); + try { jsonObject.write(writer).toString(); fail("Expected an exception, got a String value"); } catch (JSONException e) { assertEquals("Unable to write JSONObject value for key: someKey", e.getMessage()); } catch(Exception e) { fail("Expected JSONException"); + } finally { + try { + writer.close(); + } catch (Exception e) {} } //test multiElement jsonObject.put("somethingElse", "a value"); - try (StringWriter writer = new StringWriter()) { + writer = new StringWriter(); + try { jsonObject.write(writer).toString(); fail("Expected an exception, got a String value"); } catch (JSONException e) { assertEquals("Unable to write JSONObject value for key: someKey", e.getMessage()); } catch(Exception e) { fail("Expected JSONException"); + } finally { + try { + writer.close(); + } catch (Exception e) {} } // test a more complex object - try (StringWriter writer = new StringWriter()) { + writer = new StringWriter(); + try { new JSONObject() .put("somethingElse", "a value") .put("someKey", new JSONArray() @@ -2684,10 +2698,15 @@ public void testJSONWriterException() { assertEquals("Unable to write JSONObject value for key: someKey", e.getMessage()); } catch(Exception e) { fail("Expected JSONException"); + } finally { + try { + writer.close(); + } catch (Exception e) {} } // test a more slightly complex object - try (StringWriter writer = new StringWriter()) { + writer = new StringWriter(); + try { new JSONObject() .put("somethingElse", "a value") .put("someKey", new JSONArray() @@ -2700,6 +2719,10 @@ public void testJSONWriterException() { assertEquals("Unable to write JSONObject value for key: someKey", e.getMessage()); } catch(Exception e) { fail("Expected JSONException"); + } finally { + try { + writer.close(); + } catch (Exception e) {} } } @@ -2739,15 +2762,21 @@ public void write3Param() throws IOException { " ]\n" + " }"; JSONObject jsonObject = new JSONObject(str0); - try (StringWriter stringWriter = new StringWriter();) { + StringWriter stringWriter = new StringWriter(); + try { String actualStr = jsonObject.write(stringWriter,0,0).toString(); assertEquals("length", str0.length(), actualStr.length()); assertTrue("key1", actualStr.contains("\"key1\":\"value1\"")); assertTrue("key2", actualStr.contains("\"key2\":[1,false,3.14]")); + } finally { + try { + stringWriter.close(); + } catch (Exception e) {} } - try (StringWriter stringWriter = new StringWriter();) { + stringWriter = new StringWriter(); + try { String actualStr = jsonObject.write(stringWriter,2,1).toString(); assertEquals("length", str2.length(), actualStr.length()); @@ -2758,6 +2787,10 @@ public void write3Param() throws IOException { " 3.14\n" + " ]") ); + } finally { + try { + stringWriter.close(); + } catch (Exception e) {} } } @@ -3039,7 +3072,7 @@ public void testSingletonEnumBean() { @SuppressWarnings("boxing") @Test public void testGenericBean() { - GenericBean bean = new GenericBean<>(42); + GenericBean bean = new GenericBean(42); final JSONObject jo = new JSONObject(bean); assertEquals(jo.keySet().toString(), 8, jo.length()); assertEquals(42, jo.get("genericValue")); diff --git a/src/test/java/org/json/junit/JSONStringTest.java b/src/test/java/org/json/junit/JSONStringTest.java index 788d8ebb3..a19961103 100644 --- a/src/test/java/org/json/junit/JSONStringTest.java +++ b/src/test/java/org/json/junit/JSONStringTest.java @@ -49,84 +49,114 @@ public void writeValues() throws Exception { JSONArray jsonArray = new JSONArray(); jsonArray.put((Object)null); - try (StringWriter writer = new StringWriter();) { + StringWriter writer = new StringWriter(); + try { String output = jsonArray.write(writer).toString(); assertTrue("String values should be equal", "[null]".equals(output)); jsonArray = new JSONArray(); jsonArray.put(JSONObject.NULL); + } finally { + writer.close(); } - try (StringWriter writer = new StringWriter();) { + writer = new StringWriter(); + try { String output = jsonArray.write(writer).toString(); assertTrue("String values should be equal", "[null]".equals(output)); jsonArray = new JSONArray(); jsonArray.put(new JSONObject()); + } finally { + writer.close(); } - try (StringWriter writer = new StringWriter();) { + writer = new StringWriter(); + try { String output = jsonArray.write(writer).toString(); assertTrue("String values should be equal", "[{}]".equals(output)); jsonArray = new JSONArray(); jsonArray.put(new JSONArray()); + } finally { + writer.close(); } - try (StringWriter writer = new StringWriter();) { + writer = new StringWriter(); + try { String output = jsonArray.write(writer).toString(); assertTrue("String values should be equal", "[[]]".equals(output)); jsonArray = new JSONArray(); Map singleMap = Collections.singletonMap("key1", "value1"); jsonArray.put((Object)singleMap); + } finally { + writer.close(); } - try (StringWriter writer = new StringWriter();) { + writer = new StringWriter(); + try { String output = jsonArray.write(writer).toString(); assertTrue("String values should be equal", "[{\"key1\":\"value1\"}]".equals(output)); jsonArray = new JSONArray(); List singleList = Collections.singletonList("entry1"); jsonArray.put((Object)singleList); + } finally { + writer.close(); } - try (StringWriter writer = new StringWriter();) { + writer = new StringWriter(); + try { String output = jsonArray.write(writer).toString(); assertTrue("String values should be equal", "[[\"entry1\"]]".equals(output)); jsonArray = new JSONArray(); int[] intArray = new int[] { 1, 2, 3 }; jsonArray.put(intArray); + } finally { + writer.close(); } - try (StringWriter writer = new StringWriter();) { + writer = new StringWriter(); + try { String output = jsonArray.write(writer).toString(); assertTrue("String values should be equal", "[[1,2,3]]".equals(output)); jsonArray = new JSONArray(); jsonArray.put(24); + } finally { + writer.close(); } - try (StringWriter writer = new StringWriter();) { + writer = new StringWriter(); + try { String output = jsonArray.write(writer).toString(); assertTrue("String values should be equal", "[24]".equals(output)); jsonArray = new JSONArray(); jsonArray.put("string value"); + } finally { + writer.close(); } - try (StringWriter writer = new StringWriter();) { + writer = new StringWriter(); + try { String output = jsonArray.write(writer).toString(); assertTrue("String values should be equal", "[\"string value\"]".equals(output)); jsonArray = new JSONArray(); jsonArray.put(true); + } finally { + writer.close(); } - try (StringWriter writer = new StringWriter();) { + writer = new StringWriter(); + try { String output = jsonArray.write(writer).toString(); assertTrue("String values should be equal", "[true]".equals(output)); + } finally { + writer.close(); } } @@ -185,13 +215,15 @@ public void testJSONStringValue() throws Exception { jsonArray.put(jsonString); - - try (StringWriter writer = new StringWriter();) { + StringWriter writer = new StringWriter(); + try { String output = jsonArray.write(writer).toString(); assertTrue("String values should be equal", "[\"the JSON string value\"]".equals(output)); output = JSONObject.valueToString(jsonString); assertTrue("String values should be equal", "\"the JSON string value\"".equals(output)); + } finally { + writer.close(); } } @@ -206,7 +238,8 @@ public void testJSONNullStringValue() throws Exception { jsonArray.put(jsonString); - try (StringWriter writer = new StringWriter();) { + StringWriter writer = new StringWriter(); + try { String output = jsonArray.write(writer).toString(); assertTrue("String values should be equal", "[\"the toString value\"]".equals(output)); @@ -219,6 +252,8 @@ public void testJSONNullStringValue() throws Exception { assertTrue("Expected JSONException", e instanceof JSONException); assertTrue("Exception message does not match", "Bad value from toJSONString: null".equals(e.getMessage())); } + } finally { + writer.close(); } } @@ -234,13 +269,18 @@ public void testJSONStringExceptionValue() { jsonArray.put(jsonString); - try (StringWriter writer = new StringWriter();) { + StringWriter writer = new StringWriter(); + try { jsonArray.write(writer).toString(); fail("Expected an exception, got a String value"); } catch (JSONException e) { assertEquals("Unable to write JSONArray value at index: 0", e.getMessage()); } catch(Exception e) { fail("Expected JSONException"); + } finally { + try { + writer.close(); + } catch (Exception e){} } try { @@ -264,12 +304,15 @@ public void testStringValue() throws Exception { jsonArray.put(nonJsonString); - try (StringWriter writer = new StringWriter();) { + StringWriter writer = new StringWriter(); + try { String output = jsonArray.write(writer).toString(); assertTrue("String values should be equal", "[\"the toString value for StringValue\"]".equals(output)); output = JSONObject.valueToString(nonJsonString); assertTrue("String values should be equal", "\"the toString value for StringValue\"".equals(output)); + } finally { + writer.close(); } } @@ -284,13 +327,15 @@ public void testNullStringValue() throws Exception { jsonArray.put(nonJsonString); - - try (StringWriter writer = new StringWriter();) { + StringWriter writer = new StringWriter(); + try { String output = jsonArray.write(writer).toString(); assertTrue("String values should be equal", "[\"\"]".equals(output)); output = JSONObject.valueToString(nonJsonString); assertTrue("String values should be equal", "\"\"".equals(output)); + } finally { + writer.close(); } } diff --git a/src/test/java/org/json/junit/JSONTokenerTest.java b/src/test/java/org/json/junit/JSONTokenerTest.java index 86a614c55..e8e0f98a9 100644 --- a/src/test/java/org/json/junit/JSONTokenerTest.java +++ b/src/test/java/org/json/junit/JSONTokenerTest.java @@ -55,7 +55,8 @@ public class JSONTokenerTest { */ @Test public void verifyBackFailureZeroIndex() throws IOException { - try(Reader reader = new StringReader("some test string")) { + Reader reader = new StringReader("some test string"); + try { final JSONTokener tokener = new JSONTokener(reader); try { // this should fail since the index is 0; @@ -67,6 +68,8 @@ public void verifyBackFailureZeroIndex() throws IOException { fail("Unknown Exception type " + e.getClass().getCanonicalName()+" with message "+e.getMessage()); } + } finally { + reader.close(); } } /** @@ -75,7 +78,8 @@ public void verifyBackFailureZeroIndex() throws IOException { */ @Test public void verifyBackFailureDoubleBack() throws IOException { - try(Reader reader = new StringReader("some test string")) { + Reader reader = new StringReader("some test string"); + try { final JSONTokener tokener = new JSONTokener(reader); tokener.next(); tokener.back(); @@ -88,6 +92,8 @@ public void verifyBackFailureDoubleBack() throws IOException { } catch (Exception e) { fail("Unknown Exception type " + e.getClass().getCanonicalName()+" with message "+e.getMessage()); } + } finally { + reader.close(); } } @@ -164,7 +170,8 @@ private void checkError(String testStr) { * @throws Exception */ private Object nextValue(String testStr) throws JSONException { - try(StringReader sr = new StringReader(testStr);){ + StringReader sr = new StringReader(testStr); + try { JSONTokener tokener = new JSONTokener(sr); Object result = tokener.nextValue(); @@ -179,6 +186,8 @@ private Object nextValue(String testStr) throws JSONException { } return result; + } finally { + sr.close(); } } @@ -196,7 +205,10 @@ public void testSkipToFailureWithBufferedReader() throws IOException { for(int i=0;i list = new ArrayList<>(); + private final List list = new ArrayList(); /** * @param vals @@ -25,14 +25,14 @@ public WeirdList(Integer... vals) { * @return a copy of the list */ public List get() { - return new ArrayList<>(this.list); + return new ArrayList(this.list); } /** * @return a copy of the list */ public List getALL() { - return new ArrayList<>(this.list); + return new ArrayList(this.list); } /**