View Javadoc

1   /*
2    * $Id: VMFunctionalTestCase.java 22449 2011-07-19 07:40:43Z justin.calleja $
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.transport.vm;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertNotNull;
15  import static org.junit.Assert.assertNull;
16  import static org.junit.Assert.assertTrue;
17  
18  import java.io.Serializable;
19  import java.util.Arrays;
20  import java.util.Collection;
21  
22  import org.junit.Test;
23  import org.junit.runners.Parameterized.Parameters;
24  import org.mule.api.MuleMessage;
25  import org.mule.api.store.ObjectStoreException;
26  import org.mule.module.client.MuleClient;
27  import org.mule.tck.AbstractServiceAndFlowTestCase;
28  import org.mule.util.store.SimpleMemoryObjectStore;
29  
30  public class VMFunctionalTestCase extends AbstractServiceAndFlowTestCase
31  {
32      public VMFunctionalTestCase(ConfigVariant variant, String configResources)
33      {
34          super(variant, configResources);
35          setDisposeContextPerClass(true);
36      }
37  
38      @Parameters
39      public static Collection<Object[]> parameters()
40      {
41          return Arrays.asList(new Object[][]{
42              {ConfigVariant.SERVICE, "vm/vm-functional-test-service.xml"},
43              {ConfigVariant.FLOW, "vm/vm-functional-test-flow.xml"}
44          });
45      }
46  
47      @Test
48      public void testSingleMessage() throws Exception
49      {
50          MuleClient client = new MuleClient(muleContext);
51          client.dispatch("vm://in", "Marco", null);
52          MuleMessage response = client.request("vm://out", RECEIVE_TIMEOUT);
53          assertNotNull("Response is null", response);
54          assertEquals("Polo", response.getPayload());
55      }
56  
57      @Test
58      public void testRequest() throws Exception
59      {
60          MuleClient client = new MuleClient(muleContext);
61          client.dispatch("vm://in", "Marco", null);
62          MuleMessage response = client.request("vm://out", RECEIVE_TIMEOUT);
63          assertNotNull("Response is null", response);
64          assertEquals("Polo", response.getPayload());
65      }
66  
67      @Test
68      public void testMultipleMessages() throws Exception
69      {
70          MuleClient client = new MuleClient(muleContext);
71          client.dispatch("vm://in", "Marco", null);
72          client.dispatch("vm://in", "Marco", null);
73          client.dispatch("vm://in", "Marco", null);
74          MuleMessage response;
75          for (int i = 0; i < 3; ++i)
76          {
77              response = client.request("vm://out", RECEIVE_TIMEOUT);
78              assertNotNull("Response is null", response);
79              assertEquals("Polo", response.getPayload());
80          }
81  
82          MuleMessage secondMessage = client.request("vm://out", RECEIVE_TIMEOUT);
83          assertNull(secondMessage);
84      }
85  
86      @Test
87      public void testOneWayChain() throws Exception
88      {
89          MuleClient client = new MuleClient(muleContext);
90          client.dispatch("vm://in1", "Marco", null);
91          MuleMessage response = client.request("vm://out1", RECEIVE_TIMEOUT);
92          assertNotNull("Response is null", response);
93          assertEquals("Polo", response.getPayload());
94          assertTrue(CustomObjectStore.count > 0); // ensure custom store was used
95      }
96  
97      @Test
98      public void testRequestResponseChain() throws Exception
99      {
100         MuleClient client = new MuleClient(muleContext);
101         MuleMessage response = client.send("vm://in2", "Marco", null);
102         assertNotNull("Response is null", response);
103         assertEquals("Polo", response.getPayload());
104     }
105 
106     @Test
107     public void testNoMessageDuplication() throws Exception
108     {
109         MuleClient client = new MuleClient(muleContext);
110         client.dispatch("vm://in", "Marco", null);
111         MuleMessage response = client.request("vm://out", RECEIVE_TIMEOUT);
112         assertNotNull("Response is null", response);
113         assertEquals("Polo", response.getPayload());
114         MuleMessage secondMessage = client.request("vm://out", RECEIVE_TIMEOUT);
115         assertNull(secondMessage);
116     }
117 
118     public static class CustomObjectStore<T extends Serializable> extends SimpleMemoryObjectStore<T>
119     {
120         static int count;
121 
122         public CustomObjectStore()
123         {
124             super();
125         }
126 
127         @Override
128         protected void doStore(Serializable key, T value) throws ObjectStoreException
129         {
130             count++;
131             super.doStore(key, value);
132         }
133     }
134 }