View Javadoc

1   /*
2    * $Id: FileAppendConnectorTestCase.java 22421 2011-07-15 05:05:06Z dirk.olmes $
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.AbstractServiceAndFlowTestCase;
17  import org.mule.util.FileUtils;
18  import org.mule.util.IOUtils;
19  
20  import java.io.File;
21  import java.io.FileInputStream;
22  import java.util.Arrays;
23  import java.util.Collection;
24  import java.util.concurrent.CountDownLatch;
25  import java.util.concurrent.TimeUnit;
26  
27  import org.junit.Test;
28  import org.junit.runners.Parameterized.Parameters;
29  
30  import static org.junit.Assert.assertEquals;
31  import static org.junit.Assert.assertFalse;
32  import static org.junit.Assert.assertTrue;
33  
34  public class FileAppendConnectorTestCase extends AbstractServiceAndFlowTestCase implements EndpointMessageNotificationListener<EndpointMessageNotification>
35  {
36      protected static final String OUTPUT_DIR = "myout";
37      protected static final String OUTPUT_FILE = "out.txt";
38  
39      protected CountDownLatch fileReceiveLatch = new CountDownLatch(2);
40  
41      @Parameters
42      public static Collection<Object[]> parameters()
43      {
44          return Arrays.asList(new Object[][]{
45              {ConfigVariant.SERVICE, "org/mule/test/integration/providers/file/mule-fileappend-connector-config-service.xml"},
46              {ConfigVariant.FLOW, "org/mule/test/integration/providers/file/mule-fileappend-connector-config-flow.xml"}
47          });
48      }
49  
50      public FileAppendConnectorTestCase(ConfigVariant variant, String configResources)
51      {
52          super(variant, configResources);
53      }
54  
55      @Override
56      protected void doSetUp() throws Exception
57      {
58          super.doSetUp();
59          muleContext.registerListener(this);
60      }
61  
62      @Override
63      protected void doTearDown() throws Exception
64      {
65          File outputDir = FileUtils.newFile(OUTPUT_DIR);
66          FileUtils.deleteTree(outputDir);
67  
68          super.doTearDown();
69      }
70  
71      @Test
72      public void testBasic() throws Exception
73      {
74          FileInputStream myFileStream = null;
75          try
76          {
77              File myDir = FileUtils.newFile(OUTPUT_DIR);
78              File myFile = FileUtils.newFile(myDir, OUTPUT_FILE);
79              assertFalse(myFile.exists());
80  
81              MuleClient client = new MuleClient(muleContext);
82              client.send("vm://fileappend", "Hello1", null);
83              client.send("vm://fileappend", "Hello2", null);
84  
85              assertTrue(fileReceiveLatch.await(30, TimeUnit.SECONDS));
86  
87              // the output file should exist now
88              myFileStream = new FileInputStream(myFile);
89              assertEquals("Hello1Hello2", IOUtils.toString(myFileStream));
90          }
91          finally
92          {
93              IOUtils.closeQuietly(myFileStream);
94          }
95      }
96  
97      @Override
98      public void onNotification(EndpointMessageNotification notification)
99      {
100         if (notification.getEndpoint().contains("myout"))
101         {
102             fileReceiveLatch.countDown();
103         }
104     }
105 }