1
2
3
4
5
6
7
8
9
10
11 package org.mule.providers.ftp;
12
13 import org.mule.impl.endpoint.MuleEndpoint;
14 import org.mule.tck.providers.AbstractConnectorTestCase;
15 import org.mule.umo.UMOComponent;
16 import org.mule.umo.endpoint.UMOEndpoint;
17 import org.mule.umo.endpoint.UMOEndpointURI;
18 import org.mule.umo.endpoint.UMOImmutableEndpoint;
19 import org.mule.umo.lifecycle.InitialisationException;
20 import org.mule.umo.provider.UMOConnector;
21 import org.mule.umo.provider.UMOMessageReceiver;
22
23 import java.util.Properties;
24
25 import org.apache.commons.pool.ObjectPool;
26
27 public class FTPConnectorTestCase extends AbstractConnectorTestCase
28 {
29 static final long POLLING_FREQUENCY = 1234;
30 static final long POLLING_FREQUENCY_OVERRIDE = 4321;
31 static final String TEST_ENDPOINT_URI = "ftp://foo:bar@example.com";
32 static final String VALID_MESSAGE = "This is a valid FTP message";
33
34
35
36
37
38
39
40 public UMOConnector createConnector() throws Exception
41 {
42 return internalGetConnector(true);
43 }
44
45 public Object getValidMessage() throws Exception
46 {
47 return VALID_MESSAGE.getBytes();
48 }
49
50 public String getTestEndpointURI()
51 {
52 return TEST_ENDPOINT_URI;
53 }
54
55
56
57
58 public void testConnectorPollingFrequency() throws Exception
59 {
60 FtpConnector connector = (FtpConnector) getConnector();
61
62 UMOEndpoint endpoint = getTestEndpoint("mock", UMOImmutableEndpoint.ENDPOINT_TYPE_RECEIVER);
63 UMOComponent component = getTestComponent(getDescriptor());
64 UMOMessageReceiver receiver = connector.createReceiver(component, 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 FtpConnector connector = (FtpConnector) getConnector();
75
76 UMOEndpoint endpoint = getTestEndpoint("mock", UMOImmutableEndpoint.ENDPOINT_TYPE_RECEIVER);
77
78 Properties props = new Properties();
79
80 props.put(FtpConnector.PROPERTY_POLLING_FREQUENCY, String.valueOf(POLLING_FREQUENCY_OVERRIDE));
81 endpoint.setProperties(props);
82
83 UMOComponent component = getTestComponent(getDescriptor());
84 UMOMessageReceiver receiver = connector.createReceiver(component, endpoint);
85 assertEquals("Polling frequency endpoint override must not be ignored.", POLLING_FREQUENCY_OVERRIDE,
86 ((FtpMessageReceiver)receiver).getFrequency());
87 }
88
89 public void testCustomFtpConnectionFactory() throws Exception
90 {
91 final String testObject = "custom object";
92 FtpConnector connector = internalGetConnector(false);
93
94 final UMOEndpoint endpoint = new MuleEndpoint("ftp://test:test@example.com", false);
95 final UMOEndpointURI endpointURI = endpoint.getEndpointURI();
96
97 FtpConnectionFactory testFactory = new TestFtpConnectionFactory(endpointURI);
98
99 connector.setConnectionFactoryClass(testFactory.getClass().getName());
100
101 connector.setValidateConnections(false);
102 connector.initialise();
103
104 ObjectPool pool = connector.getFtpPool(endpointURI);
105 Object obj = pool.borrowObject();
106 assertEquals("Custom FTP connection factory has been ignored.", testObject, obj);
107 }
108
109 public void testInvalidCustomFtpConnectionFactory() throws Exception
110 {
111 FtpConnector connector = internalGetConnector(false);
112 connector.setConnectionFactoryClass("java.lang.Object");
113 try
114 {
115 connector.initialise();
116 fail("Should've thrown an InitialisationException");
117 }
118 catch (InitialisationException e)
119 {
120 assertEquals("Some other message?",
121 "FTP connectionFactoryClass is not an instance of org.mule.providers.ftp.FtpConnectionFactory",
122 e.getMessage());
123 }
124 }
125
126
127
128
129
130 protected FtpConnector internalGetConnector(boolean initialiseConnector) throws Exception
131 {
132 FtpConnector connector = new FtpConnector();
133 connector.setName("testFTP");
134 connector.setPollingFrequency(POLLING_FREQUENCY);
135
136 if (initialiseConnector)
137 {
138 connector.initialise();
139 }
140
141 return connector;
142 }
143
144 public static final class TestFtpConnectionFactory extends FtpConnectionFactory {
145
146 public TestFtpConnectionFactory(UMOEndpointURI uri)
147 {
148 super(uri);
149 }
150
151 public Object makeObject() throws Exception
152 {
153 return "custom object";
154 }
155
156 public void activateObject(final Object obj) throws Exception
157 {
158
159 }
160
161 }
162
163 }