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.transport.jbi;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.io.OutputStream;
024: import java.lang.reflect.Member;
025: import java.lang.reflect.Method;
026: import java.util.logging.Logger;
027:
028: import javax.jbi.messaging.InOut;
029: import javax.jbi.messaging.MessageExchangeFactory;
030: import javax.jbi.messaging.NormalizedMessage;
031: import javax.xml.stream.XMLStreamReader;
032: import javax.xml.transform.Source;
033: import javax.xml.transform.stream.StreamSource;
034:
035: import org.apache.cxf.message.Exchange;
036: import org.apache.cxf.message.ExchangeImpl;
037: import org.apache.cxf.message.Message;
038: import org.apache.cxf.message.MessageImpl;
039: import org.apache.cxf.service.model.BindingMessageInfo;
040: import org.apache.cxf.service.model.BindingOperationInfo;
041: import org.apache.cxf.staxutils.StaxUtils;
042: import org.easymock.classextension.EasyMock;
043: import org.junit.Test;
044:
045: public class JBIConduitTest extends AbstractJBITest {
046: static final Logger LOG = Logger.getLogger(JBIDestinationTest.class
047: .getName());
048:
049: @Test
050: public void testPrepare() throws Exception {
051: LOG.info("test prepare");
052: JBIConduit conduit = setupJBIConduit(false, false);
053: Message message = new MessageImpl();
054: try {
055: conduit.prepare(message);
056: } catch (Exception ex) {
057: ex.printStackTrace();
058: }
059: assertNotNull(message.getContent(OutputStream.class));
060: assertTrue(message.getContent(OutputStream.class) instanceof JBIConduitOutputStream);
061: }
062:
063: @Test
064: public void testSendOut() throws Exception {
065: LOG.info("test send");
066: JBIConduit conduit = setupJBIConduit(true, false);
067: Message message = new MessageImpl();
068: Member method = control.createMock(Member.class);
069: message.put(Method.class.getName(), method);
070:
071: EasyMock
072: .expect(method.getDeclaringClass())
073: .andStubReturn(
074: (Class<?>) org.apache.hello_world_soap_http.Greeter.class);
075:
076: Exchange exchange = new ExchangeImpl();
077: exchange.setOneWay(false);
078: message.setExchange(exchange);
079: exchange.setInMessage(message);
080: BindingOperationInfo boi = control
081: .createMock(BindingOperationInfo.class);
082: BindingMessageInfo bmi = control
083: .createMock(BindingMessageInfo.class);
084: EasyMock.expect(boi.getOutput()).andReturn(bmi);
085: exchange.put(BindingOperationInfo.class, boi);
086: MessageExchangeFactory factory = control
087: .createMock(MessageExchangeFactory.class);
088: EasyMock.expect(channel.createExchangeFactoryForService(null))
089: .andReturn(factory);
090: InOut xchg = control.createMock(InOut.class);
091: EasyMock.expect(factory.createInOutExchange()).andReturn(xchg);
092: NormalizedMessage inMsg = control
093: .createMock(NormalizedMessage.class);
094: EasyMock.expect(xchg.createMessage()).andReturn(inMsg);
095: NormalizedMessage outMsg = control
096: .createMock(NormalizedMessage.class);
097: EasyMock.expect(xchg.getOutMessage()).andReturn(outMsg);
098:
099: Source source = new StreamSource(new ByteArrayInputStream(
100: "<message>TestHelloWorld</message>".getBytes()));
101: EasyMock.expect(outMsg.getContent()).andReturn(source);
102: control.replay();
103: try {
104: conduit.prepare(message);
105: } catch (IOException ex) {
106: assertFalse("JMSConduit can't perpare to send out message",
107: false);
108: ex.printStackTrace();
109: }
110: OutputStream os = message.getContent(OutputStream.class);
111: assertTrue("The OutputStream should not be null ", os != null);
112: os.write("HelloWorld".getBytes());
113: os.close();
114: InputStream is = inMessage.getContent(InputStream.class);
115: assertNotNull(is);
116: XMLStreamReader reader = StaxUtils.createXMLStreamReader(is,
117: null);
118: assertNotNull(reader);
119: reader.nextTag();
120:
121: String reponse = reader.getElementText();
122: assertEquals("The reponse date should be equals", reponse,
123: "TestHelloWorld");
124: }
125: }
|