001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.axis2.databinding;
021:
022: import org.apache.axiom.om.OMAbstractFactory;
023: import org.apache.axiom.om.OMElement;
024: import org.apache.axiom.om.impl.dom.DOOMAbstractFactory;
025: import org.apache.axiom.om.util.StAXUtils;
026: import org.apache.axiom.soap.SOAP11Constants;
027: import org.apache.axiom.soap.SOAPEnvelope;
028: import org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder;
029: import org.apache.axis2.databinding.utils.PrintEvents;
030: import org.apache.axis2.databinding.utils.reader.ADBXMLStreamReaderImpl;
031: import org.custommonkey.xmlunit.XMLTestCase;
032: import org.w3c.dom.Document;
033: import org.xml.sax.SAXException;
034:
035: import javax.xml.namespace.QName;
036: import javax.xml.parsers.DocumentBuilder;
037: import javax.xml.parsers.DocumentBuilderFactory;
038: import javax.xml.parsers.ParserConfigurationException;
039: import javax.xml.stream.XMLStreamReader;
040: import java.io.ByteArrayInputStream;
041: import java.io.IOException;
042: import java.io.StringReader;
043: import java.io.StringWriter;
044: import java.util.ArrayList;
045:
046: public class ADBSOAPModelBuilderTest extends XMLTestCase {
047: public void testSimpleArrayList() throws Exception {
048: String expectedXML = "<?xml version='1.0' encoding='utf-8'?>"
049: + "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">"
050: + "<soapenv:Body>"
051: + "<Person xmlns=\"\">"
052: + "<Name xmlns=\"\">FooOne</Name>"
053: + "<DependentOne xmlns=\"\"><Name xmlns=\"\">FooTwo</Name>"
054: + "<Age xmlns=\"\">25</Age>"
055: + "<Sex xmlns=\"\">Male</Sex></DependentOne>"
056: + "<DependentTwo xmlns=\"\">"
057: + "<Name xmlns=\"\">FooTwo</Name>"
058: + "<Age xmlns=\"\">25</Age>"
059: + "<Sex xmlns=\"\">Male</Sex></DependentTwo>"
060: + "<Organization xmlns=\"\">Apache</Organization>"
061: + "</Person></soapenv:Body></soapenv:Envelope>";
062: ArrayList propertyList = new ArrayList();
063: propertyList.add("Name");
064: propertyList.add("FooOne");
065: propertyList.add(new QName("DependentOne"));
066: propertyList.add(new DummyADBBean());
067: propertyList.add(new QName("DependentTwo"));
068: propertyList.add(new DummyADBBean());
069: propertyList.add("Organization");
070: propertyList.add("Apache");
071: QName projectQName = new QName("Person");
072:
073: XMLStreamReader pullParser = new ADBXMLStreamReaderImpl(
074: projectQName, propertyList.toArray(), null);
075: ADBSOAPModelBuilder builder = new ADBSOAPModelBuilder(
076: pullParser, OMAbstractFactory.getSOAP11Factory());
077:
078: OMElement root = builder.getDocumentElement();
079: assertTrue("Root element can not be null", root != null);
080: Document expectedDOM = newDocument(expectedXML);
081: Document actualDom = newDocument(root.toString());
082: assertXMLEqual(actualDom, expectedDOM);
083: }
084:
085: public void testPrintEvents() throws Exception {
086: XMLStreamReader r = getTestEnvelope().getXMLStreamReader();
087: PrintEvents.print(r);
088: }
089:
090: public void testPrintEvents2() throws Exception {
091: //TODO: FIXME. Check the output difference between this method and the testPrintEvents method
092: XMLStreamReader r = getTestEnvelope()
093: .getXMLStreamReaderWithoutCaching();
094: PrintEvents.print(r);
095: }
096:
097: public void testConvertToDOOM() throws Exception {
098: String xml = "<?xml version='1.0' encoding='utf-8'?>"
099: + "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">"
100: + "<soapenv:Body><ns1:createAccountRequest xmlns:ns1=\"http://www.wso2.com/types\">"
101: + "<clientinfo xmlns=\"http://www.wso2.com/types\"><name>bob</name><ssn>123456789</ssn></clientinfo>"
102: + "<password xmlns=\"\">passwd</password></ns1:createAccountRequest></soapenv:Body></soapenv:Envelope>";
103:
104: StAXSOAPModelBuilder builder2 = new StAXSOAPModelBuilder(
105: getTestEnvelope().getXMLStreamReader(),
106: DOOMAbstractFactory.getSOAP11Factory(),
107: SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
108:
109: SOAPEnvelope envelope = builder2.getSOAPEnvelope();
110: envelope.build();
111:
112: StringWriter writer = new StringWriter();
113: envelope.serialize(writer);
114: writer.flush();
115:
116: String s2 = writer.toString();
117:
118: assertXMLEqual(s2, xml);
119: }
120:
121: private SOAPEnvelope getTestEnvelope() {
122: CreateAccountRequest request = new CreateAccountRequest();
123: ClientInfo clientInfo = new ClientInfo();
124: clientInfo.setName("bob");
125: clientInfo.setSsn("123456789");
126: request.setClientInfo(clientInfo);
127: request.setPassword("passwd");
128:
129: ADBSOAPModelBuilder builder = new ADBSOAPModelBuilder(request
130: .getPullParser(CreateAccountRequest.MY_QNAME),
131: OMAbstractFactory.getSOAP11Factory());
132:
133: return builder.getEnvelope();
134: }
135:
136: public void testConvertToDOOM2() throws Exception {
137: String xml = "<?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"><soapenv:Header /><soapenv:Body><ns1:createAccountRequest xmlns:ns1=\"http://www.wso2.com/types\"><ns1:clientinfo><name xmlns=\"\">bob</name><ssn xmlns=\"\">123456789</ssn></ns1:clientinfo><password xmlns=\"\">passwd</password></ns1:createAccountRequest></soapenv:Body></soapenv:Envelope>";
138:
139: CreateAccountRequest request = new CreateAccountRequest();
140: ClientInfo clientInfo = new ClientInfo();
141: clientInfo.setName("bob");
142: clientInfo.setSsn("123456789");
143: request.setClientInfo(clientInfo);
144: request.setPassword("passwd");
145:
146: ADBSOAPModelBuilder builder = new ADBSOAPModelBuilder(request
147: .getPullParser(CreateAccountRequest.MY_QNAME),
148: OMAbstractFactory.getSOAP11Factory());
149:
150: SOAPEnvelope env = builder.getEnvelope();
151:
152: StAXSOAPModelBuilder builder2 = new StAXSOAPModelBuilder(
153: getTestEnvelope().getXMLStreamReaderWithoutCaching(),
154: DOOMAbstractFactory.getSOAP11Factory(),
155: SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
156: SOAPEnvelope envelope = builder2.getSOAPEnvelope();
157: envelope.build();
158:
159: StringWriter writer = new StringWriter();
160: envelope.serialize(writer);
161: writer.flush();
162:
163: XMLStreamReader r = StAXUtils
164: .createXMLStreamReader(new StringReader(writer
165: .toString()));
166: PrintEvents.print(r);
167:
168: //TODO: FIXME. Simpler test in testPrintEvents2
169: //assertXMLEqual(writer.toString(),xml);
170: }
171:
172: public class DummyADBBean implements ADBBean {
173: ArrayList propertyList = new ArrayList();
174:
175: public DummyADBBean() {
176: propertyList.add("Name");
177: propertyList.add("FooTwo");
178: propertyList.add("Age");
179: propertyList.add("25");
180: propertyList.add("Sex");
181: propertyList.add("Male");
182: }
183:
184: public DummyADBBean addAnotherBean() {
185: propertyList.add(new QName("Depemdent"));
186: DummyADBBean dummyBean = new DummyADBBean();
187: propertyList.add(dummyBean);
188: return dummyBean;
189: }
190:
191: public XMLStreamReader getPullParser(QName adbBeanQName) {
192: return new ADBXMLStreamReaderImpl(adbBeanQName,
193: propertyList.toArray(), null);
194: }
195: }
196:
197: public Document newDocument(String xml)
198: throws ParserConfigurationException, SAXException,
199: IOException {
200: DocumentBuilderFactory dbf = DocumentBuilderFactory
201: .newInstance();
202: dbf.setNamespaceAware(true);
203: DocumentBuilder db = dbf.newDocumentBuilder();
204: return db.parse(new ByteArrayInputStream(xml.getBytes()));
205: }
206: }
|