Skip to content

Commit

Permalink
Merge pull request #846 from stleary/cleanup-and-merge-tests
Browse files Browse the repository at this point in the history
Cleanup warnings and merge new unit tests
  • Loading branch information
stleary authored Jan 4, 2024
2 parents ac7806d + 86bb0a1 commit 55b824d
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 6 deletions.
Empty file modified gradlew
100644 → 100755
Empty file.
3 changes: 3 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArgs>
<arg>-Xlint:unchecked</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/json/JSONArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -1359,7 +1359,8 @@ public JSONArray put(int index, long value) throws JSONException {
* The subscript.
* @param value
* The Map value.
* @return this.
* @return
* reference to self
* @throws JSONException
* If the index is negative or if the value is an invalid
* number.
Expand All @@ -1381,7 +1382,7 @@ public JSONArray put(int index, Map<?, ?> value) throws JSONException {
* The Map value.
* @param jsonParserConfiguration
* Configuration object for the JSON parser
* @return
* @return reference to self
* @throws JSONException
* If the index is negative or if the value is an invalid
* number.
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/json/JSONMLParserConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ protected JSONMLParserConfiguration clone() {
);
}

@SuppressWarnings("unchecked")
@Override
public JSONMLParserConfiguration withKeepStrings(final boolean newVal) {
return super.withKeepStrings(newVal);
}

@SuppressWarnings("unchecked")
@Override
public JSONMLParserConfiguration withMaxNestingDepth(int maxNestingDepth) {
return super.withMaxNestingDepth(maxNestingDepth);
Expand Down
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 @@ -17,6 +17,7 @@ protected JSONParserConfiguration clone() {
return new JSONParserConfiguration();
}

@SuppressWarnings("unchecked")
@Override
public JSONParserConfiguration withMaxNestingDepth(final int maxNestingDepth) {
return super.withMaxNestingDepth(maxNestingDepth);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/json/ParserConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public boolean isKeepStrings() {
*
* @return The existing configuration will not be modified. A new configuration is returned.
*/
@SuppressWarnings("unchecked")
public <T extends ParserConfiguration> T withKeepStrings(final boolean newVal) {
T newConfig = (T)this.clone();
newConfig.keepStrings = newVal;
Expand All @@ -101,6 +102,7 @@ public int getMaxNestingDepth() {
*
* @return The existing configuration will not be modified. A new configuration is returned.
*/
@SuppressWarnings("unchecked")
public <T extends ParserConfiguration> T withMaxNestingDepth(int maxNestingDepth) {
T newConfig = (T)this.clone();

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/json/XMLParserConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ protected XMLParserConfiguration clone() {
*
* @return The existing configuration will not be modified. A new configuration is returned.
*/
@SuppressWarnings("unchecked")
@Override
public XMLParserConfiguration withKeepStrings(final boolean newVal) {
return super.withKeepStrings(newVal);
Expand Down Expand Up @@ -309,14 +310,15 @@ public XMLParserConfiguration withForceList(final Set<String> forceList) {
* @param maxNestingDepth the maximum nesting depth allowed to the XML parser
* @return The existing configuration will not be modified. A new configuration is returned.
*/
@SuppressWarnings("unchecked")
@Override
public XMLParserConfiguration withMaxNestingDepth(int maxNestingDepth) {
return super.withMaxNestingDepth(maxNestingDepth);
}

/**
* To enable explicit end tag with empty value.
* @param closeEmptyTag
* @param closeEmptyTag new value for the closeEmptyTag property
* @return same instance of configuration with empty tag config updated
*/
public XMLParserConfiguration withCloseEmptyTag(boolean closeEmptyTag){
Expand Down
42 changes: 42 additions & 0 deletions src/test/java/org/json/junit/JSONObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3760,6 +3760,48 @@ public void issue743SerializationMapWith1001Objects() {
String jsonString = object.toString();
}

@Test(expected = JSONException.class)
public void testCircleReferenceFirstLevel() {
Map<Object, Object> jsonObject = new HashMap<>();

jsonObject.put("test", jsonObject);

new JSONObject(jsonObject, new JSONParserConfiguration());
}

@Test(expected = StackOverflowError.class)
public void testCircleReferenceMultiplyLevel_notConfigured_expectedStackOverflow() {
Map<Object, Object> inside = new HashMap<>();

Map<Object, Object> jsonObject = new HashMap<>();
inside.put("test", jsonObject);
jsonObject.put("test", inside);

new JSONObject(jsonObject, new JSONParserConfiguration().withMaxNestingDepth(99999));
}

@Test(expected = JSONException.class)
public void testCircleReferenceMultiplyLevel_configured_expectedJSONException() {
Map<Object, Object> inside = new HashMap<>();

Map<Object, Object> jsonObject = new HashMap<>();
inside.put("test", jsonObject);
jsonObject.put("test", inside);

new JSONObject(jsonObject, new JSONParserConfiguration());
}

@Test
public void testDifferentKeySameInstanceNotACircleReference() {
Map<Object, Object> map1 = new HashMap<>();
Map<Object, Object> map2 = new HashMap<>();

map1.put("test1", map2);
map1.put("test2", map2);

new JSONObject(map1);
}

/**
* Method to build nested map of max maxDepth
*
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/org/json/junit/data/WeirdList.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
public class WeirdList {
/** */
private final List<Integer> list = new ArrayList();
private final List<Integer> list = new ArrayList<>();

/**
* @param vals
Expand All @@ -25,14 +25,14 @@ public WeirdList(Integer... vals) {
* @return a copy of the list
*/
public List<Integer> get() {
return new ArrayList(this.list);
return new ArrayList<>(this.list);
}

/**
* @return a copy of the list
*/
public List<Integer> getALL() {
return new ArrayList(this.list);
return new ArrayList<>(this.list);
}

/**
Expand Down

0 comments on commit 55b824d

Please sign in to comment.