View Javadoc

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