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