1   /*
2    * $Id: AbstractFileFunctionalTestCase.java 11179 2008-03-05 13:46:23Z dfeist $
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.transport.file;
12  
13  import org.mule.api.MuleMessage;
14  import org.mule.tck.FunctionalTestCase;
15  import org.mule.util.FileUtils;
16  import org.mule.util.IOUtils;
17  
18  import java.io.ByteArrayOutputStream;
19  import java.io.File;
20  import java.io.FileWriter;
21  import java.io.InputStream;
22  import java.io.Writer;
23  import java.net.MalformedURLException;
24  
25  /**
26   * We are careful here to access the file system in a generic way. This means setting
27   * directories dynamically.
28   */
29  public abstract class AbstractFileFunctionalTestCase extends FunctionalTestCase
30  {
31  
32      public static final String TEST_MESSAGE = "Test file contents";
33      public static final String TARGET_FILE = "TARGET_FILE";
34  
35      private File tmpDir;
36  
37      protected String getConfigResources()
38      {
39          return "file-functional-test.xml";
40      }
41  
42      protected String fileToUrl(File file) throws MalformedURLException
43      {
44          return file.getAbsoluteFile().toURI().toURL().toString();
45      }
46  
47      // annoying but necessary wait apparently due to OS caching?
48      protected void waitForFileSystem() throws Exception
49      {
50          synchronized (this)
51          {
52              wait(1000);
53          }
54      }
55  
56      protected File initForRequest() throws Exception
57      {
58          tmpDir = File.createTempFile("mule-file-test-", "-dir");
59          tmpDir.delete();
60          tmpDir.mkdir();
61          tmpDir.deleteOnExit();
62          File target = File.createTempFile("mule-file-test-", ".txt", tmpDir);
63          Writer out = new FileWriter(target);
64          out.write(TEST_MESSAGE);
65          out.close();
66          target.deleteOnExit();
67  
68          // define the readFromDirectory on the connector
69          FileConnector connector = (FileConnector) muleContext.getRegistry().lookupConnector(
70              "receiveConnector");
71          connector.setReadFromDirectory(tmpDir.getAbsolutePath());
72          logger.debug("Directory is " + connector.getReadFromDirectory());
73  
74          waitForFileSystem();
75          return target;
76      }
77  
78      protected void checkReceivedMessage(MuleMessage message) throws Exception
79      {
80          assertNotNull(message);
81          assertNotNull(message.getPayload());
82          assertTrue(message.getPayload() instanceof InputStream);
83  
84          InputStream fis = (InputStream) message.getPayload();
85          ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
86          IOUtils.copy(fis, byteOut);
87          fis.close();
88          String result = new String(byteOut.toByteArray());
89          assertEquals(TEST_MESSAGE, result);
90      }
91  
92      protected void doTearDown() throws Exception
93      {
94          super.doTearDown();
95          FileUtils.deleteTree(tmpDir);
96      }
97  
98  }