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