View Javadoc

1   /*
2    * $Id: FtpFunctionalTestCase.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.assertEquals;
14  import static org.junit.Assert.assertNotNull;
15  import static org.junit.Assert.assertTrue;
16  
17  import org.mule.api.MuleEventContext;
18  import org.mule.module.client.MuleClient;
19  import org.mule.tck.functional.EventCallback;
20  import org.mule.tck.functional.FunctionalTestComponent;
21  
22  import java.util.Arrays;
23  import java.util.Collection;
24  import java.util.concurrent.CountDownLatch;
25  import java.util.concurrent.TimeUnit;
26  import java.util.concurrent.atomic.AtomicReference;
27  
28  import org.junit.Test;
29  import org.junit.runners.Parameterized.Parameters;
30  
31  public class FtpFunctionalTestCase extends AbstractFtpServerTestCase
32  {
33      public FtpFunctionalTestCase(ConfigVariant variant, String configResources)
34      {
35          super(variant, configResources);     
36      }
37  
38      @Parameters
39      public static Collection<Object[]> parameters()
40      {
41          return Arrays.asList(new Object[][]{
42              {ConfigVariant.SERVICE, "ftp-functional-test-service.xml"},
43              {ConfigVariant.FLOW, "ftp-functional-test-flow.xml"}
44          });
45      }      
46      
47      @Test
48      public void testSendAndRequest() throws Exception
49      {
50          CountDownLatch latch = new CountDownLatch(1);
51          AtomicReference<String> message = new AtomicReference<String>();
52  
53          Object component = getComponent("testComponent");
54          assertNotNull(component);
55          assertTrue("FunctionalTestComponent expected", component instanceof FunctionalTestComponent);
56          FunctionalTestComponent ftc = (FunctionalTestComponent) component;
57          ftc.setEventCallback(new FunctionalEventCallback(latch, message));
58  
59          MuleClient client = new MuleClient(muleContext);
60          client.dispatch(getMuleFtpEndpoint(), TEST_MESSAGE, null);
61  
62          // TODO DZ: need a reliable way to check the file once it's been written to
63          // the ftp server. Currently, once mule processes the ftp'd file, it
64          // auto-deletes it, so we can't check it
65          //assertTrue(getFtpClient().expectFileCount("/", 1, 10000));
66  
67          latch.await(getTimeout(), TimeUnit.MILLISECONDS);
68          assertEquals(TEST_MESSAGE, message.get());
69      }
70  
71      protected static class FunctionalEventCallback implements EventCallback
72      {
73          private CountDownLatch latch;
74          private AtomicReference<String> message;
75  
76          public FunctionalEventCallback(CountDownLatch latch, AtomicReference<String> message)
77          {
78              super();
79              this.latch = latch;
80              this.message = message;
81          }
82  
83          @Override
84          public synchronized void eventReceived(MuleEventContext context, Object component)
85          {
86              try
87              {
88                  FunctionalTestComponent ftc = (FunctionalTestComponent) component;
89  
90                  // without this we may have problems with the many repeats
91                  if (1 == latch.getCount())
92                  {
93                      String o = new String((byte[])ftc.getLastReceivedMessage());
94                      message.set(o);
95                      latch.countDown();
96                  }
97              }
98              catch (Exception e)
99              {
100                 throw new IllegalStateException(e);
101             }
102         }
103     }
104 }