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.activemq.jndi;
19  
20  import junit.framework.TestCase;
21  
22  import javax.jms.ConnectionFactory;
23  import javax.jms.Destination;
24  import javax.naming.Binding;
25  import javax.naming.Context;
26  import javax.naming.NamingEnumeration;
27  import javax.naming.NamingException;
28  import javax.naming.spi.InitialContextFactory;
29  import java.util.Hashtable;
30  
31  /***
32   * @version $Revision: 1.2 $
33   */
34  public class ActiveMQInitialContextFactoryTest extends TestCase {
35  
36      protected Hashtable environment = new Hashtable();
37      protected Context context;
38  
39      public void testConnectionFactoriesArePresent() throws NamingException {
40          Object connectionFactory = context.lookup("ConnectionFactory");
41  
42          assertTrue("Should have created a ConnectionFactory but got: " + connectionFactory, connectionFactory instanceof ConnectionFactory);
43      }
44  
45      public void testDestinationsArePresent() throws NamingException {
46          Object destinations = context.lookup("destinations");
47  
48          assertTrue("Should have created a destinations context but got: " + destinations, destinations instanceof Context);
49  
50          Context destContext = (Context) destinations;
51          NamingEnumeration iter = destContext.listBindings("");
52          while (iter.hasMore()) {
53              Binding binding = (Binding) iter.next();
54              System.out.println("Found key: " + binding.getName());
55              assertBinding(binding);
56          }
57      }
58  
59      protected void assertBinding(Binding binding) throws NamingException {
60          Object object = binding.getObject();
61          assertTrue("Should have got a child context but got: " + object, object instanceof Context);
62  
63          Context childContext = (Context) object;
64          NamingEnumeration iter = childContext.listBindings("");
65          while (iter.hasMore()) {
66              Binding destinationBinding = (Binding) iter.next();
67              System.out.println("Found destination: " + destinationBinding.getName());
68              Object destination = destinationBinding.getObject();
69              assertTrue("Should have a Destination but got: " + destination, destination instanceof Destination);
70          }
71      }
72  
73  
74      protected void setUp() throws Exception {
75          super.setUp();
76  
77          environment.put("useEmbeddedBroker", "true");
78          environment.put("brokerURL", "vm://localhost");
79  
80          InitialContextFactory factory = new ActiveMQInitialContextFactory();
81          context = factory.getInitialContext(environment);
82          assertTrue("No context created", context != null);
83      }
84  }