001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.servicemix.jms;
018:
019: import java.util.List;
020:
021: import javax.jbi.messaging.MessageExchange;
022: import javax.jbi.messaging.NormalizedMessage;
023: import javax.jms.QueueConnection;
024: import javax.jms.QueueSender;
025: import javax.jms.QueueSession;
026: import javax.jms.Session;
027: import javax.jms.TextMessage;
028:
029: import junit.framework.TestCase;
030:
031: import org.apache.activemq.ActiveMQConnectionFactory;
032: import org.apache.activemq.broker.BrokerService;
033: import org.apache.activemq.command.ActiveMQQueue;
034: import org.apache.activemq.xbean.BrokerFactoryBean;
035: import org.apache.servicemix.jbi.container.JBIContainer;
036: import org.apache.servicemix.jbi.jaxp.SourceTransformer;
037: import org.apache.servicemix.jbi.messaging.MessageExchangeSupport;
038: import org.apache.servicemix.tck.ReceiverComponent;
039: import org.springframework.core.io.ClassPathResource;
040:
041: public class JmsMarshalerTest extends TestCase {
042:
043: protected JBIContainer container;
044: protected BrokerService broker;
045: protected ActiveMQConnectionFactory connectionFactory;
046: protected ActiveMQQueue queue;
047:
048: protected void setUp() throws Exception {
049: BrokerFactoryBean bfb = new BrokerFactoryBean(
050: new ClassPathResource(
051: "org/apache/servicemix/jms/activemq.xml"));
052: bfb.afterPropertiesSet();
053: broker = bfb.getBroker();
054: broker.start();
055:
056: container = new JBIContainer();
057: container.setUseMBeanServer(true);
058: container.setCreateMBeanServer(true);
059: container.setMonitorInstallationDirectory(false);
060: container.init();
061: container.start();
062:
063: connectionFactory = new ActiveMQConnectionFactory(
064: "vm://localhost");
065: queue = new ActiveMQQueue("foo.queue");
066: }
067:
068: protected void tearDown() throws Exception {
069: if (container != null) {
070: container.shutDown();
071: }
072: if (broker != null) {
073: broker.stop();
074: }
075: }
076:
077: public void testMarshalTextMessage() throws Exception {
078: JmsComponent jms = new JmsComponent();
079: jms.getConfiguration().setConnectionFactory(connectionFactory);
080: JmsEndpoint ep = new JmsEndpoint();
081: ep.setService(ReceiverComponent.SERVICE);
082: ep.setEndpoint("jms");
083: ep.setTargetService(ReceiverComponent.SERVICE);
084: ep.setTargetEndpoint(ReceiverComponent.ENDPOINT);
085: ep.setRole(MessageExchange.Role.CONSUMER);
086: ep.setDestinationStyle(AbstractJmsProcessor.STYLE_QUEUE);
087: ep.setDestination(queue);
088: ep.setDefaultMep(MessageExchangeSupport.IN_ONLY);
089: ep.setMarshaler(new DefaultJmsMarshaler(ep));
090: jms.setEndpoints(new JmsEndpoint[] { ep });
091: container.activateComponent(jms, "servicemix-jms");
092:
093: ReceiverComponent receiver = new ReceiverComponent();
094: container.activateComponent(receiver, "receiver");
095:
096: QueueConnection qConn = connectionFactory
097: .createQueueConnection();
098: QueueSession qSess = qConn.createQueueSession(false,
099: Session.AUTO_ACKNOWLEDGE);
100: QueueSender qSender = qSess.createSender(queue);
101: TextMessage message = qSess
102: .createTextMessage("<?xml version=\"1.0\" encoding=\"UTF-8\"?><hello>world</hello>");
103: qSender.send(message);
104:
105: receiver.getMessageList().assertMessagesReceived(1);
106: List msgs = receiver.getMessageList().flushMessages();
107: NormalizedMessage msg = (NormalizedMessage) msgs.get(0);
108: assertEquals("Messages match", message.getText(),
109: new SourceTransformer().contentToString(msg));
110:
111: // Wait for DONE status
112: Thread.sleep(50);
113: }
114:
115: }
|