1   /*
2    * $Id: FileFunctionalTestCase.java 12017 2008-06-12 09:04:04Z rossmason $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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      //@Override
33      protected void doSetUp() throws Exception
34      {
35          super.doSetUp();
36          muleContext.registerListener(this);
37      }
38  
39      // @Override
40      protected void doTearDown() throws Exception
41      {
42          super.doTearDown();
43          muleContext.unregisterListener(this);
44      }
45  
46      // @Override
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          // create binary file data to be written
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          // atomically rename the file to make it available for polling
68          f.renameTo(FileUtils.newFile(f.getPath().replaceAll(".temp", ".data")));
69  
70          // give polling a chance
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              // save the received message data for verification
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              // there should not be any transformers configured by default, so the
98              // return message should be a byte[]
99              super.setReturnData(context.transformMessage());
100             return super.onCall(context);
101         }
102     }
103 
104 }