View Javadoc

1   /*
2    * $Id: AbstractConfigBuilderTestCase.java 22401 2011-07-13 09:10:18Z 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.tck;
12  
13  import org.mule.api.DefaultMuleException;
14  import org.mule.api.MuleException;
15  import org.mule.api.component.InterfaceBinding;
16  import org.mule.api.component.JavaComponent;
17  import org.mule.api.config.ThreadingProfile;
18  import org.mule.api.endpoint.ImmutableEndpoint;
19  import org.mule.api.endpoint.InboundEndpoint;
20  import org.mule.api.exception.MessagingExceptionHandler;
21  import org.mule.api.processor.MessageProcessor;
22  import org.mule.api.routing.OutboundRouter;
23  import org.mule.api.routing.OutboundRouterCollection;
24  import org.mule.api.routing.filter.Filter;
25  import org.mule.api.service.Service;
26  import org.mule.api.transaction.TransactionConfig;
27  import org.mule.api.transformer.Transformer;
28  import org.mule.component.AbstractComponent;
29  import org.mule.component.PooledJavaComponent;
30  import org.mule.config.PoolingProfile;
31  import org.mule.config.QueueProfile;
32  import org.mule.interceptor.InterceptorStack;
33  import org.mule.interceptor.LoggingInterceptor;
34  import org.mule.interceptor.TimerInterceptor;
35  import org.mule.model.seda.SedaService;
36  import org.mule.routing.IdempotentMessageFilter;
37  import org.mule.routing.MessageFilter;
38  import org.mule.routing.filters.MessagePropertyFilter;
39  import org.mule.routing.filters.PayloadTypeFilter;
40  import org.mule.routing.filters.RegExFilter;
41  import org.mule.routing.filters.logic.AndFilter;
42  import org.mule.routing.outbound.FilteringOutboundRouter;
43  import org.mule.service.ServiceCompositeMessageSource;
44  import org.mule.tck.testmodels.mule.TestCatchAllStrategy;
45  import org.mule.tck.testmodels.mule.TestCompressionTransformer;
46  import org.mule.tck.testmodels.mule.TestExceptionStrategy;
47  import org.mule.tck.testmodels.mule.TestTransactionFactory;
48  import org.mule.transformer.TransformerUtils;
49  import org.mule.transformer.types.DataTypeFactory;
50  import org.mule.transport.AbstractConnector;
51  
52  import org.junit.Test;
53  
54  import static org.junit.Assert.assertEquals;
55  import static org.junit.Assert.assertNotNull;
56  import static org.junit.Assert.assertNull;
57  import static org.junit.Assert.assertTrue;
58  
59  public abstract class AbstractConfigBuilderTestCase extends AbstractScriptConfigBuilderTestCase
60  {
61  
62      public AbstractConfigBuilderTestCase(boolean legacy)
63      {
64          super(legacy);
65      }
66      
67      @Override
68      protected boolean isGracefulShutdown()
69      {
70          return true;
71      }
72  
73      @Override
74      public void testManagerConfig() throws Exception
75      {
76          super.testManagerConfig();
77  
78          assertNotNull(muleContext.getTransactionManager());
79      }
80  
81      @Override
82      public void testConnectorConfig() throws Exception
83      {
84          super.testConnectorConfig();
85  
86          MessagingExceptionHandler es = muleContext.getRegistry().lookupModel("main").getExceptionListener();
87          assertNotNull(es);
88          assertTrue(es.getClass().getName(), es instanceof TestExceptionStrategy);
89      }
90  
91      @Override
92      public void testGlobalEndpointConfig() throws MuleException
93      {
94          super.testGlobalEndpointConfig();
95          ImmutableEndpoint endpoint = muleContext.getEndpointFactory().getInboundEndpoint("fruitBowlEndpoint");
96          assertNotNull(endpoint);
97          assertEquals(endpoint.getEndpointURI().getAddress(), "fruitBowlPublishQ");
98  
99          MessagePropertyFilter filter = (MessagePropertyFilter)endpoint.getFilter();
100         assertNotNull(filter);
101         assertEquals("foo=bar", filter.getPattern());
102     }
103 
104     @Override
105     public void testEndpointConfig() throws MuleException
106     {
107         super.testEndpointConfig();
108 
109         // test that targets have been resolved on targets
110         ImmutableEndpoint endpoint = muleContext.getEndpointFactory().getInboundEndpoint("waterMelonEndpoint");
111         assertNotNull(endpoint);
112         assertEquals("UTF-8-TEST", endpoint.getEncoding());
113         assertEquals("test.queue", endpoint.getEndpointURI().getAddress());
114 
115         Service service = muleContext.getRegistry().lookupService("appleComponent2");
116         assertNotNull(service);
117     }
118 
119     @Test
120     public void testExceptionStrategy2()
121     {
122         Service service = muleContext.getRegistry().lookupService("appleComponent");
123         assertNotNull(service.getExceptionListener());
124         assertTrue(service.getExceptionListener() instanceof MessagingExceptionHandler);
125     }
126 
127     @Override
128     public void testTransformerConfig()
129     {
130         super.testTransformerConfig();
131 
132         Transformer t = muleContext.getRegistry().lookupTransformer("TestCompressionTransformer");
133         assertNotNull(t);
134         assertTrue(t instanceof TestCompressionTransformer);
135         assertEquals(t.getReturnDataType(), DataTypeFactory.STRING);
136         assertNotNull(((TestCompressionTransformer) t).getContainerProperty());
137     }
138 
139     @Override
140     public void testModelConfig() throws Exception
141     {
142         super.testModelConfig();        
143         assertNotNull(muleContext.getRegistry().lookupService("appleComponent"));
144         assertNotNull(muleContext.getRegistry().lookupService("appleComponent2"));
145     }
146 
147     @Test
148     public void testOutboundRouterConfig2()
149     {
150         // test outbound message router
151         Service service = muleContext.getRegistry().lookupService("appleComponent");
152         assertNotNull(service.getOutboundMessageProcessor());
153         OutboundRouterCollection router = (OutboundRouterCollection) service.getOutboundMessageProcessor();
154         assertNotNull(router.getCatchAllStrategy());
155         assertEquals(2, router.getRoutes().size());
156         // check first Router
157         OutboundRouter route1 = (OutboundRouter) router.getRoutes().get(0);
158         assertTrue(route1 instanceof FilteringOutboundRouter);
159         assertEquals(1, route1.getRoutes().size());
160         ImmutableEndpoint ep = (ImmutableEndpoint) route1.getRoutes().get(0);
161         
162         assertNotNull(ep.getTransformers());
163         assertTrue(TransformerUtils.firstOrNull(ep.getTransformers()) instanceof TestCompressionTransformer);
164 
165         Filter filter = ((FilteringOutboundRouter) route1).getFilter();
166         assertNotNull(filter);
167         assertTrue(filter instanceof PayloadTypeFilter);
168         assertEquals(String.class, ((PayloadTypeFilter) filter).getExpectedType());
169 
170         // check second Router
171         OutboundRouter route2 = (OutboundRouter) router.getRoutes().get(1);
172         assertTrue(route2 instanceof FilteringOutboundRouter);
173 
174         Filter filter2 = ((FilteringOutboundRouter) route2).getFilter();
175         assertNotNull(filter2);
176         assertTrue(filter2 instanceof AndFilter);
177         assertEquals(2,  ((AndFilter) filter2).getFilters().size());
178         Filter left = ((AndFilter) filter2).getFilters().get(0);
179         Filter right = ((AndFilter) filter2).getFilters().get(1);
180         assertNotNull(left);
181         assertTrue(left instanceof RegExFilter);
182         assertEquals("the quick brown (.*)", ((RegExFilter) left).getPattern());
183         assertNotNull(right);
184         assertTrue(right instanceof RegExFilter);
185         assertEquals("(.*) brown (.*)", ((RegExFilter) right).getPattern());
186 
187         assertTrue(router.getCatchAllStrategy() instanceof TestCatchAllStrategy);
188     }
189 
190 
191     @Test
192     public void testInboundRouterConfig2()
193     {
194         Service service = muleContext.getRegistry().lookupService("appleComponent");
195         assertNotNull(service.getMessageSource());
196         ServiceCompositeMessageSource messageRouter = (ServiceCompositeMessageSource) service.getMessageSource();
197         assertNotNull(messageRouter.getCatchAllStrategy());
198         assertEquals(2, messageRouter.getMessageProcessors().size());
199         MessageProcessor router = messageRouter.getMessageProcessors().get(0);
200         assertTrue(router instanceof MessageFilter);
201         MessageFilter sc = (MessageFilter) router;
202 
203         assertNotNull(sc.getFilter());
204         Filter filter = sc.getFilter();
205         // check first Router
206         assertTrue(filter instanceof PayloadTypeFilter);
207         assertEquals(String.class, ((PayloadTypeFilter) filter).getExpectedType());
208 
209         MessageProcessor router2 = messageRouter.getMessageProcessors().get(1);
210         assertTrue(router2 instanceof IdempotentMessageFilter);
211     }
212 
213     @Test
214     public void testThreadingConfig() throws DefaultMuleException
215     {
216         // expected default values from the configuration;
217         // these should differ from the programmatic values!
218 
219         // globals
220         int defaultMaxBufferSize = 42;
221         int defaultMaxThreadsActive = 16;
222         int defaultMaxThreadsIdle = 3;
223         // WAIT is 0, RUN is 4
224         int defaultThreadPoolExhaustedAction = ThreadingProfile.WHEN_EXHAUSTED_WAIT;
225         int defaultThreadTTL = 60001;
226 
227         // for the connector
228         int connectorMaxBufferSize = 2;
229 
230         // for the service
231         int componentMaxBufferSize = 6;
232         int componentMaxThreadsActive = 12;
233         int componentMaxThreadsIdle = 6;
234         int componentThreadPoolExhaustedAction = ThreadingProfile.WHEN_EXHAUSTED_DISCARD;
235 
236         // test default config
237         ThreadingProfile tp = muleContext.getDefaultThreadingProfile();
238         assertEquals(defaultMaxBufferSize, tp.getMaxBufferSize());
239         assertEquals(defaultMaxThreadsActive, tp.getMaxThreadsActive());
240         assertEquals(defaultMaxThreadsIdle, tp.getMaxThreadsIdle());
241         assertEquals(defaultThreadPoolExhaustedAction, tp.getPoolExhaustedAction());
242         assertEquals(defaultThreadTTL, tp.getThreadTTL());
243 
244         // test service threading profile defaults
245         tp = muleContext.getDefaultServiceThreadingProfile();
246         assertEquals(defaultMaxBufferSize, tp.getMaxBufferSize());
247         assertEquals(defaultMaxThreadsActive, tp.getMaxThreadsActive());
248         assertEquals(defaultMaxThreadsIdle, tp.getMaxThreadsIdle());
249         assertEquals(defaultThreadPoolExhaustedAction, tp.getPoolExhaustedAction());
250         assertEquals(defaultThreadTTL, tp.getThreadTTL());
251 
252         // test that unset values retain a default value
253         AbstractConnector c = (AbstractConnector) muleContext.getRegistry().lookupConnector("dummyConnector");
254         tp = c.getDispatcherThreadingProfile();
255         // this value is configured
256         assertEquals(connectorMaxBufferSize, tp.getMaxBufferSize());
257         // these values are inherited
258         assertEquals(defaultMaxThreadsActive, tp.getMaxThreadsActive());
259         assertEquals(defaultMaxThreadsIdle, tp.getMaxThreadsIdle());
260         assertEquals(defaultThreadPoolExhaustedAction, tp.getPoolExhaustedAction());
261         assertEquals(defaultThreadTTL, tp.getThreadTTL());
262 
263         // test per-service values
264         Service service = muleContext.getRegistry().lookupService("appleComponent2");
265         assertTrue("service must be SedaService to get threading profile", service instanceof SedaService);
266         tp = ((SedaService) service).getThreadingProfile();
267         // these values are configured
268         assertEquals(componentMaxBufferSize, tp.getMaxBufferSize());
269         assertEquals(componentMaxThreadsActive, tp.getMaxThreadsActive());
270         assertEquals(componentMaxThreadsIdle, tp.getMaxThreadsIdle());
271         assertEquals(componentThreadPoolExhaustedAction, tp.getPoolExhaustedAction());
272         // this value is inherited
273         assertEquals(defaultThreadTTL, tp.getThreadTTL());
274     }
275 
276     @Test
277     public void testPoolingConfig()
278     {
279 //        //TODO RM* test config
280 //        PoolingProfile pp = muleContext.getConfiguration().getPoolingProfile();
281 //        assertEquals(10, pp.getMaxActive());
282 //        assertEquals(5, pp.getMaxIdle());
283 //        assertEquals(10001, pp.getMaxWait());
284 //        assertEquals(ObjectPool.WHEN_EXHAUSTED_WAIT, pp.getExhaustedAction());
285 //        assertEquals(PoolingProfile.INITIALISE_ONE, pp.getInitialisationPolicy());
286 //        assertTrue(pp.getPoolFactory() instanceof CommonsPoolFactory);
287 
288         // test per-descriptor overrides
289         Service service = muleContext.getRegistry().lookupService("appleComponent2");
290         PoolingProfile pp = ((PooledJavaComponent)service.getComponent()).getPoolingProfile();
291 
292         assertEquals(9, pp.getMaxActive());
293         assertEquals(6, pp.getMaxIdle());
294         assertEquals(4002, pp.getMaxWait());
295         assertEquals(PoolingProfile.WHEN_EXHAUSTED_FAIL, pp.getExhaustedAction());
296         assertEquals(PoolingProfile.INITIALISE_ALL, pp.getInitialisationPolicy());
297     }
298 
299     @Test
300     public void testQueueProfileConfig()
301     {
302 //        // test config
303 //        //TODO RM*
304 //        QueueProfile qp = muleContext.getConfiguration().getQueueProfile();
305 //        assertEquals(100, qp.getMaxOutstandingMessages());
306 //        assertTrue(qp.isPersistent());
307 
308         // test inherit
309         Service service = muleContext.getRegistry().lookupService("appleComponent2");
310         QueueProfile qp = ((SedaService)service).getQueueProfile();
311         assertEquals(102, qp.getMaxOutstandingMessages());
312         //assertTrue(qp.isPersistent());
313 
314         // test override
315 //        descriptor = (MuleDescriptor)muleContext.getModel().getDescriptor("appleComponent2");
316 //        qp = descriptor.getQueueProfile();
317 //        assertEquals(102, qp.getMaxOutstandingMessages());
318 //        assertFalse(qp.isPersistent());
319     }
320 
321     @Test
322     public void testEndpointProperties() throws Exception
323     {
324         // test transaction config
325         Service service = muleContext.getRegistry().lookupService("appleComponent2");
326         InboundEndpoint inEndpoint = ((ServiceCompositeMessageSource) service.getMessageSource()).getEndpoint(
327                 "transactedInboundEndpoint");
328         assertNotNull(inEndpoint);
329         assertNotNull(inEndpoint.getProperties());
330         assertEquals("Prop1", inEndpoint.getProperties().get("testEndpointProperty"));
331     }
332 
333     @Test
334     public void testTransactionConfig() throws Exception
335     {
336         // test transaction config
337         Service apple = muleContext.getRegistry().lookupService("appleComponent2");
338         InboundEndpoint inEndpoint = ((ServiceCompositeMessageSource) apple.getMessageSource()).getEndpoint("transactedInboundEndpoint");
339         assertNotNull(inEndpoint);
340         assertEquals(1, ((OutboundRouterCollection) apple.getOutboundMessageProcessor()).getRoutes().size());
341         assertNotNull(inEndpoint.getTransactionConfig());
342         assertEquals(TransactionConfig.ACTION_ALWAYS_BEGIN, inEndpoint.getTransactionConfig().getAction());
343         assertTrue(inEndpoint.getTransactionConfig().getFactory() instanceof TestTransactionFactory);
344         assertNull(inEndpoint.getTransactionConfig().getConstraint());
345 
346         OutboundRouter outRouter = (OutboundRouter) ((OutboundRouterCollection)apple.getOutboundMessageProcessor()).getRoutes().get(0);
347         MessageProcessor outEndpoint = outRouter.getRoutes().get(0);
348         assertNotNull(outEndpoint);
349     }
350 
351     @Test
352     public void testEnvironmentProperties()
353     {
354         assertEquals("true", muleContext.getRegistry().lookupObject("doCompression"));
355         assertEquals("this was set from the manager properties!", muleContext.getRegistry().lookupObject("beanProperty1"));
356         assertNotNull(muleContext.getRegistry().lookupObject("OS_Version"));
357     }
358 
359 
360     @Test
361     public void testBindngProxyCreation()
362     {
363         //Test that the proxy object was created and set on the service object
364         Service orange = muleContext.getRegistry().lookupService("orangeComponent");
365         assertNotNull(orange);
366         assertTrue(orange.getComponent() instanceof JavaComponent);
367         InterfaceBinding r = ((JavaComponent) orange.getComponent()).getInterfaceBindings().get(0);
368         assertNotNull(r);
369 
370         //TODO Grab an instance of the service object itself and test that the proxy has been injected
371     }
372     
373     @Test
374     public void testMuleConfiguration()
375     {
376         assertEquals(10,muleContext.getConfiguration().getDefaultResponseTimeout());
377         assertEquals(20,muleContext.getConfiguration().getDefaultTransactionTimeout());
378         assertEquals(30,muleContext.getConfiguration().getShutdownTimeout());
379     }
380 
381     @Test
382     public void testGlobalInterceptorStack()
383     {
384         InterceptorStack interceptorStack = (InterceptorStack) muleContext.getRegistry().lookupObject(
385             "testInterceptorStack");
386         assertNotNull(interceptorStack);
387         assertEquals(3, interceptorStack.getInterceptors().size());
388         assertEquals(LoggingInterceptor.class, interceptorStack.getInterceptors().get(0).getClass());
389         assertEquals(TimerInterceptor.class, interceptorStack.getInterceptors().get(1).getClass());
390         assertEquals(LoggingInterceptor.class, interceptorStack.getInterceptors().get(2).getClass());
391     }
392 
393     @Test
394     public void testInterceptors()
395     {
396         Service service = muleContext.getRegistry().lookupService("orangeComponent");
397         AbstractComponent component = (AbstractComponent) service.getComponent();
398         assertEquals(3, component.getInterceptors().size());
399         assertEquals(LoggingInterceptor.class, component.getInterceptors().get(0).getClass());
400         assertEquals(InterceptorStack.class, component.getInterceptors().get(1).getClass());
401         assertEquals(TimerInterceptor.class, component.getInterceptors().get(2).getClass());
402     }
403     
404 }