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 com.google.common.net.HostAndPort;
16 import java.lang.reflect.Constructor;
17 import java.lang.reflect.InvocationTargetException;
18 import java.net.InetSocketAddress;
19 import java.util.Arrays;
20 import java.util.List;
21 import org.junit.Assert;
22 import org.junit.Test;
24 public class InetSocketAddressUtilTest {
25 private static final String ADDRESS1 = "1.1.1.1";
26 private static final String ADDRESS2 = "2.2.2.2";
27 private static final int PORT1 = 123;
28 private static final int PORT2 = 321;
29 private static final String ADDRESSES_WO_PORT = ADDRESS1 + "," + ADDRESS2;
30 private static final String ADDRESSES = ADDRESS1 + ":" + PORT1 + "," + ADDRESS2 + ":" + PORT2;
31 private static final int DEFAULT_PORT = 179;
33 @Test(expected = UnsupportedOperationException.class)
34 public void testPrivateConstructor() throws Throwable {
35 final Constructor<InetSocketAddressUtil> c = InetSocketAddressUtil.class.getDeclaredConstructor();
36 c.setAccessible(true);
39 } catch (final InvocationTargetException e) {
45 public void parseAddresses() throws Exception {
46 final List<InetSocketAddress> actualResult = InetSocketAddressUtil.parseAddresses(ADDRESSES, DEFAULT_PORT);
47 Assert.assertEquals(Arrays.asList(new InetSocketAddress(ADDRESS1, PORT1), new InetSocketAddress(ADDRESS2, PORT2)), actualResult);
51 public void toHostAndPort() throws Exception {
52 final HostAndPort actualResult = InetSocketAddressUtil.toHostAndPort(new InetSocketAddress(ADDRESS2, PORT2));
53 final HostAndPort expected = HostAndPort.fromString("2.2.2.2:321");
54 Assert.assertEquals(expected, actualResult);
58 public void parseAddressesDefaultPort() throws Exception {
59 final List<InetSocketAddress> actualResult = InetSocketAddressUtil.parseAddresses(ADDRESSES_WO_PORT, DEFAULT_PORT);
60 final List<InetSocketAddress> expected = Arrays.asList(new InetSocketAddress(ADDRESS1, DEFAULT_PORT), new InetSocketAddress(ADDRESS2, DEFAULT_PORT));
61 Assert.assertEquals(expected, actualResult);
65 public void parseAddressesWithoutPort() throws Exception {
66 final List<InetSocketAddress> actualResult = InetSocketAddressUtil.parseAddresses(ADDRESSES);
67 Assert.assertEquals(Arrays.asList(new InetSocketAddress(ADDRESS1, PORT1), new InetSocketAddress(ADDRESS2, PORT2)), actualResult);
71 public void getInetSocketAddress() throws Exception {
72 assertEquals(new InetSocketAddress(ADDRESS1, PORT1), InetSocketAddressUtil.getInetSocketAddress(ADDRESS1 + ":" + PORT1, DEFAULT_PORT));
73 assertEquals(new InetSocketAddress(ADDRESS1, DEFAULT_PORT), InetSocketAddressUtil.getInetSocketAddress(ADDRESS1, DEFAULT_PORT));
77 public void getRandomLoopbackInetSocketAddressTest() throws Exception {
78 final InetSocketAddress addr1 = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress();
79 final InetSocketAddress addr2 = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress();
82 assertNotEquals(addr1, addr2);
83 assertNotEquals(addr1.getHostString(), addr2.getHostString());
84 assertNotEquals(addr1.getPort(), addr2.getPort());