View Javadoc

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