1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.file;
12
13 import org.mule.api.construct.FlowConstruct;
14 import org.mule.tck.junit4.FunctionalTestCase;
15 import org.mule.tck.probe.PollingProber;
16 import org.mule.tck.probe.Probe;
17 import org.mule.tck.probe.Prober;
18
19 import java.io.File;
20 import java.io.IOException;
21
22 import org.junit.Before;
23 import org.junit.Test;
24
25 import static org.junit.Assert.assertFalse;
26
27 public class FileAutoDeleteOnExceptionTestCase extends FunctionalTestCase
28 {
29
30 public static final String TEST_FOLDER1 = ".mule/testData1";
31 public static final String TEST_FOLDER2 = ".mule/testData2";
32
33 private Prober prober;
34
35 public FileAutoDeleteOnExceptionTestCase()
36 {
37 setStartContext(false);
38 }
39
40 @Override
41 protected String getConfigResources()
42 {
43 return "file-auto-delete-on-exception-config.xml";
44 }
45
46 @Before
47 public void setUp() throws IOException
48 {
49 prober = new PollingProber(10000, 100);
50 }
51
52 private File createTestFile(String folder) throws IOException
53 {
54 File testFolder = new File(folder);
55 testFolder.mkdirs();
56 prober.check(new FileExists(testFolder));
57
58 File target = File.createTempFile("mule-file-test-", ".txt", testFolder);
59 target.deleteOnExit();
60 prober.check(new FileExists(target));
61 return target;
62 }
63
64 @Test
65 public void testDoesNotAutoDeleteFileOnException() throws Exception
66 {
67 File target = createTestFile(TEST_FOLDER1);
68
69 muleContext.start();
70
71
72 final FlowConstruct fileFlow = muleContext.getRegistry().lookupFlowConstruct("fileTest");
73 prober.check(new FlowStopped(fileFlow));
74
75
76 prober.check(new FileExists(target));
77 }
78
79 @Test
80 public void testAutoDeletesFileOnExceptionIfFileWasTransformed() throws Exception
81 {
82 File target = createTestFile(TEST_FOLDER2);
83
84
85 muleContext.start();
86
87
88 final FlowConstruct fileFlow = muleContext.getRegistry().lookupFlowConstruct("fileTestWithTransformation");
89 prober.check(new FlowStopped(fileFlow));
90
91
92 assertFalse(target.exists());
93 }
94
95 private static class FileExists implements Probe
96 {
97
98 private final File target;
99
100 public FileExists(File target)
101 {
102 this.target = target;
103 }
104
105 public boolean isSatisfied()
106 {
107 return target.exists();
108 }
109
110 public String describeFailure()
111 {
112 return "File does not exists";
113 }
114 }
115
116 private static class FlowStopped implements Probe
117 {
118
119 private final FlowConstruct fileFlow;
120
121 public FlowStopped(FlowConstruct fileFlow)
122 {
123 this.fileFlow = fileFlow;
124 }
125
126 public boolean isSatisfied()
127 {
128 return fileFlow.getLifecycleState().isStopped();
129 }
130
131 public String describeFailure()
132 {
133 return "Flow was not stopped after processing the exception";
134 }
135 }
136 }