View Javadoc

1   /*
2    * $Id: FTPConnectorTestCase.java 22377 2011-07-11 12:41:42Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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  import org.junit.Test;
27  
28  import static org.junit.Assert.assertEquals;
29  
30  /**
31   * Test configuration of FTP connector. It's all done in code, no configuration files
32   * are used and Mule is not started.
33   */
34  public class FTPConnectorTestCase extends AbstractConnectorTestCase
35  {
36      static final long POLLING_FREQUENCY = 1234;
37      static final long POLLING_FREQUENCY_OVERRIDE = 4321;
38      static final String TEST_ENDPOINT_URI = "ftp://foo:bar@example.com";
39      static final String VALID_MESSAGE = "This is a valid FTP message";
40  
41      @Override
42      public Connector createConnector() throws Exception
43      {
44          return internalGetConnector(false);
45      }
46  
47      @Override
48      public Object getValidMessage() throws Exception
49      {
50          return VALID_MESSAGE.getBytes();
51      }
52  
53      @Override
54      public String getTestEndpointURI()
55      {
56          return TEST_ENDPOINT_URI;
57      }
58  
59      /**
60       * Test polling frequency set on a connector.
61       */
62      @Test
63      public void testConnectorPollingFrequency() throws Exception
64      {
65          InboundEndpoint endpoint = getTestInboundEndpoint("mock");
66          Service service = getTestService("apple", Apple.class);
67          FtpConnector connector = (FtpConnector)getConnector();
68          MessageReceiver receiver = connector.createReceiver(service, endpoint);
69          assertEquals("Connector's polling frequency must not be ignored.", POLLING_FREQUENCY,
70              ((FtpMessageReceiver)receiver).getFrequency());
71      }
72  
73      /**
74       * Test polling frequency overridden at an endpoint level.
75       */
76      @Test
77      public void testPollingFrequencyEndpointOverride() throws Exception
78      {
79          Map<Object, Object> props = new HashMap<Object, Object>();
80          // Endpoint wants String-typed properties
81          props.put(FtpConnector.PROPERTY_POLLING_FREQUENCY, String.valueOf(POLLING_FREQUENCY_OVERRIDE));
82  
83          InboundEndpoint endpoint = getTestInboundEndpoint("mock", null, null, null, props, null);
84  
85          Service service = getTestService("apple", Apple.class);
86          FtpConnector connector = (FtpConnector)getConnector();
87          MessageReceiver receiver = connector.createReceiver(service, endpoint);
88          assertEquals("Polling frequency endpoint override must not be ignored.", POLLING_FREQUENCY_OVERRIDE,
89              ((FtpMessageReceiver)receiver).getFrequency());
90      }
91  
92      /**
93       * Test setting a connection factory on a ftp endpoint.
94       * @throws Exception
95       */
96      @Test
97      public void testCustomFtpConnectionFactory() throws Exception
98      {
99          final String testObject = "custom object";
100 
101         final ImmutableEndpoint endpoint = muleContext.getEndpointFactory()
102             .getOutboundEndpoint("ftp://test:test@example.com");
103         final EndpointURI endpointURI = endpoint.getEndpointURI();
104 
105         FtpConnectionFactory testFactory = new TestFtpConnectionFactory(endpointURI);
106         FtpConnector connector = (FtpConnector)getConnector();
107 
108         connector.setConnectionFactoryClass(testFactory.getClass().getName());
109         // no validate call for simplicity
110         connector.setValidateConnections(false);
111 
112         ObjectPool pool = connector.getFtpPool(endpointURI);
113         Object obj = pool.borrowObject();
114         assertEquals("Custom FTP connection factory has been ignored.", testObject, obj);
115     }
116 
117 
118     /**
119      * Workaround. The super getConnector() call will init the connector,
120      * but for some tests we want more config steps before the initialisation.
121      */
122     protected FtpConnector internalGetConnector(boolean initialiseConnector) throws Exception
123     {
124         FtpConnector connector = new FtpConnector(muleContext);
125         connector.setName("testFTP");
126         connector.setPollingFrequency(POLLING_FREQUENCY);
127 
128         if (initialiseConnector)
129         {
130             connector.initialise();
131         }
132 
133         return connector;
134     }
135 
136     public static final class TestFtpConnectionFactory extends FtpConnectionFactory {
137 
138         public TestFtpConnectionFactory(EndpointURI uri)
139         {
140             super(uri);
141         }
142 
143         @Override
144         public Object makeObject() throws Exception
145         {
146             return "custom object";
147         }
148 
149         @Override
150         public void activateObject(final Object obj) throws Exception
151         {
152             // empty no-op, do not call super
153         }
154 
155     }
156 
157 }