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.stream.XMLInputFactory;
024: import javax.xml.stream.XMLStreamReader;
025:
026: import junit.framework.TestCase;
027: import org.apache.axiom.om.OMElement;
028: import org.apache.axiom.om.OMNamespace;
029: import org.apache.axiom.soap.SOAPEnvelope;
030: import org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder;
031: import org.apache.axis2.jaxws.message.factory.MessageFactory;
032: import org.apache.axis2.jaxws.message.factory.XMLStringBlockFactory;
033: import org.apache.axis2.jaxws.message.util.Reader2Writer;
034: import org.apache.axis2.jaxws.registry.FactoryRegistry;
035: import org.apache.axis2.jaxws.TestLogger;
036:
037: /**
038: * This suite is used to test the creation of messages based on SOAP 1.2
039: * with both inbound and outbound simulations.
040: *
041: */
042: public class SOAP12Tests extends TestCase {
043:
044: private static final String sampleText = "<echo>test string</echo>";
045:
046: private static final String sampleSoap12EnvelopeHead = "<soapenv:Envelope xmlns:soapenv=\"http://www.w3.org/2003/05/soap-envelope\">"
047: + "<soapenv:Header /><soapenv:Body>";
048:
049: private static final String sampleEnvelopeTail = "</soapenv:Body></soapenv:Envelope>";
050:
051: private static final String sampleSoap12Envelope = sampleSoap12EnvelopeHead
052: + sampleText + sampleEnvelopeTail;
053:
054: private static final String SOAP12_NS_URI = "http://www.w3.org/2003/05/soap-envelope";
055:
056: public static final XMLInputFactory inputFactory = XMLInputFactory
057: .newInstance();
058:
059: public SOAP12Tests(String name) {
060: super (name);
061: }
062:
063: /**
064: * Simulate creating a SOAP 1.2 message when the business object
065: * provided is just the payload.
066: */
067: public void testCreateSoap12FromPayload() throws Exception {
068: // Create a SOAP 1.2 Message
069: MessageFactory mf = (MessageFactory) FactoryRegistry
070: .getFactory(MessageFactory.class);
071: Message m = mf.create(Protocol.soap12);
072:
073: // Get the BlockFactory
074: XMLStringBlockFactory f = (XMLStringBlockFactory) FactoryRegistry
075: .getFactory(XMLStringBlockFactory.class);
076:
077: // Create a Block using the sample string as the content. This simulates
078: // what occurs on the outbound JAX-WS dispatch<String> client
079: Block block = f.createFrom(sampleText, null, null);
080:
081: // Add the block to the message as normal body content.
082: m.setBodyBlock(block);
083:
084: // Assuming no handlers are installed, the next thing that will happen
085: // is a XMLStreamReader will be requested...to go to OM. At this point the
086: // block should be consumed.
087: OMElement om = m.getAsOMElement();
088:
089: // The block should not be consumed yet...because the message has not been read
090: assertTrue(!block.isConsumed());
091:
092: // To check that the output is correct, get the String contents of the
093: // reader
094: Reader2Writer r2w = new Reader2Writer(om.getXMLStreamReader());
095: String newText = r2w.getAsString();
096: TestLogger.logger.debug(newText);
097: assertTrue(newText.contains(sampleText));
098: assertTrue(newText.contains("soap"));
099: assertTrue(newText.contains("Envelope"));
100: assertTrue(newText.contains("Body"));
101:
102: assertTrue(m.getProtocol().equals(Protocol.soap12));
103:
104: SOAPEnvelope omSoapEnv = (SOAPEnvelope) m.getAsOMElement();
105: OMNamespace ns = omSoapEnv.getNamespace();
106: assertTrue(ns.getNamespaceURI().equals(SOAP12_NS_URI));
107:
108: // The block should be consumed at this point
109: assertTrue(block.isConsumed());
110: }
111:
112: /**
113: * Simulate creating a SOAP 1.2 message when the business object
114: * provided is the full message.
115: */
116: public void testCreateSoap12FromMessage() throws Exception {
117: // Create a SOAP 1.2 Message
118: MessageFactory mf = (MessageFactory) FactoryRegistry
119: .getFactory(MessageFactory.class);
120:
121: // Get the BlockFactory
122: XMLStringBlockFactory f = (XMLStringBlockFactory) FactoryRegistry
123: .getFactory(XMLStringBlockFactory.class);
124:
125: // Create a Block using the sample string as the content. This simulates
126: // what occurs on the outbound JAX-WS dispatch<String> client
127: Block block = f.createFrom(sampleSoap12Envelope, null, null);
128:
129: // Create a Message with the full XML contents that we have
130: Message m = mf.createFrom(block.getXMLStreamReader(true), null);
131:
132: // Assuming no handlers are installed, the next thing that will happen
133: // is a XMLStreamReader will be requested...to go to OM. At this point the
134: // block should be consumed.
135: OMElement om = m.getAsOMElement();
136:
137: // To check that the output is correct, get the String contents of the
138: // reader
139: Reader2Writer r2w = new Reader2Writer(om
140: .getXMLStreamReaderWithoutCaching());
141: String newText = r2w.getAsString();
142: TestLogger.logger.debug(newText);
143: assertTrue(newText.contains(sampleText));
144: assertTrue(newText.contains("soap"));
145: assertTrue(newText.contains("Envelope"));
146: assertTrue(newText.contains("Body"));
147:
148: assertTrue(m.getProtocol().equals(Protocol.soap12));
149:
150: SOAPEnvelope omSoapEnv = (SOAPEnvelope) m.getAsOMElement();
151: OMNamespace ns = omSoapEnv.getNamespace();
152: assertTrue(ns.getNamespaceURI().equals(SOAP12_NS_URI));
153:
154: // The block should be consumed at this point
155: assertTrue(block.isConsumed());
156: }
157:
158: public void testGetPayloadFromSoap12() throws Exception {
159: // On inbound, there will already be an OM
160: // which represents the message. The following code simulates the input
161: // OM
162: StringReader sr = new StringReader(sampleSoap12Envelope);
163: XMLStreamReader inflow = inputFactory.createXMLStreamReader(sr);
164: StAXSOAPModelBuilder builder = new StAXSOAPModelBuilder(inflow,
165: null);
166: OMElement omElement = builder.getSOAPEnvelope();
167:
168: // The JAX-WS layer creates a Message from the OM
169: MessageFactory mf = (MessageFactory) FactoryRegistry
170: .getFactory(MessageFactory.class);
171: Message m = mf.createFrom(omElement, null);
172:
173: // Make sure the right Protocol was set on the Message
174: assertTrue(m.getProtocol().equals(Protocol.soap12));
175:
176: // Check the SOAPEnvelope to make sure we've got the right
177: // protocol namespace there as well.
178: SOAPEnvelope soapEnv = (SOAPEnvelope) m.getAsOMElement();
179: OMNamespace ns = soapEnv.getNamespace();
180: assertTrue(ns.getNamespaceURI().equals(SOAP12_NS_URI));
181:
182: // Assuming no handlers are installed, the next thing that will happen
183: // is the proxy code will ask for the business object (String).
184: XMLStringBlockFactory blockFactory = (XMLStringBlockFactory) FactoryRegistry
185: .getFactory(XMLStringBlockFactory.class);
186: Block block = m.getBodyBlock(null, blockFactory);
187: Object bo = block.getBusinessObject(true);
188: assertTrue(bo instanceof String);
189:
190: // The block should be consumed
191: assertTrue(block.isConsumed());
192:
193: // Check the String for accuracy
194: assertTrue(sampleText.equals(bo));
195: }
196:
197: public void testGetMessageFromSoap12() throws Exception {
198: // On inbound, there will already be an OM
199: // which represents the message. The following code simulates the input
200: // OM
201: StringReader sr = new StringReader(sampleSoap12Envelope);
202: XMLStreamReader inflow = inputFactory.createXMLStreamReader(sr);
203: StAXSOAPModelBuilder builder = new StAXSOAPModelBuilder(inflow,
204: null);
205: OMElement omElement = builder.getSOAPEnvelope();
206:
207: // The JAX-WS layer creates a Message from the OM
208: MessageFactory mf = (MessageFactory) FactoryRegistry
209: .getFactory(MessageFactory.class);
210: Message m = mf.createFrom(omElement, null);
211:
212: // Make sure the right Protocol was set on the Message
213: assertTrue(m.getProtocol().equals(Protocol.soap12));
214:
215: // Check the SOAPEnvelope to make sure we've got the right
216: // protocol namespace there as well.
217: SOAPEnvelope soapEnv = (SOAPEnvelope) m.getAsOMElement();
218: OMNamespace ns = soapEnv.getNamespace();
219: assertTrue(ns.getNamespaceURI().equals(SOAP12_NS_URI));
220:
221: // Assuming no handlers are installed, the next thing that will happen
222: // is the proxy code will ask for the business object (String).
223: XMLStringBlockFactory blockFactory = (XMLStringBlockFactory) FactoryRegistry
224: .getFactory(XMLStringBlockFactory.class);
225: Block block = blockFactory.createFrom(m.getAsOMElement(), null,
226: null);
227: Object bo = block.getBusinessObject(true);
228: assertTrue(bo instanceof String);
229:
230: // The block should be consumed
231: assertTrue(block.isConsumed());
232:
233: // Check the String for accuracy
234: assertTrue(((String) bo)
235: .contains("<soapenv:Body><echo>test string</echo></soapenv:Body>"));
236: }
237: }
|