/** * Tests FileURL's getters and setters. * * @throws MalformedURLException should not happen */ @Test public void testAccessors() throws MalformedURLException { FileURL url = getRootURL(); String scheme = getScheme(); Credentials credentials = new Credentials("login", "password"); String host = "host"; int port = 10000; String path = getSchemePath("/path/to"); String query = "query"; url.setScheme(scheme); url.setCredentials(credentials); url.setHost(host); url.setPort(port); url.setPath(path); url.setQuery(query); url.setProperty("name", "value"); assert scheme.equals(url.getScheme()); assert credentials.equals(url.getCredentials(), true); assert host.equals(url.getHost()); assert port == url.getPort(); assert path.equals(url.getPath()); assert query.equals(url.getQuery()); assert "to".equals(url.getFilename()); assert "value".equals(url.getProperty("name")); // Test null values url.setCredentials(null); url.setHost(null); url.setPort(-1); url.setPath("/"); url.setQuery(null); url.setProperty("name", null); assert scheme.equals(url.getScheme()); assert url.getCredentials() == null; assert !url.containsCredentials(); assert url.getHost() == null; assert -1 == url.getPort(); assert "/".equals(url.getPath()); assert url.getQuery() == null; assert url.getFilename() == null; assert url.getProperty("name") == null; assert (scheme + "://").equals(url.toString(true, false)); // Path cannot be null, the path is supposed to be "/" if a null or empty value is specified url.setPath(null); assert "/".equals(url.getPath()); url.setPath(""); assert "/".equals(url.getPath()); // Path must always start with a leading '/', if the specified does not then a '/' is // automatically added url.setPath("path/to"); assert "/path/to".equals(url.getPath()); }
/** * Ensure that non URL-safe characters in login and password parts are properly handled, both when * parsing and representing URLs as string. * * @throws MalformedURLException should not happen */ @Test public void testCredentialsURLEncoding() throws MalformedURLException { FileURL url = getRootURL(); String urlDecodedString = ":@&=+$,/?t%#[]"; String urlEncodedString = "%3A%40%26%3D%2B%24%2C%2F%3Ft%25%23%5B%5D"; url.setCredentials(new Credentials(urlDecodedString, urlDecodedString)); String urlRep = url.getScheme() + "://" + urlEncodedString + ":" + urlEncodedString + "@"; assert urlRep.equals(url.toString(true, false)); url = FileURL.getFileURL(urlRep); Credentials credentials = url.getCredentials(); assert credentials.getLogin().equals(urlDecodedString); assert credentials.getPassword().equals(urlDecodedString); }
/** * Tests <code>equals</code> methods. * * @throws MalformedURLException should not happen */ @Test public void testEquals() throws MalformedURLException { // No query part, as it is not parsed by all schemes FileURL url1 = getURL("login", "password", "host", 10000, "/path", null); url1.setProperty("name", "value"); FileURL url2 = (FileURL) url1.clone(); // Assert that both URLs are equal assertEquals(url1, url2); assertEquals(url1, url2, true, true); // Add a trailing path separator to one of the URL's path and assert they are still equal url1.setPath(url1.getPath() + url1.getPathSeparator()); assertEquals(url1, url2); assertEquals(url1, url2, true, true); // Assert that having the port part set to the standart port is equivalent to not having a port // part (-1) url1.setPort(url1.getStandardPort()); url2.setPort(-1); assertEquals(url1, url2); assertEquals(url1, url2, true, true); // Assert that the scheme comparison is case-insensitive url1.setScheme(url1.getScheme().toUpperCase()); assertEquals(url1, url2); assertEquals(url1, url2, true, true); // Assert that the host comparison is case-insensitive url1.setHost(url1.getHost().toUpperCase()); assertEquals(url1, url2); assertEquals(url1, url2, true, true); // Assert that the path comparison is case-sensitive url1.setPath(url1.getPath().toUpperCase()); assertNotEquals(url1, url2); assertNotEquals(url1, url2, true, true); // Make both URLs equal again url1.setPath(url2.getPath()); assertEquals(url1, url2); assertEquals(url1, url2, true, true); // Assert that the query comparison is case-sensitive url1.setQuery("query"); url2.setQuery("QUERY"); assertNotEquals(url1, url2); assertNotEquals(url1, url2, true, true); // Make both URLs equal again url1.setQuery(url2.getQuery()); assertEquals(url1, url2); assertEquals(url1, url2, true, true); // Assert that the credentials comparison is case-sensitive url1.setCredentials(new Credentials("LOGIN", "password")); assertEquals(url1, url2, false, false); assertNotEquals(url1, url2, true, true); url1.setCredentials(new Credentials("login", "PASSWORD")); assertEquals(url1, url2, false, false); assertNotEquals(url1, url2, true, true); // Assert that URLs are equal if credentials comparison is disabled assertEquals(url1, url2, false, true); // Make both URLs equal again url1.setCredentials(new Credentials("login", "password")); assertEquals(url1, url2, true, true); // Assert that the properties comparison is case-sensitive url1.setProperty("name", null); url1.setProperty("NAME", "value"); assertEquals(url1, url2, false, false); assertNotEquals(url1, url2, true, true); url1.setProperty("name", "VALUE"); assertEquals(url1, url2, false, false); assertNotEquals(url1, url2, true, true); // Assert that URLs are equal if properties comparison is disabled assertEquals(url1, url2, true, false); // Make both URLs equal again url1.setProperty("NAME", null); url1.setProperty("name", "value"); assertEquals(url1, url2, true, true); // Assert that the properties comparison fails if an extra property is added to one of the URLs url1.setProperty("name2", "value2"); assertNotEquals(url1, url2, true, true); // Assert that URLs are equal if properties comparison is disabled assertEquals(url1, url2, true, false); // Make both URLs equal again url1.setProperty("name2", null); assertEquals(url1, url2, true, true); }