1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
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 | 0 | public class AssertionMessageProcessor |
27 | |
implements MessageProcessor, FlowConstructAware, Startable, MuleContextAware |
28 | |
{ |
29 | |
|
30 | |
private String expression; |
31 | 0 | private String message = "?"; |
32 | 0 | private int count = 1; |
33 | |
|
34 | |
public void setExpression(String expression) |
35 | |
{ |
36 | 0 | this.expression = expression; |
37 | 0 | } |
38 | |
|
39 | 0 | 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 | 0 | private boolean result = true; |
48 | |
|
49 | |
public void start() throws InitialisationException |
50 | |
{ |
51 | 0 | this.expressionManager = muleContext.getExpressionManager(); |
52 | 0 | this.expressionManager.validateExpression(expression); |
53 | 0 | latch = new CountDownLatch(count); |
54 | 0 | FlowAssert.addAssertion(flowConstruct.getName(), this); |
55 | 0 | } |
56 | |
|
57 | |
public MuleEvent process(MuleEvent event) throws MuleException |
58 | |
{ |
59 | 0 | this.event = event; |
60 | 0 | result = result && expressionManager.evaluateBoolean(expression, event.getMessage(), false, true); |
61 | 0 | latch.countDown(); |
62 | 0 | return event; |
63 | |
} |
64 | |
|
65 | |
public void verify() throws InterruptedException |
66 | |
{ |
67 | 0 | boolean didntTimeout = latch.await(timeout, TimeUnit.MILLISECONDS); |
68 | 0 | if (!didntTimeout || event == null) |
69 | |
{ |
70 | 0 | Assert.fail("Flow assertion '" + message + "' failed. No message recieved."); |
71 | |
} |
72 | 0 | else if (!result) |
73 | |
{ |
74 | 0 | Assert.fail("Flow assertion '" + message + "' failed. Expression " + expression |
75 | |
+ " evaluated false."); |
76 | |
} |
77 | 0 | }; |
78 | |
|
79 | |
public void reset() |
80 | |
{ |
81 | 0 | this.event = null; |
82 | |
|
83 | 0 | } |
84 | |
|
85 | |
public void setFlowConstruct(FlowConstruct flowConstruct) |
86 | |
{ |
87 | 0 | this.flowConstruct = flowConstruct; |
88 | 0 | } |
89 | |
|
90 | |
public void setMuleContext(MuleContext context) |
91 | |
{ |
92 | 0 | this.muleContext = context; |
93 | 0 | } |
94 | |
|
95 | |
public void setMessage(String message) |
96 | |
{ |
97 | 0 | this.message = message; |
98 | 0 | } |
99 | |
|
100 | |
public void setCount(int count) |
101 | |
{ |
102 | 0 | this.count = count; |
103 | 0 | } |
104 | |
} |