View Javadoc

1   /*
2    * $Id: FtpStreamingTestCase.java 22491 2011-07-21 10:04:30Z claude.mamo $
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.transport.ftp;
12  
13  import static org.junit.Assert.assertNotNull;
14  import static org.junit.Assert.assertTrue;
15  import static org.junit.Assert.fail;
16  
17  import org.mule.api.MuleEventContext;
18  import org.mule.api.MuleMessage;
19  import org.mule.tck.functional.EventCallback;
20  import org.mule.tck.functional.FunctionalStreamingTestComponent;
21  
22  import java.io.InputStream;
23  import java.util.Arrays;
24  import java.util.Collection;
25  import java.util.concurrent.CountDownLatch;
26  import java.util.concurrent.TimeUnit;
27  import java.util.concurrent.atomic.AtomicReference;
28  
29  import org.junit.Test;
30  import org.junit.runners.Parameterized.Parameters;
31  
32  public class FtpStreamingTestCase extends AbstractFtpServerTestCase
33  {
34      public FtpStreamingTestCase(ConfigVariant variant, String configResources)
35      {
36          super(variant, configResources);
37      }
38      
39      @Parameters
40      public static Collection<Object[]> parameters()
41      {
42          return Arrays.asList(new Object[][]{
43              {ConfigVariant.SERVICE, "ftp-streaming-test-service.xml"},
44              {ConfigVariant.FLOW, "ftp-streaming-test-flow.xml"}
45          });
46      }      
47  
48      @Test
49      public void testRequest() throws Exception
50      {
51          final CountDownLatch latch = new CountDownLatch(1);
52          final AtomicReference<MuleMessage> messageHolder = new AtomicReference<MuleMessage>();
53  
54          EventCallback callback = new EventCallback()
55          {
56              @Override
57              public synchronized void eventReceived(MuleEventContext context, Object component)
58              {
59                  try
60                  {
61                      if (1 == latch.getCount())
62                      {
63                          messageHolder.set(context.getMessage());
64                          latch.countDown();
65                      }
66                  }
67                  catch (Exception e)
68                  {
69                      fail();
70                  }
71              }
72          };
73  
74          Object component = getComponent("testComponent");
75          assertTrue("FunctionalStreamingTestComponent expected",
76              component instanceof FunctionalStreamingTestComponent);
77          FunctionalStreamingTestComponent ftc = (FunctionalStreamingTestComponent) component;
78          ftc.setEventCallback(callback, TEST_MESSAGE.length());
79  
80          createFileOnFtpServer("input.txt");
81  
82          // poll and pull back through test service
83          assertTrue(latch.await(getTimeout(), TimeUnit.MILLISECONDS));
84  
85          MuleMessage message = messageHolder.get();
86          assertNotNull(message);
87          assertTrue(message.getPayload() instanceof InputStream);
88      }
89  }