@@ -136,9 +136,7 @@ public void testSAXBillionLaughs() throws Exception {
136136 XMLReaderUtils .parseSAX (new ByteArrayInputStream (xml .getBytes (StandardCharsets .UTF_8 )),
137137 new ToTextContentHandler (), new ParseContext ());
138138 } catch (SAXException e ) {
139- if (e .getMessage () != null && e
140- .getMessage ()
141- .contains ("entity expansions" )) {
139+ if (e .getMessage () != null && e .getMessage ().contains ("entity expansions" )) {
142140 //do nothing
143141 } else {
144142 throw e ;
@@ -150,8 +148,22 @@ public void testSAXBillionLaughs() throws Exception {
150148 @ Test
151149 public void testDOMBillionLaughs () throws Exception {
152150 //confirm that ExpandEntityReferences has been set to false.
151+
152+ //some implementations ignore the expandEntityReferences=false, and we are still
153+ //protected by the "The parser has encountered more than "20" entity expansions" SAXException.
154+ //We need to check for either: empty content and no exception, or this SAXException
153155 for (String xml : BILLION_LAUGHS ) {
154- Document doc = XMLReaderUtils .buildDOM (new ByteArrayInputStream (xml .getBytes (StandardCharsets .UTF_8 )), new ParseContext ());
156+ Document doc = null ;
157+ try {
158+ doc = XMLReaderUtils .buildDOM (new ByteArrayInputStream (xml .getBytes (StandardCharsets .UTF_8 )), new ParseContext ());
159+ } catch (SAXException e ) {
160+ if (e .getMessage () != null && e .getMessage ().contains ("entity expansions" )) {
161+ //do nothing
162+ continue ;
163+ } else {
164+ throw e ;
165+ }
166+ }
155167 NodeList nodeList = doc .getChildNodes ();
156168 StringBuilder sb = new StringBuilder ();
157169 dumpChildren (nodeList , sb );
0 commit comments