View Javadoc

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