1
2
3
4
5
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
45 }
46 }
47
48
49
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
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
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
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
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 }