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.filters;
8   
9   import org.mule.module.client.MuleClient;
10  import org.mule.tck.junit4.FunctionalTestCase;
11  import org.mule.util.FileUtils;
12  
13  import java.io.File;
14  import java.io.IOException;
15  
16  import org.junit.Test;
17  
18  import static junit.framework.Assert.assertFalse;
19  import static org.junit.Assert.assertTrue;
20  import static org.junit.Assert.fail;
21  
22  public class FilterOnGlobalFileEndpointTestCase extends FunctionalTestCase
23  {
24      private static final String TEXT_FILE = "sample.txt";
25      private static final String XML_FILE = "sample.xml";
26  
27      private File pollDirectory;
28  
29      @Override
30      protected String getConfigResources()
31      {
32          return "global-file-ep-with-filter.xml";
33      }
34  
35      @Override
36      protected void doSetUp() throws Exception
37      {
38          createPollDirectoryAndInputFiles();
39          super.doSetUp();
40      }
41  
42      @Override
43      protected void doTearDown() throws Exception
44      {
45          // discard the test directory structure
46          assertTrue(FileUtils.deleteTree(pollDirectory.getParentFile()));
47  
48          super.doTearDown();
49      }
50  
51      private void createPollDirectoryAndInputFiles() throws IOException
52      {
53          pollDirectory = createDirectory("target/FilterOnGlobalFileEndpointTestCase/testdir");
54          createDirectory("target/FilterOnGlobalFileEndpointTestCase/testdir-moveto");
55  
56          createFileInPollDirectory(TEXT_FILE);
57          createFileInPollDirectory(XML_FILE);
58      }
59  
60      private File createDirectory(String path)
61      {
62          File directory = new File(path);
63          if (directory.exists() == false)
64          {
65              if (directory.mkdirs() == false)
66              {
67                  fail("could not create poll directory");
68              }
69          }
70  
71          return directory;
72      }
73  
74      private void createFileInPollDirectory(String filename) throws IOException
75      {
76          File file  = FileUtils.newFile(pollDirectory, filename);
77  
78          String path = file.getCanonicalPath();
79  
80          File newFile = FileUtils.createFile(path);
81          newFile.deleteOnExit();
82      }
83  
84      @Test
85      public void testMoveFiles() throws Exception
86      {
87          File txtFile = new File(pollDirectory, TEXT_FILE);
88          File xmlFile = new File(pollDirectory, XML_FILE);
89          assertTrue(txtFile.exists());
90          assertTrue(xmlFile.exists());
91  
92          MuleClient client = new MuleClient(muleContext);
93          client.request("globalEP", 1000);
94          
95          assertTrue(txtFile.exists());
96          assertFalse(xmlFile.exists());
97      }
98  }