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.MuleEventContext;
10  import org.mule.tck.functional.EventCallback;
11  import org.mule.tck.functional.FunctionalTestComponent;
12  import org.mule.tck.junit4.FunctionalTestCase;
13  import org.mule.util.FileUtils;
14  
15  import java.io.File;
16  import java.io.FileNotFoundException;
17  import java.io.FileOutputStream;
18  import java.io.IOException;
19  
20  import org.junit.Test;
21  
22  import static org.junit.Assert.assertTrue;
23  import static org.junit.Assert.fail;
24  
25  public class WorkDirectoryTestCase extends FunctionalTestCase
26  {
27      
28      private static final String TEST_FILENAME = "test.txt";
29  
30      @Override
31      protected String getConfigResources()
32      {
33          return "work-directory-config.xml";
34      }
35  
36      @Override
37      protected void doTearDown() throws Exception
38      {
39          // clean out the directory tree that's used as basis for this test
40          File outputDir = new File(".mule");
41          assertTrue(FileUtils.deleteTree(outputDir));
42  
43          super.doTearDown();
44      }
45  
46      @Test
47      public void testWorkDirectory() throws Exception
48      {
49          FunctionalTestComponent ftc = (FunctionalTestComponent) getComponent("relay");
50          ftc.setEventCallback(new EventCallback()
51          {
52              public void eventReceived(MuleEventContext context, Object component) throws Exception
53              {
54                  File workDir = new File(".mule/work");
55                  String[] filenames = workDir.list();
56                  assertTrue(filenames.length > 0);
57                  for (String filename : filenames)
58                  {
59                      if (filename.contains(TEST_FILENAME))
60                      {
61                          return;
62                      }
63                  }
64                  
65                  fail("no work dir file matching filename " + TEST_FILENAME);
66              }
67          });
68          
69          writeTestMessageToInputDirectory();        
70          checkOutputDirectory();
71      }
72  
73      private void writeTestMessageToInputDirectory() throws FileNotFoundException, IOException
74      {
75          File outFile = new File(".mule/in", TEST_FILENAME);
76          FileOutputStream out = new FileOutputStream(outFile);
77          out.write(TEST_MESSAGE.getBytes());
78          out.close();
79      }
80  
81      private void checkOutputDirectory() throws Exception
82      {
83          for (int i = 0; i < 30; i++)
84          {
85              File outDir = new File(".mule/out");
86              if (outDir.exists())
87              {
88                  String[] filenames = outDir.list();
89                  if (filenames.length > 0)
90                  {
91                      for (String filename : filenames)
92                      {
93                          if (filename.contains(TEST_FILENAME))
94                          {
95                              return;
96                          }
97                      }
98                  }
99              }
100             
101             Thread.sleep(1000);
102         }
103 
104         fail("no file with name " + TEST_FILENAME + " in output directory");
105     }
106     
107 }