View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
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   * Test configuration of FTP connector. It's all done in code, no configuration files
28   * are used and Mule is not started.
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       * Test polling frequency set on a connector.
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       * Test polling frequency overridden at an endpoint level.
71       */
72      @Test
73      public void testPollingFrequencyEndpointOverride() throws Exception
74      {
75          Map<Object, Object> props = new HashMap<Object, Object>();
76          // Endpoint wants String-typed properties
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       * Test setting a connection factory on a ftp endpoint.
90       * @throws Exception
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         // no validate call for simplicity
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      * Workaround. The super getConnector() call will init the connector,
116      * but for some tests we want more config steps before the initialisation.
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             // empty no-op, do not call super
149         }
150 
151     }
152 
153 }