1   /*
2    * $Id: MultiStreamMule1696TestCase.java 11039 2008-02-26 18:18:22Z holger $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.issues;
12  
13  import org.mule.api.MuleEventContext;
14  import org.mule.module.client.MuleClient;
15  import org.mule.tck.functional.EventCallback;
16  import org.mule.tck.functional.FunctionalStreamingTestComponent;
17  import org.mule.transport.ftp.AbstractFtpServerTestCase;
18  import org.mule.transport.ftp.server.NamedPayload;
19  
20  import java.util.HashMap;
21  
22  import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
23  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
24  import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicReference;
25  
26  public class MultiStreamMule1696TestCase extends AbstractFtpServerTestCase
27  {
28  
29      public static final String TEST_MESSAGE_2 = "Another test message";
30      private static int PORT = 60197;
31  
32      public MultiStreamMule1696TestCase()
33      {
34          super(PORT);
35      }
36  
37      protected String getConfigResources()
38      {
39          return "ftp-streaming-test.xml";
40      }
41  
42      private EventCallback newCallback(final CountDownLatch latch, final AtomicReference message)
43      {
44          return new EventCallback()
45          {
46              public synchronized void eventReceived(MuleEventContext context, Object component)
47              {
48                  try
49                  {
50                      FunctionalStreamingTestComponent ftc = (FunctionalStreamingTestComponent) component;
51                      logger.debug("Callback called: " + ftc.getSummary());
52                      message.set(ftc.getSummary());
53                      latch.countDown();
54                  }
55                  catch (Exception e)
56                  {
57                      logger.error(e.getMessage(), e);
58                  }
59              }
60          };
61      }
62  
63      public void testSendAndRequest() throws Exception
64      {
65          MuleClient client = new MuleClient();
66  
67          Object ftc = getComponent("testComponent");
68          assertTrue("FunctionalStreamingTestComponent expected", ftc instanceof FunctionalStreamingTestComponent);
69  
70          assertNotNull(ftc);
71  //        assertEquals(1, ftc.getNumber());
72  
73          CountDownLatch latch = new CountDownLatch(1);
74          AtomicReference message = new AtomicReference();
75          EventCallback callback = newCallback(latch, message);
76          ((FunctionalStreamingTestComponent) ftc).setEventCallback(callback, TEST_MESSAGE.length());
77  
78          // send out to FTP server via streaming model
79          client.dispatch("tcp://localhost:60196", TEST_MESSAGE, new HashMap());
80          NamedPayload payload = awaitUpload();
81          assertNotNull(payload);
82          logger.info("received message: " + payload);
83          assertEquals(TEST_MESSAGE, new String(payload.getPayload()));
84  
85          // poll and pull back through test service
86          latch.await(getTimeout(), TimeUnit.MILLISECONDS);
87          assertEquals("Received stream; length: 16; 'Test...sage'", message.get());
88  
89          // repeat, but restart server due to simple state, connection limitations
90          stopServer();
91          Thread.sleep(3000); // wait for scket to close
92          startServer();
93  
94          CountDownLatch latch2 = new CountDownLatch(1);
95          AtomicReference message2 = new AtomicReference();
96          EventCallback callback2 = newCallback(latch2, message2);
97          ((FunctionalStreamingTestComponent) ftc).setEventCallback(callback2, TEST_MESSAGE_2.length());
98  
99          client.dispatch("tcp://localhost:60196", TEST_MESSAGE_2, new HashMap());
100         NamedPayload payload2 = awaitUpload();
101         assertNotNull(payload2);
102         logger.info("received message: " + payload2);
103         assertEquals(TEST_MESSAGE_2, new String(payload2.getPayload()));
104 
105         latch2.await(getTimeout(), TimeUnit.MILLISECONDS);
106         assertEquals("Received stream; length: 20; 'Anot...sage'", message2.get());
107     }
108 
109 }