Skip to content

Commit

Permalink
fixed failing unit tests in strict mode, issue 940
Browse files Browse the repository at this point in the history
  • Loading branch information
marilynel committed Feb 15, 2025
1 parent 42afb34 commit f112a09
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/main/java/org/json/JSONParserConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class JSONParserConfiguration extends ParserConfiguration {
public JSONParserConfiguration() {
super();
this.overwriteDuplicateKey = false;
this.strictMode = true;
}

/**
Expand Down
48 changes: 38 additions & 10 deletions src/test/java/org/json/junit/JSONTokenerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
import java.io.Reader;
import java.io.StringReader;

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

/**
Expand Down Expand Up @@ -98,7 +95,17 @@ public void testValid() {
checkValid(" [] ",JSONArray.class);
checkValid("[1,2]",JSONArray.class);
checkValid("\n\n[1,2]\n\n",JSONArray.class);
checkValid("1 2", String.class);

// Test should fail if default strictMode is true, pass if false
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
if (jsonParserConfiguration.isStrictMode()) {
try {
checkValid("1 2", String.class);
assertEquals("Expected to throw exception due to invalid string", true, false);
} catch (JSONException e) { }
} else {
checkValid("1 2", String.class);
}
}

@Test
Expand Down Expand Up @@ -330,16 +337,37 @@ public void testAutoClose(){
public void testInvalidInput_JSONObject_withoutStrictModel_shouldParseInput() {
String input = "{\"invalidInput\": [],}";
JSONTokener tokener = new JSONTokener(input);
Object value = tokener.nextValue();
assertEquals(new JSONObject(input).toString(), value.toString());

// Test should fail if default strictMode is true, pass if false
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
if (jsonParserConfiguration.isStrictMode()) {
try {
Object value = tokener.nextValue();
assertEquals(new JSONObject(input).toString(), value.toString());
assertEquals("Expected to throw exception due to invalid string", true, false);
} catch (JSONException e) { }
} else {
Object value = tokener.nextValue();
assertEquals(new JSONObject(input).toString(), value.toString());
}
}

@Test
public void testInvalidInput_JSONArray_withoutStrictModel_shouldParseInput() {
String input = "[\"invalidInput\",]";
JSONTokener tokener = new JSONTokener(input);
Object value = tokener.nextValue();
assertEquals(new JSONArray(input).toString(), value.toString());
}

// Test should fail if default strictMode is true, pass if false
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
if (jsonParserConfiguration.isStrictMode()) {
try {
Object value = tokener.nextValue();
assertEquals(new JSONArray(input).toString(), value.toString());
assertEquals("Expected to throw exception due to invalid string", true, false);
} catch (JSONException e) { }
} else {
Object value = tokener.nextValue();
assertEquals(new JSONArray(input).toString(), value.toString());
}
}
}

0 comments on commit f112a09

Please # to comment.