1   /***
2    *
3    * Copyright 2005 LogicBlaze, Inc.
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   *
17   **/
18  package org.jencks;
19  
20  import junit.framework.TestCase;
21  import org.activemq.spring.TestingConsumer;
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.springframework.context.ConfigurableApplicationContext;
25  import org.springframework.context.support.ClassPathXmlApplicationContext;
26  import org.springframework.jms.JmsException;
27  import org.springframework.jms.core.JmsTemplate;
28  
29  import javax.jms.ConnectionFactory;
30  import java.util.List;
31  
32  /***
33   * @author srt
34   * @version $Id: SpringTemplateAndJCAWithEmbeddedBrokerTest.java,v 1.1.1.1 2005/08/16 14:35:52 jstrachan Exp $
35   */
36  public class SpringTemplateAndJCAWithEmbeddedBrokerTest extends TestCase {
37      private static Log logger = LogFactory.getLog(SpringTemplateAndJCAWithEmbeddedBrokerTest.class);
38  
39      private ConfigurableApplicationContext applicationContext;
40      private ConnectionFactory connectionFactory;
41      protected int messageCount = 10;
42  
43      public void testRun() {
44          for (int i = 0; i < messageCount; i++) {
45              String text = "Message " + i;
46              logger.info("Sending " + text);
47              sendMessage(text);
48          }
49  
50          TestingConsumer consumer = (TestingConsumer) applicationContext.getBean("consumerBean");
51          consumer.waitForMessagesToArrive(messageCount);
52  
53          List list = consumer.flushMessages();
54          assertEquals("Message count: " + list, messageCount, list.size());
55  
56          System.out.println("Received all: " + list.size() + " messages");
57      }
58  
59  
60      public void sendMessage(String text) throws JmsException {
61          JmsTemplate template = new JmsTemplate(connectionFactory);
62          template.setPubSubDomain(true);
63          template.convertAndSend("myTopic", text);
64      }
65  
66      protected void setUp() throws Exception {
67          applicationContext = new ClassPathXmlApplicationContext(getSpringConfig());
68          connectionFactory = (ConnectionFactory) applicationContext.getBean("jmsFactory");
69  
70          assertTrue("Should have found a non-null connection factory", connectionFactory != null);
71      }
72  
73      protected String getSpringConfig() {
74          return "org/jencks/spring-topic-embedded-broker.xml";
75      }
76  
77      protected void tearDown() throws Exception {
78          if (applicationContext != null) {
79              System.out.println("Closing the application context");
80              applicationContext.close();
81          }
82      }
83  }