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