1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.file;
12
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.assertTrue;
16
17 import org.mule.api.MuleMessage;
18 import org.mule.tck.AbstractServiceAndFlowTestCase;
19 import org.mule.util.FileUtils;
20 import org.mule.util.IOUtils;
21
22 import java.io.ByteArrayOutputStream;
23 import java.io.File;
24 import java.io.FileWriter;
25 import java.io.InputStream;
26 import java.io.Writer;
27 import java.net.MalformedURLException;
28 import java.util.Arrays;
29 import java.util.Collection;
30
31 import org.junit.runners.Parameterized.Parameters;
32
33
34
35
36
37 public abstract class AbstractFileFunctionalTestCase extends AbstractServiceAndFlowTestCase
38 {
39 public static final String TEST_MESSAGE = "Test file contents";
40 public static final String TARGET_FILE = "TARGET_FILE";
41
42 protected File tmpDir;
43
44 public AbstractFileFunctionalTestCase(ConfigVariant variant, String configResources)
45 {
46 super(variant, configResources);
47 }
48
49 @Parameters
50 public static Collection<Object[]> parameters()
51 {
52 return Arrays.asList(new Object[][]{
53 {ConfigVariant.SERVICE, "file-functional-test-service.xml"},
54 {ConfigVariant.FLOW, "file-functional-test-flow.xml"}
55 });
56 }
57
58 protected String fileToUrl(File file) throws MalformedURLException
59 {
60 return file.getAbsoluteFile().toURI().toURL().toString();
61 }
62
63
64 protected void waitForFileSystem() throws Exception
65 {
66 synchronized (this)
67 {
68 wait(1000);
69 }
70 }
71
72 protected File initForRequest() throws Exception
73 {
74 createTempDirectory();
75 File target = createAndPopulateTempFile("mule-file-test-", ".txt");
76
77
78 FileConnector connector = (FileConnector) muleContext.getRegistry().lookupConnector(
79 "receiveConnector");
80 connector.setReadFromDirectory(tmpDir.getAbsolutePath());
81 logger.debug("Directory is " + connector.getReadFromDirectory());
82
83 waitForFileSystem();
84 return target;
85 }
86
87 private void createTempDirectory() throws Exception
88 {
89 tmpDir = File.createTempFile("mule-file-test-", "-dir");
90 tmpDir.delete();
91 tmpDir.mkdir();
92 }
93
94 protected File createAndPopulateTempFile(String prefix, String suffix) throws Exception
95 {
96 File target = File.createTempFile(prefix, suffix, tmpDir);
97 logger.info("Created temporary file: " + target.getAbsolutePath());
98
99 Writer out = new FileWriter(target);
100 out.write(TEST_MESSAGE);
101 out.close();
102
103 target.deleteOnExit();
104 return target;
105 }
106
107 protected void checkReceivedMessage(MuleMessage message) throws Exception
108 {
109 assertNotNull(message);
110 assertNotNull(message.getPayload());
111 assertTrue(message.getPayload() instanceof InputStream);
112
113 InputStream fis = (InputStream) message.getPayload();
114 ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
115 IOUtils.copy(fis, byteOut);
116 fis.close();
117 String result = new String(byteOut.toByteArray());
118 assertEquals(TEST_MESSAGE, result);
119 }
120
121 @Override
122 protected void doTearDown() throws Exception
123 {
124 super.doTearDown();
125 FileUtils.deleteTree(tmpDir);
126 }
127
128 protected File createDataFile(File folder, final String testMessage) throws Exception
129 {
130 return createDataFile(folder, testMessage, null);
131 }
132
133 protected File createDataFile(File folder, final String testMessage, String encoding) throws Exception
134 {
135 File target = File.createTempFile("mule-file-test-", ".txt", folder);
136 target.deleteOnExit();
137 FileUtils.writeStringToFile(target, testMessage, encoding);
138
139 return target;
140 }
141
142 protected File createFolder(String name)
143 {
144 File result = FileUtils.newFile(name);
145 result.delete();
146 result.mkdir();
147 result.deleteOnExit();
148
149 return result;
150 }
151 }