View Javadoc

1   /*
2    * $Id: SharedInterceptorStackTestCase.java 22421 2011-07-15 05:05:06Z dirk.olmes $
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.test.integration.interceptor;
12  
13  import org.mule.api.MuleEvent;
14  import org.mule.api.MuleException;
15  import org.mule.api.MuleMessage;
16  import org.mule.api.client.MuleClient;
17  import org.mule.api.interceptor.Interceptor;
18  import org.mule.processor.AbstractInterceptingMessageProcessor;
19  import org.mule.tck.AbstractServiceAndFlowTestCase;
20  
21  import java.util.Arrays;
22  import java.util.Collection;
23  
24  import org.junit.Test;
25  import org.junit.runners.Parameterized.Parameters;
26  
27  import static org.junit.Assert.assertEquals;
28  
29  public class SharedInterceptorStackTestCase extends AbstractServiceAndFlowTestCase
30  {
31      @Parameters
32      public static Collection<Object[]> parameters()
33      {
34          return Arrays.asList(new Object[][]{
35              {ConfigVariant.SERVICE, "shared-interceptor-stack-service.xml"},
36              {ConfigVariant.FLOW, "shared-interceptor-stack-flow.xml"}
37          });
38      }
39  
40      public SharedInterceptorStackTestCase(ConfigVariant variant, String configResources)
41      {
42          super(variant, configResources);
43      }
44  
45      @Test
46      public void testSharedInterceptorOnServiceOne() throws MuleException
47      {
48          MuleClient client = muleContext.getClient();
49  
50          MuleMessage response = client.send("vm://stackOne", TEST_MESSAGE, null);
51          assertEquals(TEST_MESSAGE + " CustomInterceptor ComponentOne", response.getPayload());
52      }
53  
54      @Test
55      public void testSharedInterceptorOnServiceTwo() throws MuleException
56      {
57          MuleClient client = muleContext.getClient();
58  
59          MuleMessage response = client.send("vm://stackTwo", TEST_MESSAGE, null);
60          assertEquals(TEST_MESSAGE + " CustomInterceptor ComponentTwo", response.getPayload());
61      }
62  
63      public static class CustomInterceptor extends AbstractInterceptingMessageProcessor implements Interceptor
64      {
65          @Override
66          public MuleEvent process(MuleEvent event) throws MuleException
67          {
68              MuleMessage message = event.getMessage();
69              String payload = message.getPayload().toString();
70              message.setPayload(payload + " CustomInterceptor");
71              return processNext(event);
72          }
73      }
74  
75      public static class CustomComponent
76      {
77          private String appendString;
78  
79          public String process(String input)
80          {
81              return input + appendString;
82          }
83  
84          public void setAppendString(String string)
85          {
86              this.appendString = string;
87          }
88      }
89  }