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