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.jms;
019:
020: import java.io.IOException;
021: import java.io.OutputStream;
022: import java.net.URL;
023:
024: import javax.xml.namespace.QName;
025:
026: import org.apache.cxf.Bus;
027: import org.apache.cxf.BusFactory;
028: import org.apache.cxf.message.Exchange;
029: import org.apache.cxf.message.ExchangeImpl;
030: import org.apache.cxf.message.Message;
031: import org.apache.cxf.service.Service;
032: import org.apache.cxf.service.model.EndpointInfo;
033: import org.apache.cxf.transport.Conduit;
034: import org.apache.cxf.transport.MessageObserver;
035: import org.apache.cxf.ws.addressing.EndpointReferenceType;
036: import org.apache.cxf.wsdl11.WSDLServiceFactory;
037: import org.easymock.classextension.EasyMock;
038: import org.junit.After;
039: import org.junit.AfterClass;
040: import org.junit.Assert;
041: import org.junit.Before;
042:
043: public abstract class AbstractJMSTester extends Assert {
044: private static JMSBrokerSetup broker;
045:
046: protected Bus bus;
047: protected EndpointInfo endpointInfo;
048: protected EndpointReferenceType target;
049: protected MessageObserver observer;
050: protected Message inMessage;
051:
052: public static void startBroker(JMSBrokerSetup b) throws Exception {
053: assertNotNull(b);
054: broker = b;
055: broker.start();
056: }
057:
058: @AfterClass
059: public static void stopBroker() throws Exception {
060: broker.stop();
061: broker = null;
062: }
063:
064: @Before
065: public void setUp() {
066: BusFactory bf = BusFactory.newInstance();
067: bus = bf.createBus();
068: BusFactory.setDefaultBus(bus);
069: }
070:
071: @After
072: public void tearDown() {
073: bus.shutdown(true);
074: if (System.getProperty("cxf.config.file") != null) {
075: System.clearProperty("cxf.config.file");
076: }
077: }
078:
079: protected void setupServiceInfo(String ns, String wsdl,
080: String serviceName, String portName) {
081: URL wsdlUrl = getClass().getResource(wsdl);
082: assertNotNull(wsdlUrl);
083: WSDLServiceFactory factory = new WSDLServiceFactory(bus,
084: wsdlUrl, new QName(ns, serviceName));
085:
086: Service service = factory.create();
087: endpointInfo = service.getEndpointInfo(new QName(ns, portName));
088:
089: }
090:
091: protected void sendoutMessage(Conduit conduit, Message message,
092: Boolean isOneWay) throws IOException {
093:
094: Exchange exchange = new ExchangeImpl();
095: exchange.setOneWay(isOneWay);
096: message.setExchange(exchange);
097: exchange.setInMessage(message);
098: try {
099: conduit.prepare(message);
100: } catch (IOException ex) {
101: assertFalse("JMSConduit can't perpare to send out message",
102: false);
103: ex.printStackTrace();
104: }
105: OutputStream os = message.getContent(OutputStream.class);
106: assertTrue("The OutputStream should not be null ", os != null);
107: os.write("HelloWorld".getBytes());
108: os.close();
109: }
110:
111: protected JMSConduit setupJMSConduit(boolean send, boolean decoupled) {
112: if (decoupled) {
113: // setup the reference type
114: } else {
115: target = EasyMock.createMock(EndpointReferenceType.class);
116: }
117:
118: JMSConduit jmsConduit = new JMSConduit(bus, endpointInfo,
119: target);
120:
121: if (send) {
122: // setMessageObserver
123: observer = new MessageObserver() {
124: public void onMessage(Message m) {
125: inMessage = m;
126: }
127: };
128: jmsConduit.setMessageObserver(observer);
129: }
130:
131: return jmsConduit;
132: }
133:
134: }
|