View Javadoc

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