KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > broker > 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.broker;
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.spring.SpringConsumer;
27 import org.apache.activemq.spring.SpringProducer;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.springframework.context.support.AbstractApplicationContext;
31 import org.springframework.context.support.ClassPathXmlApplicationContext;
32
33 public class SpringTest extends TestCase {
34     
35     private static final Log log = LogFactory.getLog(SpringTest.class);
36
37     protected AbstractApplicationContext context;
38     protected SpringConsumer consumer;
39     protected SpringProducer producer;
40
41     public void testSenderWithSpringXml() throws Exception JavaDoc {
42         assertSenderConfig("org/apache/activemq/broker/spring.xml");
43     }
44     /**
45      * assert method that is used by all the test method to send and receive messages
46      * based on each spring configuration.
47      *
48      * @param config
49      * @throws Exception
50      */

51     protected void assertSenderConfig(String JavaDoc config) throws Exception JavaDoc {
52         context = new ClassPathXmlApplicationContext(config);
53
54         consumer = (SpringConsumer) context.getBean("consumer");
55         assertTrue("Found a valid consumer", consumer != null);
56
57         consumer.start();
58
59         producer = (SpringProducer) context.getBean("producer");
60         assertTrue("Found a valid producer", producer != null);
61
62         consumer.flushMessages();
63         producer.start();
64
65         // lets sleep a little to give the JMS time to dispatch stuff
66
consumer.waitForMessagesToArrive(producer.getMessageCount());
67
68         // now lets check that the consumer has received some messages
69
List JavaDoc messages = consumer.flushMessages();
70         log.info("Consumer has received messages....");
71         for (Iterator JavaDoc iter = messages.iterator(); iter.hasNext();) {
72             Object JavaDoc message = iter.next();
73             log.info("Received: " + message);
74         }
75
76         assertEquals("Message count", producer.getMessageCount(), messages.size());
77     }
78
79     /**
80      * Clean up method.
81      *
82      * @throws Exception
83      */

84     protected void tearDown() throws Exception JavaDoc {
85         if (consumer != null) {
86             consumer.stop();
87         }
88         if (producer != null) {
89             producer.stop();
90         }
91
92         if (context != null) {
93             context.destroy();
94         }
95     }
96
97 }
98
Popular Tags