View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.test.integration.transport.file;
8   
9   
10  import org.mule.api.MuleEventContext;
11  import org.mule.api.context.notification.ServerNotification;
12  import org.mule.tck.junit4.FunctionalTestCase;
13  import org.mule.tck.functional.FunctionalTestNotification;
14  import org.mule.tck.functional.FunctionalTestNotificationListener;
15  import org.mule.tck.functional.FunctionalTestComponent;
16  import org.mule.util.FileUtils;
17  import org.mule.util.IOUtils;
18  
19  import java.io.File;
20  import java.io.FileOutputStream;
21  import java.io.IOException;
22  import java.util.Arrays;
23  
24  import org.junit.Test;
25  
26  import static org.junit.Assert.assertEquals;
27  import static org.junit.Assert.assertNotNull;
28  import static org.junit.Assert.assertTrue;
29  
30  public class FileFunctionalTestCase extends FunctionalTestCase implements FunctionalTestNotificationListener
31  {
32  
33      private Object receivedData = null;
34  
35      @Override
36      protected void doSetUp() throws Exception
37      {
38          super.doSetUp();
39          muleContext.registerListener(this);
40      }
41  
42      @Override
43      protected void doTearDown() throws Exception
44      {
45          super.doTearDown();
46          muleContext.unregisterListener(this);
47      }
48  
49      @Override
50      protected String getConfigResources()
51      {
52          return "org/mule/test/integration/providers/file/file-config.xml";
53      }
54  
55      @Test
56      public void testRelative() throws IOException, InterruptedException
57      {
58          // create binary file data to be written
59          byte[] data = new byte[100000];
60          for (int i = 0; i < data.length; i++)
61          {
62              data[i] = (byte)(Math.random() * 128);
63          }
64  
65          File f = FileUtils.newFile("./test/testfile.temp");
66          f.createNewFile();
67          FileOutputStream fos = new FileOutputStream(f);
68          IOUtils.write(data, fos);
69          IOUtils.closeQuietly(fos);
70  
71          // atomically rename the file to make it available for polling
72          f.renameTo(FileUtils.newFile(f.getPath().replaceAll(".temp", ".data")));
73  
74          // give polling a chance
75          Thread.sleep(5000);
76  
77          synchronized (this)
78          {
79              assertNotNull(receivedData);
80              assertTrue(receivedData instanceof byte[]);
81              byte[] receivedBytes = (byte[])receivedData;
82              assertEquals(data.length, receivedBytes.length);
83              assertTrue(Arrays.equals(data, receivedBytes));
84          }
85      }
86  
87      public void onNotification(ServerNotification notification)
88      {
89          synchronized (this)
90          {
91              logger.debug("received notification: " + notification);
92              // save the received message data for verification
93              this.receivedData = ((FunctionalTestNotification)notification).getReplyMessage();
94          }
95      }
96  
97      public static class FileTestComponent extends FunctionalTestComponent
98      {
99          public Object onCall(MuleEventContext context) throws Exception
100         {
101             // there should not be any transformers configured by default, so the
102             // return message should be a byte[]
103             super.setReturnData(context.getMessage().getPayload());
104             return super.onCall(context);
105         }
106     }
107 
108 }