HostParser { public Optional<NtHostInfo> parse(String rawHost) { try { String str = rawHost.startsWith("http: URI uri = new URI(str); String host = uri.getHost(); if (host == null) { log.warning("No host or invalid host specified: '" + rawHost + "'"); return Optional.empty(); } int port = uri.getPort(); if (port == -1) { return Optional.of(NtHostInfo.onDefaultPort(host)); } else { return Optional.of(new NtHostInfo(host, port)); } } catch (URISyntaxException e) { log.log(Level.WARNING, "Invalid NetworkTables host: " + rawHost, e); return Optional.empty(); } }  Optional<NtHostInfo> parse(String rawHost); static final int DEFAULT_PORT; }
@Test public void testTeamNumber() { var hostInfo = parser.parse("246").get(); assertEquals("246", hostInfo.getHost()); assertEquals(HostParser.DEFAULT_PORT, hostInfo.getPort()); }
@Test public void testTeamAndPort() { var hostInfo = parser.parse("246:65535").get(); assertEquals("246", hostInfo.getHost()); assertEquals(65535, hostInfo.getPort()); }
@Test public void testIpAddress() { var hostInfo = parser.parse("10.2.46.2").get(); assertEquals("10.2.46.2", hostInfo.getHost()); assertEquals(HostParser.DEFAULT_PORT, hostInfo.getPort()); }
@Test public void testPortNoHost() { var hostInfo = parser.parse(":9999"); assertTrue(hostInfo.isEmpty(), "No host specified should be empty"); }
@Test public void testHostNoPort() { var hostInfo = parser.parse("some-host:").get(); assertEquals("some-host", hostInfo.getHost()); assertEquals(HostParser.DEFAULT_PORT, hostInfo.getPort()); }
@Test public void testWithHttp() { var hostInfo = parser.parse("http: assertEquals("some-host", hostInfo.getHost()); assertEquals(12345, hostInfo.getPort()); }
@Test public void testWithHttps() { var hostInfo = parser.parse("https: assertEquals("some-host", hostInfo.getHost()); assertEquals(54321, hostInfo.getPort()); }