View Javadoc

1   /*
2    * $Id: ServiceTestCase.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.service;
12  
13  import org.mule.api.endpoint.InboundEndpoint;
14  import org.mule.api.model.Model;
15  import org.mule.api.registry.MuleRegistry;
16  import org.mule.api.service.Service;
17  import org.mule.api.source.CompositeMessageSource;
18  import org.mule.api.transport.Connector;
19  import org.mule.config.QueueProfile;
20  import org.mule.model.seda.SedaModel;
21  import org.mule.model.seda.SedaService;
22  import org.mule.tck.junit4.AbstractMuleContextTestCase;
23  import org.mule.tck.testmodels.mule.TestConnector;
24  import org.mule.transport.AbstractConnector;
25  
26  import org.junit.Test;
27  
28  import static org.junit.Assert.assertEquals;
29  
30  public class ServiceTestCase extends AbstractMuleContextTestCase
31  {
32      private Connector testConnector;
33      private Service service;
34  
35      @Override
36      protected void doSetUp() throws Exception
37      {
38          super.doSetUp();
39  
40          testConnector = new TestConnector(muleContext);
41          testConnector.setName("customTestConnector");
42          muleContext.getRegistry().registerConnector(testConnector);
43  
44          InboundEndpoint inboundEndpoint1 = muleContext.getEndpointFactory().getInboundEndpoint(
45              "test://test1?connector=customTestConnector");
46          InboundEndpoint inboundEndpoint2 = muleContext.getEndpointFactory().getInboundEndpoint(
47              "test://test2?connector=customTestConnector");
48  
49          service = new SedaService(muleContext);
50          service.setName("testService");
51          ((CompositeMessageSource) service.getMessageSource()).addSource(inboundEndpoint1);
52          ((CompositeMessageSource) service.getMessageSource()).addSource(inboundEndpoint2);
53          Model model = new SedaModel();
54          model.setMuleContext(muleContext);
55          model.initialise();
56          service.setModel(model);
57          
58          QueueProfile queueProfile = QueueProfile.newInstancePersistingToDefaultMemoryQueueStore(muleContext);
59          ((SedaService) service).setQueueProfile(queueProfile);
60  
61          muleContext.getRegistry().registerService(service);
62      }
63  
64      @Override
65      protected void doTearDown() throws Exception
66      {
67          muleContext.getRegistry().unregisterObject(service.getName(), MuleRegistry.LIFECYCLE_BYPASS_FLAG);
68      }
69  
70      @Test
71      public void testUnregisterListenersOnServiceDisposal() throws Exception
72      {
73          // Start muleContext, this starts connectors and services
74          muleContext.start();
75  
76          // Assert that connector has two receivers registered, one for each endpoint
77          assertEquals(2, ((AbstractConnector) testConnector).getReceivers().size());
78  
79          service.stop();
80          service.dispose();
81  
82          // Assert that connector has no receivers registered after service disposal
83          assertEquals(0, ((AbstractConnector) testConnector).getReceivers().size());
84      }
85  
86      @Test
87      public void testUnregisterListenersOnServiceStop() throws Exception
88      {
89  
90          // Start muleContext, this starts connectors and services
91          muleContext.start();
92  
93          // Assert that connector has two receivers registered, one for each endpoint
94          assertEquals(2, ((AbstractConnector) testConnector).getReceivers().size());
95  
96          service.stop();
97  
98          // Assert that connector has no receivers registered after service disposal
99          assertEquals(0, ((AbstractConnector) testConnector).getReceivers().size());
100     }
101 }