View Javadoc

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