KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > JmsTestSupport


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 package org.apache.activemq;
19
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.net.URI JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28 import javax.jms.Connection JavaDoc;
29 import javax.jms.ConnectionFactory JavaDoc;
30 import javax.jms.Destination JavaDoc;
31 import javax.jms.JMSException JavaDoc;
32 import javax.jms.MessageConsumer JavaDoc;
33 import javax.jms.MessageProducer JavaDoc;
34 import javax.jms.Session JavaDoc;
35
36 import org.apache.activemq.broker.BrokerFactory;
37 import org.apache.activemq.broker.BrokerService;
38 import org.apache.activemq.command.ActiveMQDestination;
39
40 /**
41  * Test cases used to test the JMS message comsumer.
42  *
43  * @version $Revision$
44  */

45 public class JmsTestSupport extends CombinationTestSupport {
46
47     public String JavaDoc userName;
48     public String JavaDoc password;
49
50     protected ConnectionFactory JavaDoc factory;
51     protected ActiveMQConnection connection;
52     protected BrokerService broker;
53     
54     protected List JavaDoc connections = Collections.synchronizedList(new ArrayList JavaDoc());
55
56     // /////////////////////////////////////////////////////////////////
57
//
58
// Test support methods.
59
//
60
// /////////////////////////////////////////////////////////////////
61
protected ActiveMQDestination createDestination(Session JavaDoc session, byte type) throws JMSException JavaDoc {
62         switch (type) {
63         case ActiveMQDestination.QUEUE_TYPE:
64             return (ActiveMQDestination) session.createQueue("TEST");
65         case ActiveMQDestination.TOPIC_TYPE:
66             return (ActiveMQDestination) session.createTopic("TEST");
67         case ActiveMQDestination.TEMP_QUEUE_TYPE:
68             return (ActiveMQDestination) session.createTemporaryQueue();
69         case ActiveMQDestination.TEMP_TOPIC_TYPE:
70             return (ActiveMQDestination) session.createTemporaryTopic();
71         }
72         throw new IllegalArgumentException JavaDoc("type: " + type);
73     }
74
75     protected void sendMessages(Destination JavaDoc destination, int count) throws Exception JavaDoc {
76         ConnectionFactory JavaDoc factory = createConnectionFactory();
77         Connection JavaDoc connection = factory.createConnection();
78         connection.start();
79         sendMessages(connection, destination, count);
80         connection.close();
81     }
82
83     protected void sendMessages(Connection JavaDoc connection, Destination JavaDoc destination, int count) throws JMSException JavaDoc {
84         Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
85         sendMessages(session, destination, count);
86         session.close();
87     }
88
89     protected void sendMessages(Session JavaDoc session, Destination JavaDoc destination, int count) throws JMSException JavaDoc {
90         MessageProducer JavaDoc producer = session.createProducer(destination);
91         for (int i = 0; i < count; i++) {
92             producer.send(session.createTextMessage(""+i));
93         }
94         producer.close();
95     }
96
97     protected ConnectionFactory JavaDoc createConnectionFactory() throws Exception JavaDoc {
98         return new ActiveMQConnectionFactory("vm://localhost");
99     }
100
101     protected BrokerService createBroker() throws Exception JavaDoc {
102         return BrokerFactory.createBroker(new URI JavaDoc("broker://()/localhost?persistent=false"));
103     }
104
105     protected void setUp() throws Exception JavaDoc {
106         super.setUp();
107
108         if(System.getProperty("basedir")==null){
109             File JavaDoc file=new File JavaDoc(".");
110             System.setProperty("basedir",file.getAbsolutePath());
111         }
112
113         broker = createBroker();
114         broker.start();
115         factory = createConnectionFactory();
116         connection = (ActiveMQConnection) factory.createConnection(userName, password);
117         connections.add(connection);
118     }
119
120     protected void tearDown() throws Exception JavaDoc {
121         for (Iterator JavaDoc iter = connections.iterator(); iter.hasNext();) {
122             Connection JavaDoc conn= (Connection JavaDoc) iter.next();
123             try {
124                 conn.close();
125             } catch (Throwable JavaDoc e) {
126             }
127         }
128         broker.stop();
129         super.tearDown();
130     }
131     
132     protected void safeClose(Connection JavaDoc c) {
133         try {
134             c.close();
135         } catch (Throwable JavaDoc e) {
136         }
137     }
138
139     protected void safeClose(Session JavaDoc s) {
140         try {
141             s.close();
142         } catch (Throwable JavaDoc e) {
143         }
144     }
145
146     protected void safeClose(MessageConsumer JavaDoc c) {
147         try {
148             c.close();
149         } catch (Throwable JavaDoc e) {
150         }
151     }
152
153     protected void safeClose(MessageProducer JavaDoc p) {
154         try {
155             p.close();
156         } catch (Throwable JavaDoc e) {
157         }
158     }
159     
160     protected void profilerPause(String JavaDoc prompt) throws IOException JavaDoc {
161         if( System.getProperty("profiler")!=null ) {
162             pause(prompt);
163         }
164     }
165     
166     protected void pause(String JavaDoc prompt) throws IOException JavaDoc {
167         System.out.println();
168         System.out.println(prompt+"> Press enter to continue: ");
169         while( System.in.read()!='\n' ) {
170         }
171     }
172
173 }
174
Popular Tags