From 4929fc99c1d7d1532aee092868cbb990ffd34b6d Mon Sep 17 00:00:00 2001 From: rikkarth Date: Sat, 30 Mar 2024 10:15:47 +0000 Subject: [PATCH] test(#871-strictMode): added more test cases, improved existing ones --- .../junit/JSONParserConfigurationTest.java | 107 +++++- src/test/resources/compliantJsonArray.json | 317 ++++++++++++++++++ 2 files changed, 416 insertions(+), 8 deletions(-) create mode 100644 src/test/resources/compliantJsonArray.json diff --git a/src/test/java/org/json/junit/JSONParserConfigurationTest.java b/src/test/java/org/json/junit/JSONParserConfigurationTest.java index 60cf2ce..dd23c70 100644 --- a/src/test/java/org/json/junit/JSONParserConfigurationTest.java +++ b/src/test/java/org/json/junit/JSONParserConfigurationTest.java @@ -1,7 +1,12 @@ package org.json.junit; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.Arrays; import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -13,6 +18,7 @@ import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; public class JSONParserConfigurationTest { + private static final String TEST_SOURCE = "{\"key\": \"value1\", \"key\": \"value2\"}"; @Test(expected = JSONException.class) @@ -23,28 +29,42 @@ public class JSONParserConfigurationTest { @Test public void testOverwrite() { JSONObject jsonObject = new JSONObject(TEST_SOURCE, - new JSONParserConfiguration().withOverwriteDuplicateKey(true)); + new JSONParserConfiguration().withOverwriteDuplicateKey(true)); assertEquals("duplicate key should be overwritten", "value2", jsonObject.getString("key")); } @Test public void givenInvalidInputArrays_testStrictModeTrue_shouldThrowJsonException() { - List strictModeInputTestCases = getNonCompliantJSONList(); JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration() .withStrictMode(true); + List strictModeInputTestCases = getNonCompliantJSONList(); + strictModeInputTestCases.forEach( testCase -> assertThrows("expected non-compliant array but got instead: " + testCase, JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration))); } + @Test + public void givenCompliantJSONArrayFile_testStrictModeTrue_shouldNotThrowAnyException() throws IOException { + try (Stream lines = Files.lines(Paths.get("src/test/resources/compliantJsonArray.json"))) { + String compliantJsonArrayAsString = lines.collect(Collectors.joining()); + JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration() + .withStrictMode(true); + + new JSONArray(compliantJsonArrayAsString, jsonParserConfiguration); + } + + } + @Test public void givenInvalidInputArrays_testStrictModeFalse_shouldNotThrowAnyException() { - List strictModeInputTestCases = getNonCompliantJSONList(); JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration() .withStrictMode(false); + List strictModeInputTestCases = getNonCompliantJSONList(); + strictModeInputTestCases.forEach(testCase -> new JSONArray(testCase, jsonParserConfiguration)); } @@ -54,24 +74,88 @@ public class JSONParserConfigurationTest { .withStrictMode(true); String testCase = "[1,2];[3,4]"; + JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase, JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration)); assertEquals("invalid character found after end of array: ; at 6 [character 7 line 1]", je.getMessage()); } + @Test + public void givenInvalidInputArrayWithNumericStrings_testStrictModeTrue_shouldThrowInvalidCharacterErrorMessage() { + JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration() + .withStrictMode(true); + + String testCase = "[\"1\",\"2\"];[3,4]"; + + JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase, + JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration)); + + assertEquals("invalid character found after end of array: ; at 10 [character 11 line 1]", je.getMessage()); + } + @Test public void givenInvalidInputArray_testStrictModeTrue_shouldThrowValueNotSurroundedByQuotesErrorMessage() { JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration() .withStrictMode(true); String testCase = "[{\"test\": implied}]"; + JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase, JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration)); assertEquals("Value is not surrounded by quotes: implied", je.getMessage()); } + @Test + public void givenInvalidInputArray_testStrictModeFalse_shouldNotThrowAnyException() { + JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration() + .withStrictMode(false); + + String testCase = "[{\"test\": implied}]"; + + new JSONArray(testCase, jsonParserConfiguration); + } + + @Test + public void givenUnbalancedQuotes_testStrictModeTrue_shouldThrowJsonExceptionWtihConcreteErrorDescription() { + JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration() + .withStrictMode(true); + + String testCaseOne = "[\"abc', \"test\"]"; + String testCaseTwo = "['abc\", \"test\"]"; + + JSONException jeOne = assertThrows(JSONException.class, + () -> new JSONArray(testCaseOne, jsonParserConfiguration)); + JSONException jeTwo = assertThrows(JSONException.class, + () -> new JSONArray(testCaseTwo, jsonParserConfiguration)); + + assertEquals( + "Field contains unbalanced quotes. Starts with \" but ends with single quote. at 6 [character 7 line 1]", + jeOne.getMessage()); + assertEquals( + "Field contains unbalanced quotes. Starts with ' but ends with double quote. at 6 [character 7 line 1]", + jeTwo.getMessage()); + } + + @Test + public void givenUnbalancedQuotes_testStrictModeFalse_shouldThrowJsonException() { + JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration() + .withStrictMode(false); + + String testCaseOne = "[\"abc', \"test\"]"; + String testCaseTwo = "['abc\", \"test\"]"; + + JSONException jeOne = assertThrows(JSONException.class, + () -> new JSONArray(testCaseOne, jsonParserConfiguration)); + JSONException jeTwo = assertThrows(JSONException.class, + () -> new JSONArray(testCaseTwo, jsonParserConfiguration)); + + assertEquals("Expected a ',' or ']' at 10 [character 11 line 1]", jeOne.getMessage()); + assertEquals("Unterminated string at 15 [character 16 line 1]", jeTwo.getMessage()); + } + + @Test public void givenInvalidInputArray_testStrictModeTrue_shouldThrowKeyNotSurroundedByQuotesErrorMessage() { JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration() @@ -81,14 +165,14 @@ public class JSONParserConfigurationTest { JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase, JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration)); - assertEquals("Key is not surrounded by quotes: test", je.getMessage()); + assertEquals(String.format("Value is not surrounded by quotes: %s", "test"), je.getMessage()); } @Test public void verifyDuplicateKeyThenMaxDepth() { JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration() - .withOverwriteDuplicateKey(true) - .withMaxNestingDepth(42); + .withOverwriteDuplicateKey(true) + .withMaxNestingDepth(42); assertEquals(42, jsonParserConfiguration.getMaxNestingDepth()); assertTrue(jsonParserConfiguration.isOverwriteDuplicateKey()); @@ -97,16 +181,23 @@ public class JSONParserConfigurationTest { @Test public void verifyMaxDepthThenDuplicateKey() { JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration() - .withMaxNestingDepth(42) - .withOverwriteDuplicateKey(true); + .withMaxNestingDepth(42) + .withOverwriteDuplicateKey(true); assertTrue(jsonParserConfiguration.isOverwriteDuplicateKey()); assertEquals(42, jsonParserConfiguration.getMaxNestingDepth()); } + /** + * This method contains short but focused use-case samples and is exclusively used to test strictMode unit tests in + * this class. + * + * @return List with JSON strings. + */ private List getNonCompliantJSONList() { return Arrays.asList( "[1,2];[3,4]", + "[test]", "[1, 2,3]:[4,5]", "[{test: implied}]", "[{\"test\": implied}]", diff --git a/src/test/resources/compliantJsonArray.json b/src/test/resources/compliantJsonArray.json new file mode 100644 index 0000000..6d06e77 --- /dev/null +++ b/src/test/resources/compliantJsonArray.json @@ -0,0 +1,317 @@ +[ + { + "_id": "6606c27d2ab4a0102d49420a", + "index": 0, + "guid": "441331fb-84d1-4873-a649-3814621a0370", + "isActive": true, + "balance": "$2,691.63", + "picture": "http://placehold.it/32x32", + "age": 26, + "eyeColor": "blue", + "name": "Susie Estes", + "gender": "female", + "company": "ZENTURY", + "email": "susieestes@zentury.com", + "phone": "+1 (853) 428-2292", + "address": "424 Hewes Street, Grahamtown, Tennessee, 8138", + "about": "Laborum magna tempor officia irure cillum nulla incididunt Lorem dolor veniam elit cupidatat amet. Veniam veniam exercitation nulla consectetur officia esse ex sunt nulla nisi ea cillum nisi reprehenderit. Qui aliquip reprehenderit aliqua aliquip aliquip anim sit magna nostrud dolore veniam velit elit aliquip.\r\n", + "registered": "2016-07-22T03:18:11 -01:00", + "latitude": -21.544934, + "longitude": 72.765495, + "tags": [ + "consectetur", + "minim", + "sunt", + "in", + "ut", + "velit", + "anim" + ], + "friends": [ + { + "id": 0, + "name": "Simon Garrett" + }, + { + "id": 1, + "name": "Conrad Moon" + }, + { + "id": 2, + "name": "Mccall Rosario" + } + ], + "greeting": "Hello, Susie Estes! You have 10 unread messages.", + "favoriteFruit": "banana" + }, + { + "_id": "6606c27d0a45df5121fb765f", + "index": 1, + "guid": "fd774715-de85-44b9-b498-c214d8f68d9f", + "isActive": true, + "balance": "$2,713.96", + "picture": "http://placehold.it/32x32", + "age": 27, + "eyeColor": "green", + "name": "Irma Greer", + "gender": "female", + "company": "ANIVET", + "email": "irmagreer@anivet.com", + "phone": "+1 (936) 539-2196", + "address": "285 Roebling Street, Hardyville, Hawaii, 8410", + "about": "Ea id cupidatat eiusmod culpa. Nulla consequat esse elit enim et pariatur eiusmod ipsum. Consequat eu non reprehenderit in.\r\n", + "registered": "2015-04-06T07:54:22 -01:00", + "latitude": 83.512347, + "longitude": -9.368739, + "tags": [ + "excepteur", + "non", + "nostrud", + "laboris", + "laboris", + "qui", + "aute" + ], + "friends": [ + { + "id": 0, + "name": "Terri Morrison" + }, + { + "id": 1, + "name": "Foley Turner" + }, + { + "id": 2, + "name": "Leila Brewer" + } + ], + "greeting": "Hello, Irma Greer! You have 7 unread messages.", + "favoriteFruit": "apple" + }, + { + "_id": "6606c27dfb3a0e4e7e7183d3", + "index": 2, + "guid": "688b0c36-98e0-4ee7-86b8-863638d79b5f", + "isActive": false, + "balance": "$3,514.35", + "picture": "http://placehold.it/32x32", + "age": 32, + "eyeColor": "green", + "name": "Rebecca Garner", + "gender": "female", + "company": "OCEANICA", + "email": "rebeccagarner@oceanica.com", + "phone": "+1 (868) 551-3810", + "address": "425 Navy Walk, Spokane, Vermont, 7439", + "about": "Mollit officia adipisicing ex nisi non Lorem sunt quis est. Irure exercitation duis ipsum qui ullamco eu ea commodo occaecat minim proident. Incididunt nostrud ex cupidatat eiusmod mollit anim irure culpa. Labore voluptate voluptate labore nisi sit eu. Dolor sit proident velit dolor deserunt labore sit ipsum incididunt eiusmod reprehenderit voluptate. Duis anim velit officia laboris consequat officia dolor sint dolor nisi ex.\r\n", + "registered": "2021-11-02T12:50:05 -00:00", + "latitude": -82.969939, + "longitude": 86.415645, + "tags": [ + "aliquip", + "et", + "est", + "nulla", + "nulla", + "tempor", + "adipisicing" + ], + "friends": [ + { + "id": 0, + "name": "Cox Trujillo" + }, + { + "id": 1, + "name": "Stanton Wilcox" + }, + { + "id": 2, + "name": "Dolores Cooper" + } + ], + "greeting": "Hello, Rebecca Garner! You have 1 unread messages.", + "favoriteFruit": "strawberry" + }, + { + "_id": "6606c27d204bc2327fc9ba23", + "index": 3, + "guid": "be970cba-306e-4cbd-be08-c265a43a61fa", + "isActive": true, + "balance": "$3,691.63", + "picture": "http://placehold.it/32x32", + "age": 35, + "eyeColor": "brown", + "name": "Foster Pratt", + "gender": "male", + "company": "COMTEST", + "email": "fosterpratt@comtest.com", + "phone": "+1 (829) 400-3497", + "address": "390 Montague Street, Sandston, Wyoming, 9116", + "about": "Do proident consectetur minim quis. In adipisicing culpa Lorem fugiat cillum exercitation velit velit. Non voluptate laboris deserunt veniam et sint consectetur irure aliqua quis eiusmod consectetur elit id. Ex sint do anim Lorem excepteur eu nulla.\r\n", + "registered": "2020-06-25T04:55:25 -01:00", + "latitude": 63.614955, + "longitude": -109.299405, + "tags": [ + "irure", + "esse", + "non", + "mollit", + "laborum", + "adipisicing", + "ad" + ], + "friends": [ + { + "id": 0, + "name": "Savage Combs" + }, + { + "id": 1, + "name": "Cecelia Kemp" + }, + { + "id": 2, + "name": "Priscilla Stephenson" + } + ], + "greeting": "Hello, Foster Pratt! You have 5 unread messages.", + "favoriteFruit": "apple" + }, + { + "_id": "6606c27df63eb5f390cb9989", + "index": 4, + "guid": "2c3e5115-758d-468e-99c5-c9afa26e1f9f", + "isActive": true, + "balance": "$1,047.20", + "picture": "http://placehold.it/32x32", + "age": 30, + "eyeColor": "green", + "name": "Willie Wallace", + "gender": "female", + "company": "KNEEDLES", + "email": "williewallace@kneedles.com", + "phone": "+1 (816) 510-2695", + "address": "411 Dunne Place, Rosewood, Iowa, 283", + "about": "Voluptate exercitation tempor consectetur velit magna ea occaecat cupidatat consectetur anim aute. Aliquip est aute ipsum laboris non irure qui consectetur tempor quis do ea Lorem. Cupidatat exercitation ad culpa aliqua amet commodo mollit reprehenderit exercitation adipisicing amet et laborum pariatur.\r\n", + "registered": "2023-01-19T02:43:18 -00:00", + "latitude": 14.15208, + "longitude": 170.411535, + "tags": [ + "dolor", + "qui", + "cupidatat", + "aliqua", + "laboris", + "reprehenderit", + "sint" + ], + "friends": [ + { + "id": 0, + "name": "Burks Lowe" + }, + { + "id": 1, + "name": "Ray Sanchez" + }, + { + "id": 2, + "name": "Tasha Weiss" + } + ], + "greeting": "Hello, Willie Wallace! You have 6 unread messages.", + "favoriteFruit": "apple" + }, + { + "_id": "6606c27d01d19fa29853d59c", + "index": 5, + "guid": "816cda74-5d4b-498f-9724-20f340d5f5bf", + "isActive": false, + "balance": "$2,628.74", + "picture": "http://placehold.it/32x32", + "age": 28, + "eyeColor": "green", + "name": "Pearl Aguilar", + "gender": "female", + "company": "TELEPARK", + "email": "pearlaguilar@telepark.com", + "phone": "+1 (801) 427-2087", + "address": "633 Highlawn Avenue, Defiance, Illinois, 2118", + "about": "Cupidatat non ut nulla qui excepteur in minim non et nulla fugiat. Dolor quis laborum occaecat veniam dolor ullamco deserunt amet veniam dolor quis proident tempor laboris. In cillum duis ut quis. Aliqua cupidatat magna proident velit tempor veniam et consequat laborum ex dolore qui. Incididunt deserunt magna minim Lorem consectetur.\r\n", + "registered": "2017-10-14T11:14:08 -01:00", + "latitude": -5.345728, + "longitude": -9.706491, + "tags": [ + "officia", + "velit", + "laboris", + "qui", + "cupidatat", + "cupidatat", + "ad" + ], + "friends": [ + { + "id": 0, + "name": "Edwards Wolf" + }, + { + "id": 1, + "name": "Hunter Fitzgerald" + }, + { + "id": 2, + "name": "Valencia Norton" + } + ], + "greeting": "Hello, Pearl Aguilar! You have 2 unread messages.", + "favoriteFruit": "strawberry" + }, + { + "_id": "6606c27d803003cede1d6deb", + "index": 6, + "guid": "4ee550bc-0920-4104-b3ce-ebf9db6a803f", + "isActive": true, + "balance": "$1,709.31", + "picture": "http://placehold.it/32x32", + "age": 31, + "eyeColor": "blue", + "name": "Tanisha Pacheco", + "gender": "female", + "company": "VISUALIX", + "email": "tanishapacheco@visualix.com", + "phone": "+1 (867) 577-2549", + "address": "466 Coleridge Street, Dotsero, Delaware, 8574", + "about": "Lorem ex proident ipsum ullamco velit sit nisi eiusmod cillum. Id tempor irure culpa nisi sit non qui veniam non ut. Aliquip reprehenderit excepteur mollit quis excepteur ex sit. Quis do eu veniam do ullamco occaecat eu cupidatat nisi laborum tempor minim fugiat pariatur. Ex in nulla ex velit.\r\n", + "registered": "2019-04-08T03:54:36 -01:00", + "latitude": -70.660321, + "longitude": 71.547525, + "tags": [ + "consequat", + "veniam", + "pariatur", + "aliqua", + "cillum", + "eu", + "officia" + ], + "friends": [ + { + "id": 0, + "name": "Carter Galloway" + }, + { + "id": 1, + "name": "Moody Cameron" + }, + { + "id": 2, + "name": "June Gillespie" + } + ], + "greeting": "Hello, Tanisha Pacheco! You have 6 unread messages.", + "favoriteFruit": "apple" + } +] \ No newline at end of file