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.bpmscript.jbi.tasklist;
018:
019: import java.util.Enumeration;
020: import java.util.Properties;
021:
022: import javax.jms.Connection;
023: import javax.jms.JMSException;
024: import javax.jms.MessageConsumer;
025: import javax.jms.MessageProducer;
026: import javax.jms.ObjectMessage;
027: import javax.jms.Queue;
028: import javax.jms.QueueBrowser;
029: import javax.jms.Session;
030:
031: import junit.framework.TestCase;
032:
033: import org.apache.activemq.ActiveMQConnectionFactory;
034: import org.apache.activemq.pool.PooledConnectionFactory;
035: import org.apache.activemq.xbean.BrokerFactoryBean;
036: import org.apache.log4j.PropertyConfigurator;
037: import org.bpmscript.jms.JmsTemplate;
038: import org.bpmscript.jms.SessionCallback;
039: import org.springframework.core.io.ClassPathResource;
040:
041: public class JmsTemplateTest extends TestCase {
042:
043: private BrokerFactoryBean bean;
044:
045: protected void setUp() throws Exception {
046: super .setUp();
047: Properties properties = new Properties();
048: properties.put("log4j.rootCategory", "INFO, stdout");
049: properties.put("log4j.appender.stdout",
050: "org.apache.log4j.ConsoleAppender");
051: properties.put("log4j.appender.stdout.layout",
052: "org.apache.log4j.PatternLayout");
053: PropertyConfigurator.configure(properties);
054:
055: bean = new BrokerFactoryBean();
056: bean.setConfig(new ClassPathResource("inmemoryactivemq.xml"));
057: bean.afterPropertiesSet();
058: }
059:
060: protected void tearDown() throws Exception {
061: super .tearDown();
062: bean.destroy();
063: }
064:
065: public void testSomething() throws Exception {
066:
067: ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
068: Connection connection = connectionFactory.createConnection();
069: connection.start();
070:
071: JmsTemplate template = new JmsTemplate(
072: new PooledConnectionFactory(connectionFactory));
073: template.afterPropertiesSet();
074:
075: template.execute(new SessionCallback() {
076:
077: public Object doInJms(Session session) throws JMSException {
078: ObjectMessage message = session
079: .createObjectMessage("Hello World!");
080: Queue queue = session
081: .createQueue("org.bpmscript.jbi.tasklist.JmsTest");
082: MessageProducer producer = session
083: .createProducer(queue);
084: message.setStringProperty("id", "something");
085: producer.send(message);
086: return null;
087: }
088:
089: });
090:
091: template.execute(new SessionCallback() {
092:
093: public Object doInJms(Session session) throws JMSException {
094: Queue queue = session
095: .createQueue("org.bpmscript.jbi.tasklist.JmsTest");
096: QueueBrowser taskBrowser = session.createBrowser(queue,
097: "id = 'something'");
098: Enumeration enumeration = taskBrowser.getEnumeration();
099: assertTrue(enumeration.hasMoreElements());
100: return null;
101: }
102:
103: });
104:
105: template.execute(new SessionCallback() {
106:
107: public Object doInJms(Session session) throws JMSException {
108: Queue queue = session
109: .createQueue("org.bpmscript.jbi.tasklist.JmsTest");
110: QueueBrowser browser = session.createBrowser(queue);
111: Enumeration enumeration = browser.getEnumeration();
112: assertTrue(enumeration.hasMoreElements());
113: return null;
114: }
115:
116: });
117:
118: template.execute(new SessionCallback() {
119:
120: public Object doInJms(Session session) throws JMSException {
121: Queue queue = session
122: .createQueue("org.bpmscript.jbi.tasklist.JmsTest");
123: MessageConsumer consumer = session
124: .createConsumer(queue);
125: while (consumer.receiveNoWait() != null) {
126: }
127: return null;
128: }
129:
130: });
131:
132: }
133:
134: }
|