001: /*
002: * Copyright 2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.ws.server.endpoint;
018:
019: import javax.xml.stream.XMLOutputFactory;
020: import javax.xml.stream.XMLStreamConstants;
021: import javax.xml.stream.XMLStreamReader;
022: import javax.xml.stream.XMLStreamWriter;
023: import javax.xml.transform.Transformer;
024: import javax.xml.transform.TransformerFactory;
025:
026: import org.apache.axiom.om.OMAbstractFactory;
027: import org.apache.axiom.soap.SOAPFactory;
028: import org.springframework.ws.context.DefaultMessageContext;
029: import org.springframework.ws.context.MessageContext;
030: import org.springframework.ws.soap.axiom.AxiomSoapMessage;
031: import org.springframework.ws.soap.axiom.AxiomSoapMessageFactory;
032: import org.springframework.xml.transform.StringResult;
033: import org.springframework.xml.transform.StringSource;
034:
035: /**
036: * Test case for AbstractStaxStreamPayloadEndpoint.
037: *
038: * @see AbstractStaxStreamPayloadEndpoint
039: */
040: public class StaxStreamPayloadEndpointTest extends
041: AbstractMessageEndpointTestCase {
042:
043: protected MessageEndpoint createNoResponseEndpoint() {
044: return new AbstractStaxStreamPayloadEndpoint() {
045: protected void invokeInternal(XMLStreamReader streamReader,
046: XMLStreamWriter streamWriter) throws Exception {
047: }
048: };
049: }
050:
051: protected MessageEndpoint createResponseEndpoint() {
052: return new AbstractStaxStreamPayloadEndpoint() {
053: protected void invokeInternal(XMLStreamReader streamReader,
054: XMLStreamWriter streamWriter) throws Exception {
055: assertNotNull("No streamReader passed", streamReader);
056: assertNotNull("No streamWriter passed", streamReader);
057: assertEquals("Not a start element",
058: XMLStreamConstants.START_ELEMENT, streamReader
059: .next());
060: assertEquals("Invalid start event local name",
061: REQUEST_ELEMENT, streamReader.getLocalName());
062: assertEquals("Invalid start event namespace",
063: NAMESPACE_URI, streamReader.getNamespaceURI());
064: assertTrue("streamReader has no next element",
065: streamReader.hasNext());
066: assertEquals("Not a end element",
067: XMLStreamConstants.END_ELEMENT, streamReader
068: .next());
069: assertEquals("Invalid end event local name",
070: REQUEST_ELEMENT, streamReader.getLocalName());
071: assertEquals("Invalid end event namespace",
072: NAMESPACE_URI, streamReader.getNamespaceURI());
073: streamWriter.writeEmptyElement(NAMESPACE_URI,
074: RESPONSE_ELEMENT);
075: streamWriter.flush();
076: streamWriter.close();
077: }
078:
079: protected XMLOutputFactory createXmlOutputFactory() {
080: XMLOutputFactory outputFactory = XMLOutputFactory
081: .newInstance();
082: outputFactory.setProperty(
083: "javax.xml.stream.isRepairingNamespaces",
084: Boolean.TRUE);
085: return outputFactory;
086: }
087: };
088: }
089:
090: public void testAxiomResponse() throws Exception {
091: Transformer transformer = TransformerFactory.newInstance()
092: .newTransformer();
093: SOAPFactory axiomFactory = OMAbstractFactory.getSOAP11Factory();
094: AxiomSoapMessage request = new AxiomSoapMessage(axiomFactory);
095: transformer.transform(new StringSource(REQUEST), request
096: .getPayloadResult());
097: AxiomSoapMessageFactory soapMessageFactory = new AxiomSoapMessageFactory();
098: soapMessageFactory.afterPropertiesSet();
099: MessageContext context = new DefaultMessageContext(request,
100: soapMessageFactory);
101:
102: MessageEndpoint endpoint = createResponseEndpoint();
103: endpoint.invoke(context);
104: assertTrue("context has not response", context.hasResponse());
105: StringResult stringResult = new StringResult();
106: transformer.transform(context.getResponse().getPayloadSource(),
107: stringResult);
108: assertXMLEqual(RESPONSE, stringResult.toString());
109: }
110:
111: public void testAxiomNoResponse() throws Exception {
112: Transformer transformer = TransformerFactory.newInstance()
113: .newTransformer();
114: SOAPFactory axiomFactory = OMAbstractFactory.getSOAP11Factory();
115: AxiomSoapMessage request = new AxiomSoapMessage(axiomFactory);
116: transformer.transform(new StringSource(REQUEST), request
117: .getPayloadResult());
118: AxiomSoapMessageFactory soapMessageFactory = new AxiomSoapMessageFactory();
119: soapMessageFactory.afterPropertiesSet();
120: MessageContext context = new DefaultMessageContext(request,
121: soapMessageFactory);
122:
123: MessageEndpoint endpoint = createNoResponseEndpoint();
124: endpoint.invoke(context);
125: assertFalse("context has response", context.hasResponse());
126: }
127:
128: }
|