View Javadoc

1   /*
2    * $Id: AbstractFileMoveDeleteTestCase.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.transport.AbstractMuleMessageFactory;
14  
15  import java.io.BufferedReader;
16  import java.io.File;
17  import java.io.FileReader;
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  public abstract class AbstractFileMoveDeleteTestCase extends AbstractFileFunctionalTestCase
22  {
23  
24      protected File configureConnector(File inFile, boolean stream, boolean move, boolean delete, 
25          Class<? extends AbstractMuleMessageFactory> messageFactoryClass) throws Exception
26      {
27          FileConnector fc = new FileConnector(muleContext);
28          
29          // some tests assert that a sinlge message arrives from this connector. Use a very large
30          // polling frequency to avoid multiple messages coming in during a single test run.
31          fc.setPollingFrequency(3000000);
32          
33          if (messageFactoryClass != null)
34          {
35              Map<String, String> overrides = new HashMap<String, String>();
36              overrides.put("message.factory", messageFactoryClass.getName());
37              fc.setServiceOverrides(overrides);
38          }
39          
40          fc.setName("moveDeleteConnector");
41          File moveToDir = new File(inFile.getParent() + "/moveto/");
42          moveToDir.mkdir();
43          muleContext.getRegistry().registerConnector(fc);
44          if (move)
45          {
46              fc.setMoveToDirectory(moveToDir.getPath());
47          }
48          fc.setAutoDelete(delete);
49          fc.setStreaming(stream);
50          
51          return moveToDir;
52      }
53  
54      protected void assertFiles(File inFile, File moveToDir, boolean move, boolean delete) throws Exception
55      {
56          waitForFileSystem();
57  
58          boolean inFileShouldExst = !delete && !move;
59  
60          assertTrue(inFile.exists() == inFileShouldExst);
61  
62          if (inFileShouldExst)
63          {
64              assertEquals(TEST_MESSAGE, new BufferedReader(new FileReader(inFile)).readLine());
65          }
66  
67          File movedFile = new File(moveToDir.getPath() + "/" + inFile.getName());
68          assertTrue(movedFile.exists() == move);
69  
70          if (move)
71          {
72              assertEquals(TEST_MESSAGE, new BufferedReader(new FileReader(movedFile)).readLine());
73          }
74      }
75  
76      protected void assertFilesUntouched(File inFile)
77      {
78          assertTrue(inFile.exists());
79  
80          File movedFile = new File(inFile.getParent() + "/moveto/" + inFile.getName());
81          assertFalse(movedFile.exists());
82      }
83  
84  }