1
2
3
4
5
6
7 package org.mule.api;
8
9 import org.mule.api.processor.MessageProcessorChain;
10 import org.mule.api.processor.policy.AroundPolicy;
11 import org.mule.api.processor.policy.PolicyInvocation;
12 import org.mule.processor.chain.DefaultMessageProcessorChain;
13 import org.mule.tck.junit4.AbstractMuleContextTestCase;
14 import org.mule.transformer.simple.StringAppendTransformer;
15
16 import org.junit.Test;
17
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertNotNull;
20 import static org.junit.Assert.assertSame;
21 import static org.junit.Assert.fail;
22
23 public class PolicyTestCase extends AbstractMuleContextTestCase
24 {
25
26 @Test
27 public void testSinglePolicy() throws Exception
28 {
29 AroundPolicy ap = new TestPolicy("test around policy");
30
31
32 MessageProcessorChain chain = DefaultMessageProcessorChain.from(
33 new StringAppendTransformer("first"),
34 new StringAppendTransformer(" second"));
35 initialiseObject(chain);
36
37
38 assertEquals("No policies should have been registered.", 0, chain.getPolicies().list().size());
39 chain.getPolicies().add(ap);
40 assertSame("Policy has not been registered.", ap, chain.getPolicies().list().iterator().next());
41
42 System.out.println(chain);
43
44
45 final MuleEvent result = chain.process(getTestEvent("payload "));
46 assertNotNull(result);
47 final MuleMessage message = result.getMessage();
48 assertNotNull(message);
49 assertEquals("payload {before} first second {after}", message.getPayload());
50
51
52 final AroundPolicy policy = chain.getPolicies().remove(ap.getName());
53 assertSame("Wrong policy returned?", ap, policy);
54 assertEquals("No policies should have been registered.", 0, chain.getPolicies().list().size());
55 }
56
57 @Test
58 public void testMultiplePolicies() throws Exception
59 {
60
61
62 MessageProcessorChain chain = DefaultMessageProcessorChain.from(
63 new StringAppendTransformer("first"),
64 new StringAppendTransformer(" second"));
65 initialiseObject(chain);
66
67
68 assertEquals("No policies should have been registered.", 0, chain.getPolicies().list().size());
69 AroundPolicy policy1 = new TestPolicy("test around policy 1");
70 chain.getPolicies().add(policy1);
71
72 final TestPolicy policy2 = new TestPolicy("test around policy 2");
73 chain.getPolicies().add(policy2);
74 assertEquals("Wrong policies count.", 2, chain.getPolicies().list().size());
75
76 System.out.println(chain);
77
78
79 final MuleEvent result = chain.process(getTestEvent("payload "));
80 assertNotNull(result);
81 final MuleMessage message = result.getMessage();
82 assertNotNull(message);
83 assertEquals("payload {before} {before} first second {after} {after}", message.getPayload());
84
85
86 final AroundPolicy policy = chain.getPolicies().remove(policy1.getName());
87 assertSame("Wrong policy returned?", policy1, policy);
88 chain.getPolicies().remove(policy2.getName());
89 assertEquals("No policies should have been registered.", 0, chain.getPolicies().list().size());
90 }
91
92 @Test
93 public void testDuplicateName() throws Exception
94 {
95 MessageProcessorChain chain = DefaultMessageProcessorChain.from();
96 chain.getPolicies().add(new TestPolicy("test"));
97 try
98 {
99 chain.getPolicies().add(new TestPolicy("test"));
100 fail("Should've thrown an exception, no duplicates allowed");
101 }
102 catch (IllegalArgumentException e)
103 {
104 System.out.println(e);
105
106 }
107 }
108
109 private static class TestPolicy implements AroundPolicy
110 {
111
112 private String name;
113
114 public TestPolicy(final String name)
115 {
116 this.name = name;
117 }
118
119 public MuleEvent invoke(PolicyInvocation invocation) throws MuleException
120 {
121
122 new StringAppendTransformer("{before} ").process(invocation.getEvent());
123 final MuleEvent result = invocation.proceed();
124
125 new StringAppendTransformer(" {after}").process(invocation.getEvent());
126 return result;
127 }
128
129 public String getName()
130 {
131 return name;
132 }
133 }
134 }