1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.ftp;
12
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.assertTrue;
16
17 import org.mule.api.MuleEventContext;
18 import org.mule.module.client.MuleClient;
19 import org.mule.tck.functional.EventCallback;
20 import org.mule.tck.functional.FunctionalTestComponent;
21
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.concurrent.CountDownLatch;
25 import java.util.concurrent.TimeUnit;
26 import java.util.concurrent.atomic.AtomicReference;
27
28 import org.junit.Test;
29 import org.junit.runners.Parameterized.Parameters;
30
31 public class FtpFunctionalTestCase extends AbstractFtpServerTestCase
32 {
33 public FtpFunctionalTestCase(ConfigVariant variant, String configResources)
34 {
35 super(variant, configResources);
36 }
37
38 @Parameters
39 public static Collection<Object[]> parameters()
40 {
41 return Arrays.asList(new Object[][]{
42 {ConfigVariant.SERVICE, "ftp-functional-test-service.xml"},
43 {ConfigVariant.FLOW, "ftp-functional-test-flow.xml"}
44 });
45 }
46
47 @Test
48 public void testSendAndRequest() throws Exception
49 {
50 CountDownLatch latch = new CountDownLatch(1);
51 AtomicReference<String> message = new AtomicReference<String>();
52
53 Object component = getComponent("testComponent");
54 assertNotNull(component);
55 assertTrue("FunctionalTestComponent expected", component instanceof FunctionalTestComponent);
56 FunctionalTestComponent ftc = (FunctionalTestComponent) component;
57 ftc.setEventCallback(new FunctionalEventCallback(latch, message));
58
59 MuleClient client = new MuleClient(muleContext);
60 client.dispatch(getMuleFtpEndpoint(), TEST_MESSAGE, null);
61
62
63
64
65
66
67 latch.await(getTimeout(), TimeUnit.MILLISECONDS);
68 assertEquals(TEST_MESSAGE, message.get());
69 }
70
71 protected static class FunctionalEventCallback implements EventCallback
72 {
73 private CountDownLatch latch;
74 private AtomicReference<String> message;
75
76 public FunctionalEventCallback(CountDownLatch latch, AtomicReference<String> message)
77 {
78 super();
79 this.latch = latch;
80 this.message = message;
81 }
82
83 @Override
84 public synchronized void eventReceived(MuleEventContext context, Object component)
85 {
86 try
87 {
88 FunctionalTestComponent ftc = (FunctionalTestComponent) component;
89
90
91 if (1 == latch.getCount())
92 {
93 String o = new String((byte[])ftc.getLastReceivedMessage());
94 message.set(o);
95 latch.countDown();
96 }
97 }
98 catch (Exception e)
99 {
100 throw new IllegalStateException(e);
101 }
102 }
103 }
104 }