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