View Javadoc

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