View Javadoc

1   // Copyright (C) 2003, 2004, 2005 Philip Aston
2   // All rights reserved.
3   //
4   // This file is part of The Grinder software distribution. Refer to
5   // the file LICENSE which is part of The Grinder distribution for
6   // licensing details. The Grinder distribution is available on the
7   // Internet at http://grinder.sourceforge.net/
8   //
9   // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
13  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
14  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
17  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
18  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
20  // OF THE POSSIBILITY OF SUCH DAMAGE.
21  
22  package net.grinder.tools.tcpproxy;
23  
24  import java.net.ServerSocket;
25  import java.net.Socket;
26  import java.util.Random;
27  
28  import junit.framework.TestCase;
29  
30  
31  /**
32   * Unit test case for {@link EndPoint}.
33   *
34   * @author Philip Aston
35   */
36  public class TestEndPoint extends TestCase {
37  
38    public void testAccessors() throws Exception {
39  
40      final Random random = new Random();
41  
42      for (int i=0; i<10; ++i) {
43        final byte[] bytes = new byte[random.nextInt(30)];
44        random.nextBytes(bytes);
45        final String hostname = new String(bytes);
46        final int port = random.nextInt(65536);
47  
48        final EndPoint endPoint = new EndPoint(hostname, port);
49        assertEquals(hostname.toLowerCase(), endPoint.getHost());
50        assertEquals(port, endPoint.getPort());
51      }
52    }
53  
54    public void testEquality() throws Exception   {
55      final EndPoint[] endPoint = {
56        new EndPoint("Abcdef", 55),
57        new EndPoint("aBCDef", 55),
58        new EndPoint("c", 55),
59        new EndPoint("a", 5512),
60        new EndPoint("a", 56),
61      };
62  
63      assertEquals(endPoint[0], endPoint[0]);
64      assertEquals(endPoint[0], endPoint[1]);
65      assertEquals(endPoint[1], endPoint[0]);
66      assertFalse(endPoint[0].equals(endPoint[2]));
67      assertFalse(endPoint[1].equals(endPoint[3]));
68      assertFalse(endPoint[1].equals(endPoint[4]));
69  
70      assertFalse(endPoint[0].equals(this));
71    }
72  
73    public void testComparable() throws Exception {
74      final EndPoint[] ordered = {
75        new EndPoint("abc", 21331),
76        new EndPoint("abc", 21431),
77        new EndPoint("x", 131),
78        new EndPoint("X", 132),
79        new EndPoint("X", 91321),
80      };
81  
82      for (int i=0; i<ordered.length; ++i) {
83        for (int j=0; j<i; ++j) {
84          assertTrue("EndPoint " + i + " is greater than EndPoint " + j,
85                     ordered[i].compareTo(ordered[j]) > 0);
86        }
87  
88        assertEquals(0, ordered[i].compareTo(ordered[i]));
89  
90        for (int j=i+1; j<ordered.length; ++j) {
91          assertTrue("EndPoint " + i + " is less than EndPoint " + j,
92                     ordered[i].compareTo(ordered[j]) < 0);
93        }
94      }
95  
96      // Check two different but equal EndPoint's.
97      assertEquals(0, new EndPoint("blah", 999).compareTo(
98                     new EndPoint("blah", 999)));
99    }
100 
101   public void testServerEndPoint() throws Exception {
102     final ServerSocket serverSocket = new ServerSocket(0);
103     final EndPoint endPoint = EndPoint.serverEndPoint(serverSocket);
104     assertNotNull(endPoint);
105     serverSocket.close();
106   }
107 
108   public void testClientEndPoint() throws Exception {
109     final ServerSocket serverSocket = new ServerSocket(0);
110     final Socket socket = new Socket(serverSocket.getInetAddress(),
111                                      serverSocket.getLocalPort());
112     final EndPoint endPoint = EndPoint.clientEndPoint(socket);
113     assertNotNull(endPoint);
114     socket.close();
115     serverSocket.close();
116   }
117 }