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