View Javadoc

1   /*
2    * $Id: AbstractFlowConstuctTestCase.java 20320 2010-11-24 15:03:31Z dfeist $
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.construct;
12  
13  import org.mule.api.MuleEvent;
14  import org.mule.api.MuleException;
15  import org.mule.api.processor.MessageProcessor;
16  import org.mule.api.source.MessageSource;
17  import org.mule.tck.AbstractMuleTestCase;
18  import org.mule.util.ObjectUtils;
19  
20  public abstract class AbstractFlowConstuctTestCase extends AbstractMuleTestCase
21  {
22      public static class DirectInboundMessageSource implements MessageSource
23      {
24          private MessageProcessor listener;
25  
26          public void setListener(MessageProcessor listener)
27          {
28              this.listener = listener;
29          }
30  
31          public MuleEvent process(MuleEvent event) throws MuleException
32          {
33              return listener.process(event);
34          }
35  
36          @Override
37          public String toString()
38          {
39              return ObjectUtils.toString(this);
40          }
41      }
42  
43      protected DirectInboundMessageSource directInboundMessageSource;
44  
45      @Override
46      protected void doSetUp() throws Exception
47      {
48          super.doSetUp();
49  
50          directInboundMessageSource = new DirectInboundMessageSource();
51      }
52  
53      protected abstract AbstractFlowConstruct getFlowConstruct() throws Exception;
54  
55      public void testStart() throws Exception
56      {
57          try
58          {
59              getFlowConstruct().start();
60              fail("Exception expected: Cannot start an uninitialised service");
61          }
62          catch (final Exception e)
63          {
64              // expected
65          }
66  
67          getFlowConstruct().initialise();
68          getFlowConstruct().start();
69  
70          try
71          {
72              getFlowConstruct().initialise();
73              fail("Exception expected: Cannot initialise an already initialised service");
74          }
75          catch (final IllegalStateException e)
76          {
77              // expected
78          }
79          getFlowConstruct().dispose();
80  
81      }
82  
83      public void testStop() throws Exception
84      {
85          assertFalse(getFlowConstruct().isStarted());
86  
87          try
88          {
89              getFlowConstruct().stop();
90              fail("Exception expected: Cannot stop an uninitialised service");
91          }
92          catch (final IllegalStateException e)
93          {
94              // expected
95          }
96  
97          getFlowConstruct().initialise();
98          assertFalse(getFlowConstruct().isStarted());
99  
100         // Can stop a service that is not started
101         getFlowConstruct().stop();
102 
103         assertFalse(getFlowConstruct().isStarted());
104         getFlowConstruct().start();
105         assertTrue(getFlowConstruct().isStarted());
106         getFlowConstruct().stop();
107         assertFalse(getFlowConstruct().isStarted());
108         try
109         {
110             getFlowConstruct().stop();
111             fail("Exception expected: Cannot stop a service that is not started");
112         }
113         catch (final IllegalStateException e)
114         {
115             // expected
116         }
117         assertFalse(getFlowConstruct().isStarted());
118         getFlowConstruct().dispose();
119 
120     }
121 
122     public void testDispose() throws Exception
123     {
124         assertFalse(getFlowConstruct().isStarted());
125         getFlowConstruct().dispose();
126 
127         try
128         {
129             getFlowConstruct().dispose();
130             fail("Exception expected: Cannot dispose a service that is already disposed");
131         }
132         catch (final IllegalStateException e)
133         {
134             // expected
135         }
136 
137         try
138         {
139             getFlowConstruct().initialise();
140             fail("Exception expected: Cannot invoke initialise (or any lifecycle) on an object once it is disposed");
141         }
142         catch (final IllegalStateException e)
143         {
144             // expected
145         }
146     }
147 
148 }