View Javadoc

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