1   /*
2    * $Id: AbstractFileMoveDeleteTestCase.java 11532 2008-04-08 16:33:08Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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 java.io.BufferedReader;
14  import java.io.File;
15  import java.io.FileReader;
16  import java.util.Map;
17  
18  import org.apache.commons.collections.map.HashedMap;
19  
20  public abstract class AbstractFileMoveDeleteTestCase extends AbstractFileFunctionalTestCase
21  {
22  
23      protected File configureConnector(File inFile, boolean stream, boolean move, boolean delete, Class messageAdaptor)
24          throws Exception
25      {
26          FileConnector fc = new FileConnector();
27          if (messageAdaptor != null)
28          {
29              Map overrides = new HashedMap();
30              overrides.put("message.adapter", messageAdaptor.getName());
31              fc.setServiceOverrides(overrides);
32          }
33          fc.setName("moveDeleteConnector");
34          File moveToDir = new File(inFile.getParent() + "/moveto/");
35          moveToDir.mkdir();
36          muleContext.getRegistry().registerConnector(fc);
37          if (move)
38          {
39              fc.setMoveToDirectory(moveToDir.getPath());
40          }
41          fc.setAutoDelete(delete);
42          fc.setStreaming(stream);
43          return moveToDir;
44      }
45  
46      protected void assertFiles(File inFile, File moveToDir, boolean move, boolean delete) throws Exception
47      {
48          waitForFileSystem();
49  
50          boolean inFileShouldExst = !delete && !move;
51  
52          assertTrue(inFile.exists() == inFileShouldExst);
53  
54          if (inFileShouldExst)
55          {
56              assertEquals(TEST_MESSAGE, new BufferedReader(new FileReader(inFile)).readLine());
57          }
58  
59          File movedFile = new File(moveToDir.getPath() + "/" + inFile.getName());
60          assertTrue(movedFile.exists() == move);
61  
62          if (move)
63          {
64              assertEquals(TEST_MESSAGE, new BufferedReader(new FileReader(movedFile)).readLine());
65          }
66      }
67  
68      protected void assertFilesUntouched(File inFile)
69      {
70          assertTrue(inFile.exists());
71  
72          File movedFile = new File(inFile.getParent() + "/moveto/" + inFile.getName());
73          assertFalse(movedFile.exists());
74      }
75  
76  }