1   /*
2    * $Id: FTPConnectorTestCase.java 10961 2008-02-22 19:01:02Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.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       * (non-Javadoc)
35       * 
36       * @see org.mule.tck.providers.AbstractConnectorTestCase#createConnector()
37       */
38      // @Override
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       * Test polling frequency set on a connector.
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       * Test polling frequency overridden at an endpoint level.
69       */
70      public void testPollingFrequencyEndpointOverride() throws Exception
71      {
72          Properties props = new Properties();
73          // Endpoint wants String-typed properties
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          // no validate call for simplicity
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      * Workaround. The super getConnector() call will init the connector,
109      * but for some tests we want more config steps before the initialisation.
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             // empty no-op, do not call super
140         }
141 
142     }
143     
144 }