1   /*
2    * $Id: AutoDeleteOnFileDispatcherReceiverTestCase.java 7963 2007-08-21 08:53:15Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.providers.file;
12  
13  import org.mule.MuleManager;
14  import org.mule.impl.RequestContext;
15  import org.mule.tck.AbstractMuleTestCase;
16  import org.mule.umo.UMOEvent;
17  import org.mule.umo.UMOMessage;
18  import org.mule.umo.provider.UMOConnector;
19  import org.mule.util.FileUtils;
20  
21  import java.io.File;
22  
23  public class AutoDeleteOnFileDispatcherReceiverTestCase extends AbstractMuleTestCase
24  {
25  
26      private File validMessage;
27      private String tempDirName = "input";
28      File tempDir;
29      UMOConnector connector;
30          
31      public void testAutoDeleteFalseOnDispatcher() throws Exception
32      {
33          ((FileConnector)connector).setAutoDelete(false);
34                  
35          UMOEvent event = getTestEvent("TestData");
36          event = RequestContext.setEvent(event);
37  
38          UMOMessage message = RequestContext.getEventContext().receiveEvent(getTestEndpointURI()+"/"+tempDirName+"?connector=FileConnector", 50000);
39          assertNotNull(message.getPayload());
40               
41          File[] files = tempDir.listFiles();
42          assertTrue(files.length > 0);
43          for (int i = 0; i < files.length; i++)
44          {
45              assertTrue(files[i].getName().equals(message.getProperty(FileConnector.PROPERTY_ORIGINAL_FILENAME)));
46              files[i].delete();
47          }
48      }
49      
50      public void testAutoDeleteTrueOnDispatcher() throws Exception
51      {
52          ((FileConnector)connector).setAutoDelete(true);
53          
54          UMOEvent event = getTestEvent("TestData");
55          event = RequestContext.setEvent(event);
56          
57          UMOMessage message = RequestContext.getEventContext().receiveEvent(getTestEndpointURI()+"/"+tempDirName, 50000);
58          assertNotNull(message.getPayload());
59          
60          File[] files = tempDir.listFiles();
61          assertTrue(files.length == 0);
62      }
63      
64      protected void doSetUp() throws Exception
65      {
66          super.doSetUp();
67          // The working directory is deleted on tearDown
68          tempDir = FileUtils.newFile(MuleManager.getConfiguration().getWorkingDirectory(), tempDirName);
69          if (!tempDir.exists())
70          {
71              tempDir.mkdirs();
72          }
73          validMessage = File.createTempFile("hello", ".txt", tempDir);
74          assertNotNull(validMessage);
75          connector = getConnector();
76      }
77  
78      protected void doTearDown() throws Exception
79      {
80          // TestConnector dispatches events via the test: protocol to test://test
81          // endpoints, which seems to end up in a directory called "test" :(
82          FileUtils.deleteTree(FileUtils.newFile(getTestConnector().getProtocol()));
83          super.doTearDown();
84      }
85      
86      public UMOConnector getConnector() throws Exception {
87          UMOConnector connector = new FileConnector();
88          connector.setName("FileConnector");
89          MuleManager.getInstance().registerConnector(connector);
90          connector.initialise();
91          connector.startConnector();
92          return connector;
93      }
94  
95      public String getTestEndpointURI()
96      {
97          return "file://" + MuleManager.getConfiguration().getWorkingDirectory();
98      }
99  }