Created
April 10, 2013 11:58
-
-
Save manuel-mauky/5354000 to your computer and use it in GitHub Desktop.
Demonstrates a Bug (?) with javas DocumentBuilder for String nodes with very long text. The only difference between the two test cases is that the first "testShortText" uses a text with 100 lines (see line 21) and the second "testLongText" uses 1000 lines (see line 39). While "testShortText" is running without problems, "testLongText" is red. Th…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.junit.Assert.assertEquals; | |
import static org.junit.Assert.assertNotNull; | |
import java.io.ByteArrayInputStream; | |
import java.io.InputStream; | |
import javax.xml.parsers.DocumentBuilder; | |
import javax.xml.parsers.DocumentBuilderFactory; | |
import org.junit.Test; | |
import org.w3c.dom.Document; | |
import org.w3c.dom.Node; | |
public class DocumentBuilderTest { | |
@Test | |
public void testShortText() { | |
final String content = createText(100); | |
final String html = createHtmlWithContent(content); | |
final Document doc = createDocumentFromString(html); | |
assertNotNull(doc); | |
final Node body = doc.getElementsByTagName("body").item(0); | |
assertEquals(content.trim(), body.getTextContent().trim()); | |
} | |
@Test | |
public void testLongText() { | |
final String content = createText(1000); | |
final String html = createHtmlWithContent(content); | |
final Document doc = createDocumentFromString(html); | |
assertNotNull(doc); | |
final Node body = doc.getElementsByTagName("body").item(0); | |
assertEquals(content, body.getTextContent().trim()); | |
} | |
private Document createDocumentFromString(final String html) { | |
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | |
try { | |
final DocumentBuilder documentBuilder = factory.newDocumentBuilder(); | |
final InputStream is = new ByteArrayInputStream(html.getBytes("UTF-8")); | |
return documentBuilder.parse(is); | |
} catch (final Exception e) { | |
e.printStackTrace(); | |
return null; | |
} | |
} | |
private String createHtmlWithContent(final String content) { | |
final StringBuilder builder = new StringBuilder(); | |
builder.append("<?xml version=\"1.1\" encoding=\"UTF-8\" ?>"); | |
builder.append("<!DOCTYPE html>\n"); | |
builder.append("<html lang=\"en\">\n"); | |
builder.append(" <head>\n"); | |
builder.append(" <meta charset=\"utf-8\" />\n"); | |
builder.append(" <title>test document</title>\n"); | |
builder.append(" </head>\n"); | |
builder.append(" <body>\n"); | |
builder.append(content); | |
builder.append(" </body>\n"); | |
builder.append("</html>\n"); | |
return builder.toString(); | |
} | |
private String createText(final int lines) { | |
final StringBuilder builder = new StringBuilder(); | |
for (int i = 0; i < lines; i++) { | |
builder.append("A new Line with the linenumber " + i + "\n"); | |
} | |
return builder.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment