View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.config.spring;
8   
9   import org.mule.api.MuleContext;
10  import org.mule.api.config.ConfigurationBuilder;
11  import org.mule.context.DefaultMuleContextFactory;
12  import org.mule.tck.junit4.AbstractMuleTestCase;
13  import org.mule.tck.testmodels.fruit.Orange;
14  
15  import org.junit.Test;
16  import org.springframework.beans.factory.NoSuchBeanDefinitionException;
17  import org.springframework.context.ApplicationContext;
18  import org.springframework.context.support.ClassPathXmlApplicationContext;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertNotNull;
22  import static org.junit.Assert.assertTrue;
23  import static org.junit.Assert.fail;
24  
25  public class ApplicationContextsTestCase extends AbstractMuleTestCase
26  {
27  
28      @Test
29      public void testSanity() throws Exception
30      {
31          ApplicationContext appContext = new ClassPathXmlApplicationContext("application-context.xml");
32          
33          Object orange = appContext.getBean("orange");
34          assertNotNull(orange);
35          assertTrue(orange instanceof Orange);
36          
37          try
38          {
39              appContext.getBean("plum");
40              fail("Bean should not have been found");
41          }
42          catch (NoSuchBeanDefinitionException e)
43          {
44              // expected
45          }
46      }
47  
48      /** 
49       * Test that an existing appContext can be added to Mule's internal Registries 
50       */
51      @Test
52      public void testSpringConfigurationBuilder() throws Exception
53      {
54          MuleContext muleContext = new DefaultMuleContextFactory().createMuleContext();
55          
56          ApplicationContext appContext = new ClassPathXmlApplicationContext("application-context.xml");
57          ConfigurationBuilder builder = new SpringConfigurationBuilder(appContext);
58          builder.configure(muleContext); 
59  
60          muleContext.start();
61          
62          Object orange = muleContext.getRegistry().lookupObject("orange");
63          assertNotNull(orange);
64          assertTrue(orange instanceof Orange);
65          assertEquals("Pirulo", ((Orange) orange).getBrand());
66      }
67  
68      /** 
69       * Test that the same bean from the 2nd appContext will have precedence over the 1st appContext 
70       */
71      @Test
72      public void testSpringConfigurationBuilderPrecedence() throws Exception
73      {
74          MuleContext muleContext = new DefaultMuleContextFactory().createMuleContext();
75          
76          ApplicationContext appContext = new ClassPathXmlApplicationContext("application-context.xml");
77          ConfigurationBuilder builder = new SpringConfigurationBuilder(appContext);
78          builder.configure(muleContext); 
79  
80          appContext = new ClassPathXmlApplicationContext("application-context-2.xml");
81          builder = new SpringConfigurationBuilder(appContext);
82          builder.configure(muleContext); 
83  
84          muleContext.start();
85          
86          Object orange = muleContext.getRegistry().lookupObject("orange");
87          assertNotNull(orange);
88          assertTrue(orange instanceof Orange);
89          assertEquals("Tropicana", ((Orange) orange).getBrand());
90      }
91  
92      @Test
93      public void testSpringConfigurationBuilderBackwardsPrecedence() throws Exception
94      {
95          MuleContext muleContext = new DefaultMuleContextFactory().createMuleContext();
96          
97          ApplicationContext appContext = new ClassPathXmlApplicationContext("application-context-2.xml");
98          ConfigurationBuilder builder = new SpringConfigurationBuilder(appContext);
99          builder.configure(muleContext); 
100 
101         appContext = new ClassPathXmlApplicationContext("application-context.xml");
102         builder = new SpringConfigurationBuilder(appContext);
103         builder.configure(muleContext); 
104 
105         muleContext.start();
106         
107         Object orange = muleContext.getRegistry().lookupObject("orange");
108         assertNotNull(orange);
109         assertTrue(orange instanceof Orange);
110         assertEquals("Pirulo", ((Orange) orange).getBrand());
111     }
112 
113     /** 
114      * Test that the same bean from the TransientRegistry will have precedence over the 1st appContext 
115      */
116     @Test
117     public void testTransientRegistryPrecedence() throws Exception
118     {
119         MuleContext muleContext = new DefaultMuleContextFactory().createMuleContext();
120         
121         muleContext.getRegistry().registerObject("orange", new Orange(12, 5.5, "Tutti Frutti"));
122         
123         ApplicationContext appContext = new ClassPathXmlApplicationContext("application-context.xml");
124         ConfigurationBuilder builder = new SpringConfigurationBuilder(appContext);
125         builder.configure(muleContext); 
126 
127         muleContext.start();
128         
129         Object orange = muleContext.getRegistry().lookupObject("orange");
130         assertNotNull(orange);
131         assertTrue(orange instanceof Orange);
132         assertEquals("Tutti Frutti", ((Orange) orange).getBrand());
133     }
134 
135     /** 
136      * Test that an existing appContext can be used as a parent AppContext for Mule 
137      */
138     @Test
139     public void testParentContext() throws Exception
140     {
141         MuleContext muleContext = new DefaultMuleContextFactory().createMuleContext();
142 
143         ApplicationContext appContext = new ClassPathXmlApplicationContext("application-context.xml");
144 
145         SpringXmlConfigurationBuilder builder = new SpringXmlConfigurationBuilder("mule-config.xml");
146         builder.setParentContext(appContext);
147         builder.configure(muleContext);
148 
149         muleContext.start();
150 
151         Object orange = muleContext.getRegistry().lookupObject("orange");
152         assertNotNull(orange);
153         assertTrue(orange instanceof Orange);
154         assertEquals("Pirulo", ((Orange) orange).getBrand());
155     }
156 
157     /**
158      * Test the most common approach: Create the Spring config + Mule config in a single AppContext.
159      */
160     @Test
161     public void testAppContextTogetherWithMuleConfig() throws Exception
162     {
163         MuleContext muleContext = new DefaultMuleContextFactory().createMuleContext();
164 
165         SpringXmlConfigurationBuilder builder = new SpringXmlConfigurationBuilder("application-context.xml, mule-config.xml");
166         builder.configure(muleContext);
167 
168         muleContext.start();
169 
170         Object orange = muleContext.getRegistry().lookupObject("orange");
171         assertNotNull(orange);
172         assertTrue(orange instanceof Orange);
173         assertEquals("Pirulo", ((Orange) orange).getBrand());
174     }
175 }