1   /*
2    * $Id: HiveMindContextTestCase.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.extras.hivemind;
12  
13  import org.mule.tck.model.AbstractContainerContextTestCase;
14  import org.mule.tck.testmodels.fruit.Apple;
15  import org.mule.tck.testmodels.fruit.FruitBowl;
16  import org.mule.umo.manager.ObjectNotFoundException;
17  import org.mule.umo.manager.UMOContainerContext;
18  
19  /**
20   * Test case for a context container whose backend is an HiveMind Registry
21   */
22  public class HiveMindContextTestCase extends AbstractContainerContextTestCase
23  {
24      HiveMindContext context;
25  
26      protected void doSetUp() throws Exception
27      {
28          context = new HiveMindContext();
29          context.initialise();
30      }
31  
32      /*
33       * (non-Javadoc)
34       * 
35       * @see org.mule.tck.model.AbstractComponentResolverTestCase#getConfiguredResolver()
36       */
37      public UMOContainerContext getContainerContext()
38      {
39          return context;
40      }
41  
42      /**
43       * Container should never be null
44       * 
45       * @throws Exception
46       */
47      public void testContainerNotNull() throws Exception
48      {
49          assertNotNull(getContainerContext());
50      }
51  
52      /**
53       * Registry should never be null
54       */
55      public void testRegistryNotNull()
56      {
57          assertNotNull(context.getRegistry());
58      }
59  
60      /**
61       * Test getting an object before initializing the Registry
62       */
63      public void testIllegalState()
64      {
65          try
66          {
67              HiveMindContext ctx = new HiveMindContext();
68              ctx.getComponent(new Object());
69              fail();
70          }
71          catch (IllegalStateException ise)
72          {
73              // Expected
74          }
75          catch (ObjectNotFoundException obfe)
76          {
77              fail("Should throw a IllegalStateExeption instead of ObjectNotFoundException");
78          }
79      }
80  
81      /**
82       * When an object is not in the Registry
83       */
84      public void testObjectNotFound()
85      {
86          try
87          {
88              context.getComponent(new String("Not present").getClass());
89              fail("Should have thrown ObjectNotFoundException");
90          }
91          catch (ObjectNotFoundException onfe)
92          {
93              // Expeced
94          }
95      }
96  
97      /**
98       * When an object is not in the Registry caused by a key not recognized
99       */
100     public void testObjectNotFoundByKeyType()
101     {
102         try
103         {
104             context.getComponent(new FruitBowl());
105             fail("Should have thrown ObjectNotFoundException");
106         }
107         catch (ObjectNotFoundException onfe)
108         {
109             // Expeced
110         }
111     }
112 
113     /**
114      * Shouldn't be possibile to get a component after disposing the container
115      * 
116      * @throws Exception
117      */
118     public void testDispose() throws Exception
119     {
120         HiveMindContext context = new HiveMindContext();
121         context.initialise();
122         context.dispose();
123 
124         try
125         {
126             context.getComponent(Class.class);
127             fail("Shouldn't be possibile to get a component after disposing the container");
128         }
129         catch (NullPointerException npe)
130         {
131             // Expected
132         }
133     }
134 
135     /**
136      * Test the real Registry built to serve the correct services
137      * 
138      * @throws Exception
139      */
140     public void testFruitBowl() throws Exception
141     {
142         FruitBowl result = null;
143         try
144         {
145             result = (FruitBowl)context.getComponent(FruitBowl.class.getName());
146             assertNotNull("Component FruitBwol should exist in container", result);
147             Apple apple = result.getApple();
148             assertNotNull("Component Apple should be in FruitBowl", apple);
149         }
150         catch (ObjectNotFoundException e)
151         {
152             fail("Component should exist in the container");
153         }
154     }
155 
156 }