diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java
index 9c9ada4..6c0de9f 100644
--- a/src/test/java/org/json/junit/XMLTest.java
+++ b/src/test/java/org/json/junit/XMLTest.java
@@ -7,7 +7,6 @@ Public Domain.
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
-import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@@ -25,7 +24,6 @@ import java.util.Map;
import org.json.*;
import org.junit.Rule;
import org.junit.Test;
-import org.junit.function.ThrowingRunnable;
import org.junit.rules.TemporaryFolder;
@@ -1251,19 +1249,23 @@ public class XMLTest {
}
@Test
- public void testMaxNestingDepthIsRespected() {
- final String wayTooLongMalformedXML = "";
+ public void testMaxNestingDepthOf42IsRespected() {
+ final String wayTooLongMalformedXML = new String(new char[6000]).replace("\0", "");
- Throwable throwable = assertThrows(JSONException.class, new ThrowingRunnable() {
+ final int maxNestingDepth = 42;
- @Override
- public void run() throws Throwable {
- XML.toJSONObject(wayTooLongMalformedXML, XMLParserConfiguration.ORIGINAL.withMaxNestingDepth(42));
- }
- });
+ try {
+ XML.toJSONObject(wayTooLongMalformedXML, XMLParserConfiguration.ORIGINAL.withMaxNestingDepth(maxNestingDepth));
- assertTrue("Wrong throwable thrown: not expecting message <" + throwable.getMessage() + ">", throwable.getMessage().startsWith("Maximum nesting depth of"));
+ fail("Expecting a JSONException");
+ } catch (JSONException e) {
+ assertTrue("Wrong throwable thrown: not expecting message <" + e.getMessage() + ">",
+ e.getMessage().startsWith("Maximum nesting depth of " + maxNestingDepth));
+ }
+ }
+ @Test
+ public void testMaxNestingDepthIsRespectedWithValidXML() {
final String perfectlyFineXML = "\n" +
" \n" +
" sonoo\n" +
@@ -1272,15 +1274,16 @@ public class XMLTest {
" \n" +
"\n";
- throwable = assertThrows(JSONException.class, new ThrowingRunnable() {
+ final int maxNestingDepth = 1;
- @Override
- public void run() throws Throwable {
- XML.toJSONObject(perfectlyFineXML, XMLParserConfiguration.ORIGINAL.withMaxNestingDepth(1));
- }
- });
+ try {
+ XML.toJSONObject(perfectlyFineXML, XMLParserConfiguration.ORIGINAL.withMaxNestingDepth(maxNestingDepth));
- assertTrue("Wrong throwable thrown: not expecting message <" + throwable.getMessage() + ">", throwable.getMessage().startsWith("Maximum nesting depth of"));
+ fail("Expecting a JSONException");
+ } catch (JSONException e) {
+ assertTrue("Wrong throwable thrown: not expecting message <" + e.getMessage() + ">",
+ e.getMessage().startsWith("Maximum nesting depth of " + maxNestingDepth));
+ }
}
}