# HG changeset patch # User Severin Gehwolf # Date 1479133140 -3600 # Node ID f09130ac15b7fa08a3f6365284ae2485102d9ad0 # Parent 5f1cb273e6098f6a84d3b175308336e41c707687 External form for HostPortPair. Reviewed-by: neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2016-November/021689.html PR3231 diff -r 5f1cb273e609 -r f09130ac15b7 agent/core/src/main/java/com/redhat/thermostat/agent/Agent.java --- a/agent/core/src/main/java/com/redhat/thermostat/agent/Agent.java Wed Nov 16 17:51:01 2016 +0100 +++ b/agent/core/src/main/java/com/redhat/thermostat/agent/Agent.java Mon Nov 14 15:19:00 2016 +0100 @@ -159,11 +159,7 @@ agentInfo.setStartTime(config.getStartTime()); agentInfo.setAlive(true); HostPortPair hostPort = config.getConfigListenAddress(); - String host = hostPort.getHost(); - if (host.indexOf(":") != -1) { - host = "[" + host + "]"; - } - agentInfo.setConfigListenAddress(String.format("%s:%d", host, hostPort.getPort())); + agentInfo.setConfigListenAddress(hostPort.toExternalForm()); return agentInfo; } diff -r 5f1cb273e609 -r f09130ac15b7 common/core/src/main/java/com/redhat/thermostat/common/utils/HostPortPair.java --- a/common/core/src/main/java/com/redhat/thermostat/common/utils/HostPortPair.java Wed Nov 16 17:51:01 2016 +0100 +++ b/common/core/src/main/java/com/redhat/thermostat/common/utils/HostPortPair.java Mon Nov 14 15:19:00 2016 +0100 @@ -36,13 +36,25 @@ package com.redhat.thermostat.common.utils; +/** + * Model class for a hostname/port pair. + * + * @see HostPortsParser + */ public class HostPortPair { - private String host; - private int port; + + private final String host; + private final int port; + private final boolean isIPv6; public HostPortPair(String host, int port) { + this(host, port, false); + } + + HostPortPair(String host, int port, boolean isIPv6) { this.host = host; this.port = port; + this.isIPv6 = isIPv6; } public String getHost() { @@ -52,5 +64,17 @@ public int getPort() { return port; } + + /** + * + * @return A parseable external form of this host/port pair. Examples: + * {@code host.example.com:12345} {@code [::1]:123} + */ + public String toExternalForm() { + String myHost = host; + if (isIPv6) { + myHost = "[" + myHost + "]"; + } + return String.format("%s:%d", myHost, port); + } } - diff -r 5f1cb273e609 -r f09130ac15b7 common/core/src/main/java/com/redhat/thermostat/common/utils/HostPortsParser.java --- a/common/core/src/main/java/com/redhat/thermostat/common/utils/HostPortsParser.java Wed Nov 16 17:51:01 2016 +0100 +++ b/common/core/src/main/java/com/redhat/thermostat/common/utils/HostPortsParser.java Mon Nov 14 15:19:00 2016 +0100 @@ -110,7 +110,7 @@ } catch (NumberFormatException e) { throw formatException; } - ipPorts.add(new HostPortPair(ipPortPair.substring(idxLParen + 1, idxRparen), port)); + ipPorts.add(new HostPortPair(ipPortPair.substring(idxLParen + 1, idxRparen), port, true)); } } } diff -r 5f1cb273e609 -r f09130ac15b7 common/core/src/test/java/com/redhat/thermostat/common/utils/HostPortPairTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/common/core/src/test/java/com/redhat/thermostat/common/utils/HostPortPairTest.java Mon Nov 14 15:19:00 2016 +0100 @@ -0,0 +1,60 @@ +/* + * Copyright 2012-2016 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * . + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.common.utils; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class HostPortPairTest { + + @Test + public void testExternalFormIPv4() { + HostPortPair pair = new HostPortPair("foo.example.com", 33); + assertEquals("foo.example.com:33", pair.toExternalForm()); + pair = new HostPortPair("127.0.0.1", 90); + assertEquals("127.0.0.1:90", pair.toExternalForm()); + } + + @Test + public void testExternalFormIPv6() { + HostPortPair pair = new HostPortPair("::1", 999, true); + assertEquals("[::1]:999", pair.toExternalForm()); + pair = new HostPortPair("fe80::565a:be2e:e9d3:ba1b", 12000, true); + assertEquals("[fe80::565a:be2e:e9d3:ba1b]:12000", pair.toExternalForm()); + } +} diff -r 5f1cb273e609 -r f09130ac15b7 common/core/src/test/java/com/redhat/thermostat/common/utils/HostPortsParserTest.java --- a/common/core/src/test/java/com/redhat/thermostat/common/utils/HostPortsParserTest.java Wed Nov 16 17:51:01 2016 +0100 +++ b/common/core/src/test/java/com/redhat/thermostat/common/utils/HostPortsParserTest.java Mon Nov 14 15:19:00 2016 +0100 @@ -59,6 +59,7 @@ assertEquals("127.0.0.1", ipPorts.get(0).getHost()); assertEquals(9999, (long) ipPorts.get(1).getPort()); assertEquals("127.0.0.1", ipPorts.get(1).getHost()); + assertEquals("127.0.0.1:8080", ipPorts.get(0).toExternalForm()); } @Test @@ -70,6 +71,7 @@ assertEquals(2, ipPorts.size()); assertEquals(8080, (long) ipPorts.get(0).getPort()); assertEquals("somehost.example.com", ipPorts.get(0).getHost()); + assertEquals("somehost.example.com:8080", ipPorts.get(0).toExternalForm()); assertEquals(9999, (long) ipPorts.get(1).getPort()); assertEquals("host2.example.com", ipPorts.get(1).getHost()); parser = new HostPortsParser( @@ -89,9 +91,29 @@ assertEquals(2, ipPorts.size()); assertEquals(8001, (long) ipPorts.get(0).getPort()); assertEquals("1fff:0:a88:85a3::ac1f", ipPorts.get(0).getHost()); + assertEquals("[1fff:0:a88:85a3::ac1f]:8001", ipPorts.get(0).toExternalForm()); assertEquals(8001, (long) ipPorts.get(1).getPort()); assertEquals("1fff:0:a88:85a3::ac2f", ipPorts.get(1).getHost()); } + + @Test + public void canParseExternalizedPair() { + HostPortPair original = new HostPortPair("foo.example.com", 999, false); + doExternalizedPairTest(original); + original = new HostPortPair("::1", 38, true); + doExternalizedPairTest(original); + original = new HostPortPair("127.0.0.1", 8000); + doExternalizedPairTest(original); + } + + private void doExternalizedPairTest(HostPortPair original) { + HostPortsParser parser = new HostPortsParser(original.toExternalForm()); + parser.parse(); + HostPortPair newPair = parser.getHostsPorts().get(0); + assertEquals(original.getHost(), newPair.getHost()); + assertEquals(original.getPort(), newPair.getPort()); + assertEquals(original.toExternalForm(), newPair.toExternalForm()); + } @Test public void failsParsingInvalidString() {