1
2
3
4
5
6
7
8
9
10
11 package org.mule.test.integration.transport.file;
12
13
14 import org.mule.api.MuleEventContext;
15 import org.mule.api.context.notification.ServerNotification;
16 import org.mule.tck.FunctionalTestCase;
17 import org.mule.tck.functional.FunctionalTestNotification;
18 import org.mule.tck.functional.FunctionalTestNotificationListener;
19 import org.mule.tck.functional.FunctionalTestComponent;
20 import org.mule.util.FileUtils;
21 import org.mule.util.IOUtils;
22
23 import java.io.File;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.util.Arrays;
27
28 public class FileFunctionalTestCase extends FunctionalTestCase implements FunctionalTestNotificationListener
29 {
30 private Object receivedData = null;
31
32
33 protected void doSetUp() throws Exception
34 {
35 super.doSetUp();
36 muleContext.registerListener(this);
37 }
38
39
40 protected void doTearDown() throws Exception
41 {
42 super.doTearDown();
43 muleContext.unregisterListener(this);
44 }
45
46
47 protected String getConfigResources()
48 {
49 return "org/mule/test/integration/providers/file/file-config.xml";
50 }
51
52 public void testRelative() throws IOException, InterruptedException
53 {
54
55 byte[] data = new byte[100000];
56 for (int i = 0; i < data.length; i++)
57 {
58 data[i] = (byte)(Math.random() * 128);
59 }
60
61 File f = FileUtils.newFile("./test/testfile.temp");
62 f.createNewFile();
63 FileOutputStream fos = new FileOutputStream(f);
64 IOUtils.write(data, fos);
65 IOUtils.closeQuietly(fos);
66
67
68 f.renameTo(FileUtils.newFile(f.getPath().replaceAll(".temp", ".data")));
69
70
71 Thread.sleep(5000);
72
73 synchronized (this)
74 {
75 assertNotNull(receivedData);
76 assertTrue(receivedData instanceof byte[]);
77 byte[] receivedBytes = (byte[])receivedData;
78 assertEquals(data.length, receivedBytes.length);
79 assertTrue(Arrays.equals(data, receivedBytes));
80 }
81 }
82
83 public void onNotification(ServerNotification notification)
84 {
85 synchronized (this)
86 {
87 logger.debug("received notification: " + notification);
88
89 this.receivedData = ((FunctionalTestNotification)notification).getReplyMessage();
90 }
91 }
92
93 public static class FileTestComponent extends FunctionalTestComponent
94 {
95 public Object onCall(MuleEventContext context) throws Exception
96 {
97
98
99 super.setReturnData(context.transformMessage());
100 return super.onCall(context);
101 }
102 }
103
104 }