1
2
3
4
5
6
7 package org.mule.transport.ftp;
8
9 import org.mule.api.endpoint.EndpointURI;
10 import org.mule.api.endpoint.ImmutableEndpoint;
11 import org.mule.api.endpoint.InboundEndpoint;
12 import org.mule.api.service.Service;
13 import org.mule.api.transport.Connector;
14 import org.mule.api.transport.MessageReceiver;
15 import org.mule.tck.testmodels.fruit.Apple;
16 import org.mule.transport.AbstractConnectorTestCase;
17
18 import java.util.HashMap;
19 import java.util.Map;
20
21 import org.apache.commons.pool.ObjectPool;
22 import org.junit.Test;
23
24 import static org.junit.Assert.assertEquals;
25
26
27
28
29
30 public class FTPConnectorTestCase extends AbstractConnectorTestCase
31 {
32 static final long POLLING_FREQUENCY = 1234;
33 static final long POLLING_FREQUENCY_OVERRIDE = 4321;
34 static final String TEST_ENDPOINT_URI = "ftp://foo:bar@example.com";
35 static final String VALID_MESSAGE = "This is a valid FTP message";
36
37 @Override
38 public Connector createConnector() throws Exception
39 {
40 return internalGetConnector(false);
41 }
42
43 @Override
44 public Object getValidMessage() throws Exception
45 {
46 return VALID_MESSAGE.getBytes();
47 }
48
49 @Override
50 public String getTestEndpointURI()
51 {
52 return TEST_ENDPOINT_URI;
53 }
54
55
56
57
58 @Test
59 public void testConnectorPollingFrequency() throws Exception
60 {
61 InboundEndpoint endpoint = getTestInboundEndpoint("mock");
62 Service service = getTestService("apple", Apple.class);
63 FtpConnector connector = (FtpConnector)getConnector();
64 MessageReceiver receiver = connector.createReceiver(service, endpoint);
65 assertEquals("Connector's polling frequency must not be ignored.", POLLING_FREQUENCY,
66 ((FtpMessageReceiver)receiver).getFrequency());
67 }
68
69
70
71
72 @Test
73 public void testPollingFrequencyEndpointOverride() throws Exception
74 {
75 Map<Object, Object> props = new HashMap<Object, Object>();
76
77 props.put(FtpConnector.PROPERTY_POLLING_FREQUENCY, String.valueOf(POLLING_FREQUENCY_OVERRIDE));
78
79 InboundEndpoint endpoint = getTestInboundEndpoint("mock", null, null, null, props, null);
80
81 Service service = getTestService("apple", Apple.class);
82 FtpConnector connector = (FtpConnector)getConnector();
83 MessageReceiver receiver = connector.createReceiver(service, endpoint);
84 assertEquals("Polling frequency endpoint override must not be ignored.", POLLING_FREQUENCY_OVERRIDE,
85 ((FtpMessageReceiver)receiver).getFrequency());
86 }
87
88
89
90
91
92 @Test
93 public void testCustomFtpConnectionFactory() throws Exception
94 {
95 final String testObject = "custom object";
96
97 final ImmutableEndpoint endpoint = muleContext.getEndpointFactory()
98 .getOutboundEndpoint("ftp://test:test@example.com");
99 final EndpointURI endpointURI = endpoint.getEndpointURI();
100
101 FtpConnectionFactory testFactory = new TestFtpConnectionFactory(endpointURI);
102 FtpConnector connector = (FtpConnector)getConnector();
103
104 connector.setConnectionFactoryClass(testFactory.getClass().getName());
105
106 connector.setValidateConnections(false);
107
108 ObjectPool pool = connector.getFtpPool(endpointURI);
109 Object obj = pool.borrowObject();
110 assertEquals("Custom FTP connection factory has been ignored.", testObject, obj);
111 }
112
113
114
115
116
117
118 protected FtpConnector internalGetConnector(boolean initialiseConnector) throws Exception
119 {
120 FtpConnector connector = new FtpConnector(muleContext);
121 connector.setName("testFTP");
122 connector.setPollingFrequency(POLLING_FREQUENCY);
123
124 if (initialiseConnector)
125 {
126 connector.initialise();
127 }
128
129 return connector;
130 }
131
132 public static final class TestFtpConnectionFactory extends FtpConnectionFactory {
133
134 public TestFtpConnectionFactory(EndpointURI uri)
135 {
136 super(uri);
137 }
138
139 @Override
140 public Object makeObject() throws Exception
141 {
142 return "custom object";
143 }
144
145 @Override
146 public void activateObject(final Object obj) throws Exception
147 {
148
149 }
150
151 }
152
153 }