View Javadoc

1   /*
2    * $Id: FileConnectorTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
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.file;
12  
13  import org.mule.api.MuleMessage;
14  import org.mule.api.endpoint.EndpointBuilder;
15  import org.mule.api.endpoint.InboundEndpoint;
16  import org.mule.api.endpoint.OutboundEndpoint;
17  import org.mule.api.lifecycle.InitialisationException;
18  import org.mule.api.service.Service;
19  import org.mule.api.transport.Connector;
20  import org.mule.api.transport.MessageReceiver;
21  import org.mule.endpoint.EndpointURIEndpointBuilder;
22  import org.mule.endpoint.URIBuilder;
23  import org.mule.transport.AbstractConnectorTestCase;
24  import org.mule.util.FileUtils;
25  
26  import java.io.File;
27  import java.util.Arrays;
28  
29  public class FileConnectorTestCase extends AbstractConnectorTestCase
30  {
31      private static final long POLLING_FREQUENCY = 1234;
32      private static final long POLLING_FREQUENCY_OVERRIDE = 4321;
33      private static final String VALID_MESSAGE = "validMessage";
34      
35      private File validMessage;
36  
37      @Override
38      protected void doSetUp() throws Exception
39      {
40          super.doSetUp();
41  
42          // The working directory is deleted on tearDown
43          File tempDir = FileUtils.newFile(muleContext.getConfiguration().getWorkingDirectory(), "tmp");
44          if (!tempDir.exists())
45          {
46              tempDir.mkdirs();
47          }
48  
49          validMessage = File.createTempFile("simple", ".mule", tempDir);
50          assertNotNull(validMessage);
51          FileUtils.writeStringToFile(validMessage, VALID_MESSAGE);
52      }
53  
54      @Override
55      protected void doTearDown() throws Exception
56      {
57          // TestConnector dispatches events via the test: protocol to test://test
58          // endpoints, which seems to end up in a directory called "test" :(
59          FileUtils.deleteTree(FileUtils.newFile(getTestConnector().getProtocol()));
60          super.doTearDown();
61      }
62  
63      @Override
64      public Connector createConnector() throws Exception
65      {
66          FileConnector connector = new FileConnector(muleContext);
67          connector.setName("testFile");
68          connector.setOutputAppend(true);
69          return connector;
70      }
71  
72      @Override
73      public String getTestEndpointURI()
74      {
75          return "file://" + muleContext.getConfiguration().getWorkingDirectory();
76      }
77  
78      @Override
79      public Object getValidMessage() throws Exception
80      {
81          return validMessage;
82      }
83  
84      /**
85       * Test polling frequency set on a connector.
86       */
87      public void testConnectorPollingFrequency() throws Exception
88      {
89          FileConnector connector = (FileConnector) getConnector();
90          connector.setPollingFrequency(POLLING_FREQUENCY);
91  
92          InboundEndpoint endpoint = getTestInboundEndpoint("simple");
93          Service service = getTestService();
94          MessageReceiver receiver = connector.createReceiver(service, endpoint);
95          assertEquals("Connector's polling frequency must not be ignored.", POLLING_FREQUENCY,
96                  ((FileMessageReceiver) receiver).getFrequency());
97      }
98  
99      /**
100      * Test polling frequency overridden at an endpoint level.
101      */
102     public void testPollingFrequencyEndpointOverride() throws Exception
103     {
104         FileConnector connector = (FileConnector) getConnector();
105         // set some connector-level value which we are about to override
106         connector.setPollingFrequency(-1);
107 
108         InboundEndpoint endpoint = getTestInboundEndpoint("simple");
109 
110         // Endpoint wants String-typed properties
111         endpoint.getProperties().put(FileConnector.PROPERTY_POLLING_FREQUENCY, String.valueOf(POLLING_FREQUENCY_OVERRIDE));
112 
113         Service service = getTestService();
114         MessageReceiver receiver = connector.createReceiver(service, endpoint);
115         assertEquals("Polling frequency endpoint override must not be ignored.", POLLING_FREQUENCY_OVERRIDE,
116                 ((FileMessageReceiver) receiver).getFrequency());
117     }
118 
119     public void testOutputAppendEndpointOverride() throws Exception
120     {
121         FileConnector connector = (FileConnector) getConnector();
122 
123         EndpointBuilder endpointBuilder = new EndpointURIEndpointBuilder(new URIBuilder("file://foo", muleContext));
124         OutboundEndpoint endpoint = endpointBuilder.buildOutboundEndpoint();
125 
126         // Endpoint wants String-typed properties
127         endpoint.getProperties().put("outputAppend", "true");
128 
129         try
130         {
131             connector.getDispatcherFactory().create(endpoint);
132             fail("outputAppend cannot be configured on File endpoints");
133         }
134         catch (IllegalArgumentException e)
135         {
136             //expected
137         }
138     }
139 
140     public void testOnlySingleDispatcherPerEndpoint() throws InitialisationException
141     {
142         // MULE-1773 implies that we must only have one dispatcher per endpoint
143         FileConnector connector = (FileConnector) getConnector();
144 
145         assertEquals(1, connector.getMaxDispatchersActive());
146 
147         connector.setMaxDispatchersActive(2);
148 
149         // value must be unchanged
150         assertEquals(1, connector.getMaxDispatchersActive());
151     }
152     
153     /**
154      * If the connector is configured not to do streaming it converts to byte[] so the original
155      * input payload is not the same as the payload in the MuleMessage
156      */
157     public void testConnectorMessageFactoryNonStreaming() throws Exception
158     {
159         Connector connector = getConnectorAndAssert();
160         ((FileConnector) connector).setStreaming(false);
161 
162         Object payload = getValidMessage();
163         MuleMessage message = connector.createMuleMessageFactory().create(payload, encoding);
164         assertNotNull(message);
165         
166         byte[] messagePayload = (byte[]) message.getPayload();
167         assertTrue(Arrays.equals(VALID_MESSAGE.getBytes(), messagePayload));
168     }
169 }