View Javadoc

1   /*
2    * $Id: Mule4412TestCase.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;
12  
13  import org.mule.api.MuleMessage;
14  import org.mule.api.transport.PropertyScope;
15  import org.mule.module.client.MuleClient;
16  import org.mule.tck.FunctionalTestCase;
17  import org.mule.test.filters.FilterCounter;
18  
19  /**
20   * Test for MULE-4412 : selective-consumer filter is applied twice. We test that the
21   * filter is only applied once in the positive case, plus make sure it doesn't get
22   * filtered at all when the message does not meet the filter criteria
23   */
24  public class Mule4412TestCase extends FunctionalTestCase
25  {
26      private int RECEIVE_TIMEOUT_MS = 3000;
27      
28      protected String getConfigResources()
29      {
30          return "mule-4412.xml";
31      }
32  
33      @Override
34      protected void doSetUp() throws Exception
35      {
36          super.doSetUp();
37          // reset the counter for every test
38          FilterCounter.counter.set(0);
39      }
40  
41      @Override
42      protected void doTearDown() throws Exception
43      {
44          super.doTearDown();
45          // reset the counter for every test
46          FilterCounter.counter.set(0);
47      }
48  
49      /**
50       * Make sure that the message only gets filtered once
51       * 
52       * @throws Exception
53       */
54      public void testFilterOnce() throws Exception
55      {
56          DefaultMuleMessage msg = new DefaultMuleMessage(TEST_MESSAGE, muleContext);
57          msg.setOutboundProperty("pass", "true");
58          MuleClient client = new MuleClient(muleContext);
59          client.send("vm://async", msg);
60          MuleMessage reply = client.request("vm://asyncResponse", RECEIVE_TIMEOUT_MS);
61          int times = FilterCounter.counter.get();
62          assertTrue("did not filter one time as expected, times filtered " + times, times == 1);
63          assertNotNull(reply);
64          assertEquals("wrong message received : " + reply.getPayloadAsString(), TEST_MESSAGE,
65              reply.getPayloadAsString());
66          assertEquals("'pass' property value not correct", "true", reply.getInboundProperty("pass"));
67  
68          // make sure there are no more messages
69          assertNull(client.request("vm://asyncResponse", RECEIVE_TIMEOUT_MS));
70      }
71  
72      /**
73       * Make sure the message does not get filtered when the property key is incorrect
74       * 
75       * @throws Exception
76       */
77      public void testWrongPropertyKey() throws Exception
78      {
79          DefaultMuleMessage msg = new DefaultMuleMessage(TEST_MESSAGE, muleContext);
80          msg.setProperty("fail", "true", PropertyScope.INVOCATION);
81          MuleClient client = new MuleClient(muleContext);
82          client.send("vm://async", msg);
83          MuleMessage reply = client.request("vm://asyncResponse", RECEIVE_TIMEOUT_MS);
84          assertNull(reply);
85          assertTrue("should not have filtered", FilterCounter.counter.get() == 0);
86      }
87  
88      /**
89       * Make sure the message does not get filtered when the property value is not as
90       * expected
91       * 
92       * @throws Exception
93       */
94      public void testWrongPropertyValue() throws Exception
95      {
96          DefaultMuleMessage msg = new DefaultMuleMessage(TEST_MESSAGE, muleContext);
97          msg.setInboundProperty("pass", "false");
98          MuleClient client = new MuleClient(muleContext);
99          client.send("vm://async", msg);
100         MuleMessage reply = client.request("vm://asyncResponse", RECEIVE_TIMEOUT_MS);
101         assertNull(reply);
102         assertTrue("should not have filtered", FilterCounter.counter.get() == 0);
103     }
104 
105     /**
106      * Make sure the message does not get filtered at all when the expected property
107      * is not defined
108      * 
109      * @throws Exception
110      */
111     public void testNoProperty() throws Exception
112     {
113         DefaultMuleMessage msg = new DefaultMuleMessage(TEST_MESSAGE, muleContext);
114         MuleClient client = new MuleClient(muleContext);
115         client.send("vm://async", msg);
116         MuleMessage reply = client.request("vm://asyncResponse", RECEIVE_TIMEOUT_MS);
117         assertNull(reply);
118         assertTrue("should not have filtered", FilterCounter.counter.get() == 0);
119     }
120 }