View Javadoc

1   /*
2    * ------------------------------------------------------------------------------
3    * Hermes FTP Server
4    * Copyright (c) 2005-2007 Lars Behnke
5    * ------------------------------------------------------------------------------
6    * 
7    * This file is part of Hermes FTP Server.
8    * 
9    * Hermes FTP Server is free software; you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as published by
11   * the Free Software Foundation; either version 2 of the License, or
12   * (at your option) any later version.
13   * 
14   * Hermes FTP Server is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public License
20   * along with Hermes FTP Server; if not, write to the Free Software
21   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22   * ------------------------------------------------------------------------------
23   */
24  
25  package net.sf.hermesftp.server.impl;
26  
27  import java.io.IOException;
28  
29  import net.sf.hermesftp.SpringUtil;
30  import net.sf.hermesftp.client.FtpTestClient;
31  import net.sf.hermesftp.common.BeanConstants;
32  import net.sf.hermesftp.common.FtpConstants;
33  import net.sf.hermesftp.server.FtpServer;
34  
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  import org.junit.After;
38  import org.junit.Before;
39  
40  // CHECKSTYLE:OFF
41  
42  /***
43   * Abstract test case that sets up the communication infrastructure between FTP
44   * client and server.
45   * 
46   * @author Lars Behnke
47   */
48  public abstract class AbstractClientServerTestCase implements FtpConstants,
49          BeanConstants {
50  
51      private static final int SERVER_DELAY = 800;
52  
53      private static Log       log          = LogFactory
54                                                    .getLog(ServerRFC959Test.class);
55  
56      private FtpTestClient    client;
57  
58      static {
59          log.info("Starting FTP servers...");
60          startServer((FtpServer) SpringUtil.getBean("testServer"));
61      }
62  
63      private static void startServer(FtpServer svr) {
64          Thread svrThread = new Thread(svr);
65          svrThread.start();
66          while (svr.getStatus() != SERVER_STATUS_READY) {
67              try {
68                  Thread.sleep(SERVER_DELAY);
69              } catch (InterruptedException e) {
70                  log.error(e);
71                  break;
72              }
73          }
74      }
75  
76      /***
77       * {@inheritDoc}
78       */
79      @Before
80      public void setUp() {
81          try {
82              getClient().openConnection(null, "user", "user");
83              getClient().openPassiveMode();
84          } catch (IOException e) {
85              log.error(e);
86          }
87      }
88  
89      /***
90       * {@inheritDoc}
91       */
92      @After
93      public void tearDown() throws Exception {
94          getClient().closeConnection();
95      }
96  
97      /***
98       * Getter method for the java bean <code>client</code>.
99       * 
100      * @return Returns the value of the java bean <code>client</code>.
101      * @throws IOException Error on initializing the FTP client.
102      */
103     protected FtpTestClient getClient() throws IOException {
104         if (client == null) {
105             client = new FtpTestClient();
106         }
107         return client;
108     }
109 
110 }
111 
112 // CHECKSTYLE:ON
113