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: package org.apache.axis2.jaxws.message;
020:
021: import java.io.StringReader;
022:
023: import javax.xml.soap.MessageFactory;
024: import javax.xml.soap.SOAPBody;
025: import javax.xml.soap.SOAPBodyElement;
026: import javax.xml.soap.SOAPElement;
027: import javax.xml.soap.SOAPEnvelope;
028: import javax.xml.soap.SOAPFactory;
029: import javax.xml.soap.SOAPMessage;
030: import javax.xml.stream.XMLInputFactory;
031: import javax.xml.stream.XMLStreamReader;
032:
033: import junit.framework.TestCase;
034: import org.apache.axiom.om.OMAbstractFactory;
035: import org.apache.axiom.om.OMElement;
036: import org.apache.axiom.om.OMFactory;
037: import org.apache.axiom.om.OMNamespace;
038: import org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder;
039: import org.apache.axis2.jaxws.message.factory.SAAJConverterFactory;
040: import org.apache.axis2.jaxws.message.util.SAAJConverter;
041: import org.apache.axis2.jaxws.registry.FactoryRegistry;
042:
043: /**
044: * SAAJConverterTests
045: *
046: * Test the basic functionality of the SAAJConverter.
047: * You can also use these tests to as sample code on how to use
048: * the converter.
049: *
050: */
051: public class SAAJConverterTests extends TestCase {
052:
053: private static final String sampleText = "<pre:a xmlns:pre=\"urn://sample\">"
054: + "<b>Hello</b>" + "<c>World</c>" + "</pre:a>";
055:
056: private static final String sampleEnvelope = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">"
057: + "<soapenv:Header /><soapenv:Body>"
058: + sampleText
059: + "</soapenv:Body></soapenv:Envelope>";
060:
061: private static XMLInputFactory inputFactory = XMLInputFactory
062: .newInstance();
063:
064: public SAAJConverterTests() {
065: super ();
066: }
067:
068: public SAAJConverterTests(String arg0) {
069: super (arg0);
070: }
071:
072: /**
073: * @testStrategy Tests conversions between SAAJ and OM SOAPEnvelopes
074: */
075: public void test1() throws Exception {
076:
077: // Bootstrap: Create an OM SOAPEnvelope from the sample text
078: StringReader sr = new StringReader(sampleEnvelope);
079: XMLStreamReader inflow = inputFactory.createXMLStreamReader(sr);
080: StAXSOAPModelBuilder builder = new StAXSOAPModelBuilder(inflow,
081: null);
082: org.apache.axiom.soap.SOAPEnvelope omEnvelope = builder
083: .getSOAPEnvelope();
084:
085: // Step 1: Get the SAAJConverter object from the Factory
086: SAAJConverterFactory f = (SAAJConverterFactory) FactoryRegistry
087: .getFactory(SAAJConverterFactory.class);
088: SAAJConverter converter = f.getSAAJConverter();
089:
090: // Step 2: Convert the OM SOAPEnvelope to an SAAJ SOAPEnvelope
091: SOAPEnvelope saajEnvelope = converter.toSAAJ(omEnvelope);
092:
093: // Step 2a: Simple assertion check to ensure correctness.
094: String name = saajEnvelope.getBody().getFirstChild()
095: .getLocalName();
096: assertTrue("a".equals(name));
097:
098: // Step 3: Convert the SAAJ SOAPEnvelope to an OM SOAPEnvelope
099: omEnvelope = converter.toOM(saajEnvelope);
100:
101: // Step 3a: Simple assertion check to ensure correctness
102: name = omEnvelope.getBody().getFirstElement().getLocalName();
103: assertTrue("a".equals(name));
104:
105: // Step 4: Rinse and repeat
106: saajEnvelope = converter.toSAAJ(omEnvelope);
107: name = saajEnvelope.getBody().getFirstChild().getLocalName();
108: assertTrue("a".equals(name));
109: omEnvelope = converter.toOM(saajEnvelope);
110: name = omEnvelope.getBody().getFirstElement().getLocalName();
111: assertTrue("a".equals(name));
112: }
113:
114: /**
115: * @testStrategy Tests conversions between SAAJ and OM for normal element
116: */
117: public void test2() throws Exception {
118:
119: // Bootstrap: Create an OM SOAPEnvelope from the sample text.
120: StringReader sr = new StringReader(sampleEnvelope);
121: XMLStreamReader inflow = inputFactory.createXMLStreamReader(sr);
122: StAXSOAPModelBuilder builder = new StAXSOAPModelBuilder(inflow,
123: null);
124: org.apache.axiom.soap.SOAPEnvelope omEnvelope = builder
125: .getSOAPEnvelope();
126:
127: // Bootstrap: Get an OMElement from the body
128: OMElement om = omEnvelope.getBody().getFirstElement();
129:
130: // Bootstrap: Get an SAAJ Body to hold the target SOAPElement
131: MessageFactory msgFactory = MessageFactory.newInstance();
132: SOAPMessage message = msgFactory.createMessage();
133: SOAPBody body = message.getSOAPBody();
134:
135: // Step 1: Get the SAAJConverter object from the Factory
136: SAAJConverterFactory f = (SAAJConverterFactory) FactoryRegistry
137: .getFactory(SAAJConverterFactory.class);
138: SAAJConverter converter = f.getSAAJConverter();
139:
140: // Step 2: Convert OM to SAAJ
141: SOAPElement se = converter.toSAAJ(om, body);
142:
143: // Step 2a: Verify
144: assertTrue(se instanceof SOAPBodyElement);
145: assertTrue(se.getLocalName().equals("a"));
146:
147: // Step 3: Convert SAAJ to OM
148: om = converter.toOM(se);
149:
150: // Step 3a: Verify
151: assertTrue(om.getLocalName().equals("a"));
152:
153: // Step 4: Rinse and Repeat
154: se = converter.toSAAJ(om, body);
155: assertTrue(se instanceof SOAPBodyElement);
156: assertTrue(se.getLocalName().equals("a"));
157: om = converter.toOM(se);
158: assertTrue(om.getLocalName().equals("a"));
159: }
160:
161: /**
162: * @testStrategy: Create an OMElement, without using a builder. Verification of AXIS2-970
163: */
164: public void test3() throws Exception {
165:
166: // Step 1: Get the SAAJConverter object from the Factory
167: SAAJConverterFactory f = (SAAJConverterFactory) FactoryRegistry
168: .getFactory(SAAJConverterFactory.class);
169: SAAJConverter converter = f.getSAAJConverter();
170:
171: // Stept 2: Create OM and parent SOAPElement
172: OMFactory fac = OMAbstractFactory.getOMFactory();
173: OMNamespace wrapNs = fac.createOMNamespace("namespace",
174: "prefix");
175: OMElement ome = fac.createOMElement("localname", wrapNs);
176: SOAPFactory sf = SOAPFactory.newInstance();
177: SOAPElement se = sf.createElement("name");
178:
179: // Step 3: Do the conversion
180: converter.toSAAJ(ome, se, sf);
181: }
182: }
|