View Javadoc

1   /*
2    * $Id: AbstractFileMoveDeleteTestCase.java 20433 2010-12-02 04:48:11Z mike.schilling $
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          return configureConnector(inFile, stream, move, delete, false, messageFactoryClass);
28      }
29  
30      protected File configureConnector(File inFile, boolean stream, boolean move, boolean delete,
31          boolean useWorkDir, Class<? extends AbstractMuleMessageFactory> messageFactoryClass) throws Exception
32      {
33          FileConnector fc = new FileConnector(muleContext);
34          
35          // some tests assert that a sinlge message arrives from this connector. Use a very large
36          // polling frequency to avoid multiple messages coming in during a single test run.
37          fc.setPollingFrequency(3000000);
38          
39          if (messageFactoryClass != null)
40          {
41              Map<String, String> overrides = new HashMap<String, String>();
42              overrides.put("message.factory", messageFactoryClass.getName());
43              fc.setServiceOverrides(overrides);
44          }
45          
46          fc.setName("moveDeleteConnector");
47          File moveToDir = new File(inFile.getParent() + "/moveto/");
48          moveToDir.mkdir();
49          File workDir = new File(inFile.getParent() + "/work/");
50          workDir.mkdir();
51          muleContext.getRegistry().registerConnector(fc);
52          if (move)
53          {
54              fc.setMoveToDirectory(moveToDir.getPath());
55          }
56          if (useWorkDir)
57          {
58              fc.setWorkDirectory(workDir.getPath());
59          }
60          fc.setAutoDelete(delete);
61          fc.setStreaming(stream);
62          
63          return moveToDir;
64      }
65  
66      protected void assertFiles(File inFile, File moveToDir, boolean move, boolean delete) throws Exception
67      {
68          waitForFileSystem();
69  
70          boolean inFileShouldExst = !delete && !move;
71  
72          assertTrue(inFile.exists() == inFileShouldExst);
73  
74          if (inFileShouldExst)
75          {
76              assertEquals(TEST_MESSAGE, new BufferedReader(new FileReader(inFile)).readLine());
77          }
78  
79          File movedFile = new File(moveToDir.getPath() + "/" + inFile.getName());
80          assertTrue(movedFile.exists() == move);
81  
82          if (move)
83          {
84              assertEquals(TEST_MESSAGE, new BufferedReader(new FileReader(movedFile)).readLine());
85          }
86      }
87  
88      protected void assertFilesUntouched(File inFile)
89      {
90          assertTrue(inFile.exists());
91  
92          File movedFile = new File(inFile.getParent() + "/moveto/" + inFile.getName());
93          assertFalse(movedFile.exists());
94      }
95  
96  }