View Javadoc

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