View Javadoc

1   /*
2    * $Id: FTPConnectorTestCase.java 19855 2010-10-06 19:50:56Z aperepel $
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  
27  /**
28   * Test configuration of FTP connector. It's all done in code, no configuration files
29   * are used and Mule is not started.
30   */
31  public class FTPConnectorTestCase extends AbstractConnectorTestCase
32  {
33      static final long POLLING_FREQUENCY = 1234;
34      static final long POLLING_FREQUENCY_OVERRIDE = 4321;
35      static final String TEST_ENDPOINT_URI = "ftp://foo:bar@example.com";
36      static final String VALID_MESSAGE = "This is a valid FTP message";
37  
38      @Override
39      public Connector createConnector() throws Exception
40      {
41          return internalGetConnector(false);
42      }
43  
44      @Override
45      public Object getValidMessage() throws Exception
46      {
47          return VALID_MESSAGE.getBytes();
48      }
49  
50      @Override
51      public String getTestEndpointURI()
52      {
53          return TEST_ENDPOINT_URI;
54      }
55  
56      /**
57       * Test polling frequency set on a connector.
58       */
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      public void testPollingFrequencyEndpointOverride() throws Exception
73      {
74          Map<Object, Object> props = new HashMap<Object, Object>();
75          // Endpoint wants String-typed properties
76          props.put(FtpConnector.PROPERTY_POLLING_FREQUENCY, String.valueOf(POLLING_FREQUENCY_OVERRIDE));
77          
78          InboundEndpoint endpoint = getTestInboundEndpoint("mock", null, null, null, props, null);
79  
80          Service service = getTestService("apple", Apple.class);
81          FtpConnector connector = (FtpConnector)getConnector();
82          MessageReceiver receiver = connector.createReceiver(service, endpoint);
83          assertEquals("Polling frequency endpoint override must not be ignored.", POLLING_FREQUENCY_OVERRIDE,
84              ((FtpMessageReceiver)receiver).getFrequency());
85      }
86  
87      /**
88       * Test setting a connection factory on a ftp endpoint.
89       * @throws Exception
90       */
91      public void testCustomFtpConnectionFactory() throws Exception
92      {
93          final String testObject = "custom object";
94  
95          final ImmutableEndpoint endpoint = muleContext.getRegistry()
96              .lookupEndpointFactory()
97              .getOutboundEndpoint("ftp://test:test@example.com");
98          final EndpointURI endpointURI = endpoint.getEndpointURI();
99  
100         FtpConnectionFactory testFactory = new TestFtpConnectionFactory(endpointURI);
101         FtpConnector connector = (FtpConnector)getConnector();
102 
103         connector.setConnectionFactoryClass(testFactory.getClass().getName());
104         // no validate call for simplicity
105         connector.setValidateConnections(false);
106 
107         ObjectPool pool = connector.getFtpPool(endpointURI);
108         Object obj = pool.borrowObject();
109         assertEquals("Custom FTP connection factory has been ignored.", testObject, obj);
110     }
111 
112 
113     /**
114      * Workaround. The super getConnector() call will init the connector,
115      * but for some tests we want more config steps before the initialisation.
116      */
117     protected FtpConnector internalGetConnector(boolean initialiseConnector) throws Exception
118     {
119         FtpConnector connector = new FtpConnector(muleContext);
120         connector.setName("testFTP");
121         connector.setPollingFrequency(POLLING_FREQUENCY);
122 
123         if (initialiseConnector)
124         {
125             connector.initialise();
126         }
127 
128         return connector;
129     }
130 
131     public static final class TestFtpConnectionFactory extends FtpConnectionFactory {
132 
133         public TestFtpConnectionFactory(EndpointURI uri)
134         {
135             super(uri);
136         }
137 
138         @Override
139         public Object makeObject() throws Exception
140         {
141             return "custom object";
142         }
143 
144         @Override
145         public void activateObject(final Object obj) throws Exception
146         {
147             // empty no-op, do not call super
148         }
149 
150     }
151     
152 }