22
22
23
23
import org .custommonkey .xmlunit .XMLTestCase ;
24
24
import org .w3c .dom .Document ;
25
+ import org .w3c .dom .Element ;
25
26
import org .xml .sax .InputSource ;
26
27
import org .xml .sax .XMLReader ;
27
28
import org .xml .sax .helpers .XMLReaderFactory ;
28
29
29
30
public class DomContentHandlerTest extends XMLTestCase {
30
31
31
- private static final String XML_CONTENT_HANDLER = "<?xml version='1.0' encoding='UTF-8'?>" + "<?pi content?>" +
32
+ private static final String XML_1 = "<?xml version='1.0' encoding='UTF-8'?>" + "<?pi content?>" +
32
33
"<root xmlns='namespace'>" +
33
34
"<prefix:child xmlns:prefix='namespace2' xmlns:prefix2='namespace3' prefix2:attr='value'>content</prefix:child>" +
34
35
"</root>" ;
35
36
37
+ private static final String XML_2_EXPECTED = "<?xml version='1.0' encoding='UTF-8'?>" + "<root xmlns='namespace'>" +
38
+ "<child xmlns='namespace2' />" + "</root>" ;
39
+
40
+ private static final String XML_2_SNIPPET =
41
+ "<?xml version='1.0' encoding='UTF-8'?>" + "<child xmlns='namespace2' />" ;
42
+
36
43
private Document expected ;
37
44
38
45
private DomContentHandler handler ;
@@ -41,20 +48,33 @@ public class DomContentHandlerTest extends XMLTestCase {
41
48
42
49
private XMLReader xmlReader ;
43
50
51
+ private DocumentBuilder documentBuilder ;
52
+
44
53
protected void setUp () throws Exception {
45
54
xmlReader = XMLReaderFactory .createXMLReader ();
46
55
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory .newInstance ();
47
56
documentBuilderFactory .setNamespaceAware (true );
48
- DocumentBuilder documentBuilder = documentBuilderFactory .newDocumentBuilder ();
57
+ documentBuilder = documentBuilderFactory .newDocumentBuilder ();
49
58
result = documentBuilder .newDocument ();
59
+ xmlReader .setFeature ("http://xml.org/sax/features/namespace-prefixes" , false );
60
+ }
61
+
62
+ public void testContentHandlerDocument () throws Exception {
50
63
handler = new DomContentHandler (result );
51
- expected = documentBuilder .parse (new InputSource (new StringReader (XML_CONTENT_HANDLER )));
64
+ expected = documentBuilder .parse (new InputSource (new StringReader (XML_1 )));
65
+ xmlReader .setContentHandler (handler );
66
+ xmlReader .parse (new InputSource (new StringReader (XML_1 )));
67
+ assertXMLEqual ("Invalid result" , expected , result );
52
68
}
53
69
54
- public void testContentHandler () throws Exception {
55
- xmlReader .setFeature ("http://xml.org/sax/features/namespace-prefixes" , false );
70
+ public void testContentHandlerElement () throws Exception {
71
+ Element rootElement = result .createElementNS ("namespace" , "root" );
72
+ result .appendChild (rootElement );
73
+ handler = new DomContentHandler (rootElement );
74
+ expected = documentBuilder .parse (new InputSource (new StringReader (XML_2_EXPECTED )));
56
75
xmlReader .setContentHandler (handler );
57
- xmlReader .parse (new InputSource (new StringReader (XML_CONTENT_HANDLER )));
76
+ xmlReader .parse (new InputSource (new StringReader (XML_2_SNIPPET )));
58
77
assertXMLEqual ("Invalid result" , expected , result );
78
+
59
79
}
60
80
}
0 commit comments