KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > jndi > JNDITestSupport


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.jndi;
19
20 import junit.framework.TestCase;
21
22 import javax.naming.NamingException JavaDoc;
23 import javax.naming.Binding JavaDoc;
24 import javax.naming.Context JavaDoc;
25 import javax.naming.NamingEnumeration JavaDoc;
26 import javax.naming.spi.InitialContextFactory JavaDoc;
27 import javax.jms.ConnectionFactory JavaDoc;
28 import javax.jms.Destination JavaDoc;
29 import javax.jms.JMSException JavaDoc;
30 import java.util.Hashtable JavaDoc;
31
32 import org.apache.activemq.ActiveMQConnectionFactory;
33 import org.apache.activemq.jndi.ActiveMQInitialContextFactory;
34
35 /**
36  * @version $Revision: 1.3 $
37  */

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 JavaDoc environment = new Hashtable JavaDoc();
44     protected Context JavaDoc context;
45
46     protected void assertConnectionFactoryPresent(String JavaDoc lookupName) throws NamingException JavaDoc {
47         Object JavaDoc connectionFactory = context.lookup(lookupName);
48
49         assertTrue("Should have created a ConnectionFactory for key: " + lookupName
50                 + " but got: " + connectionFactory, connectionFactory instanceof ConnectionFactory JavaDoc);
51     }
52
53     protected void assertBinding(Binding JavaDoc binding) throws NamingException JavaDoc {
54         Object JavaDoc object = binding.getObject();
55         assertTrue("Should have got a child context but got: " + object, object instanceof Context JavaDoc);
56
57         Context JavaDoc childContext = (Context JavaDoc) object;
58         NamingEnumeration JavaDoc iter = childContext.listBindings("");
59         while (iter.hasMore()) {
60             Binding JavaDoc destinationBinding = (Binding JavaDoc) iter.next();
61             log.info("Found destination: " + destinationBinding.getName());
62             Object JavaDoc destination = destinationBinding.getObject();
63             assertTrue("Should have a Destination but got: " + destination, destination instanceof Destination JavaDoc);
64         }
65     }
66
67     protected void setUp() throws Exception JavaDoc {
68         super.setUp();
69
70         configureEnvironment();
71
72         InitialContextFactory JavaDoc factory = new ActiveMQInitialContextFactory();
73         context = factory.getInitialContext(environment);
74         assertTrue("No context created", context != null);
75     }
76
77     /**
78      * Stops all existing ActiveMQConnectionFactory in Context.
79      *
80      * @throws javax.naming.NamingException
81      */

82     protected void tearDown() throws NamingException JavaDoc, JMSException JavaDoc {
83         NamingEnumeration JavaDoc iter = context.listBindings("");
84         while (iter.hasMore()) {
85             Binding JavaDoc binding = (Binding JavaDoc) iter.next();
86             Object JavaDoc connFactory = binding.getObject();
87             if (connFactory instanceof ActiveMQConnectionFactory) {
88                // ((ActiveMQConnectionFactory) connFactory).stop();
89
}
90         }
91     }
92
93     protected void configureEnvironment() {
94         environment.put("brokerURL", "vm://localhost");
95     }
96
97     protected void assertDestinationExists(String JavaDoc name) throws NamingException JavaDoc {
98         Object JavaDoc object = context.lookup(name);
99         assertTrue("Should have received a Destination for name: " + name + " but instead found: " + object,
100                 object instanceof Destination JavaDoc);
101     }
102 }
103
Popular Tags