1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ws.soap.axiom.support;
18
19 import java.io.StringWriter;
20 import java.util.Locale;
21 import javax.xml.namespace.QName;
22 import javax.xml.parsers.DocumentBuilder;
23 import javax.xml.parsers.DocumentBuilderFactory;
24 import javax.xml.stream.XMLInputFactory;
25 import javax.xml.stream.XMLStreamReader;
26
27 import org.apache.axiom.om.OMAbstractFactory;
28 import org.apache.axiom.om.OMElement;
29 import org.apache.axiom.om.OMFactory;
30 import org.apache.axiom.om.OMNamespace;
31 import org.apache.axiom.soap.SOAP11Constants;
32 import org.apache.axiom.soap.SOAPEnvelope;
33 import org.apache.axiom.soap.SOAPMessage;
34 import org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder;
35 import org.apache.axiom.soap.impl.llom.soap11.SOAP11Factory;
36 import org.custommonkey.xmlunit.XMLTestCase;
37 import org.custommonkey.xmlunit.XMLUnit;
38 import org.w3c.dom.Document;
39
40 import org.springframework.core.io.ClassPathResource;
41 import org.springframework.core.io.Resource;
42 import org.springframework.util.FileCopyUtils;
43 import org.springframework.xml.sax.SaxUtils;
44
45 public class AxiomUtilsTest extends XMLTestCase {
46
47 private OMElement element;
48
49 protected void setUp() throws Exception {
50 OMFactory factory = OMAbstractFactory.getOMFactory();
51 OMNamespace namespace = factory.createOMNamespace("http://www.springframework.org", "prefix");
52 element = factory.createOMElement("element", namespace);
53 XMLUnit.setIgnoreWhitespace(true);
54 }
55
56 public void testToNamespaceDeclared() throws Exception {
57 QName qName = new QName(element.getNamespace().getNamespaceURI(), "localPart");
58 OMNamespace namespace = AxiomUtils.toNamespace(qName, element);
59 assertNotNull("Invalid namespace", namespace);
60 assertEquals("Invalid namespace", qName.getNamespaceURI(), namespace.getNamespaceURI());
61 }
62
63 public void testToNamespaceUndeclared() throws Exception {
64 QName qName = new QName("http://www.example.com", "localPart");
65 OMNamespace namespace = AxiomUtils.toNamespace(qName, element);
66 assertNotNull("Invalid namespace", namespace);
67 assertEquals("Invalid namespace", qName.getNamespaceURI(), namespace.getNamespaceURI());
68 assertFalse("Invalid prefix", "prefix".equals(namespace.getPrefix()));
69 }
70
71 public void testToNamespacePrefixDeclared() throws Exception {
72 QName qName = new QName(element.getNamespace().getNamespaceURI(), "localPart", "prefix");
73 OMNamespace namespace = AxiomUtils.toNamespace(qName, element);
74 assertNotNull("Invalid namespace", namespace);
75 assertEquals("Invalid namespace", qName.getNamespaceURI(), namespace.getNamespaceURI());
76 assertEquals("Invalid prefix", "prefix", namespace.getPrefix());
77 }
78
79 public void testToNamespacePrefixUndeclared() throws Exception {
80 QName qName = new QName("http://www.example.com", "localPart", "otherPrefix");
81 OMNamespace namespace = AxiomUtils.toNamespace(qName, element);
82 assertNotNull("Invalid namespace", namespace);
83 assertEquals("Invalid namespace", qName.getNamespaceURI(), namespace.getNamespaceURI());
84 assertEquals("Invalid prefix", qName.getPrefix(), namespace.getPrefix());
85 }
86
87 public void testToLanguage() throws Exception {
88 assertEquals("Invalid conversion", "fr-CA", AxiomUtils.toLanguage(Locale.CANADA_FRENCH));
89 assertEquals("Invalid conversion", "en", AxiomUtils.toLanguage(Locale.ENGLISH));
90 }
91
92 public void testToLocale() throws Exception {
93 assertEquals("Invalid conversion", Locale.CANADA_FRENCH, AxiomUtils.toLocale("fr-CA"));
94 assertEquals("Invalid conversion", Locale.ENGLISH, AxiomUtils.toLocale("en"));
95 }
96
97 public void testToDocument() throws Exception {
98 Resource resource = new ClassPathResource("org/springframework/ws/soap/soap11/soap11.xml");
99
100 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
101 documentBuilderFactory.setNamespaceAware(true);
102 DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
103 Document expected = documentBuilder.parse(SaxUtils.createInputSource(resource));
104
105 XMLInputFactory inputFactory = XMLInputFactory.newInstance();
106 XMLStreamReader reader = inputFactory.createXMLStreamReader(resource.getInputStream());
107 StAXSOAPModelBuilder builder =
108 new StAXSOAPModelBuilder(reader, new SOAP11Factory(), SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
109 SOAPMessage soapMessage = builder.getSoapMessage();
110
111 Document result = AxiomUtils.toDocument(soapMessage.getSOAPEnvelope());
112
113 assertXMLEqual("Invalid document generated from SOAPEnvelope", expected, result);
114 }
115
116 public void testToEnvelope() throws Exception {
117 Resource resource = new ClassPathResource("org/springframework/ws/soap/soap11/soap11.xml");
118
119 byte[] buf = FileCopyUtils.copyToByteArray(resource.getFile());
120 String expected = new String(buf, "UTF-8");
121
122 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
123 documentBuilderFactory.setNamespaceAware(true);
124 DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
125 Document document = documentBuilder.parse(SaxUtils.createInputSource(resource));
126
127 SOAPEnvelope envelope = AxiomUtils.toEnvelope(document);
128 StringWriter writer = new StringWriter();
129 envelope.serialize(writer);
130 String result = writer.toString();
131
132 assertXMLEqual("Invalid SOAPEnvelope generated from document", expected, result);
133 }
134 }