/** @tests java.net.URI(java.lang.String) */ public void test_ConstructorLjava_lang_String() throws URISyntaxException { // Regression test for HARMONY-23 try { new URI("%3"); fail("Assert 0: URI constructor failed to throw exception on invalid input."); } catch (URISyntaxException e) { // Expected assertEquals("Assert 1: Wrong index in URISyntaxException.", 0, e.getIndex()); } // Regression test for HARMONY-25 // if port value is negative, the authority should be considered registry-based. URI uri = new URI("http://host:-8096/path/index.html"); assertEquals("Assert 2: returned wrong port value,", -1, uri.getPort()); assertNull("Assert 3: returned wrong host value,", uri.getHost()); try { uri.parseServerAuthority(); fail("Assert 4: Expected URISyntaxException"); } catch (URISyntaxException e) { // Expected } uri = new URI("http", "//myhost:-8096", null); assertEquals("Assert 5: returned wrong port value,", -1, uri.getPort()); assertNull("Assert 6: returned wrong host value,", uri.getHost()); try { uri.parseServerAuthority(); fail("Assert 7: Expected URISyntaxException"); } catch (URISyntaxException e) { // Expected } }
/** * Sets fields of this URI by parsing the given string. * * @param str The string to parse * @exception URISyntaxException If the given string violates RFC 2396 */ private void parseURI(String str) throws URISyntaxException { if (URI_PATTERN == null) URI_PATTERN = Pattern.compile(URI_REGEXP); Matcher matcher = URI_PATTERN.matcher(str); if (matcher.matches()) { scheme = getURIGroup(matcher, SCHEME_GROUP); rawSchemeSpecificPart = matcher.group(SCHEME_SPEC_PART_GROUP); schemeSpecificPart = unquote(rawSchemeSpecificPart); if (!isOpaque()) { rawAuthority = getURIGroup(matcher, AUTHORITY_GROUP); rawPath = matcher.group(PATH_GROUP); rawQuery = getURIGroup(matcher, QUERY_GROUP); } rawFragment = getURIGroup(matcher, FRAGMENT_GROUP); } else throw new URISyntaxException(str, "doesn't match URI regular expression"); parseServerAuthority(); // We must eagerly unquote the parts, because this is the only time // we may throw an exception. authority = unquote(rawAuthority); userInfo = unquote(rawUserInfo); host = unquote(rawHost); path = unquote(rawPath); query = unquote(rawQuery); fragment = unquote(rawFragment); }
/** * Creates a "key" {@code URI} by dropping the <i>user-info</i>, <i>path</i>, <i>query</i>, and * <i>fragment</i> portions of the {@code URI}. * * @param requestURI the {@code URI} for which the key is to be generated * @return a {@code URI} instance with the <i>user-info</i>, <i>path</i>, <i>query</i>, and * <i>fragment</i> discarded */ private static URI createKey(URI requestURI) { try { URI keyURI = requestURI.parseServerAuthority(); return new URI( keyURI.getScheme(), null, keyURI.getHost(), keyURI.getPort(), null, null, null); } catch (URISyntaxException e) { throw new IllegalArgumentException(e); } }