1   /*
2    * $Id: JndiContainerContextTestCase.java 7976 2007-08-21 14:26:13Z dirk.olmes $
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  
11  package org.mule.impl.container;
12  
13  import org.mule.impl.jndi.MuleInitialContextFactory;
14  import org.mule.tck.model.AbstractContainerContextTestCase;
15  import org.mule.tck.testmodels.fruit.Apple;
16  import org.mule.tck.testmodels.fruit.Banana;
17  import org.mule.tck.testmodels.fruit.FruitBowl;
18  import org.mule.umo.UMODescriptor;
19  import org.mule.umo.manager.UMOContainerContext;
20  
21  import java.util.Collections;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import javax.naming.Context;
26  
27  /**
28   * @author <a href="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
29   * @version $Revision: 7976 $
30   */
31  public class JndiContainerContextTestCase extends AbstractContainerContextTestCase
32  {
33      JndiContainerContext context;
34  
35      /*
36       * (non-Javadoc)
37       * 
38       * @see org.mule.tck.model.AbstractComponentResolverTestCase#getConfiguredResolver()
39       */
40      public UMOContainerContext getContainerContext()
41      {
42          return context;
43      }
44  
45      /*
46       * (non-Javadoc)
47       * 
48       * @see junit.framework.TestCase#setUp()
49       */
50      protected void doSetUp() throws Exception
51      {
52          context = new JndiContainerContext();
53          Map env = new HashMap();
54          env.put(Context.INITIAL_CONTEXT_FACTORY, MuleInitialContextFactory.class.getName());
55          context.setEnvironment(env);
56          context.initialise();
57          Context ic = context.getContext();
58          ic.bind(FruitBowl.class.getName(), new FruitBowl(new Apple(), new Banana()));
59          ic.bind(Apple.class.getName(), new Apple());
60      }
61  
62      public void testExternalUMOReference() throws Exception
63      {
64          UMOContainerContext ctx = getContainerContext();
65          assertNotNull(ctx);
66  
67          UMODescriptor descriptor = getTestDescriptor("fruit Bowl", "org.mule.tck.testmodels.fruit.FruitBowl");
68          FruitBowl fruitBowl = (FruitBowl)ctx.getComponent(descriptor.getImplementation());
69  
70          assertNotNull(fruitBowl);
71      }
72  
73      /**
74       * If no 'environment' is specified, shall use the default jndi config via 'new
75       * InitialContext()'.
76       */
77      public void testDefaultInitialContext() throws Exception
78      {
79          Context icEnv = context.getContext();
80          assertNotNull(icEnv);
81  
82          // reset initial context
83          context.setEnvironment(null);
84          context.setContext(null);
85          context.initialise();
86          Context icDefault = context.getContext();
87          assertNotNull(icDefault);
88          assertNotSame(icEnv, icDefault);
89  
90          // reset initial context (same, but empty map)
91          context.setEnvironment(Collections.EMPTY_MAP);
92          context.setContext(null);
93          context.initialise();
94          icDefault = context.getContext();
95          assertNotNull(icDefault);
96  
97          assertNotSame(icEnv, icDefault);
98      }
99  }