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.util.queue;
8   
9   import static org.junit.Assert.assertEquals;
10  import static org.junit.Assert.assertSame;
11  
12  import org.mule.api.MuleContext;
13  import org.mule.api.MuleException;
14  import org.mule.api.config.MuleProperties;
15  import org.mule.api.construct.FlowConstruct;
16  import org.mule.config.builders.DefaultsConfigurationBuilder;
17  import org.mule.construct.SimpleFlowConstruct;
18  import org.mule.context.DefaultMuleContextFactory;
19  import org.mule.security.MuleSecurityManager;
20  
21  import java.io.IOException;
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import org.junit.Test;
26  
27  public class QueueManagerLifecycleOrderTestCase
28  {
29      List<Object> startStopOrder = new ArrayList<Object>();
30  
31      @Test
32      public void testStartupOrder() throws Exception
33      {
34          MuleContext muleContext = new DefaultMuleContextFactory().createMuleContext(new QueueManagerOnlyConfigurationBuilder());
35          TransactionalQueueManager tqm = (TransactionalQueueManager) muleContext.getQueueManager();
36          PersistenceStrategy ps = new PersistenceStrategy();
37          tqm.setPersistenceStrategy(ps);
38          FlowConstruct fc = new RecordingFlowConstruct("dummy", muleContext);
39          muleContext.getRegistry().registerFlowConstruct(fc);
40          muleContext.start();
41          muleContext.stop();
42          assertEquals(4, startStopOrder.size());
43          assertSame(ps, startStopOrder.get(0));
44          assertSame(fc, startStopOrder.get(1));
45          assertSame(fc, startStopOrder.get(2));
46          assertSame(ps, startStopOrder.get(3));
47      }
48  
49      private class PersistenceStrategy extends MemoryPersistenceStrategy
50      {
51          public void open() throws IOException
52          {
53              startStopOrder.add(this);
54          }
55  
56          public void close() throws IOException
57          {
58              startStopOrder.add(this);
59          }
60      }
61  
62      private class RecordingFlowConstruct extends SimpleFlowConstruct
63      {
64          public RecordingFlowConstruct(String name, MuleContext muleContext)
65          {
66              super(name, muleContext);
67          }
68  
69          public void doStart() throws MuleException
70          {
71              startStopOrder.add(this);
72          }
73  
74          public void doStop() throws MuleException
75          {
76              startStopOrder.add(this);
77          }
78      }
79  
80      private static class QueueManagerOnlyConfigurationBuilder extends DefaultsConfigurationBuilder
81      {
82          @Override
83          protected void doConfigure(MuleContext muleContext) throws Exception
84          {
85              muleContext.getRegistry().registerObject(MuleProperties.OBJECT_QUEUE_MANAGER,
86                  new TransactionalQueueManager());
87              muleContext.getRegistry().registerObject(MuleProperties.OBJECT_SECURITY_MANAGER,
88                  new MuleSecurityManager());
89  
90          }
91      }
92  }