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.api.MuleMessage;
10  import org.mule.tck.junit4.FunctionalTestCase;
11  import org.mule.util.FileUtils;
12  import org.mule.util.IOUtils;
13  
14  import java.io.ByteArrayOutputStream;
15  import java.io.File;
16  import java.io.FileWriter;
17  import java.io.InputStream;
18  import java.io.Writer;
19  import java.net.MalformedURLException;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertNotNull;
23  import static org.junit.Assert.assertTrue;
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      @Override
38      protected String getConfigResources()
39      {
40          return "file-functional-test.xml";
41      }
42  
43      protected String fileToUrl(File file) throws MalformedURLException
44      {
45          return file.getAbsoluteFile().toURI().toURL().toString();
46      }
47  
48      // annoying but necessary wait apparently due to OS caching?
49      protected void waitForFileSystem() throws Exception
50      {
51          synchronized (this)
52          {
53              wait(1000);
54          }
55      }
56  
57      protected File initForRequest() throws Exception
58      {
59          createTempDirectory();
60          File target = createAndPopulateTempFile();
61  
62          // define the readFromDirectory on the connector
63          FileConnector connector = (FileConnector) muleContext.getRegistry().lookupConnector(
64              "receiveConnector");
65          connector.setReadFromDirectory(tmpDir.getAbsolutePath());
66          logger.debug("Directory is " + connector.getReadFromDirectory());
67  
68          waitForFileSystem();
69          return target;
70      }
71  
72      private void createTempDirectory() throws Exception
73      {
74          tmpDir = File.createTempFile("mule-file-test-", "-dir");
75          tmpDir.delete();
76          tmpDir.mkdir();
77      }
78  
79      private File createAndPopulateTempFile() throws Exception
80      {
81          File target = File.createTempFile("mule-file-test-", ".txt", tmpDir);
82  
83          Writer out = new FileWriter(target);
84          out.write(TEST_MESSAGE);
85          out.close();
86  
87          return target;
88      }
89  
90      protected void checkReceivedMessage(MuleMessage message) throws Exception
91      {
92          assertNotNull(message);
93          assertNotNull(message.getPayload());
94          assertTrue(message.getPayload() instanceof InputStream);
95  
96          InputStream fis = (InputStream) message.getPayload();
97          ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
98          IOUtils.copy(fis, byteOut);
99          fis.close();
100         String result = new String(byteOut.toByteArray());
101         assertEquals(TEST_MESSAGE, result);
102     }
103 
104     @Override
105     protected void doTearDown() throws Exception
106     {
107         super.doTearDown();
108         FileUtils.deleteTree(tmpDir);
109     }
110 
111 }