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.xml.interceptor;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.ByteArrayOutputStream;
022: import java.io.InputStream;
023: import java.io.OutputStream;
024: import java.util.List;
025:
026: import javax.xml.namespace.QName;
027: import javax.xml.stream.XMLStreamReader;
028: import javax.xml.stream.XMLStreamWriter;
029:
030: import org.apache.cxf.message.Message;
031: import org.apache.cxf.message.MessageContentsList;
032: import org.apache.cxf.service.model.BindingInfo;
033: import org.apache.cxf.service.model.BindingOperationInfo;
034: import org.apache.cxf.staxutils.DepthXMLStreamReader;
035: import org.apache.cxf.staxutils.StaxUtils;
036: import org.apache.hello_world_xml_http.bare.types.MyComplexStructType;
037: import org.apache.hello_world_xml_http.wrapped.types.GreetMe;
038: import org.junit.Before;
039: import org.junit.Test;
040:
041: public class XMLMessageOutInterceptorTest extends TestBase {
042:
043: static String bareNs = "http://apache.org/hello_world_xml_http/bare";
044:
045: static String wrapNs = "http://apache.org/hello_world_xml_http/wrapped";
046:
047: static String bareNsType = "http://apache.org/hello_world_xml_http/bare/types";
048:
049: static String wrapNsType = "http://apache.org/hello_world_xml_http/wrapped/types";
050:
051: OutputStream outputStream;
052:
053: XMLStreamWriter writer;
054:
055: XMLMessageOutInterceptor out = new XMLMessageOutInterceptor(
056: "phase1");
057:
058: MessageContentsList params = new MessageContentsList();
059:
060: QName bareMyComplexStructTypeQName = new QName(bareNs, "in");
061:
062: QName bareMyComplexStructQName = new QName(bareNsType,
063: "myComplexStruct");
064:
065: QName bareRequestTypeQName = new QName(bareNsType, "requestType");
066:
067: QName wrapGreetMeQName = new QName(wrapNsType, "greetMe");
068:
069: QName wrapRequestTypeQName = new QName(wrapNsType, "requestType");
070:
071: @Before
072: public void setUp() throws Exception {
073: super .setUp();
074: chain.add(out);
075: prepareMessage(params);
076: }
077:
078: @Test
079: public void testBareOutSingle() throws Exception {
080:
081: MyComplexStructType myComplexStruct = new MyComplexStructType();
082: myComplexStruct.setElem1("elem1");
083: myComplexStruct.setElem2("elem2");
084: myComplexStruct.setElem3(45);
085: params.add(myComplexStruct);
086:
087: common("/wsdl/hello_world_xml_bare.wsdl", new QName(bareNs,
088: "XMLPort"), MyComplexStructType.class);
089:
090: BindingInfo bi = super .serviceInfo.getBinding(new QName(bareNs,
091: "Greeter_XMLBinding"));
092: BindingOperationInfo boi = bi.getOperation(new QName(bareNs,
093: "sendReceiveData"));
094: xmlMessage.getExchange().put(BindingOperationInfo.class, boi);
095:
096: out.handleMessage(xmlMessage);
097:
098: XMLStreamReader reader = getXMLReader();
099: DepthXMLStreamReader dxr = new DepthXMLStreamReader(reader);
100: StaxUtils.nextEvent(dxr);
101: StaxUtils.toNextElement(dxr);
102:
103: assertEquals(bareMyComplexStructTypeQName.getLocalPart(), dxr
104: .getLocalName());
105: StaxUtils.toNextElement(dxr);
106: StaxUtils.toNextText(dxr);
107: assertEquals(myComplexStruct.getElem1(), dxr.getText());
108: }
109:
110: @Test
111: public void testBareOutMultiWithRoot() throws Exception {
112:
113: MyComplexStructType myComplexStruct = new MyComplexStructType();
114: myComplexStruct.setElem1("elem1");
115: myComplexStruct.setElem2("elem2");
116: myComplexStruct.setElem3(45);
117: params.add("tli");
118: params.add(myComplexStruct);
119:
120: common("/wsdl/hello_world_xml_bare.wsdl", new QName(bareNs,
121: "XMLPort"), MyComplexStructType.class);
122:
123: BindingInfo bi = super .serviceInfo.getBinding(new QName(bareNs,
124: "Greeter_XMLBinding"));
125: BindingOperationInfo boi = bi.getOperation(new QName(bareNs,
126: "testMultiParamPart"));
127: xmlMessage.getExchange().put(BindingOperationInfo.class, boi);
128:
129: out.handleMessage(xmlMessage);
130:
131: XMLStreamReader reader = getXMLReader();
132: DepthXMLStreamReader dxr = new DepthXMLStreamReader(reader);
133: StaxUtils.nextEvent(dxr);
134: StaxUtils.toNextElement(dxr);
135:
136: assertEquals(bareNs, dxr.getNamespaceURI());
137: assertEquals("multiParamRootReq", dxr.getLocalName());
138: StaxUtils.nextEvent(dxr);
139: StaxUtils.toNextElement(dxr);
140:
141: assertEquals(bareRequestTypeQName, dxr.getName());
142: StaxUtils.nextEvent(dxr);
143: if (StaxUtils.toNextText(dxr)) {
144: assertEquals("tli", dxr.getText());
145: }
146:
147: boolean foundRequest = false;
148: while (true) {
149: StaxUtils.nextEvent(dxr);
150: StaxUtils.toNextElement(dxr);
151: QName requestType = new QName(dxr.getNamespaceURI(), dxr
152: .getLocalName());
153: if (requestType.equals(bareMyComplexStructQName)) {
154: foundRequest = true;
155: break;
156: }
157: }
158: assertEquals("found request type", true, foundRequest);
159: }
160:
161: @Test
162: public void testWrapOut() throws Exception {
163: GreetMe greetMe = new GreetMe();
164: greetMe.setRequestType("tli");
165: params.add(greetMe);
166: common("/wsdl/hello_world_xml_wrapped.wsdl", new QName(wrapNs,
167: "XMLPort"), GreetMe.class);
168:
169: BindingInfo bi = super .serviceInfo.getBinding(new QName(wrapNs,
170: "Greeter_XMLBinding"));
171: BindingOperationInfo boi = bi.getOperation(new QName(wrapNs,
172: "greetMe"));
173: xmlMessage.getExchange().put(BindingOperationInfo.class, boi);
174:
175: out.handleMessage(xmlMessage);
176:
177: XMLStreamReader reader = getXMLReader();
178: DepthXMLStreamReader dxr = new DepthXMLStreamReader(reader);
179: StaxUtils.nextEvent(dxr);
180: StaxUtils.toNextElement(dxr);
181: assertEquals(wrapGreetMeQName.getNamespaceURI(), dxr
182: .getNamespaceURI());
183: assertEquals(wrapGreetMeQName.getLocalPart(), dxr
184: .getLocalName());
185: StaxUtils.toNextElement(dxr);
186: StaxUtils.toNextText(dxr);
187: assertEquals(greetMe.getRequestType(), dxr.getText());
188: }
189:
190: private void prepareMessage(List paramsList) throws Exception {
191: outputStream = new ByteArrayOutputStream();
192: // all test case here use input message to do test,
193: // that means the out interceptor's role is Server-Out
194: xmlMessage.put(Message.REQUESTOR_ROLE, Boolean.TRUE);
195: xmlMessage.setContent(OutputStream.class, outputStream);
196: writer = StaxUtils.createXMLStreamWriter(outputStream);
197: xmlMessage.setContent(XMLStreamWriter.class, writer);
198: xmlMessage.setContent(List.class, paramsList);
199: }
200:
201: private XMLStreamReader getXMLReader() throws Exception {
202: ByteArrayOutputStream o = (ByteArrayOutputStream) xmlMessage
203: .getContent(OutputStream.class);
204: writer.flush();
205: InputStream in = new ByteArrayInputStream(o.toByteArray());
206: return StaxUtils.createXMLStreamReader(in);
207: }
208: }
|