Host implements NamedEndpoint, Serializable { public static Host create(final String s) throws URISyntaxException { Args.notEmpty(s, "HTTP Host"); final int portIdx = s.lastIndexOf(":"); final int port; if (portIdx > 0) { try { port = Integer.parseInt(s.substring(portIdx + 1)); } catch (final NumberFormatException ex) { throw new URISyntaxException(s, "invalid port"); } final String hostname = s.substring(0, portIdx); if (TextUtils.containsBlanks(hostname)) { throw new URISyntaxException(s, "hostname contains blanks"); } return new Host(hostname, port); } throw new URISyntaxException(s, "port not found"); } Host(final String name, final int port); static Host create(final String s); @Override String getHostName(); @Override int getPort(); @Override boolean equals(final Object o); @Override int hashCode(); @Override String toString();  }
@Test public void testCreateFromString() throws Exception { Assert.assertEquals(new Host("somehost", 8080), Host.create("somehost:8080")); Assert.assertEquals(new Host("somehost", 1234), Host.create("somehost:1234")); Assert.assertEquals(new Host("somehost", 0), Host.create("somehost:0")); }
@Test public void testCreateFromStringInvalid() throws Exception { try { Host.create(" host "); Assert.fail("URISyntaxException expected"); } catch (final URISyntaxException expected) { } try { Host.create("host :8080"); Assert.fail("URISyntaxException expected"); } catch (final URISyntaxException expected) { } try { Host.create(""); Assert.fail("IllegalArgumentException expected"); } catch (final IllegalArgumentException expected) { } }