View Javadoc

1   /*
2    * $Id: DefaultMuleContextFactoryTestCase.java 22377 2011-07-11 12:41:42Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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;
12  
13  import org.mule.api.MuleContext;
14  import org.mule.api.config.ConfigurationBuilder;
15  import org.mule.api.config.ConfigurationException;
16  import org.mule.api.config.MuleConfiguration;
17  import org.mule.api.config.MuleProperties;
18  import org.mule.api.context.WorkManager;
19  import org.mule.api.lifecycle.InitialisationException;
20  import org.mule.api.model.Model;
21  import org.mule.config.DefaultMuleConfiguration;
22  import org.mule.config.builders.AbstractConfigurationBuilder;
23  import org.mule.config.builders.SimpleConfigurationBuilder;
24  import org.mule.context.DefaultMuleContextBuilder;
25  import org.mule.context.DefaultMuleContextFactory;
26  import org.mule.context.notification.ServerNotificationManager;
27  import org.mule.lifecycle.MuleContextLifecycleManager;
28  import org.mule.model.seda.SedaModel;
29  import org.mule.tck.junit4.AbstractMuleTestCase;
30  import org.mule.tck.testmodels.fruit.Banana;
31  
32  import java.util.ArrayList;
33  import java.util.List;
34  import java.util.Properties;
35  
36  import javax.resource.spi.work.WorkListener;
37  
38  import org.junit.Test;
39  
40  import static junit.framework.Assert.assertNotNull;
41  import static org.junit.Assert.assertEquals;
42  import static org.junit.Assert.assertNull;
43  import static org.junit.Assert.assertTrue;
44  
45  public class DefaultMuleContextFactoryTestCase extends AbstractMuleTestCase
46  {
47      private DefaultMuleContextFactory muleContextFactory = new DefaultMuleContextFactory();
48      private static String TEST_STRING_KEY = "test";
49      private static String TEST_STRING_VALUE = "test_value";
50      private static String TEST_STRING_KEY2 = "test2";
51      private static String TEST_STRING_VALUE2 = "test_value2";
52      private static String TEST_OBJECT_NAME = "testObject";
53      private static String TEST_MODEL_NAME = "testModel";
54  
55      @Test
56      public void testCreateMuleContext() throws InitialisationException, ConfigurationException
57      {
58          MuleContext context = muleContextFactory.createMuleContext();
59  
60          assertMuleContextConfiguration(context);
61          assertDefaults(context);
62      }
63  
64      @Test
65      public void testCreateMuleContextConfigurationBuilder() throws InitialisationException, ConfigurationException
66      {
67          MuleContext context = muleContextFactory.createMuleContext(new TestConfigurationBuilder());
68  
69          assertMuleContextConfiguration(context);
70          assertConfigurationBuilder1Objects(context);
71          assertNoDefaults(context);
72      }
73  
74      @Test
75      public void testCreateMuleContextListMuleContextBuilder() throws InitialisationException, ConfigurationException
76      {
77          List<ConfigurationBuilder> configBuilders = new ArrayList<ConfigurationBuilder>();
78          configBuilders.add(new TestConfigurationBuilder());
79          configBuilders.add(new TestConfigurationBuilder2());
80  
81          TestMuleContextBuilder muleContextBuilder = new TestMuleContextBuilder();
82          MuleContext context = muleContextFactory.createMuleContext(configBuilders, muleContextBuilder);
83  
84          assertCustomMuleContext(context);
85          assertConfigurationBuilder1Objects(context);
86          assertConfigurationBuilder2Objects(context);
87          assertNoDefaults(context);
88      }
89  
90      @Test
91      public void testCreateMuleContextMuleContextBuilder() throws InitialisationException, ConfigurationException
92      {
93          TestMuleContextBuilder muleContextBuilder = new TestMuleContextBuilder();
94          MuleContext context = muleContextFactory.createMuleContext(new SimpleConfigurationBuilder(null), muleContextBuilder);
95  
96          assertCustomMuleContext(context);
97          assertNoDefaults(context);
98      }
99  
100     @Test
101     public void testCreateMuleContextConfigurationBuilderMuleContextBuilder() throws InitialisationException, ConfigurationException
102     {
103         TestMuleContextBuilder muleContextBuilder = new TestMuleContextBuilder();
104         MuleContext context = muleContextFactory.createMuleContext(new TestConfigurationBuilder2(),
105             muleContextBuilder);
106 
107         assertCustomMuleContext(context);
108         assertConfigurationBuilder2Objects(context);
109         assertNoDefaults(context);
110     }
111 
112     @Test
113     public void testCreateMuleContextString() throws InitialisationException, ConfigurationException
114     {
115         MuleContext context = null;
116         try
117         {
118             context = muleContextFactory.createMuleContext("log4j.properties");
119         }
120         catch (ConfigurationException e)
121         {
122             assertEquals(
123                 "No suitable configuration builder for resource \"[ConfigResource{resourceName='log4j.properties'}]\" found.  "
124                                 + "Check you have configuration module on your classpath and are using correct file extension. "
125                                 + "(org.mule.api.config.ConfigurationException)", e.getMessage());
126         }
127         assertNull(context);
128     }
129 
130     @Test
131     public void testCreateMuleContextStringProperties() throws InitialisationException, ConfigurationException
132     {
133         Properties properties = new Properties();
134         properties.put("testKey1", "testValue1");
135         properties.put("testKey2", "testValue2");
136 
137         MuleContext context = null;
138         try
139         {
140             context = muleContextFactory.createMuleContext("log4j.properties", properties);
141         }
142         catch (ConfigurationException e)
143         {
144             assertEquals(
145                 "No suitable configuration builder for resource \"[ConfigResource{resourceName='log4j.properties'}]\" found.  "
146                                 + "Check you have configuration module on your classpath and are using correct file extension. "
147                                 + "(org.mule.api.config.ConfigurationException)", e.getMessage());
148         }
149 
150         assertNull(context);
151     }
152 
153     @Test
154     public void testCreateMuleContextConfigurationBuilderProperties() throws InitialisationException, ConfigurationException
155     {
156         Properties properties = new Properties();
157         properties.put("testKey3", "testValue3");
158         properties.put("testKey4", "testValue4");
159 
160         MuleContext context = muleContextFactory.createMuleContext(new TestConfigurationBuilder(), properties);
161 
162         assertMuleContextConfiguration(context);
163         assertConfigurationBuilder1Objects(context);
164 
165         assertEquals("testValue3", context.getRegistry().lookupObject("testKey3"));
166         assertEquals("testValue4", context.getRegistry().lookupObject("testKey4"));
167 
168         assertNoDefaults(context);
169     }
170 
171     private void assertDefaults(MuleContext context)
172     {
173         // Asert existance of defauts in registry
174         assertNotNull(context.getRegistry().lookupObject(MuleProperties.OBJECT_QUEUE_MANAGER));
175         assertNotNull(context.getRegistry().lookupObject(MuleProperties.OBJECT_SECURITY_MANAGER));
176         assertNotNull(context.getRegistry().lookupObject(MuleProperties.OBJECT_STORE_DEFAULT_IN_MEMORY_NAME));
177         assertNotNull(context.getRegistry().lookupObject(MuleProperties.OBJECT_SYSTEM_MODEL));
178         assertNotNull(context.getRegistry().lookupObject(MuleProperties.OBJECT_MULE_ENDPOINT_FACTORY));
179         assertNotNull(context.getRegistry().lookupObject(MuleProperties.OBJECT_MULE_SIMPLE_REGISTRY_BOOTSTRAP));
180     }
181 
182     private void assertNoDefaults(MuleContext context)
183     {
184         // Asert non-existance of defauts in registry
185         assertNull(context.getRegistry().lookupObject(MuleProperties.OBJECT_QUEUE_MANAGER));
186         assertNull(context.getRegistry().lookupObject(MuleProperties.OBJECT_SECURITY_MANAGER));
187         assertNull(context.getRegistry().lookupObject(MuleProperties.OBJECT_STORE_DEFAULT_IN_MEMORY_NAME));
188         assertNull(context.getRegistry().lookupObject(MuleProperties.OBJECT_SYSTEM_MODEL));
189         assertNull(context.getRegistry().lookupObject(MuleProperties.OBJECT_MULE_ENDPOINT_FACTORY));
190         assertNull(context.getRegistry().lookupObject(MuleProperties.OBJECT_MULE_SIMPLE_REGISTRY_BOOTSTRAP));
191     }
192 
193     private void assertMuleContextConfiguration(MuleContext context)
194     {
195         assertNotNull(context);
196         assertEquals(DefaultMuleContext.class, context.getClass());
197         assertTrue(context.isInitialised());
198         assertNotNull(context.getConfiguration());
199         assertEquals(DefaultMuleConfiguration.class, context.getConfiguration().getClass());
200         assertNotNull(context.getLifecycleManager().getClass());
201         assertNotNull(context.getNotificationManager());
202         assertNotNull(context.getWorkManager());
203     }
204 
205     private void assertCustomMuleContext(MuleContext context)
206     {
207         assertNotNull(context);
208         assertEquals(TestMuleContext.class, context.getClass());
209         assertTrue(context.isInitialised());
210         assertNotNull(context.getConfiguration());
211         assertNotNull(context.getLifecycleManager().getClass());
212         assertNotNull(context.getNotificationManager());
213         assertNotNull(context.getWorkManager());
214     }
215 
216     private void assertConfigurationBuilder1Objects(MuleContext context)
217     {
218         // Test Registry contents for existance of object configured by
219         // TestConfigurationBuilder
220         assertEquals(TEST_STRING_VALUE, context.getRegistry().lookupObject(TEST_STRING_KEY));
221 
222         Object obj = context.getRegistry().lookupObject(TEST_OBJECT_NAME);
223         assertNotNull(obj);
224         assertEquals(Banana.class, obj.getClass());
225     }
226 
227     private void assertConfigurationBuilder2Objects(MuleContext context)
228     {
229         // Test Registry contents for existance of object configured by
230         // TestConfigurationBuilder2
231         assertEquals(TEST_STRING_VALUE2, context.getRegistry().lookupObject(TEST_STRING_KEY2));
232         assertNotNull(context.getRegistry().lookupModel(TEST_MODEL_NAME));
233         assertEquals(TEST_MODEL_NAME, context.getRegistry().lookupModel(TEST_MODEL_NAME).getName());
234     }
235 
236     static class TestConfigurationBuilder extends AbstractConfigurationBuilder
237     {
238         @Override
239         protected void doConfigure(MuleContext context) throws Exception
240         {
241             context.getRegistry().registerObject(TEST_STRING_KEY, TEST_STRING_VALUE);
242             context.getRegistry().registerObject(TEST_OBJECT_NAME, new Banana());
243         }
244     }
245 
246     static class TestConfigurationBuilder2 extends AbstractConfigurationBuilder
247     {
248         @Override
249         protected void doConfigure(MuleContext context) throws Exception
250         {
251             context.getRegistry().registerObject(TEST_STRING_KEY2, TEST_STRING_VALUE2);
252             Model testModel = new SedaModel();
253             testModel.setName(TEST_MODEL_NAME);
254             context.getRegistry().registerModel(testModel);
255         }
256     }
257 
258     static class TestMuleContextBuilder extends DefaultMuleContextBuilder
259     {
260         @Override
261         public MuleContext buildMuleContext()
262         {
263             MuleContextLifecycleManager manager = getLifecycleManager();
264 
265             MuleContext context = new TestMuleContext(getMuleConfiguration(), getWorkManager(),
266                 getWorkListener(), manager, getNotificationManager());
267             manager.setMuleContext(context);
268             return context;
269         }
270     }
271 
272     static class TestMuleContext extends DefaultMuleContext
273     {
274         public TestMuleContext(MuleConfiguration config, WorkManager workManager,
275             WorkListener workListener, MuleContextLifecycleManager lifecycleManager,
276             ServerNotificationManager notificationManager)
277         {
278             super(config, workManager, workListener, lifecycleManager, notificationManager);
279         }
280     }
281 
282     static class TestMuleConfiguration extends DefaultMuleConfiguration
283     {
284         // just a skeleton
285     }
286 }