changing license to BSD, assigning Yahoo copyrights where appropriate
[lwes-java.git] / src / test / java / org / lwes / util / IPAddressTest.java
blob195541a1d956b129bd57ecb0390c751bfe00d874
1 /*======================================================================*
2 * Copyright (c) 2010, Frank Maritato All rights reserved. *
3 * *
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;
14 /**
15 * @author fmaritato
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 {
32 @Test
33 public void testEmptyConstructor() {
34 IPAddress ip = new IPAddress();
35 assertEquals("IPAddress.toString incorrect. ", "0.0.0.0", ip.toString());
38 @Test
39 public void testStringConstructor() {
40 IPAddress ip = new IPAddress("192.168.1.1");
41 assertEquals("IPAddress.toString incorrect.", "192.168.1.1", ip.toString());
44 @Test
45 public void testByteConstructor() {
46 byte[] ipBytes = new byte[]{
47 (byte) 192,
48 (byte) 168,
49 (byte) 1,
50 (byte) 1,
52 IPAddress ip = new IPAddress(ipBytes);
53 assertEquals("Byte constructor incorrect", "192.168.1.1", ip.toString());
56 @Test
57 public void testInetConstructor() throws UnknownHostException {
58 IPAddress ip = new IPAddress(InetAddress.getByAddress(new byte[]
59 {(byte) 127, (byte) 0, (byte) 0, (byte) 1}
60 ));
61 assertEquals("Inet constructor incorrect", "127.0.0.1", ip.toString());
64 @Test
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());
76 @Test
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());