View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.test.integration.transport.cxf;
8   
9   import org.mule.api.MuleMessage;
10  import org.mule.api.processor.MessageProcessor;
11  import org.mule.endpoint.AbstractEndpointBuilder;
12  import org.mule.module.client.MuleClient;
13  import org.mule.module.cxf.CxfOutboundMessageProcessor;
14  import org.mule.module.cxf.config.FlowConfiguringMessageProcessor;
15  import org.mule.tck.junit4.FunctionalTestCase;
16  
17  import java.util.List;
18  import java.util.Map;
19  
20  import org.apache.cxf.endpoint.Client;
21  import org.junit.Test;
22  
23  import static org.junit.Assert.assertNotNull;
24  import static org.junit.Assert.assertTrue;
25  
26  public class CxfClientPassivateTestCase extends FunctionalTestCase
27  {
28  
29      @Override
30      protected String getConfigResources()
31      {
32          return "org/mule/test/integration/transport/cxf/cxf-memoryleak-config.xml";
33      }
34  
35      /**
36       * Test that CxfMessageDispatcher's passivate method cleans up the CXF
37       * client request and response contexts. This is needed in order to release
38       * the memory occupied by that objects, which could be a lot of wasted
39       * memory because of the number of CxfMessageDispatchers that live in the
40       * dispatcher pool. See MULE-4899 for more details.
41       */
42      @Test
43      public void testPassivateCleansClientRequestAndResponseContext() throws Exception
44      {
45          MuleClient muleClient = new MuleClient(muleContext);
46  
47          // Sends data to process
48          muleClient.send("vm://in", TEST_MESSAGE, null);
49  
50          // Waits for a response
51          MuleMessage message = muleClient.request("vm://out", 5000);
52          assertNotNull(message);
53  
54          CxfOutboundMessageProcessor processor = getOutboundMessageProcessor();
55  
56          Client client = processor.getClient();
57  
58          final Map<String, Object> requestContext = client.getRequestContext();
59          assertTrue("Request context should be empty", requestContext.isEmpty());
60  
61          final Map<String, Object> responseContext = client.getResponseContext();
62          assertTrue("Response context should be empty", responseContext.isEmpty());
63      }
64  
65      private CxfOutboundMessageProcessor getOutboundMessageProcessor()
66      {
67          AbstractEndpointBuilder epbuilder = (AbstractEndpointBuilder) muleContext.getRegistry().lookupEndpointBuilder("clientEndpoint");
68          
69          List<MessageProcessor> mps = epbuilder.getMessageProcessors();
70          return (CxfOutboundMessageProcessor) ((FlowConfiguringMessageProcessor)mps.get(0)).getWrappedMessageProcessor();
71      }
72  
73  }