View Javadoc

1   /*
2    * $Id: InterceptorTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
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.interceptor;
12  
13  import org.mule.api.MuleEvent;
14  import org.mule.api.service.Service;
15  import org.mule.component.AbstractComponent;
16  import org.mule.model.seda.SedaService;
17  import org.mule.tck.AbstractMuleTestCase;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  public class InterceptorTestCase extends AbstractMuleTestCase
23  {
24  
25      private final String BEFORE = "Before";
26      private final String AFTER = "After";
27      private final String COMPONENT = "component";
28      private final String INTERCEPTOR_ONE = "inteceptor1";
29      private final String INTERCEPTOR_TWO = "inteceptor2";
30      private final String INTERCEPTOR_THREE = "inteceptor3";
31  
32      private final String SINGLE_INTERCEPTOR_RESULT = INTERCEPTOR_ONE + BEFORE + COMPONENT + INTERCEPTOR_ONE
33                                                       + AFTER;
34      private final String MULTIPLE_INTERCEPTOR_RESULT = INTERCEPTOR_ONE + BEFORE + INTERCEPTOR_TWO + BEFORE
35                                                         + INTERCEPTOR_THREE + BEFORE + COMPONENT
36                                                         + INTERCEPTOR_THREE + AFTER + INTERCEPTOR_TWO + AFTER
37                                                         + INTERCEPTOR_ONE + AFTER;
38  
39      public void testSingleInterceptor() throws Exception
40      {
41          Service service = createUninitializedService();
42          TestComponent component = (TestComponent) service.getComponent();
43  
44          List interceptors = new ArrayList();
45          interceptors.add(new TestInterceptor(INTERCEPTOR_ONE));
46          component.setInterceptors(interceptors);
47          service.initialise();
48          service.start();
49  
50          MuleEvent result = component.process(getTestInboundEvent(""));
51  
52          assertEquals(SINGLE_INTERCEPTOR_RESULT, result.getMessageAsString());
53      }
54  
55      public void testMultipleInterceptor() throws Exception
56      {
57          Service service = createUninitializedService();
58          TestComponent component = (TestComponent) service.getComponent();
59  
60          List interceptors = new ArrayList();
61          interceptors.add(new TestInterceptor(INTERCEPTOR_ONE));
62          interceptors.add(new TestInterceptor(INTERCEPTOR_TWO));
63          interceptors.add(new TestInterceptor(INTERCEPTOR_THREE));
64          component.setInterceptors(interceptors);
65          service.initialise();
66          service.start();
67  
68          MuleEvent result = component.process(getTestInboundEvent(""));
69  
70          assertEquals(MULTIPLE_INTERCEPTOR_RESULT, result.getMessageAsString());
71      }
72  
73      public void testSingleInterceptorStack() throws Exception
74      {
75          Service service = createUninitializedService();
76          TestComponent component = (TestComponent) service.getComponent();
77  
78          List interceptors = new ArrayList();
79          List stackInterceptors = new ArrayList();
80          stackInterceptors.add(new TestInterceptor(INTERCEPTOR_ONE));
81          interceptors.add(new InterceptorStack(stackInterceptors));
82          component.setInterceptors(interceptors);
83          service.initialise();
84          service.start();
85  
86          MuleEvent result = component.process(getTestInboundEvent(""));
87  
88          assertEquals(SINGLE_INTERCEPTOR_RESULT, result.getMessageAsString());
89      }
90  
91      public void testMultipleInterceptorStack() throws Exception
92      {
93          Service service = createUninitializedService();
94          TestComponent component = (TestComponent) service.getComponent();
95  
96          List interceptors = new ArrayList();
97          interceptors.add(new TestInterceptor(INTERCEPTOR_ONE));
98          List stackInterceptors = new ArrayList();
99          stackInterceptors.add(new TestInterceptor(INTERCEPTOR_TWO));
100         stackInterceptors.add(new TestInterceptor(INTERCEPTOR_THREE));
101         interceptors.add(new InterceptorStack(stackInterceptors));
102         component.setInterceptors(interceptors);
103         service.initialise();
104         service.start();
105 
106         MuleEvent result = component.process(getTestInboundEvent(""));
107 
108         assertEquals(MULTIPLE_INTERCEPTOR_RESULT, result.getMessageAsString());
109     }
110 
111     public void testMultipleInterceptorStack2() throws Exception
112     {
113         Service service = createUninitializedService();
114         TestComponent component = (TestComponent) service.getComponent();
115 
116         List interceptors = new ArrayList();
117         interceptors.add(new TestInterceptor(INTERCEPTOR_ONE));
118         interceptors.add(new TestInterceptor(INTERCEPTOR_TWO));
119         interceptors.add(new TestInterceptor(INTERCEPTOR_THREE));
120         List stackInterceptors = new ArrayList();
121         stackInterceptors.add(new TestInterceptor(INTERCEPTOR_ONE));
122         stackInterceptors.add(new TestInterceptor(INTERCEPTOR_TWO));
123         stackInterceptors.add(new TestInterceptor(INTERCEPTOR_THREE));
124         interceptors.add(new InterceptorStack(stackInterceptors));
125         component.setInterceptors(interceptors);
126         service.initialise();
127         service.start();
128 
129         MuleEvent result = component.process(getTestInboundEvent(""));
130 
131         assertEquals(INTERCEPTOR_ONE + BEFORE + INTERCEPTOR_TWO + BEFORE + INTERCEPTOR_THREE + BEFORE
132                      + INTERCEPTOR_ONE + BEFORE + INTERCEPTOR_TWO + BEFORE + INTERCEPTOR_THREE + BEFORE
133                      + COMPONENT + INTERCEPTOR_THREE + AFTER + INTERCEPTOR_TWO + AFTER + INTERCEPTOR_ONE
134                      + AFTER + INTERCEPTOR_THREE + AFTER + INTERCEPTOR_TWO + AFTER + INTERCEPTOR_ONE + AFTER,
135             result.getMessageAsString());
136     }
137 
138     class TestInterceptor extends AbstractEnvelopeInterceptor
139     {
140 
141         private String name;
142 
143         public TestInterceptor(String name)
144         {
145             this.name = name;
146         }
147 
148         @Override
149         public MuleEvent after(MuleEvent event)
150         {
151             try
152             {
153                 event.getMessage().setPayload(event.getMessage().getPayloadAsString() + name + AFTER);
154             }
155             catch (Exception e)
156             {
157                 e.printStackTrace();
158                 fail(e.getMessage());
159             }
160             return event;
161         }
162 
163         @Override
164         public MuleEvent before(MuleEvent event)
165         {
166             try
167             {
168                 event.getMessage().setPayload(event.getMessage().getPayloadAsString() + name + BEFORE);
169             }
170             catch (Exception e)
171             {
172                 e.printStackTrace();
173                 fail(e.getMessage());
174             }
175             return event;
176         }
177     }
178 
179     protected Service createUninitializedService() throws Exception
180     {
181         TestComponent component = new TestComponent();
182         Service service = new SedaService(muleContext);
183         service.setName("name");
184         service.setComponent(component);
185         service.setModel(muleContext.getRegistry().lookupSystemModel());
186         return service;
187     }
188 
189     class TestComponent extends AbstractComponent
190     {
191         @Override
192         protected Object doInvoke(MuleEvent event) throws Exception
193         {
194             return event.getMessageAsString() + COMPONENT;
195         }
196     }
197 
198 }