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