1   /*
2    * $Id: FTPConnectorTestCase.java 7968 2007-08-21 12:01:57Z holger $
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      // @Override
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       * Test polling frequency set on a connector.
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       * Test polling frequency overridden at an endpoint level.
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          // Endpoint wants String-typed properties
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         // no validate call for simplicity
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      * Workaround. The super getConnector() call will init the connector,
128      * but for some tests we want more config steps before the initialisation.
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             // empty no-op, do not call super
159         }
160 
161     }
162     
163 }