KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > spring > SpringTest


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.apache.activemq.spring;
20
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23
24 import junit.framework.TestCase;
25
26 import org.apache.activemq.broker.BrokerService;
27 import org.springframework.context.support.AbstractApplicationContext;
28 import org.springframework.context.support.ClassPathXmlApplicationContext;
29
30 public class SpringTest extends TestCase {
31     
32     private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
33             .getLog(SpringTest.class);
34
35     protected AbstractApplicationContext context;
36     protected SpringConsumer consumer;
37     protected SpringProducer producer;
38
39     /**
40      * Make sure that brokers are being pooled properly.
41      *
42      * @throws Exception
43      */

44     public void testSenderWithSpringXmlEmbeddedPooledBrokerConfiguredViaXml() throws Exception JavaDoc {
45         String JavaDoc config = "spring-embedded-pooled.xml";
46         
47         Thread.currentThread().setContextClassLoader(SpringTest.class.getClassLoader());
48         ClassPathXmlApplicationContext context1 = new ClassPathXmlApplicationContext(config);
49
50         BrokerService bs1 = (BrokerService) context1.getBean("broker1");
51         assertNotNull(bs1);
52         BrokerService bs2 = (BrokerService) context1.getBean("broker2");
53         assertNotNull(bs1);
54         
55         // It should be the same broker;
56
assertEquals(bs1, bs2);
57
58         // Even if we load up another context, it should still be the same broker.
59
ClassPathXmlApplicationContext context2 = new ClassPathXmlApplicationContext(config);
60
61         BrokerService bs3 = (BrokerService) context2.getBean("broker1");
62         assertNotNull(bs3);
63         BrokerService bs4 = (BrokerService) context2.getBean("broker2");
64         assertNotNull(bs4);
65
66         // It should be the same broker;
67
assertEquals(bs1, bs3);
68         assertEquals(bs1, bs4);
69         
70         // And it should be started.
71
assertTrue(bs1.isStarted());
72         
73         // should still be started asfter the 2nd context closes.
74
context2.close();
75         assertTrue(bs1.isStarted());
76         
77         // Should stop once all contexts close.
78
context1.close();
79         assertFalse(bs1.isStarted());
80
81     }
82
83     /**
84      * Uses ActiveMQConnectionFactory to create the connection context.
85      * Configuration file is /resources/spring.xml
86      *
87      * @throws Exception
88      */

89     public void testSenderWithSpringXml() throws Exception JavaDoc {
90         String JavaDoc config = "spring.xml";
91         assertSenderConfig(config);
92     }
93
94     /**
95      * Spring configured test that uses ActiveMQConnectionFactory for
96      * connection context and ActiveMQQueue for destination. Configuration
97      * file is /resources/spring-queue.xml.
98      *
99      * @throws Exception
100      */

101     public void testSenderWithSpringXmlAndQueue() throws Exception JavaDoc {
102         String JavaDoc config = "spring-queue.xml";
103         assertSenderConfig(config);
104     }
105
106     /**
107      * Spring configured test that uses JNDI. Configuration file is
108      * /resources/spring-jndi.xml.
109      *
110      * @throws Exception
111      */

112     public void testSenderWithSpringXmlUsingJNDI() throws Exception JavaDoc {
113         String JavaDoc config = "spring-jndi.xml";
114         assertSenderConfig(config);
115     }
116     
117     /**
118      * Spring configured test where in the connection context is set to use
119      * an embedded broker. Configuration file is /resources/spring-embedded.xml
120      * and /resources/activemq.xml.
121      *
122      * @throws Exception
123      */

124     public void testSenderWithSpringXmlEmbeddedBrokerConfiguredViaXml() throws Exception JavaDoc {
125         String JavaDoc config = "spring-embedded.xml";
126         assertSenderConfig(config);
127     }
128     
129     /**
130      * Broken: http://issues.apache.org/activemq/browse/AMQ-1002
131      * Needs to be fixed.
132      *
133      * @throws Exception
134      */

135     public void XtestSenderWithSpringXmlUsingSpring2NamespacesWithEmbeddedBrokerConfiguredViaXml() throws Exception JavaDoc {
136         String JavaDoc config = "spring-embedded-xbean.xml";
137         assertSenderConfig(config);
138     }
139     
140     /**
141      * assert method that is used by all the test method to send and receive messages
142      * based on each spring configuration.
143      *
144      * @param config
145      * @throws Exception
146      */

147     protected void assertSenderConfig(String JavaDoc config) throws Exception JavaDoc {
148         Thread.currentThread().setContextClassLoader(SpringTest.class.getClassLoader());
149         context = new ClassPathXmlApplicationContext(config);
150
151         consumer = (SpringConsumer) context.getBean("consumer");
152         assertTrue("Found a valid consumer", consumer != null);
153
154         consumer.start();
155         
156         // Wait a little to drain any left over messages.
157
Thread.sleep(1000);
158         consumer.flushMessages();
159
160         producer = (SpringProducer) context.getBean("producer");
161         assertTrue("Found a valid producer", producer != null);
162
163         producer.start();
164
165         // lets sleep a little to give the JMS time to dispatch stuff
166
consumer.waitForMessagesToArrive(producer.getMessageCount());
167
168         // now lets check that the consumer has received some messages
169
List JavaDoc messages = consumer.flushMessages();
170         log.info("Consumer has received messages....");
171         for (Iterator JavaDoc iter = messages.iterator(); iter.hasNext();) {
172             Object JavaDoc message = iter.next();
173             log.info("Received: " + message);
174         }
175
176         assertEquals("Message count", producer.getMessageCount(), messages.size());
177     }
178
179     /**
180      * Clean up method.
181      *
182      * @throws Exception
183      */

184     protected void tearDown() throws Exception JavaDoc {
185         if (consumer != null) {
186             consumer.stop();
187         }
188         if (producer != null) {
189             producer.stop();
190         }
191
192         if (context != null) {
193             context.destroy();
194         }
195     }
196
197 }
198
Popular Tags