1   /*
2    * $Id: FTPConnectorTestCase.java 7976 2007-08-21 14:26:13Z dirk.olmes $
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.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       * (non-Javadoc)
36       * 
37       * @see org.mule.tck.providers.AbstractConnectorTestCase#createConnector()
38       */
39      public UMOConnector getConnector() throws Exception
40      {
41          return internalGetConnector(true);
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          FtpConnector connector = (FtpConnector) getConnector();
60  
61          UMOEndpoint endpoint = getTestEndpoint("mock", UMOImmutableEndpoint.ENDPOINT_TYPE_RECEIVER);
62          UMOComponent component = getTestComponent(descriptor);
63          UMOMessageReceiver receiver = connector.createReceiver(component, endpoint);
64          assertEquals("Connector's polling frequency must not be ignored.", POLLING_FREQUENCY,
65              ((FtpMessageReceiver)receiver).getFrequency());
66      }
67  
68      /**
69       * Test polling frequency overridden at an endpoint level.
70       */
71      public void testPollingFrequencyEndpointOverride() throws Exception
72      {
73          FtpConnector connector = (FtpConnector) getConnector();
74  
75          UMOEndpoint endpoint = getTestEndpoint("mock", UMOImmutableEndpoint.ENDPOINT_TYPE_RECEIVER);
76  
77          Properties props = new Properties();
78          // Endpoint wants String-typed properties
79          props.put(FtpConnector.PROPERTY_POLLING_FREQUENCY, String.valueOf(POLLING_FREQUENCY_OVERRIDE));
80          endpoint.setProperties(props);
81  
82          UMOComponent component = getTestComponent(descriptor);
83          UMOMessageReceiver receiver = connector.createReceiver(component, endpoint);
84          assertEquals("Polling frequency endpoint override must not be ignored.", POLLING_FREQUENCY_OVERRIDE,
85              ((FtpMessageReceiver)receiver).getFrequency());
86      }
87  
88      public void testCustomFtpConnectionFactory() throws Exception
89      {
90          final String testObject = "custom object";
91          FtpConnector connector = internalGetConnector(false);
92  
93          final UMOEndpoint endpoint = new MuleEndpoint("ftp://test:test@example.com", false);
94          final UMOEndpointURI endpointURI = endpoint.getEndpointURI();
95  
96          FtpConnectionFactory testFactory = new TestFtpConnectionFactory(endpointURI);
97  
98          connector.setConnectionFactoryClass(testFactory.getClass().getName());
99          // no validate call for simplicity
100         connector.setValidateConnections(false);
101         connector.initialise();
102 
103         ObjectPool pool = connector.getFtpPool(endpointURI);
104         Object obj = pool.borrowObject();
105         assertEquals("Custom FTP connection factory has been ignored.", testObject, obj);
106     }
107 
108     public void testInvalidCustomFtpConnectionFactory() throws Exception
109     {
110         FtpConnector connector = internalGetConnector(false);
111         connector.setConnectionFactoryClass("java.lang.Object");
112         try
113         {
114             connector.initialise();
115             fail("Should've thrown an InitialisationException");
116         }
117         catch (InitialisationException e)
118         {
119             assertEquals("Some other message?",
120                          "FTP connectionFactoryClass is not an instance of org.mule.providers.ftp.FtpConnectionFactory",
121                          e.getMessage());
122         }
123     }
124 
125     /**
126      * Workaround. The super getConnector() call will init the connector,
127      * but for some tests we want more config steps before the initialisation.
128      */
129     protected FtpConnector internalGetConnector(boolean initialiseConnector) throws Exception
130     {
131         FtpConnector connector = new FtpConnector();
132         connector.setName("testFTP");
133         connector.setPollingFrequency(POLLING_FREQUENCY);
134 
135         if (initialiseConnector)
136         {
137             connector.initialise();
138         }
139 
140         return connector;
141     }
142 
143     public static final class TestFtpConnectionFactory extends FtpConnectionFactory {
144 
145         public TestFtpConnectionFactory(UMOEndpointURI uri)
146         {
147             super(uri);
148         }
149 
150         public Object makeObject() throws Exception
151         {
152             return "custom object";
153         }
154 
155         public void activateObject(final Object obj) throws Exception
156         {
157             // empty no-op, do not call super
158         }
159 
160     }
161     
162 }