URLParser { static public URL parse(String url) throws MalformedURLException { int pos = 0; int i = url.indexOf(": if (i < 0) { throw new MalformedURLException("Invalid URL [" + url + "]: no protocol specified"); } URL r = new URL(); String protocol = url.substring(0, i); r.setProtocol(protocol); pos = i + 3; i = url.indexOf('@', pos); if (i >= 0) { String userPass = url.substring(pos, i); int atPos = userPass.indexOf(':'); if (atPos >= 0) { String username = userPass.substring(0, atPos); String password = userPass.substring(atPos+1); if (username != null && username.length() > 0) { r.setUsername(decode(username)); } if (password != null && password.length() > 0) { r.setPassword(decode(password)); } } else { if (userPass.length() > 0) { r.setUsername(decode(userPass)); } } pos = i + 1; } i = url.indexOf('/', pos); if (i < 0) { i = url.indexOf('?', pos); if (i < 0) { i = url.length(); } } String hostPort = url.substring(pos, i); if (hostPort != null && hostPort.length() > 0) { int colPos = hostPort.indexOf(':'); if (colPos >= 0) { String host = hostPort.substring(0, colPos); r.setHost(host); String tempPort = hostPort.substring(colPos+1); try { Integer port = Integer.valueOf(tempPort); r.setPort(port); } catch (NumberFormatException e) { throw new MalformedURLException("Invalid URL [" + url + "]: port '" + tempPort + "' was not an integer"); } } else { r.setHost(hostPort); } } else { } pos = i; if (pos >= url.length()) { return r; } if (url.charAt(pos) == '/') { i = url.indexOf('?'); if (i < 0) { i = url.length(); } String path = url.substring(pos, i); r.setPath(decode(path)); } pos = i; if (pos >= url.length()) { return r; } if (url.charAt(pos) == '?') { String query = url.substring(pos+1); if (query != null && query.length() > 0) { r.setQuery(query); } } return r; } private  URLParser(); static URL parse(String url);  }
@Test(expected=MalformedURLException.class) public void parseNoProtocol() throws Exception { URL url0 = URLParser.parse("/"); }
@Test public void parse0() throws Exception { URL url0 = URLParser.parse("http: Assert.assertEquals("http", url0.getProtocol()); Assert.assertEquals(null, url0.getUsername()); Assert.assertEquals(null, url0.getPassword()); Assert.assertEquals("www.google.com", url0.getHost()); Assert.assertEquals(null, url0.getPort()); Assert.assertEquals("/", url0.getPath()); }
@Test public void parse1() throws Exception { URL url0 = URLParser.parse("file: Assert.assertEquals("file", url0.getProtocol()); Assert.assertEquals(null, url0.getUsername()); Assert.assertEquals(null, url0.getPassword()); Assert.assertEquals(null, url0.getHost()); Assert.assertEquals(null, url0.getPort()); Assert.assertEquals("/", url0.getPath()); }
@Test public void parse2() throws Exception { URL url0 = URLParser.parse("http: Assert.assertEquals("http", url0.getProtocol()); Assert.assertEquals(null, url0.getUsername()); Assert.assertEquals(null, url0.getPassword()); Assert.assertEquals("www.google.com", url0.getHost()); Assert.assertEquals(null, url0.getPort()); Assert.assertEquals(null, url0.getPath()); }
@Test public void parse3() throws Exception { URL url0 = URLParser.parse("http: Assert.assertEquals("http", url0.getProtocol()); Assert.assertEquals(null, url0.getUsername()); Assert.assertEquals(null, url0.getPassword()); Assert.assertEquals("www.google.com", url0.getHost()); Assert.assertEquals(new Integer(80), url0.getPort()); Assert.assertEquals(null, url0.getPath()); }
@Test public void parse4() throws Exception { URL url0 = URLParser.parse("http: Assert.assertEquals("http", url0.getProtocol()); Assert.assertEquals(null, url0.getUsername()); Assert.assertEquals(null, url0.getPassword()); Assert.assertEquals("www.google.com", url0.getHost()); Assert.assertEquals(new Integer(80), url0.getPort()); Assert.assertEquals("/", url0.getPath()); }
@Test(expected=MalformedURLException.class) public void parseBadPort() throws Exception { URL url0 = URLParser.parse("http: Assert.assertEquals("http", url0.getProtocol()); Assert.assertEquals(null, url0.getUsername()); Assert.assertEquals(null, url0.getPassword()); Assert.assertEquals("www.google.com", url0.getHost()); Assert.assertEquals(new Integer(80), url0.getPort()); Assert.assertEquals("/", url0.getPath()); }
@Test public void parse5() throws Exception { URL url0 = URLParser.parse("http: Assert.assertEquals("http", url0.getProtocol()); Assert.assertEquals("u", url0.getUsername()); Assert.assertEquals(null, url0.getPassword()); Assert.assertEquals("h", url0.getHost()); Assert.assertEquals(null, url0.getPort()); Assert.assertEquals(null, url0.getPath()); }
@Test public void parse6() throws Exception { URL url0 = URLParser.parse("http: Assert.assertEquals("http", url0.getProtocol()); Assert.assertEquals("u", url0.getUsername()); Assert.assertEquals("p", url0.getPassword()); Assert.assertEquals("h", url0.getHost()); Assert.assertEquals(null, url0.getPort()); Assert.assertEquals(null, url0.getPath()); }
@Test public void parse7() throws Exception { URL url0 = URLParser.parse("http: Assert.assertEquals("http", url0.getProtocol()); Assert.assertEquals(null, url0.getUsername()); Assert.assertEquals(null, url0.getPassword()); Assert.assertEquals(null, url0.getHost()); Assert.assertEquals(null, url0.getPort()); Assert.assertEquals(null, url0.getPath()); }
@Test public void parse8() throws Exception { URL url0 = URLParser.parse("http: Assert.assertEquals("http", url0.getProtocol()); Assert.assertEquals(null, url0.getUsername()); Assert.assertEquals(null, url0.getPassword()); Assert.assertEquals(null, url0.getHost()); Assert.assertEquals(null, url0.getPort()); Assert.assertEquals(null, url0.getPath()); }
@Test(expected=MalformedURLException.class) public void parsePortSepButNoPort() throws Exception { URL url0 = URLParser.parse("http: Assert.assertEquals("http", url0.getProtocol()); Assert.assertEquals(null, url0.getUsername()); Assert.assertEquals(null, url0.getPassword()); Assert.assertEquals(null, url0.getHost()); Assert.assertEquals(null, url0.getPort()); Assert.assertEquals(null, url0.getPath()); }
@Test public void parseEncodedDifficult() throws Exception { URL url0 = URLParser.parse("http: Assert.assertEquals("http", url0.getProtocol()); Assert.assertEquals("@", url0.getUsername()); Assert.assertEquals("@", url0.getPassword()); Assert.assertEquals("h", url0.getHost()); Assert.assertEquals(new Integer(80), url0.getPort()); Assert.assertEquals("/ / / / ", url0.getPath()); }
@Test public void parseQueryString() throws Exception { URL url0 = URLParser.parse("http: Assert.assertEquals("http", url0.getProtocol()); Assert.assertNull(url0.getUsername()); Assert.assertNull(url0.getPassword()); Assert.assertEquals("www.google.com", url0.getHost()); Assert.assertNull(url0.getPort()); Assert.assertNull(url0.getPath()); url0 = URLParser.parse("http: Assert.assertEquals("http", url0.getProtocol()); Assert.assertNull(url0.getUsername()); Assert.assertNull(url0.getPassword()); Assert.assertEquals("www.google.com", url0.getHost()); Assert.assertNull(url0.getPort()); Assert.assertEquals("/", url0.getPath()); url0 = URLParser.parse("http: Assert.assertEquals("http", url0.getProtocol()); Assert.assertNull(url0.getUsername()); Assert.assertNull(url0.getPassword()); Assert.assertEquals("www.google.com", url0.getHost()); Assert.assertNull(url0.getPort()); Assert.assertEquals("/", url0.getPath()); Assert.assertEquals("1", url0.getQueryProperty("x")); url0 = URLParser.parse("http: Assert.assertEquals("http", url0.getProtocol()); Assert.assertNull(url0.getUsername()); Assert.assertNull(url0.getPassword()); Assert.assertEquals("www.google.com", url0.getHost()); Assert.assertNull(url0.getPort()); Assert.assertEquals("/", url0.getPath()); Assert.assertEquals("1", url0.getQueryProperty("x")); Assert.assertEquals("2", url0.getQueryProperty("y")); url0 = URLParser.parse("http: Assert.assertEquals("http", url0.getProtocol()); Assert.assertNull(url0.getUsername()); Assert.assertNull(url0.getPassword()); Assert.assertEquals("www.google.com", url0.getHost()); Assert.assertNull(url0.getPort()); Assert.assertEquals("/", url0.getPath()); Assert.assertEquals("@", url0.getQueryProperty("x")); Assert.assertEquals("2", url0.getQueryProperty("y")); }