View Javadoc

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