From fe45fa9cfbaf1a4b4df223ff3357a0d73d3a2932 Mon Sep 17 00:00:00 2001 From: Allon Mureinik Date: Thu, 5 Oct 2023 15:29:51 +0300 Subject: [PATCH 1/4] Fix XMLTest on Windows XMLTest.testIndentComplicatedJsonObjectWithArrayAndWithConfig fails when run on Windows due to mismatching linebreaks (that aren't important for the test's functionality) between the actual and expected strings. For the actual strings, linebreaks are canonized to the platform's native linebreak using `replaceAll("\\n|\\r\\n", System.getProperty("line.separator")`. However, the expected result is read from a file, and is left with the linebreaks that were originally used to create it. The solution is to perform the same canonization on both strings. Closes #781 --- src/test/java/org/json/junit/XMLTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index e940032..6e7b1a9 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -1239,7 +1239,8 @@ public class XMLTest { for (int numRead; (numRead = in.read(buffer, 0, buffer.length)) > 0; ) { expected.append(buffer, 0, numRead); } - assertEquals(expected.toString(), actualString.replaceAll("\\n|\\r\\n", System.getProperty("line.separator"))); + assertEquals(expected.toString().replaceAll("\\n|\\r\\n", System.getProperty("line.separator")), + actualString.replaceAll("\\n|\\r\\n", System.getProperty("line.separator"))); } finally { if (xmlStream != null) { xmlStream.close(); From 4c8cac22a89069209439809243ac4eddf6b0dd47 Mon Sep 17 00:00:00 2001 From: Allon Mureinik Date: Thu, 5 Oct 2023 19:47:33 +0300 Subject: [PATCH 2/4] Use System.lineSeparator() Use the built-in System.lineSeparator() instead of implementing it ourselves with System.getProperty("line.separator") in order to clean up the code and make it easier to maintain. --- src/test/java/org/json/junit/XMLTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index 6e7b1a9..712b8ee 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -1239,8 +1239,8 @@ public class XMLTest { for (int numRead; (numRead = in.read(buffer, 0, buffer.length)) > 0; ) { expected.append(buffer, 0, numRead); } - assertEquals(expected.toString().replaceAll("\\n|\\r\\n", System.getProperty("line.separator")), - actualString.replaceAll("\\n|\\r\\n", System.getProperty("line.separator"))); + assertEquals(expected.toString().replaceAll("\\n|\\r\\n", System.lineSeparator()), + actualString.replaceAll("\\n|\\r\\n", System.lineSeparator())); } finally { if (xmlStream != null) { xmlStream.close(); From 0e4a94d91db64ae9eafed0ff7db5fff87770f0a4 Mon Sep 17 00:00:00 2001 From: Madjosz <28844868+Madjosz@users.noreply.github.com> Date: Wed, 4 Oct 2023 11:17:13 +0200 Subject: [PATCH 3/4] fix failing test XML test on Windows machines --- src/test/java/org/json/junit/XMLTest.java | 36 +++++++---------------- 1 file changed, 11 insertions(+), 25 deletions(-) diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index e940032..22d6131 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -1223,32 +1223,18 @@ public class XMLTest { @Test public void testIndentComplicatedJsonObjectWithArrayAndWithConfig(){ - try { - InputStream jsonStream = null; - try { - jsonStream = XMLTest.class.getClassLoader().getResourceAsStream("Issue593.json"); - final JSONObject object = new JSONObject(new JSONTokener(jsonStream)); - String actualString = XML.toString(object, null, XMLParserConfiguration.KEEP_STRINGS,2); - InputStream xmlStream = null; - try { - xmlStream = XMLTest.class.getClassLoader().getResourceAsStream("Issue593.xml"); - int bufferSize = 1024; - char[] buffer = new char[bufferSize]; - StringBuilder expected = new StringBuilder(); - Reader in = new InputStreamReader(xmlStream, "UTF-8"); - for (int numRead; (numRead = in.read(buffer, 0, buffer.length)) > 0; ) { - expected.append(buffer, 0, numRead); - } - assertEquals(expected.toString(), actualString.replaceAll("\\n|\\r\\n", System.getProperty("line.separator"))); - } finally { - if (xmlStream != null) { - xmlStream.close(); - } - } - } finally { - if (jsonStream != null) { - jsonStream.close(); + try (InputStream jsonStream = XMLTest.class.getClassLoader().getResourceAsStream("Issue593.json")) { + final JSONObject object = new JSONObject(new JSONTokener(jsonStream)); + String actualString = XML.toString(object, null, XMLParserConfiguration.KEEP_STRINGS, 2); + try (InputStream xmlStream = XMLTest.class.getClassLoader().getResourceAsStream("Issue593.xml")) { + int bufferSize = 1024; + char[] buffer = new char[bufferSize]; + StringBuilder expected = new StringBuilder(); + Reader in = new InputStreamReader(xmlStream, "UTF-8"); + for (int numRead; (numRead = in.read(buffer, 0, buffer.length)) > 0; ) { + expected.append(buffer, 0, numRead); } + assertEquals(expected.toString(), actualString); } } catch (IOException e) { fail("file writer error: " +e.getMessage()); From c93014cb5379bb93f2155e48ee0a4382b4d05ae1 Mon Sep 17 00:00:00 2001 From: Madjosz <28844868+Madjosz@users.noreply.github.com> Date: Wed, 4 Oct 2023 12:00:47 +0200 Subject: [PATCH 4/4] add validity check for JSONObject constructors * fixes #713 * document JSONException in JavaDoc * remove unused Comparable boundary to reuse GenericBean in test --- src/main/java/org/json/JSONObject.java | 6 +++++ .../java/org/json/junit/JSONObjectTest.java | 26 +++++++++++++++++-- .../java/org/json/junit/data/GenericBean.java | 2 +- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index acef67d..08ccdab 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -283,6 +283,7 @@ public class JSONObject { } final Object value = e.getValue(); if (value != null) { + testValidity(value); this.map.put(String.valueOf(e.getKey()), wrap(value)); } } @@ -346,6 +347,8 @@ public class JSONObject { * @param bean * An object that has getter methods that should be used to make * a JSONObject. + * @throws JSONException + * If a getter returned a non-finite number. */ public JSONObject(Object bean) { this(); @@ -1691,6 +1694,8 @@ public class JSONObject { * * @param bean * the bean + * @throws JSONException + * If a getter returned a non-finite number. */ private void populateMap(Object bean) { populateMap(bean, Collections.newSetFromMap(new IdentityHashMap())); @@ -1726,6 +1731,7 @@ public class JSONObject { objectsRecord.add(result); + testValidity(result); this.map.put(key, wrap(result, objectsRecord)); objectsRecord.remove(result); diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 01889d5..0503dbb 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -9,6 +9,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; @@ -1972,7 +1973,7 @@ public class JSONObjectTest { @Test public void jsonObjectToStringSuppressWarningOnCastToMap() { JSONObject jsonObject = new JSONObject(); - Map map = new HashMap(); + Map map = new HashMap<>(); map.put("abc", "def"); jsonObject.put("key", map); @@ -3283,7 +3284,7 @@ public class JSONObjectTest { @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")); @@ -3627,4 +3628,25 @@ public class JSONObjectTest { .put("b", 2); assertFalse(jo1.similar(jo3)); } + + private static final Number[] NON_FINITE_NUMBERS = { Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NaN, + Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY, Float.NaN }; + + @Test + public void issue713MapConstructorWithNonFiniteNumbers() { + for (Number nonFinite : NON_FINITE_NUMBERS) { + Map map = new HashMap<>(); + map.put("a", nonFinite); + + assertThrows(JSONException.class, () -> new JSONObject(map)); + } + } + + @Test + public void issue713BeanConstructorWithNonFiniteNumbers() { + for (Number nonFinite : NON_FINITE_NUMBERS) { + GenericBean bean = new GenericBean<>(nonFinite); + assertThrows(JSONException.class, () -> new JSONObject(bean)); + } + } } diff --git a/src/test/java/org/json/junit/data/GenericBean.java b/src/test/java/org/json/junit/data/GenericBean.java index da6370d..dd46b88 100644 --- a/src/test/java/org/json/junit/data/GenericBean.java +++ b/src/test/java/org/json/junit/data/GenericBean.java @@ -9,7 +9,7 @@ import java.io.StringReader; * @param * generic number value */ -public class GenericBean> implements MyBean { +public class GenericBean implements MyBean { /** * @param genericValue * value to initiate with