View Javadoc

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