1 18 package org.apache.activemq.jndi; 19 20 import junit.framework.TestCase; 21 22 import javax.naming.NamingException ; 23 import javax.naming.Binding ; 24 import javax.naming.Context ; 25 import javax.naming.NamingEnumeration ; 26 import javax.naming.spi.InitialContextFactory ; 27 import javax.jms.ConnectionFactory ; 28 import javax.jms.Destination ; 29 import javax.jms.JMSException ; 30 import java.util.Hashtable ; 31 32 import org.apache.activemq.ActiveMQConnectionFactory; 33 import org.apache.activemq.jndi.ActiveMQInitialContextFactory; 34 35 38 public abstract class JNDITestSupport extends TestCase { 39 40 private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory 41 .getLog(JNDITestSupport.class); 42 43 protected Hashtable environment = new Hashtable (); 44 protected Context context; 45 46 protected void assertConnectionFactoryPresent(String lookupName) throws NamingException { 47 Object connectionFactory = context.lookup(lookupName); 48 49 assertTrue("Should have created a ConnectionFactory for key: " + lookupName 50 + " but got: " + connectionFactory, connectionFactory instanceof ConnectionFactory ); 51 } 52 53 protected void assertBinding(Binding binding) throws NamingException { 54 Object object = binding.getObject(); 55 assertTrue("Should have got a child context but got: " + object, object instanceof Context ); 56 57 Context childContext = (Context ) object; 58 NamingEnumeration iter = childContext.listBindings(""); 59 while (iter.hasMore()) { 60 Binding destinationBinding = (Binding ) iter.next(); 61 log.info("Found destination: " + destinationBinding.getName()); 62 Object destination = destinationBinding.getObject(); 63 assertTrue("Should have a Destination but got: " + destination, destination instanceof Destination ); 64 } 65 } 66 67 protected void setUp() throws Exception { 68 super.setUp(); 69 70 configureEnvironment(); 71 72 InitialContextFactory factory = new ActiveMQInitialContextFactory(); 73 context = factory.getInitialContext(environment); 74 assertTrue("No context created", context != null); 75 } 76 77 82 protected void tearDown() throws NamingException , JMSException { 83 NamingEnumeration iter = context.listBindings(""); 84 while (iter.hasMore()) { 85 Binding binding = (Binding ) iter.next(); 86 Object connFactory = binding.getObject(); 87 if (connFactory instanceof ActiveMQConnectionFactory) { 88 } 90 } 91 } 92 93 protected void configureEnvironment() { 94 environment.put("brokerURL", "vm://localhost"); 95 } 96 97 protected void assertDestinationExists(String name) throws NamingException { 98 Object object = context.lookup(name); 99 assertTrue("Should have received a Destination for name: " + name + " but instead found: " + object, 100 object instanceof Destination ); 101 } 102 } 103 | Popular Tags |