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: */package org.apache.cxf.binding.soap;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.ByteArrayOutputStream;
022: import java.util.Arrays;
023: import java.util.List;
024:
025: import javax.xml.namespace.QName;
026: import javax.xml.stream.XMLOutputFactory;
027: import javax.xml.stream.XMLStreamReader;
028: import javax.xml.stream.XMLStreamWriter;
029:
030: import org.apache.cxf.binding.soap.interceptor.RPCOutInterceptor;
031: import org.apache.cxf.jaxb.JAXBDataBinding;
032: import org.apache.cxf.message.Message;
033: import org.apache.cxf.message.MessageContentsList;
034: import org.apache.cxf.service.Service;
035: import org.apache.cxf.service.model.BindingInfo;
036: import org.apache.cxf.service.model.BindingOperationInfo;
037: import org.apache.cxf.service.model.ServiceInfo;
038: import org.apache.cxf.staxutils.DepthXMLStreamReader;
039: import org.apache.cxf.staxutils.StaxUtils;
040: import org.apache.hello_world_rpclit.types.MyComplexStruct;
041: import org.easymock.classextension.EasyMock;
042: import org.easymock.classextension.IMocksControl;
043: import org.junit.After;
044: import org.junit.Before;
045: import org.junit.Test;
046:
047: public class RPCOutInterceptorTest extends TestBase {
048:
049: private static final String TNS = "http://apache.org/hello_world_rpclit";
050:
051: private static final String OPNAME = "sendReceiveData";
052:
053: private ByteArrayOutputStream baos = new ByteArrayOutputStream(
054: 64 * 1024);
055:
056: private IMocksControl control = EasyMock.createNiceControl();
057:
058: @Before
059: public void setUp() throws Exception {
060: super .setUp();
061: ServiceInfo si = getMockedServiceModel(this .getClass()
062: .getResource("/wsdl/hello_world_rpc_lit.wsdl")
063: .toString());
064: BindingInfo bi = si.getBinding(new QName(TNS,
065: "Greeter_SOAPBinding_RPCLit"));
066: BindingOperationInfo boi = bi.getOperation(new QName(TNS,
067: OPNAME));
068: boi.getOperationInfo().getOutput().getMessagePartByIndex(0)
069: .setIndex(0);
070: soapMessage.getExchange().put(BindingOperationInfo.class, boi);
071:
072: control.reset();
073: Service service = control.createMock(Service.class);
074: JAXBDataBinding dataBinding = new JAXBDataBinding(
075: MyComplexStruct.class);
076: service.getDataBinding();
077: EasyMock.expectLastCall().andReturn(dataBinding).anyTimes();
078: service.getServiceInfos();
079: List<ServiceInfo> list = Arrays.asList(si);
080: EasyMock.expectLastCall().andReturn(list).anyTimes();
081:
082: soapMessage.getExchange().put(Service.class, service);
083: soapMessage.getExchange().put(
084: Message.SCHEMA_VALIDATION_ENABLED, Boolean.FALSE);
085: control.replay();
086:
087: MyComplexStruct mcs = new MyComplexStruct();
088: mcs.setElem1("elem1");
089: mcs.setElem2("elem2");
090: mcs.setElem3(45);
091: MessageContentsList param = new MessageContentsList();
092: param.add(mcs);
093: soapMessage.setContent(List.class, param);
094: }
095:
096: @After
097: public void tearDown() throws Exception {
098: baos.close();
099: }
100:
101: @Test
102: public void testWriteOutbound() throws Exception {
103: RPCOutInterceptor interceptor = new RPCOutInterceptor();
104:
105: soapMessage.setContent(XMLStreamWriter.class, XMLOutputFactory
106: .newInstance().createXMLStreamWriter(baos));
107:
108: soapMessage.put(Message.REQUESTOR_ROLE, Boolean.TRUE);
109:
110: interceptor.handleMessage(soapMessage);
111: assertNull(soapMessage.getContent(Exception.class));
112: soapMessage.getContent(XMLStreamWriter.class).flush();
113: baos.flush();
114:
115: ByteArrayInputStream bais = new ByteArrayInputStream(baos
116: .toByteArray());
117: XMLStreamReader xr = StaxUtils.createXMLStreamReader(bais);
118: DepthXMLStreamReader reader = new DepthXMLStreamReader(xr);
119: StaxUtils.toNextElement(reader);
120: assertEquals(new QName("http://apache.org/hello_world_rpclit",
121: "sendReceiveData"), reader.getName());
122:
123: StaxUtils.nextEvent(reader);
124: StaxUtils.toNextElement(reader);
125: assertEquals(new QName(null, "in"), reader.getName());
126:
127: StaxUtils.toNextText(reader);
128: assertEquals("elem1", reader.getText());
129: }
130:
131: @Test
132: public void testWriteInbound() throws Exception {
133: RPCOutInterceptor interceptor = new RPCOutInterceptor();
134: soapMessage.setContent(XMLStreamWriter.class, XMLOutputFactory
135: .newInstance().createXMLStreamWriter(baos));
136:
137: interceptor.handleMessage(soapMessage);
138: assertNull(soapMessage.getContent(Exception.class));
139: soapMessage.getContent(XMLStreamWriter.class).flush();
140: baos.flush();
141:
142: ByteArrayInputStream bais = new ByteArrayInputStream(baos
143: .toByteArray());
144: XMLStreamReader xr = StaxUtils.createXMLStreamReader(bais);
145: DepthXMLStreamReader reader = new DepthXMLStreamReader(xr);
146: StaxUtils.toNextElement(reader);
147: assertEquals(new QName("http://apache.org/hello_world_rpclit",
148: "sendReceiveDataResponse"), reader.getName());
149:
150: StaxUtils.nextEvent(reader);
151: StaxUtils.toNextElement(reader);
152:
153: assertEquals(new QName(null, "out"), reader.getName());
154:
155: StaxUtils.nextEvent(reader);
156: StaxUtils.toNextElement(reader);
157:
158: assertEquals(new QName(
159: "http://apache.org/hello_world_rpclit/types", "elem1"),
160: reader.getName());
161:
162: StaxUtils.nextEvent(reader);
163: StaxUtils.toNextText(reader);
164: assertEquals("elem1", reader.getText());
165: }
166:
167: }
|