1   /*
2    * $Id: HiveMindContextTestCase.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.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   * @author <a href="mailto:mlusetti@gmail.com">Massimo Lusetti</a>
23   * @version $Revision: 7976 $
24   */
25  public class HiveMindContextTestCase extends AbstractContainerContextTestCase
26  {
27      HiveMindContext context;
28  
29      protected void doSetUp() throws Exception
30      {
31          context = new HiveMindContext();
32          context.initialise();
33      }
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       * Container should never be null
47       * 
48       * @throws Exception
49       */
50      public void testContainerNotNull() throws Exception
51      {
52          assertNotNull(getContainerContext());
53      }
54  
55      /**
56       * Registry should never be null
57       */
58      public void testRegistryNotNull()
59      {
60          assertNotNull(context.getRegistry());
61      }
62  
63      /**
64       * Test getting an object before initializing the Registry
65       */
66      public void testIllegalState()
67      {
68          try
69          {
70              HiveMindContext ctx = new HiveMindContext();
71              ctx.getComponent(new Object());
72              fail();
73          }
74          catch (IllegalStateException ise)
75          {
76              // Expected
77          }
78          catch (ObjectNotFoundException obfe)
79          {
80              fail("Should throw a IllegalStateExeption instead of ObjectNotFoundException");
81          }
82      }
83  
84      /**
85       * When an object is not in the Registry
86       */
87      public void testObjectNotFound()
88      {
89          try
90          {
91              context.getComponent(new String("Not present").getClass());
92              fail("Should have thrown ObjectNotFoundException");
93          }
94          catch (ObjectNotFoundException onfe)
95          {
96              // Expeced
97          }
98      }
99  
100     /**
101      * When an object is not in the Registry caused by a key not recognized
102      */
103     public void testObjectNotFoundByKeyType()
104     {
105         try
106         {
107             context.getComponent(new FruitBowl());
108             fail("Should have thrown ObjectNotFoundException");
109         }
110         catch (ObjectNotFoundException onfe)
111         {
112             // Expeced
113         }
114     }
115 
116     /**
117      * Shouldn't be possibile to get a component after disposing the container
118      * 
119      * @throws Exception
120      */
121     public void testDispose() throws Exception
122     {
123         HiveMindContext context = new HiveMindContext();
124         context.initialise();
125         context.dispose();
126 
127         try
128         {
129             context.getComponent(Class.class);
130             fail("Shouldn't be possibile to get a component after disposing the container");
131         }
132         catch (NullPointerException npe)
133         {
134             // Expected
135         }
136     }
137 
138     /**
139      * Test the real Registry built to serve the correct services
140      * 
141      * @throws Exception
142      */
143     public void testFruitBowl() throws Exception
144     {
145         FruitBowl result = null;
146         try
147         {
148             result = (FruitBowl)context.getComponent(FruitBowl.class.getName());
149             assertNotNull("Component FruitBwol should exist in container", result);
150             Apple apple = result.getApple();
151             assertNotNull("Component Apple should be in FruitBowl", apple);
152         }
153         catch (ObjectNotFoundException e)
154         {
155             fail("Component should exist in the container");
156         }
157     }
158 
159 }