1
2
3
4
5
6
7 package org.mule.transport.sftp;
8
9 import org.mule.api.MuleEventContext;
10 import org.mule.module.client.MuleClient;
11 import org.mule.tck.functional.EventCallback;
12
13 import java.io.IOException;
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.List;
17
18 import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
19 import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
20 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger;
21 import org.apache.commons.io.IOUtils;
22 import org.junit.Test;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27
28
29
30
31
32 public class SftpPoolingFunctionalTestCase extends AbstractSftpTestCase
33 {
34 private static final long TIMEOUT = 30000;
35
36 private List<String> sendFiles;
37 private List<String> receiveFiles;
38
39 private int nrOfFiles = 100;
40
41 @Override
42 protected String getConfigResources()
43 {
44 return "mule-pooling-test-config.xml";
45 }
46
47 @Override
48 protected void doSetUp() throws Exception
49 {
50 super.doSetUp();
51
52 initEndpointDirectory("inboundEndpoint");
53 }
54
55 @Test
56 public void testSftpConfig() throws Exception
57 {
58 SftpConnector c = (SftpConnector) muleContext.getRegistry().lookupConnector("sftp-pool");
59 assertEquals(3, c.getMaxConnectionPoolSize());
60 assertEquals(true, c.useConnectionPool());
61
62 SftpConnector c2 = (SftpConnector) muleContext.getRegistry().lookupConnector("sftp-no-pool");
63 assertEquals(false, c2.useConnectionPool());
64 }
65
66 @Test
67 public void testSendAndReceiveMultipleFiles() throws Exception
68 {
69 sendFiles = new ArrayList<String>();
70
71 for (int i = 1; i <= nrOfFiles; i++)
72 {
73 sendFiles.add("file" + i);
74 }
75 sendAndReceiveFiles();
76 }
77
78 protected void sendAndReceiveFiles() throws Exception
79 {
80 final CountDownLatch latch = new CountDownLatch(sendFiles.size());
81 final AtomicInteger loopCount = new AtomicInteger(0);
82
83 MuleClient client = new MuleClient(muleContext);
84
85 receiveFiles = new ArrayList<String>();
86
87 EventCallback callback = new EventCallback()
88 {
89 public void eventReceived(MuleEventContext context, Object component) throws Exception
90 {
91
92 String filename = context.getMessage().getProperty(SftpConnector.PROPERTY_ORIGINAL_FILENAME,
93 null);
94 SftpInputStream inputStream = null;
95 try
96 {
97 logger.info("called " + loopCount.incrementAndGet() + " times. Filename = " + filename);
98
99
100
101
102
103
104
105
106 inputStream = (SftpInputStream) context.getMessage().getPayload();
107 String o = IOUtils.toString(inputStream);
108 if (sendFiles.contains(o))
109 {
110 logger.info("The received file was added. Received: '" + o + "'");
111 receiveFiles.add(o);
112 }
113 else
114 {
115 fail("The received file was not sent. Received: '" + o + "'");
116 }
117
118 latch.countDown();
119 }
120 catch (IOException e)
121 {
122 logger.error("Error occured while processing callback for file=" + filename, e);
123 throw e;
124 }
125 finally
126 {
127 if (inputStream != null)
128 {
129 inputStream.close();
130 }
131 }
132 }
133 };
134
135 getFunctionalTestComponent("receiving").setEventCallback(callback);
136
137 for (String sendFile : sendFiles)
138 {
139 HashMap<String, String> props = new HashMap<String, String>(1);
140 props.put(SftpConnector.PROPERTY_FILENAME, sendFile + ".txt");
141
142 client.dispatch("vm://test.upload", sendFile, props);
143 }
144
145 boolean done = latch.await(TIMEOUT, TimeUnit.MILLISECONDS);
146
147
148 logger.debug("Number of files sent: " + sendFiles.size());
149 logger.debug("Number of files received: " + receiveFiles.size());
150
151
152
153
154 assertTrue("expected : " + sendFiles.size() + " but got " + receiveFiles.size(),
155 sendFiles.size() == receiveFiles.size());
156 }
157 }