View Javadoc

1   /*
2    * $Id: SftpPoolingFunctionalTestCase.java 22475 2011-07-20 14:30:04Z 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.sftp;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertTrue;
15  import static org.junit.Assert.fail;
16  
17  import java.io.IOException;
18  import java.util.ArrayList;
19  import java.util.Arrays;
20  import java.util.Collection;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.concurrent.CountDownLatch;
24  import java.util.concurrent.TimeUnit;
25  import java.util.concurrent.atomic.AtomicInteger;
26  
27  import org.apache.commons.io.IOUtils;
28  import org.junit.Test;
29  import org.junit.runners.Parameterized.Parameters;
30  import org.mule.api.MuleEventContext;
31  import org.mule.module.client.MuleClient;
32  import org.mule.tck.functional.EventCallback;
33  
34  /**
35   * <code>SftpPoolingFunctionalTestCase</code> tests sending an receiving multiple
36   * small text files.
37   */
38  public class SftpPoolingFunctionalTestCase extends AbstractSftpTestCase
39  {
40  
41      private static final long TIMEOUT = 30000;
42  
43      private List<String> sendFiles;
44      private List<String> receiveFiles;
45  
46      private int nrOfFiles = 100;
47  
48      public SftpPoolingFunctionalTestCase(ConfigVariant variant, String configResources)
49      {
50          super(variant, configResources);
51      }
52  
53      @Parameters
54      public static Collection<Object[]> parameters()
55      {
56          return Arrays.asList(new Object[][]{
57              {ConfigVariant.SERVICE, "mule-pooling-test-config-service.xml"},
58              {ConfigVariant.FLOW, "mule-pooling-test-config-flow.xml"}
59          });
60      }
61      @Override
62      protected void doSetUp() throws Exception
63      {
64          super.doSetUp();
65  
66          initEndpointDirectory("inboundEndpoint");
67      }
68  
69      @Test
70      public void testSftpConfig() throws Exception
71      {
72          SftpConnector c = (SftpConnector) muleContext.getRegistry().lookupConnector("sftp-pool");
73          assertEquals(3, c.getMaxConnectionPoolSize());
74          assertEquals(true, c.useConnectionPool());
75  
76          SftpConnector c2 = (SftpConnector) muleContext.getRegistry().lookupConnector("sftp-no-pool");
77          assertEquals(false, c2.useConnectionPool());
78      }
79  
80      @Test
81      public void testSendAndReceiveMultipleFiles() throws Exception
82      {
83          sendFiles = new ArrayList<String>();
84  
85          for (int i = 1; i <= nrOfFiles; i++)
86          {
87              sendFiles.add("file" + i);
88          }
89          sendAndReceiveFiles();
90      }
91  
92      protected void sendAndReceiveFiles() throws Exception
93      {
94          final CountDownLatch latch = new CountDownLatch(sendFiles.size());
95          final AtomicInteger loopCount = new AtomicInteger(0);
96  
97          MuleClient client = new MuleClient(muleContext);
98  
99          receiveFiles = new ArrayList<String>();
100 
101         EventCallback callback = new EventCallback()
102         {
103             @Override
104             public void eventReceived(MuleEventContext context, Object component) throws Exception
105             {
106 
107                 String filename = context.getMessage().getProperty(SftpConnector.PROPERTY_ORIGINAL_FILENAME,
108                     null);
109                 SftpInputStream inputStream = null;
110                 try
111                 {
112                     logger.info("called " + loopCount.incrementAndGet() + " times. Filename = " + filename);
113 
114                     // This is not thread safe! (it should be safe if
115                     // synchronous="true" is used)
116                     // FunctionalTestComponent ftc = (FunctionalTestComponent)
117                     // component;
118                     // inputStream = (SftpInputStream) ftc.getLastReceivedMessage();
119 
120                     // Use this instead!
121                     inputStream = (SftpInputStream) context.getMessage().getPayload();
122                     String o = IOUtils.toString(inputStream);
123                     if (sendFiles.contains(o))
124                     {
125                         logger.info("The received file was added. Received: '" + o + "'");
126                         receiveFiles.add(o);
127                     }
128                     else
129                     {
130                         fail("The received file was not sent. Received: '" + o + "'");
131                     }
132 
133                     latch.countDown();
134                 }
135                 catch (IOException e)
136                 {
137                     logger.error("Error occured while processing callback for file=" + filename, e);
138                     throw e;
139                 }
140                 finally
141                 {
142                     if (inputStream != null)
143                     {
144                         inputStream.close();
145                     }
146                 }
147             }
148         };
149 
150         getFunctionalTestComponent("receiving").setEventCallback(callback);
151 
152         for (String sendFile : sendFiles)
153         {
154             HashMap<String, String> props = new HashMap<String, String>(1);
155             props.put(SftpConnector.PROPERTY_FILENAME, sendFile + ".txt");
156 
157             client.dispatch("vm://test.upload", sendFile, props);
158         }
159 
160         boolean done = latch.await(TIMEOUT, TimeUnit.MILLISECONDS);
161         // assertTrue("The test should not time out", done);
162 
163         logger.debug("Number of files sent: " + sendFiles.size());
164         logger.debug("Number of files received: " + receiveFiles.size());
165 
166         // This makes sure we received the same number of files we sent, and that
167         // the content was a match (since only matched content gets on the
168         // receiveFiles ArrayList)
169         assertTrue("expected : " + sendFiles.size() + " but got " + receiveFiles.size(),
170             sendFiles.size() == receiveFiles.size());
171     }
172 }