MotechURL { public String getHost() { if (StringUtils.isNotBlank(url) && url.matches(URL_PATTERN)) { final String protocolDelimiter = ": final int protocolDelimiterLength = protocolDelimiter.length(); int index = url.indexOf(protocolDelimiter); int indexOfBackslash = url.indexOf("/", index + protocolDelimiterLength); if (indexOfBackslash == -1) { return url.substring(index + protocolDelimiterLength, url.length()); } return url.substring(index + protocolDelimiterLength, indexOfBackslash); } return null; } MotechURL(String url); @Override String toString(); String getHost(); static final String URL_PATTERN; }
@Test public void shouldReturnHostFromURL() { assertThat(new MotechURL("https: assertThat(new MotechURL("https: assertThat(new MotechURL("https: assertThat(new MotechURL("").getHost(), IsNull.nullValue()); assertThat(new MotechURL(null).getHost(), IsNull.nullValue()); }