コード例 #1
0
  /**
   * Tests {@link FileURL#clone()}.
   *
   * @throws MalformedURLException should not happen
   */
  @Test
  public void testClone() throws MalformedURLException {
    FileURL url = getURL("login", "password", "host", 10000, "/path/to", "query");
    url.setProperty("name", "value");

    FileURL clonedURL = (FileURL) url.clone();

    // Assert that both instances are equal according to FileURL#equals
    assertEquals(url, clonedURL);

    // Assert that both URL's string representations (with credentials) are equal
    assert url.toString(true).equals(clonedURL.toString(true));

    // Assert that both instances are not one and the same
    assert url != clonedURL;

    // Assert that the property has survived the cloning
    assert "value".equals(clonedURL.getProperty("name"));
  }
コード例 #2
0
  /**
   * 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);
  }