1   /*
2    * $Id: JmsReconnectionFunctionalMule1720TestCase.java 12222 2008-07-01 20:09:45Z aguenther $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  package org.mule.transport.jms.issues;
11  
12  import org.mule.api.MuleEventContext;
13  import org.mule.component.DefaultJavaComponent;
14  import org.mule.transport.jms.JmsConnector;
15  import org.mule.tck.functional.EventCallback;
16  import org.mule.tck.functional.FunctionalTestComponent;
17  import org.mule.transport.jms.integration.AbstractJmsFunctionalTestCase;
18  import org.mule.util.concurrent.Latch;
19  
20  import javax.jms.JMSException;
21  
22  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
23  
24  public class JmsReconnectionFunctionalMule1720TestCase extends AbstractJmsFunctionalTestCase
25  {
26      private long RECONNECTION_TIMEOUT = 5000;
27      private Latch componentCallbackLatch;
28  
29      protected String getConfigResources()
30      {
31          return "providers/activemq/jms-reconnection-strategy.xml";
32      }
33      
34  	public void testReconnectionAfterConnectionFailure() throws Exception {
35  	    componentCallbackLatch = new Latch();
36  	    EventCallback callback = new EventCallback()
37          {
38              public void eventReceived(MuleEventContext context, Object component) throws Exception
39              {
40                  componentCallbackLatch.countDown();
41              }
42          };
43          DefaultJavaComponent defaultComponent = (DefaultJavaComponent) muleContext.getRegistry().lookupService("InputPart").getComponent();
44          FunctionalTestComponent testComponent = (FunctionalTestComponent) defaultComponent.getObjectFactory().getInstance();
45          testComponent.setEventCallback(callback);
46          
47          send(super.scenarioNoTx);
48          assertTrue(componentCallbackLatch.await(LOCK_WAIT, TimeUnit.MILLISECONDS));
49          
50          componentCallbackLatch = new Latch();
51          simulateConnectionFailure();
52          Thread.sleep(RECONNECTION_TIMEOUT);
53  
54          send(super.scenarioNoTx);
55          assertTrue(componentCallbackLatch.await(LOCK_WAIT, TimeUnit.MILLISECONDS));
56  	}
57  	
58  	private void simulateConnectionFailure() throws Exception
59  	{
60          JmsConnector connector = (JmsConnector) muleContext.getRegistry().lookupConnector("jmsConnector");
61          connector.getConnection().getExceptionListener().onException(new JMSException("fake disconnect!"));
62  	}
63  }