1
2
3
4
5
6
7
8
9
10
11 package org.mule.test.integration.transport.file;
12
13 import org.mule.DefaultMuleContext;
14 import org.mule.api.MuleEventContext;
15 import org.mule.api.context.notification.ServerNotification;
16 import org.mule.tck.AbstractServiceAndFlowTestCase;
17 import org.mule.tck.functional.FunctionalTestComponent;
18 import org.mule.tck.functional.FunctionalTestNotification;
19 import org.mule.tck.functional.FunctionalTestNotificationListener;
20 import org.mule.transport.PollingController;
21 import org.mule.util.FileUtils;
22 import org.mule.util.IOUtils;
23
24 import java.io.File;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.util.Arrays;
28 import java.util.Collection;
29
30 import org.junit.Test;
31 import org.junit.runners.Parameterized.Parameters;
32
33 import static org.junit.Assert.assertEquals;
34 import static org.junit.Assert.assertNotNull;
35 import static org.junit.Assert.assertNull;
36 import static org.junit.Assert.assertTrue;
37
38 public class FileFunctionalTestCase extends AbstractServiceAndFlowTestCase implements FunctionalTestNotificationListener
39 {
40 private Object receivedData = null;
41 private boolean shouldPoll;
42
43 @Parameters
44 public static Collection<Object[]> parameters()
45 {
46 return Arrays.asList(new Object[][]{
47 {ConfigVariant.SERVICE, "org/mule/test/integration/providers/file/file-config-service.xml"},
48 {ConfigVariant.FLOW, "org/mule/test/integration/providers/file/file-config-flow.xml"}});
49 }
50
51 public FileFunctionalTestCase(ConfigVariant variant, String configResources)
52 {
53 super(variant, configResources);
54 }
55
56 @Override
57 protected void doSetUp() throws Exception
58 {
59 super.doSetUp();
60 muleContext.registerListener(this);
61 }
62
63 @Override
64 protected void doTearDown() throws Exception
65 {
66 super.doTearDown();
67 muleContext.unregisterListener(this);
68 }
69
70 @Test
71 public void testRelative() throws IOException, InterruptedException
72 {
73
74 byte[] data = new byte[100000];
75 for (int i = 0; i < data.length; i++)
76 {
77 data[i] = (byte) (Math.random() * 128);
78 }
79
80 File f = FileUtils.newFile("./test/testfile.temp");
81 f.createNewFile();
82 FileOutputStream fos = new FileOutputStream(f);
83 IOUtils.write(data, fos);
84 IOUtils.closeQuietly(fos);
85
86 shouldPoll = false;
87
88 ((DefaultMuleContext) muleContext).setPollingController(new PollingController()
89 {
90 @Override
91 public boolean isPrimaryPollingInstance()
92 {
93 return shouldPoll;
94 }
95 });
96
97
98 f.renameTo(FileUtils.newFile(f.getPath().replaceAll(".temp", ".data")));
99
100
101 Thread.sleep(5000);
102
103 synchronized (this)
104 {
105 assertNull(receivedData);
106 }
107
108 shouldPoll = true;
109
110 Thread.sleep(5000);
111 synchronized (this)
112 {
113 assertNotNull(receivedData);
114 assertTrue(receivedData instanceof byte[]);
115 byte[] receivedBytes = (byte[]) receivedData;
116 assertEquals(data.length, receivedBytes.length);
117 assertTrue(Arrays.equals(data, receivedBytes));
118 }
119 }
120
121 @Override
122 public void onNotification(ServerNotification notification)
123 {
124 synchronized (this)
125 {
126 logger.debug("received notification: " + notification);
127
128 this.receivedData = ((FunctionalTestNotification) notification).getReplyMessage();
129 }
130 }
131
132 public static class FileTestComponent extends FunctionalTestComponent
133 {
134 @Override
135 public Object onCall(MuleEventContext context) throws Exception
136 {
137
138
139 super.setReturnData(context.getMessage().getPayload());
140 return super.onCall(context);
141 }
142 }
143 }