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.test.integration.transport.file;
8   
9   import org.mule.module.client.MuleClient;
10  import org.mule.tck.junit4.FunctionalTestCase;
11  import org.mule.tck.probe.PollingProber;
12  import org.mule.tck.probe.Probe;
13  import org.mule.tck.probe.Prober;
14  import org.mule.util.FileUtils;
15  
16  import java.io.File;
17  
18  import junit.framework.AssertionFailedError;
19  import org.junit.Test;
20  
21  import static org.junit.Assert.assertFalse;
22  import static org.junit.Assert.assertTrue;
23  
24  public class OutputPatternFromEndpointTestCase extends FunctionalTestCase
25  {
26  
27      @Override
28      protected String getConfigResources()
29      {
30          return "org/mule/test/integration/providers/file/mule-file-output-pattern-from-endpoint.xml";
31      }
32  
33      @Test
34      public void testBasic() throws Exception
35      {
36          String myFirstDirName = "FirstWrite";
37          String mySecondDirName = "SecondWrite";
38          final String myFileName1 = "export.txt";
39          final String myFileName2 = "export.txt.OK";
40  
41          // make sure there is no directory and file
42          final File myDir = FileUtils.newFile(myFirstDirName);
43          if (myDir.isDirectory())
44          {
45              // Delete Any Existing Files
46              File[] files = myDir.listFiles();
47              for (int i = 0; i < files.length; i++)
48              {
49                  assertTrue(files[i].delete());
50              }
51              // This may fail if this directory contains other directories.
52              assertTrue(myDir.delete());
53          }
54  
55          final File myDir2 = FileUtils.newFile(mySecondDirName);
56          if (myDir2.isDirectory())
57          {
58              // Delete Any Existing Files
59              File[] files = myDir2.listFiles();
60              for (int i = 0; i < files.length; i++)
61              {
62                  assertTrue(files[i].delete());
63              }
64              // This may fail if this directory contains other directories.
65              assertTrue(myDir2.delete());
66          }
67  
68          try
69          {
70              assertFalse(FileUtils.newFile(myDir, myFileName1).exists());
71              assertFalse(FileUtils.newFile(myDir2, myFileName2).exists());
72  
73              MuleClient client = new MuleClient(muleContext);
74              client.send("vm://filesend", "Hello", null);
75  
76              // the output file should exist now
77              // check that the files with the correct output pattern were generated
78              Prober prober = new PollingProber(2000, 50);
79  
80              prober.check(new Probe()
81              {
82  
83                  public boolean isSatisfied()
84                  {
85                    return FileUtils.newFile(myDir, myFileName1).exists() && FileUtils.newFile(myDir2, myFileName2).exists();
86                  }
87  
88                  public String describeFailure()
89                  {
90                      return "Failed to create the expected files";
91                  }
92              });
93          }
94          catch (AssertionFailedError e1)
95          {
96              //The original assertion was getting masked by a failure in the finally block
97              e1.printStackTrace();
98          }
99          finally
100         {
101             FileUtils.newFile(myDir, myFileName1).delete();
102             FileUtils.newFile(myDir2, myFileName2).delete();
103             myDir.delete();
104             myDir2.delete();
105         }
106     }
107 }