Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/activemq/advisories/ConsumerAdvisorTest.java


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  
19  package org.activemq.advisories;
20  
21  import javax.jms.Connection;
22  import javax.jms.Destination;
23  import javax.jms.MessageConsumer;
24  import javax.jms.Session;
25  import junit.framework.TestCase;
26  import org.activemq.ActiveMQConnectionFactory;
27  import org.activemq.message.ConsumerInfo;
28  import EDU.oswego.cs.dl.util.concurrent.SynchronizedBoolean;
29  /**
30   * A helper class for listening for MessageConsumer advisories
31   */
32  
33  
34  
35  /**
36   *
37   * ConsumerAdvisorTest
38   */
39  public class ConsumerAdvisorTest extends TestCase implements ConsumerAdvisoryEventListener{
40      
41      private Connection connection;
42      private Session session;
43      private Destination destination;
44      private String destinationName = "foo.bar";
45      private SynchronizedBoolean started;
46      
47      
48      protected void setUp() throws Exception{
49          started = new SynchronizedBoolean(false);
50          ActiveMQConnectionFactory fac = new ActiveMQConnectionFactory("vm://localhost");
51          connection = fac.createConnection();
52          session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
53          destination = session.createTopic(destinationName);
54          connection.start();
55      }
56      
57      protected void tearDown() throws  Exception{
58          connection.close();
59      }
60      public void testAdvisories() throws Exception{
61          ConsumerAdvisor test = new ConsumerAdvisor(connection,destination);
62          test.addListener(this);
63          test.start();
64          MessageConsumer consumer = session.createConsumer(destination);
65          synchronized(started){
66              if (!started.get()){
67                  started.wait(2000);
68              }
69          }
70          assertTrue(started.get());
71          session.close();
72          synchronized(started){
73              if (started.get()){
74                  started.wait(2000);
75              }
76          }
77          assertFalse(started.get());
78      }
79  
80      public void onEvent(ConsumerAdvisoryEvent event) {
81          ConsumerInfo info = event.getInfo();
82          started.set(info.isStarted());
83          synchronized(started){
84              started.notify();
85          }
86          
87      }
88      
89  }