View Javadoc

1   /*
2    * $Id: FlowTestCase.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.MessageExchangePattern;
14  import org.mule.api.MuleEvent;
15  import org.mule.api.MuleException;
16  import org.mule.api.processor.MessageProcessor;
17  import org.mule.processor.ResponseMessageProcessorAdapter;
18  import org.mule.tck.MuleTestUtils;
19  import org.mule.tck.SensingNullMessageProcessor;
20  import org.mule.transformer.simple.StringAppendTransformer;
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  public class FlowTestCase extends AbstractFlowConstuctTestCase
26  {
27  
28      private SimpleFlowConstruct flow;
29      private SensingNullMessageProcessor sensingMessageProcessor;
30  
31      @Override
32      protected void doSetUp() throws Exception
33      {
34          super.doSetUp();
35  
36          sensingMessageProcessor = getSensingNullMessageProcessor();
37  
38          flow = new SimpleFlowConstruct("test-flow", muleContext);
39          flow.setMessageSource(directInboundMessageSource);
40  
41          List<MessageProcessor> processors = new ArrayList<MessageProcessor>();
42          processors.add(new ResponseMessageProcessorAdapter(new StringAppendTransformer("f")));
43          processors.add(new ResponseMessageProcessorAdapter(new StringAppendTransformer("e")));
44          processors.add(new ResponseMessageProcessorAdapter(new StringAppendTransformer("d")));
45          processors.add(new StringAppendTransformer("a"));
46          processors.add(new StringAppendTransformer("b"));
47          processors.add(new StringAppendTransformer("c"));
48          processors.add(new MessageProcessor()
49          {
50              public MuleEvent process(MuleEvent event) throws MuleException
51              {
52                  event.getMessage().setOutboundProperty("thread", Thread.currentThread());
53                  return event;
54              }
55          });
56          processors.add(sensingMessageProcessor);
57          flow.setMessageProcessors(processors);
58      }
59  
60      @Override
61      protected AbstractFlowConstruct getFlowConstruct() throws Exception
62      {
63          return flow;
64      }
65  
66      public void testProcessOneWayEndpoint() throws Exception
67      {
68          flow.initialise();
69          flow.start();
70          MuleEvent response = directInboundMessageSource.process(MuleTestUtils.getTestInboundEvent("hello",
71              MessageExchangePattern.ONE_WAY, muleContext));
72          Thread.sleep(10);
73  
74          assertNull(response);
75  
76          assertEquals("helloabc", sensingMessageProcessor.event.getMessageAsString());
77          assertNotSame(Thread.currentThread(), sensingMessageProcessor.event.getMessage().getOutboundProperty(
78              "thread"));
79      }
80  
81      public void testProcessRequestResponseEndpoint() throws Exception
82      {
83          flow.initialise();
84          flow.start();
85          MuleEvent response = directInboundMessageSource.process(MuleTestUtils.getTestInboundEvent("hello",
86              MessageExchangePattern.REQUEST_RESPONSE, muleContext));
87  
88          assertEquals("helloabcdef", response.getMessageAsString());
89          assertEquals(Thread.currentThread(), response.getMessage().getOutboundProperty("thread"));
90  
91          // Sensed (out) event also is appended with 'def' because it's the same event
92          // instance
93          assertEquals("helloabcdef", sensingMessageProcessor.event.getMessageAsString());
94          assertEquals(Thread.currentThread(), sensingMessageProcessor.event.getMessage().getOutboundProperty(
95              "thread"));
96  
97      }
98  
99      public void testProcessStopped() throws Exception
100     {
101         flow.initialise();
102 
103         try
104         {
105             directInboundMessageSource.process(MuleTestUtils.getTestInboundEvent("hello", muleContext));
106             fail("exception expected");
107         }
108         catch (Exception e)
109         {
110         }
111     }
112 }