1   /*
2    * $Id: AbstractFtpServerTestCase.java 11179 2008-03-05 13:46:23Z dfeist $
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;
12  
13  import org.mule.tck.FunctionalTestCase;
14  import org.mule.transport.ftp.server.NamedPayload;
15  import org.mule.transport.ftp.server.Server;
16  
17  
18  public abstract class AbstractFtpServerTestCase extends FunctionalTestCase
19  {
20  
21      public static final String TEST_MESSAGE = "Test FTP message";
22      private static int DEFAULT_TIMEOUT = 10000;
23      private int timeout;
24      private int port;
25      private Server server;
26  
27      public AbstractFtpServerTestCase(int port, int timeout)
28      {
29          this.port = port;
30          this.timeout = timeout;
31      }
32  
33      public AbstractFtpServerTestCase(int port)
34      {
35          this(port, DEFAULT_TIMEOUT);
36      }
37  
38      protected void startServer() throws Exception
39      {
40          server = new Server(port);
41          server.awaitStart(timeout);
42          // this is really ugly, but the above doesn't get to waiting.
43          // need to improve this as part of ftp server work
44          synchronized(this)
45          {
46              wait(500);
47          }
48      }
49  
50      protected void stopServer() throws Exception
51      {
52          // stop the server
53          if (null != server)
54          {
55              server.stop();
56          }
57      }
58  
59      // @Override
60      protected void doSetUp() throws Exception
61      {
62          startServer();
63      }
64  
65      // @Override
66      protected void doTearDown() throws Exception
67      {
68          stopServer();
69      }
70  
71      protected int getTimeout()
72      {
73          return timeout;
74      }
75  
76      protected NamedPayload awaitUpload() throws InterruptedException
77      {
78          return server.awaitUpload(timeout);
79      }
80  
81  }