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.functional;
8   
9   import org.mule.api.MuleContext;
10  import org.mule.api.MuleEvent;
11  import org.mule.api.MuleException;
12  import org.mule.api.construct.FlowConstruct;
13  import org.mule.api.construct.FlowConstructAware;
14  import org.mule.api.context.MuleContextAware;
15  import org.mule.api.expression.ExpressionManager;
16  import org.mule.api.lifecycle.InitialisationException;
17  import org.mule.api.lifecycle.Startable;
18  import org.mule.api.processor.MessageProcessor;
19  import org.mule.tck.AbstractMuleTestCase;
20  
21  import java.util.concurrent.CountDownLatch;
22  import java.util.concurrent.TimeUnit;
23  
24  import org.junit.Assert;
25  
26  public class AssertionMessageProcessor
27      implements MessageProcessor, FlowConstructAware, Startable, MuleContextAware
28  {
29  
30      private String expression;
31      private String message = "?";
32      private int count = 1;
33  
34      public void setExpression(String expression)
35      {
36          this.expression = expression;
37      }
38  
39      private int timeout = AbstractMuleTestCase.RECEIVE_TIMEOUT;
40  
41      private MuleEvent event;
42      private CountDownLatch latch;
43  
44      private MuleContext muleContext;
45      private FlowConstruct flowConstruct;
46      private ExpressionManager expressionManager;
47      private boolean result = true;
48  
49      public void start() throws InitialisationException
50      {
51          this.expressionManager = muleContext.getExpressionManager();
52          this.expressionManager.validateExpression(expression);
53          latch = new CountDownLatch(count);
54          FlowAssert.addAssertion(flowConstruct.getName(), this);
55      }
56  
57      public MuleEvent process(MuleEvent event) throws MuleException
58      {
59          this.event = event;
60          result = result && expressionManager.evaluateBoolean(expression, event.getMessage(), false, true);
61          latch.countDown();
62          return event;
63      }
64  
65      public void verify() throws InterruptedException
66      {
67          boolean didntTimeout = latch.await(timeout, TimeUnit.MILLISECONDS);
68          if (!didntTimeout || event == null)
69          {
70              Assert.fail("Flow assertion '" + message + "' failed.  No message recieved.");
71          }
72          else if (!result)
73          {
74              Assert.fail("Flow assertion '" + message + "' failed. Expression " + expression
75                          + " evaluated false.");
76          }
77      };
78  
79      public void reset()
80      {
81          this.event = null;
82  
83      }
84  
85      public void setFlowConstruct(FlowConstruct flowConstruct)
86      {
87          this.flowConstruct = flowConstruct;
88      }
89  
90      public void setMuleContext(MuleContext context)
91      {
92          this.muleContext = context;
93      }
94  
95      public void setMessage(String message)
96      {
97          this.message = message;
98      }
99  
100     public void setCount(int count)
101     {
102         this.count = count;
103     }
104 }