View Javadoc

1   /*
2    * $Id: FileExceptionStrategyFunctionalTestCase.java 22738 2011-08-25 19:20:42Z pablo.lagreca $
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.junit.After;
14  import org.junit.Before;
15  import org.junit.Test;
16  import org.mule.api.MuleEvent;
17  import org.mule.api.MuleException;
18  import org.mule.api.processor.MessageProcessor;
19  import org.mule.construct.Flow;
20  import org.mule.exception.DefaultMessagingExceptionStrategy;
21  import org.mule.tck.junit4.FunctionalTestCase;
22  import org.mule.util.FileUtils;
23  import org.mule.util.concurrent.Latch;
24  
25  import java.io.File;
26  
27  import static java.util.concurrent.TimeUnit.MILLISECONDS;
28  import static org.hamcrest.core.Is.is;
29  import static org.junit.Assert.assertThat;
30  
31  public class FileExceptionStrategyFunctionalTestCase extends FunctionalTestCase
32  {
33      public static final String TEST_MESSAGE = "Test file contents";
34      public static final String WORKING_DIRECTORY = ".mule/temp/work-directory/";
35  
36      private Latch latch = new Latch();
37      protected File inputDir;
38      private Flow flow;
39      private File inputFile;
40  
41      @Override
42      protected String getConfigResources()
43      {
44          return "file-exception-strategy-config.xml";
45      }
46  
47      @Test
48      public void testMoveFile() throws Exception
49      {
50          attacheLatchCountdownProcessor("moveFile");
51          inputDir = new File(".mule/temp/input-move-file");
52          inputFile = createDataFile(inputDir, "test1.txt");
53          latch.await(2000l, MILLISECONDS);
54          flow.stop();
55          File outputFile = new File(".mule/temp/output-directory/" + inputFile.getName());
56          assertThat(inputFile.exists(), is(false));
57          assertThat(outputFile.exists(), is(true));
58      }
59  
60      @Test
61      public void testMoveFileWithWorDir() throws Exception
62      {
63          attacheLatchCountdownProcessor("moveFileWithWorkDir");
64          inputDir = new File(".mule/temp/input-move-file-wd");
65          inputFile = createDataFile(inputDir, "test1.txt");
66          latch.await(2000l, MILLISECONDS);
67          flow.stop();
68          File outputFile = new File(".mule/temp/output-directory/" + inputFile.getName());
69          File workDirFile = new File(WORKING_DIRECTORY + inputFile.getName());
70          assertThat(inputFile.exists(), is(false));
71          assertThat(outputFile.exists(), is(true));
72          assertThat(workDirFile.exists(), is(false));
73      }
74  
75  
76      @Test
77      public void testCopyFile() throws Exception
78      {
79          attacheLatchCountdownProcessor("copyFile");
80          inputDir = new File(".mule/temp/input-copy-file");
81          inputFile = createDataFile(inputDir, "test1.txt");
82          latch.await(2000l, MILLISECONDS);
83          flow.stop();
84          File outputFile = new File(".mule/temp/output-directory/" + inputFile.getName());
85          assertThat(inputFile.exists(), is(false));
86          assertThat(outputFile.exists(), is(false));
87      }
88  
89  
90      @Test
91      public void testCopyFileWithWorkDir() throws Exception
92      {
93          attacheLatchCountdownProcessor("copyFileWithWorkDir");
94          inputDir = new File(".mule/temp/input-copy-file-with-work-directory");
95          inputFile = createDataFile(inputDir, "test1.txt");
96          latch.await(2000l, MILLISECONDS);
97          flow.stop();
98          File outputFile = new File(".mule/temp/output-directory/" + inputFile.getName());
99          File workDirFile = new File(WORKING_DIRECTORY + inputFile.getName());
100         assertThat(inputFile.exists(), is(false));
101         assertThat(outputFile.exists(), is(false));
102         assertThat(workDirFile.exists(), is(false));
103     }
104 
105     private void attacheLatchCountdownProcessor(String flowName)
106     {
107         flow = (Flow) muleContext.getRegistry().lookupFlowConstruct(flowName);
108         DefaultMessagingExceptionStrategy exceptionListener = (DefaultMessagingExceptionStrategy) flow.getExceptionListener();
109         exceptionListener.getMessageProcessors().add(new MessageProcessor()
110         {
111             public MuleEvent process(MuleEvent event) throws MuleException
112             {
113                 latch.countDown();
114                 return event;
115             }
116         });
117     }
118 
119     @Before
120     public void doSetUp()
121     {
122         FileUtils.deleteTree(new File("./mule/temp"));
123     }
124 
125     @After
126     public void tearDown()
127     {
128         FileUtils.deleteTree(new File("./mule/temp"));
129     }
130 
131     protected File createDataFile(File folder, final String testMessage) throws Exception
132     {
133         return createDataFile(folder, testMessage, null);
134     }
135 
136     protected File createDataFile(File folder, final String testMessage, String encoding) throws Exception
137     {
138         File target = File.createTempFile("data", ".txt", folder);
139         target.deleteOnExit();
140         FileUtils.writeStringToFile(target, testMessage, encoding);
141 
142         return target;
143     }
144 
145 }