1 /*======================================================================*
2 * Copyright (c) 2010, Frank Maritato All rights reserved. *
4 * Licensed under the New BSD License (the "License"); you may not use *
5 * this file except in compliance with the License. Unless required *
6 * by applicable law or agreed to in writing, software distributed *
7 * under the License is distributed on an "AS IS" BASIS, WITHOUT *
8 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
9 * See the License for the specific language governing permissions and *
10 * limitations under the License. See accompanying LICENSE file. *
11 *======================================================================*/
13 package org
.lwes
.util
;
18 import org
.junit
.Test
;
19 import org
.lwes
.Event
;
20 import org
.lwes
.EventSystemException
;
22 import java
.net
.InetAddress
;
23 import java
.net
.UnknownHostException
;
25 import static org
.junit
.Assert
.assertEquals
;
26 import static org
.junit
.Assert
.assertFalse
;
27 import static org
.junit
.Assert
.assertNotSame
;
28 import static org
.junit
.Assert
.assertTrue
;
30 public class IPAddressTest
{
33 public void testEmptyConstructor() {
34 IPAddress ip
= new IPAddress();
35 assertEquals("IPAddress.toString incorrect. ", "0.0.0.0", ip
.toString());
39 public void testStringConstructor() {
40 IPAddress ip
= new IPAddress("192.168.1.1");
41 assertEquals("IPAddress.toString incorrect.", "192.168.1.1", ip
.toString());
45 public void testByteConstructor() {
46 byte[] ipBytes
= new byte[]{
52 IPAddress ip
= new IPAddress(ipBytes
);
53 assertEquals("Byte constructor incorrect", "192.168.1.1", ip
.toString());
57 public void testInetConstructor() throws UnknownHostException
{
58 IPAddress ip
= new IPAddress(InetAddress
.getByAddress(new byte[]
59 {(byte) 127, (byte) 0, (byte) 0, (byte) 1}
61 assertEquals("Inet constructor incorrect", "127.0.0.1", ip
.toString());
65 public void testEquality() throws UnknownHostException
{
66 IPAddress one
= new IPAddress("192.168.1.1");
67 IPAddress two
= new IPAddress("192.168.1.1");
68 assertTrue("192.168.1.1 werent equal", one
.equals(two
));
69 assertEquals("hash codes not equal", one
.hashCode(), two
.hashCode());
71 two
= new IPAddress(InetAddress
.getByName("www.yahoo.com"));
72 assertFalse("InetAddress.getByName not equal", one
.equals(two
));
73 assertNotSame("hash codes equal", one
.hashCode(), two
.hashCode());
77 public void testGetSet() throws EventSystemException
, UnknownHostException
{
78 Event e
= new Event("TestEvent", null);
79 IPAddress ip
= new IPAddress("127.0.0.1");
80 e
.setIPAddress("ipaddr", ip
);
81 assertEquals("Type fail",
82 ip
.getClass().getName(),
83 e
.getIPAddressObj("ipaddr").getClass().getName());
84 assertEquals("Type fail",
85 new byte[]{}.getClass().getName(),
86 e
.getIPAddress("ipaddr").getClass().getName());
88 e
.setIPAddress("inetaddr", InetAddress
.getLocalHost());
89 assertEquals("Type fail",
90 ip
.getClass().getName(),
91 e
.getIPAddressObj("inetaddr").getClass().getName());
92 assertEquals("Type fail",
93 new byte[]{}.getClass().getName(),
94 e
.getIPAddress("inetaddr").getClass().getName());
96 e
.setIPAddress("bytesaddr", new byte[]{(byte) 127, (byte) 0, (byte) 0, (byte) 1});
97 assertEquals("Type fail",
98 ip
.getClass().getName(),
99 e
.getIPAddressObj("bytesaddr").getClass().getName());
100 assertEquals("Type fail",
101 new byte[]{}.getClass().getName(),
102 e
.getIPAddress("bytesaddr").getClass().getName());