Skip to content

Commit

Permalink
add new test cases for JSONObject and JSONArray Constructors with JSO…
Browse files Browse the repository at this point in the history
…NTokener and strict mode

they are failing, because the expected strict mode is not validated
  • Loading branch information
Simulant87 committed Jan 8, 2025
1 parent 391c869 commit 20f3f99
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions src/test/java/org/json/junit/JSONParserConfigurationTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package org.json.junit;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONParserConfiguration;
import org.json.*;
import org.junit.Test;

import java.io.IOException;
Expand Down Expand Up @@ -490,6 +487,40 @@ public void givenInvalidInputObject_testStrictModeTrue_shouldThrowKeyNotSurround
je.getMessage());
}

@Test
public void givenInvalidInputObject_testStrictModeTrue_JSONObjectUsingJSONTokener_shouldThrowJSONException() {
JSONException exception = assertThrows(JSONException.class, () -> {
new JSONObject(new JSONTokener("{\"key\":\"value\"} invalid trailing text"), new JSONParserConfiguration().withStrictMode(true));
});

assertEquals("Strict mode error: Unparsed characters found at end of input text", exception.getMessage());
}

@Test
public void givenInvalidInputObject_testStrictModeTrue_JSONObjectUsingString_shouldThrowJSONException() {
JSONException exception = assertThrows(JSONException.class, () -> {
new JSONObject("{\"key\":\"value\"} invalid trailing text", new JSONParserConfiguration().withStrictMode(true));
});
assertEquals("Strict mode error: Unparsed characters found at end of input text", exception.getMessage());
}

@Test
public void givenInvalidInputObject_testStrictModeTrue_JSONArrayUsingJSONTokener_shouldThrowJSONException() {
JSONException exception = assertThrows(JSONException.class, () -> {
new JSONArray(new JSONTokener("[\"value\"] invalid trailing text"), new JSONParserConfiguration().withStrictMode(true));
});

assertEquals("Strict mode error: Unparsed characters found at end of input text at 11 [character 12 line 1]", exception.getMessage());
}

@Test
public void givenInvalidInputObject_testStrictModeTrue_JSONArrayUsingString_shouldThrowJSONException() {
JSONException exception = assertThrows(JSONException.class, () -> {
new JSONArray("[\"value\"] invalid trailing text", new JSONParserConfiguration().withStrictMode(true));
});
assertEquals("Strict mode error: Unparsed characters found at end of input text at 11 [character 12 line 1]", exception.getMessage());
}

/**
* This method contains short but focused use-case samples and is exclusively used to test strictMode unit tests in
* this class.
Expand Down

0 comments on commit 20f3f99

Please sign in to comment.