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   import org.mule.api.context.notification.EndpointMessageNotificationListener;
10  import org.mule.context.notification.EndpointMessageNotification;
11  import org.mule.module.client.MuleClient;
12  import org.mule.tck.junit4.FunctionalTestCase;
13  import org.mule.util.FileUtils;
14  import org.mule.util.IOUtils;
15  
16  import java.io.File;
17  import java.io.FileInputStream;
18  
19  import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
20  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
21  import org.junit.Test;
22  
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertFalse;
25  import static org.junit.Assert.assertTrue;
26  
27  public class FileAppendConnectorTestCase extends FunctionalTestCase implements EndpointMessageNotificationListener<EndpointMessageNotification>
28  {
29  
30      protected static final String OUTPUT_DIR = "myout";
31      protected static final String OUTPUT_FILE = "out.txt";
32      
33      protected CountDownLatch fileReceiveLatch = new CountDownLatch(2);
34      
35      @Override
36      protected void doSetUp() throws Exception
37      {
38          super.doSetUp();
39          muleContext.registerListener(this);
40      }
41      
42      @Override
43      protected String getConfigResources()
44      {
45          return "org/mule/test/integration/providers/file/mule-fileappend-connector-config.xml";
46      }
47      
48      @Override
49      protected void doTearDown() throws Exception
50      {
51          File outputDir = FileUtils.newFile(OUTPUT_DIR);
52          FileUtils.deleteTree(outputDir);
53          
54          super.doTearDown();
55      }
56  
57      @Test
58      public void testBasic() throws Exception
59      {
60          FileInputStream myFileStream = null;
61          try
62          {
63              File myDir = FileUtils.newFile(OUTPUT_DIR);
64              File myFile = FileUtils.newFile(myDir, OUTPUT_FILE);
65              assertFalse(myFile.exists());
66  
67              MuleClient client = new MuleClient(muleContext);
68              client.send("vm://fileappend", "Hello1", null);
69              client.send("vm://fileappend", "Hello2", null);
70  
71              assertTrue(fileReceiveLatch.await(30, TimeUnit.SECONDS));
72              
73              // the output file should exist now
74              myFileStream = new FileInputStream(myFile);
75              assertEquals("Hello1Hello2", IOUtils.toString(myFileStream));
76          }
77          finally
78          {
79              IOUtils.closeQuietly(myFileStream);
80          }
81      }
82  
83      public void onNotification(EndpointMessageNotification notification)
84      {
85          if (notification.getEndpoint().contains("myout"))
86          {
87              fileReceiveLatch.countDown();
88          }
89      }
90  }