1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.file;
12
13 import org.mule.api.MuleMessage;
14 import org.mule.tck.FunctionalTestCase;
15 import org.mule.util.FileUtils;
16 import org.mule.util.IOUtils;
17
18 import java.io.ByteArrayOutputStream;
19 import java.io.File;
20 import java.io.FileWriter;
21 import java.io.InputStream;
22 import java.io.Writer;
23 import java.net.MalformedURLException;
24
25
26
27
28
29 public abstract class AbstractFileFunctionalTestCase extends FunctionalTestCase
30 {
31
32 public static final String TEST_MESSAGE = "Test file contents";
33 public static final String TARGET_FILE = "TARGET_FILE";
34
35 private File tmpDir;
36
37 protected String getConfigResources()
38 {
39 return "file-functional-test.xml";
40 }
41
42 protected String fileToUrl(File file) throws MalformedURLException
43 {
44 return file.getAbsoluteFile().toURI().toURL().toString();
45 }
46
47
48 protected void waitForFileSystem() throws Exception
49 {
50 synchronized (this)
51 {
52 wait(1000);
53 }
54 }
55
56 protected File initForRequest() throws Exception
57 {
58 tmpDir = File.createTempFile("mule-file-test-", "-dir");
59 tmpDir.delete();
60 tmpDir.mkdir();
61 tmpDir.deleteOnExit();
62 File target = File.createTempFile("mule-file-test-", ".txt", tmpDir);
63 Writer out = new FileWriter(target);
64 out.write(TEST_MESSAGE);
65 out.close();
66 target.deleteOnExit();
67
68
69 FileConnector connector = (FileConnector) muleContext.getRegistry().lookupConnector(
70 "receiveConnector");
71 connector.setReadFromDirectory(tmpDir.getAbsolutePath());
72 logger.debug("Directory is " + connector.getReadFromDirectory());
73
74 waitForFileSystem();
75 return target;
76 }
77
78 protected void checkReceivedMessage(MuleMessage message) throws Exception
79 {
80 assertNotNull(message);
81 assertNotNull(message.getPayload());
82 assertTrue(message.getPayload() instanceof InputStream);
83
84 InputStream fis = (InputStream) message.getPayload();
85 ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
86 IOUtils.copy(fis, byteOut);
87 fis.close();
88 String result = new String(byteOut.toByteArray());
89 assertEquals(TEST_MESSAGE, result);
90 }
91
92 protected void doTearDown() throws Exception
93 {
94 super.doTearDown();
95 FileUtils.deleteTree(tmpDir);
96 }
97
98 }