View Javadoc

1   /*
2    * $Id: AbstractCachingStrategyTestCase.java 23186 2011-10-16 14:17:10Z pablo.kraan $
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.cache.integration;
12  
13  import org.mule.DefaultMuleMessage;
14  import org.mule.api.MuleMessage;
15  import org.mule.api.client.MuleClient;
16  import org.mule.tck.junit4.FunctionalTestCase;
17  
18  import java.util.Map;
19  
20  import org.junit.Test;
21  
22  import static org.junit.Assert.assertEquals;
23  
24  /**
25   * Defines a contract test for {@link org.mule.cache.CachingStrategy}.
26   * <p/>
27   * Subclasses must provide a configuration of a cachingStrategy bean
28   */
29  public abstract class AbstractCachingStrategyTestCase extends FunctionalTestCase
30  {
31  
32      @Override
33      protected String getConfigResources()
34      {
35          return "org/mule/cache/integration/message-caching-config.xml," + getCachingStrategyConfigResource();
36      }
37  
38      /**
39       * @return comma separated list of the mule config file path's containing
40       *         the declaration of the caching strategy bean and any extra
41       *         needed configuration.
42       */
43      protected abstract String getCachingStrategyConfigResource();
44  
45      @Test
46      public void testCachesMessageRequestResponse() throws Exception
47      {
48          MuleClient client = muleContext.getClient();
49  
50          DefaultMuleMessage message1 = new DefaultMuleMessage("test1", (Map) null, muleContext);
51          DefaultMuleMessage message2 = new DefaultMuleMessage("test2", (Map) null, muleContext);
52  
53          MuleMessage msg = client.send("vm://testRequestResponse", message1);
54          assertEquals("0 Processed", msg.getPayload());
55  
56          msg = client.send("vm://testRequestResponse", message2);
57          assertEquals("1 Processed", msg.getPayload());
58  
59          // Checks that resending message 1 gets the response form the cache
60          msg = client.send("vm://testRequestResponse", message1);
61          assertEquals("0 Processed", msg.getPayload());
62      }
63  
64      @Test
65      public void testCachesMessageOneWay() throws Exception
66      {
67          MuleClient client = muleContext.getClient();
68  
69          DefaultMuleMessage message1 = new DefaultMuleMessage("test3", (Map) null, muleContext);
70          DefaultMuleMessage message2 = new DefaultMuleMessage("test4", (Map) null, muleContext);
71  
72          client.dispatch("vm://testOneWay", message1);
73          MuleMessage msg = client.request("vm://output", 5000);
74          assertEquals("0 Processed", msg.getPayload());
75  
76          client.dispatch("vm://testOneWay", message2);
77          msg = client.request("vm://output", 5000);
78          assertEquals("1 Processed", msg.getPayload());
79  
80          // Checks that resending message 1 gets the response form the cache
81          client.dispatch("vm://testOneWay", message1);
82          msg = client.request("vm://output", 5000);
83          assertEquals("0 Processed", msg.getPayload());
84      }
85  }