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.RequestContext;
10  import org.mule.api.MuleEvent;
11  import org.mule.api.MuleMessage;
12  import org.mule.api.transport.Connector;
13  import org.mule.tck.junit4.AbstractMuleContextTestCase;
14  import org.mule.util.FileUtils;
15  
16  import java.io.File;
17  import java.io.InputStream;
18  
19  import org.junit.Test;
20  
21  import static org.junit.Assert.assertNotNull;
22  import static org.junit.Assert.assertTrue;
23  
24  public class AutoDeleteOnFileDispatcherReceiverTestCase extends AbstractMuleContextTestCase
25  {
26  
27      private File validMessage;
28      private String tempDirName = "input";
29      File tempDir;
30      Connector connector;
31  
32      @Test
33      public void testAutoDeleteFalseOnDispatcher() throws Exception
34      {
35          ((FileConnector)connector).setAutoDelete(false);
36  
37          MuleEvent event = getTestEvent("TestData");
38          event = RequestContext.setEvent(event);
39  
40          MuleMessage message = RequestContext.getEventContext().requestEvent(getTestEndpointURI()+"/"+tempDirName+"?connector=FileConnector", 50000);
41          // read the payload into a string so the file is deleted on InputStream.close()
42          assertNotNull(message.getPayloadAsString());
43  
44          File[] files = tempDir.listFiles();
45          assertTrue(files.length > 0);
46          for (int i = 0; i < files.length; i++)
47          {
48              assertTrue(files[i].getName().equals(message.getOutboundProperty(FileConnector.PROPERTY_ORIGINAL_FILENAME)));
49              files[i].delete();
50          }
51      }
52  
53      @Test
54      public void testAutoDeleteTrueOnDispatcher() throws Exception
55      {
56          ((FileConnector)connector).setAutoDelete(true);
57  
58          MuleEvent event = getTestEvent("TestData");
59          event = RequestContext.setEvent(event);
60  
61          MuleMessage message = RequestContext.getEventContext().requestEvent(getTestEndpointURI()+"/"+tempDirName, 50000);
62          assertNotNull(message.getPayload());
63          assertTrue(message.getPayload() instanceof InputStream);
64  
65          // Auto-delete happens after FileInputStream.close() when streaming.  Streaming is default.
66          assertTrue(tempDir.listFiles().length > 0);
67          ((InputStream) message.getPayload()).close();
68          // Give file-system some time (annoying but necessary wait apparently due to OS caching?)
69          Thread.sleep(1000);
70          assertTrue(tempDir.listFiles().length == 0);
71          
72          
73      }
74  
75      protected void doSetUp() throws Exception
76      {
77          super.doSetUp();
78          // The working directory is deleted on tearDown
79          tempDir = FileUtils.newFile(muleContext.getConfiguration().getWorkingDirectory(), tempDirName);
80          tempDir.deleteOnExit();
81          if (!tempDir.exists())
82          {
83              tempDir.mkdirs();
84          }
85          validMessage = File.createTempFile("hello", ".txt", tempDir);
86          assertNotNull(validMessage);
87          connector = getConnector();
88          connector.start();
89      }
90  
91      protected void doTearDown() throws Exception
92      {
93          // TestConnector dispatches events via the test: protocol to test://test
94          // endpoints, which seems to end up in a directory called "test" :(
95          FileUtils.deleteTree(FileUtils.newFile(getTestConnector().getProtocol()));
96          super.doTearDown();
97      }
98  
99      public Connector getConnector() throws Exception {
100         Connector connector = new FileConnector(muleContext);
101         connector.setName("FileConnector");
102         muleContext.getRegistry().registerConnector(connector);
103         return connector;
104     }
105 
106     public String getTestEndpointURI()
107     {
108         return "file://" + muleContext.getConfiguration().getWorkingDirectory();
109     }
110 }