1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ws.soap.axiom;
18
19 import java.io.ByteArrayOutputStream;
20 import java.io.StringReader;
21
22 import org.apache.axiom.om.OMAbstractFactory;
23 import org.apache.axiom.om.OMDocument;
24 import org.apache.axiom.om.OMElement;
25 import org.apache.axiom.om.OMFactory;
26 import org.apache.axiom.om.OMNamespace;
27 import org.custommonkey.xmlunit.XMLTestCase;
28 import org.xml.sax.InputSource;
29 import org.xml.sax.XMLReader;
30 import org.xml.sax.helpers.XMLReaderFactory;
31
32 public class AxiomHandlerTest extends XMLTestCase {
33
34 private static final String XML_1 =
35 "<?xml version='1.0' encoding='UTF-8'?>" + "<?pi content?>" + "<root xmlns='namespace'>" +
36 "<prefix:child xmlns:prefix='namespace2' xmlns:prefix2='namespace3' prefix2:attr='value'>content</prefix:child>" +
37 "</root>";
38
39 private static final String XML_2_EXPECTED =
40 "<?xml version='1.0' encoding='UTF-8'?>" + "<root xmlns='namespace'>" + "<child xmlns='namespace2' />" +
41 "</root>";
42
43 private static final String XML_2_SNIPPET =
44 "<?xml version='1.0' encoding='UTF-8'?>" + "<child xmlns='namespace2' />";
45
46 private static final String XML_3_ENTITY =
47 "<predefined-entity-reference><>&"'</predefined-entity-reference>";
48
49 private AxiomHandler handler;
50
51 private OMDocument result;
52
53 private XMLReader xmlReader;
54
55 private OMFactory factory;
56
57 protected void setUp() throws Exception {
58 factory = OMAbstractFactory.getOMFactory();
59 result = factory.createOMDocument();
60 xmlReader = XMLReaderFactory.createXMLReader();
61 }
62
63 public void testContentHandlerDocumentNamespacePrefixes() throws Exception {
64 xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
65 handler = new AxiomHandler(result, factory);
66 xmlReader.setContentHandler(handler);
67 xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", handler);
68 xmlReader.parse(new InputSource(new StringReader(XML_1)));
69 ByteArrayOutputStream bos = new ByteArrayOutputStream();
70 result.serialize(bos);
71 assertXMLEqual("Invalid result", XML_1, bos.toString("UTF-8"));
72 }
73
74 public void testContentHandlerDocumentNoNamespacePrefixes() throws Exception {
75 xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
76 handler = new AxiomHandler(result, factory);
77 xmlReader.setContentHandler(handler);
78 xmlReader.parse(new InputSource(new StringReader(XML_1)));
79 ByteArrayOutputStream bos = new ByteArrayOutputStream();
80 result.serialize(bos);
81 assertXMLEqual("Invalid result", XML_1, bos.toString("UTF-8"));
82 }
83
84 public void testContentHandlerElement() throws Exception {
85 OMNamespace namespace = factory.createOMNamespace("namespace", "");
86 OMElement rootElement = factory.createOMElement("root", namespace, result);
87 handler = new AxiomHandler(rootElement, factory);
88 xmlReader.setContentHandler(handler);
89 xmlReader.parse(new InputSource(new StringReader(XML_2_SNIPPET)));
90 ByteArrayOutputStream bos = new ByteArrayOutputStream();
91 result.serialize(bos);
92 assertXMLEqual("Invalid result", XML_2_EXPECTED, bos.toString("UTF-8"));
93 }
94
95 public void testContentHandlerPredefinedEntityReference() throws Exception {
96 handler = new AxiomHandler(result, factory);
97 xmlReader.setContentHandler(handler);
98 xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", handler);
99 xmlReader.parse(new InputSource(new StringReader(XML_3_ENTITY)));
100 ByteArrayOutputStream bos = new ByteArrayOutputStream();
101 result.serialize(bos);
102 assertXMLEqual("Invalid result", XML_3_ENTITY, bos.toString("UTF-8"));
103 }
104 }