01: /*
02: * This file is part of DrFTPD, Distributed FTP Daemon.
03: *
04: * DrFTPD is free software; you can redistribute it and/or modify
05: * it under the terms of the GNU General Public License as published by
06: * the Free Software Foundation; either version 2 of the License, or
07: * (at your option) any later version.
08: *
09: * DrFTPD is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: * GNU General Public License for more details.
13: *
14: * You should have received a copy of the GNU General Public License
15: * along with DrFTPD; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: */
18: package net.sf.drftpd.util;
19:
20: import junit.framework.TestCase;
21:
22: import java.io.IOException;
23:
24: import java.net.ServerSocket;
25:
26: import java.util.ArrayList;
27: import java.util.Iterator;
28:
29: import javax.net.ServerSocketFactory;
30:
31: /**
32: * @author zubov
33: * @version $Id: PortRangeTest.java 1634 2007-02-21 21:09:40Z tdsoul $
34: */
35: public class PortRangeTest extends TestCase {
36: public void testGetPort() throws IOException {
37: PortRange pr = new PortRange(45300, 45310, 0);
38: ArrayList<Integer> ports = new ArrayList<Integer>();
39: ArrayList<ServerSocket> sockets = new ArrayList<ServerSocket>();
40:
41: for (int x = 45300; x <= 45310; x++) {
42: ports.add(new Integer(x));
43: }
44:
45: assertEquals(11, ports.size());
46:
47: ServerSocket ss = new ServerSocket(45305);
48: sockets.add(ss);
49: ports.remove(new Integer(ss.getLocalPort()));
50: assertEquals(10, ports.size());
51:
52: for (int x = 0; x < 10; x++) {
53: ServerSocket socket = pr.getPort(ServerSocketFactory
54: .getDefault());
55: sockets.add(socket);
56: ports.remove(new Integer(socket.getLocalPort()));
57: assertEquals(9 - x, ports.size());
58: }
59:
60: assertEquals(0, ports.size());
61:
62: try {
63: pr.getPort(ServerSocketFactory.getDefault());
64: throw new RuntimeException("PortRange should be exhausted!");
65: } catch (RuntimeException e) {
66: assertTrue(e.getMessage().equals("PortRange exhausted"));
67: }
68:
69: ss.close();
70: ss = pr.getPort(ServerSocketFactory.getDefault());
71:
72: // clean up
73: ss.close();
74: for (Iterator<ServerSocket> iter = sockets.iterator(); iter
75: .hasNext();) {
76: iter.next().close();
77: }
78: }
79: }
|