View Javadoc

1   /*
2    * $Id: ApplicationContextsTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
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.config.spring;
12  
13  import org.mule.api.MuleContext;
14  import org.mule.api.config.ConfigurationBuilder;
15  import org.mule.context.DefaultMuleContextFactory;
16  import org.mule.tck.AbstractMuleTestCase;
17  import org.mule.tck.testmodels.fruit.Orange;
18  
19  import org.springframework.beans.factory.NoSuchBeanDefinitionException;
20  import org.springframework.context.ApplicationContext;
21  import org.springframework.context.support.ClassPathXmlApplicationContext;
22  
23  public class ApplicationContextsTestCase extends AbstractMuleTestCase
24  {
25      public void testSanity() throws Exception
26      {
27          ApplicationContext appContext = new ClassPathXmlApplicationContext("application-context.xml");
28          
29          Object orange = appContext.getBean("orange");
30          assertNotNull(orange);
31          assertTrue(orange instanceof Orange);
32          
33          try
34          {
35              appContext.getBean("plum");
36              fail("Bean should not have been found");
37          }
38          catch (NoSuchBeanDefinitionException e)
39          {
40              // expected
41          }
42      }
43  
44      /** 
45       * Test that an existing appContext can be added to Mule's internal Registries 
46       */
47      public void testSpringConfigurationBuilder() throws Exception
48      {
49          MuleContext muleContext = new DefaultMuleContextFactory().createMuleContext();
50          
51          ApplicationContext appContext = new ClassPathXmlApplicationContext("application-context.xml");
52          ConfigurationBuilder builder = new SpringConfigurationBuilder(appContext);
53          builder.configure(muleContext); 
54  
55          muleContext.start();
56          
57          Object orange = muleContext.getRegistry().lookupObject("orange");
58          assertNotNull(orange);
59          assertTrue(orange instanceof Orange);
60          assertEquals("Pirulo", ((Orange) orange).getBrand());
61      }
62  
63      /** 
64       * Test that the same bean from the 2nd appContext will have precedence over the 1st appContext 
65       */
66      public void testSpringConfigurationBuilderPrecedence() throws Exception
67      {
68          MuleContext muleContext = new DefaultMuleContextFactory().createMuleContext();
69          
70          ApplicationContext appContext = new ClassPathXmlApplicationContext("application-context.xml");
71          ConfigurationBuilder builder = new SpringConfigurationBuilder(appContext);
72          builder.configure(muleContext); 
73  
74          appContext = new ClassPathXmlApplicationContext("application-context-2.xml");
75          builder = new SpringConfigurationBuilder(appContext);
76          builder.configure(muleContext); 
77  
78          muleContext.start();
79          
80          Object orange = muleContext.getRegistry().lookupObject("orange");
81          assertNotNull(orange);
82          assertTrue(orange instanceof Orange);
83          assertEquals("Tropicana", ((Orange) orange).getBrand());
84      }
85  
86      public void testSpringConfigurationBuilderBackwardsPrecedence() throws Exception
87      {
88          MuleContext muleContext = new DefaultMuleContextFactory().createMuleContext();
89          
90          ApplicationContext appContext = new ClassPathXmlApplicationContext("application-context-2.xml");
91          ConfigurationBuilder builder = new SpringConfigurationBuilder(appContext);
92          builder.configure(muleContext); 
93  
94          appContext = new ClassPathXmlApplicationContext("application-context.xml");
95          builder = new SpringConfigurationBuilder(appContext);
96          builder.configure(muleContext); 
97  
98          muleContext.start();
99          
100         Object orange = muleContext.getRegistry().lookupObject("orange");
101         assertNotNull(orange);
102         assertTrue(orange instanceof Orange);
103         assertEquals("Pirulo", ((Orange) orange).getBrand());
104     }
105 
106     /** 
107      * Test that the same bean from the TransientRegistry will have precedence over the 1st appContext 
108      */
109     public void testTransientRegistryPrecedence() throws Exception
110     {
111         MuleContext muleContext = new DefaultMuleContextFactory().createMuleContext();
112         
113         muleContext.getRegistry().registerObject("orange", new Orange(12, 5.5, "Tutti Frutti"));
114         
115         ApplicationContext appContext = new ClassPathXmlApplicationContext("application-context.xml");
116         ConfigurationBuilder builder = new SpringConfigurationBuilder(appContext);
117         builder.configure(muleContext); 
118 
119         muleContext.start();
120         
121         Object orange = muleContext.getRegistry().lookupObject("orange");
122         assertNotNull(orange);
123         assertTrue(orange instanceof Orange);
124         assertEquals("Tutti Frutti", ((Orange) orange).getBrand());
125     }
126 
127     /** 
128      * Test that an existing appContext can be used as a parent AppContext for Mule 
129      */
130     public void testParentContext() throws Exception
131     {
132         MuleContext muleContext = new DefaultMuleContextFactory().createMuleContext();
133 
134         ApplicationContext appContext = new ClassPathXmlApplicationContext("application-context.xml");
135 
136         SpringXmlConfigurationBuilder builder = new SpringXmlConfigurationBuilder("mule-config.xml");
137         builder.setParentContext(appContext);
138         builder.configure(muleContext);
139 
140         muleContext.start();
141 
142         Object orange = muleContext.getRegistry().lookupObject("orange");
143         assertNotNull(orange);
144         assertTrue(orange instanceof Orange);
145         assertEquals("Pirulo", ((Orange) orange).getBrand());
146     }
147 
148     /**
149      * Test the most common approach: Create the Spring config + Mule config in a single AppContext.
150      */
151     public void testAppContextTogetherWithMuleConfig() throws Exception
152     {
153         MuleContext muleContext = new DefaultMuleContextFactory().createMuleContext();
154 
155         SpringXmlConfigurationBuilder builder = new SpringXmlConfigurationBuilder("application-context.xml, mule-config.xml");
156         builder.configure(muleContext);
157 
158         muleContext.start();
159 
160         Object orange = muleContext.getRegistry().lookupObject("orange");
161         assertNotNull(orange);
162         assertTrue(orange instanceof Orange);
163         assertEquals("Pirulo", ((Orange) orange).getBrand());
164     }
165 }