View Javadoc

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