1   /*** 
2    * 
3    * Copyright 2004 Protique Ltd
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.codehaus.activecluster;
19  
20  import org.codehaus.activecluster.impl.ActiveMQClusterFactory;
21  
22  import javax.jms.Destination;
23  import javax.jms.JMSException;
24  import javax.jms.Message;
25  import javax.jms.MessageConsumer;
26  import javax.jms.Topic;
27  
28  /***
29   * @version $Revision: 1.7 $
30   */
31  public abstract class ClusterTestSupport extends TestSupport {
32  
33      protected Cluster cluster;
34      protected StubMessageListener clusterListener = new StubMessageListener();
35      protected StubMessageListener inboxListener = new StubMessageListener();
36      private MessageConsumer clusterConsumer;
37      private MessageConsumer inboxConsumer;
38  
39  
40      protected void sendMessageToNode(Node node, String text) throws Exception {
41          Message message = cluster.createTextMessage(text);
42          cluster.send(node.getDestination(), message);
43      }
44  
45      protected void sendMessageToCluster(String text) throws Exception {
46          Message message = cluster.createTextMessage(text);
47          cluster.send(cluster.getDestination(), message);
48      }
49  
50      protected void subscribeToCluster() throws Exception {
51  
52          // listen to cluster messages
53          Topic clusterDestination = cluster.getDestination();
54          assertTrue("Local destination must not be null", clusterDestination != null);
55          clusterConsumer = cluster.createConsumer(clusterDestination);
56          clusterConsumer.setMessageListener(clusterListener);
57  
58          // listen to inbox messages (individual messages)
59          Destination localDestination = cluster.getLocalNode().getDestination();
60          assertTrue("Local destination must not be null", localDestination != null);
61  
62          System.out.println("Consuming from local destination: " + localDestination);
63          inboxConsumer = cluster.createConsumer(localDestination);
64          inboxConsumer.setMessageListener(inboxListener);
65      }
66  
67      protected void setUp() throws Exception {
68      }
69  
70      protected void tearDown() throws Exception {
71          if (cluster != null) {
72              cluster.stop();
73          }
74      }
75  }