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 import org.mule.tck.functional.FunctionalTestComponent;
13
14 import java.util.HashMap;
15 import java.util.Map;
16
17 import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
18 import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
19 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger;
20 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicReference;
21 import org.apache.commons.io.IOUtils;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.junit.Test;
25
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
29
30
31
32
33
34
35
36 public class SftpFileAgeFunctionalTestCase extends AbstractSftpTestCase
37 {
38
39 private static final Log logger = LogFactory.getLog(SftpFileAgeFunctionalTestCase.class);
40
41 private static final String INBOUND_ENDPOINT_NAME = "inboundEndpoint";
42
43 protected static final long TIMEOUT = 10000 * 6;
44
45 @Override
46 protected String getConfigResources()
47 {
48 return "mule-sftp-file-age-config.xml";
49 }
50
51 @Override
52 protected void doSetUp() throws Exception
53 {
54 super.doSetUp();
55
56 initEndpointDirectory("inboundEndpoint");
57 }
58
59 @Test
60 public void testFileAge() throws Exception
61 {
62 final CountDownLatch latch = new CountDownLatch(1);
63 final AtomicReference message = new AtomicReference();
64 final AtomicInteger loopCount = new AtomicInteger(0);
65
66 EventCallback callback = new EventCallback()
67 {
68 public synchronized void eventReceived(MuleEventContext context, Object component)
69 {
70 try
71 {
72 logger.info("called " + loopCount.incrementAndGet() + " times");
73
74 if (1 == latch.getCount())
75 {
76 String o = IOUtils.toString((SftpInputStream) context.getMessage().getPayload());
77 message.set(o);
78 latch.countDown();
79 }
80 }
81 catch (Exception e)
82 {
83 logger.error(e.getMessage(), e);
84 }
85 }
86 };
87
88 MuleClient client = new MuleClient(muleContext);
89
90
91
92
93 Object component = getComponent("testComponent");
94 assertTrue("FunctionalTestComponent expected", component instanceof FunctionalTestComponent);
95 FunctionalTestComponent ftc = (FunctionalTestComponent) component;
96 assertNotNull(ftc);
97
98 ftc.setEventCallback(callback);
99
100
101 Map properties = new HashMap();
102
103
104 long startTime = System.currentTimeMillis();
105
106 logger.debug("before dispatch");
107
108
109 client.dispatch(getAddressByEndpoint(client, INBOUND_ENDPOINT_NAME), TEST_MESSAGE, properties);
110 logger.debug("before retrieve");
111
112 latch.await(TIMEOUT, TimeUnit.MILLISECONDS);
113
114
115
116
117 long time = System.currentTimeMillis() - startTime;
118
119 int maxTimeDiff = 1000;
120
121
122
123 int expectedMinTime = 2000 - maxTimeDiff;
124 assertTrue("The total time should never be less the 'fileAge' ms (was " + time + ", expected "
125 + expectedMinTime + ")", time > expectedMinTime);
126
127 assertEquals(TEST_MESSAGE, message.get());
128 }
129
130 }