2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.protocol.util;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotEquals;
13 import static org.junit.Assert.assertNotNull;
15 import java.net.InetSocketAddress;
16 import java.util.Arrays;
17 import java.util.List;
18 import org.junit.Assert;
19 import org.junit.Test;
21 public class InetSocketAddressUtilTest {
22 private static String ADDRESS1 = "1.1.1.1";
23 private static String ADDRESS2 = "2.2.2.2";
24 private static int PORT1 = 123;
25 private static int PORT2 = 321;
26 private static String ADDRESSES_WO_PORT = ADDRESS1 + "," + ADDRESS2;
27 private static String ADDRESSES = ADDRESS1 + ":" + PORT1 + "," + ADDRESS2 + ":" + PORT2;
28 private static int DEFAULT_PORT = 179;
31 public void parseAddresses() throws Exception {
32 final List<InetSocketAddress> actualResult = InetSocketAddressUtil.parseAddresses(ADDRESSES, DEFAULT_PORT);
33 Assert.assertEquals(Arrays.asList(new InetSocketAddress(ADDRESS1, PORT1), new InetSocketAddress(ADDRESS2, PORT2)), actualResult);
37 public void parseAddressesDefaultPort() throws Exception {
38 final List<InetSocketAddress> actualResult = InetSocketAddressUtil.parseAddresses(ADDRESSES_WO_PORT, DEFAULT_PORT);
39 final List<InetSocketAddress> expected = Arrays.asList(new InetSocketAddress(ADDRESS1, DEFAULT_PORT), new InetSocketAddress(ADDRESS2, DEFAULT_PORT));
40 Assert.assertEquals(expected, actualResult);
44 public void parseAddressesWithoutPort() throws Exception {
45 final List<InetSocketAddress> actualResult = InetSocketAddressUtil.parseAddresses(ADDRESSES);
46 Assert.assertEquals(Arrays.asList(new InetSocketAddress(ADDRESS1, PORT1), new InetSocketAddress(ADDRESS2, PORT2)), actualResult);
50 public void getInetSocketAddress() throws Exception {
51 assertEquals(new InetSocketAddress(ADDRESS1, PORT1), InetSocketAddressUtil.getInetSocketAddress(ADDRESS1 + ":" + PORT1, DEFAULT_PORT));
52 assertEquals(new InetSocketAddress(ADDRESS1, DEFAULT_PORT), InetSocketAddressUtil.getInetSocketAddress(ADDRESS1, DEFAULT_PORT));
56 public void getRandomLoopbackInetSocketAddressTest() throws Exception {
57 final InetSocketAddress addr1 = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress();
58 final InetSocketAddress addr2 = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress();
61 assertNotEquals(addr1, addr2);
62 assertNotEquals(addr1.getHostString(), addr2.getHostString());
63 assertNotEquals(addr1.getPort(), addr2.getPort());