From a14cb12c85d50060b1f95840c01f0be5de24503d Mon Sep 17 00:00:00 2001 From: Cleydyr de Albuquerque Date: Wed, 1 Feb 2023 20:20:18 +0100 Subject: [PATCH] refactor: keep consistence with other tests and tidy up constant --- src/test/java/org/json/junit/XMLTest.java | 39 ++++++++++++----------- 1 file changed, 21 insertions(+), 18 deletions(-) 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)); + } } }