gt:         int expectedDefaultPort = getDefaultPort();
context: /**
   * This file is part of muCommander, http://www.mucommander.com
   * Copyright (C) 2002-2010 Maxence Bernard
   *
   * muCommander is free software; you can redistribute it and/or modify
   * it under the terms of the GNU Lesser General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   *
   * muCommander is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public License
   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
   */
  
  package com.mucommander.commons.file;
  
  import org.testng.annotations.Test;
  
  import java.net.MalformedURLException;
  
  /**
   * A generic test case for {@link com.mucommander.commons.file.FileURL}. This class is abstract and must be extended for
   * each URL scheme to be tested. Those tests should be completed by additional scheme-specific tests for
   * certain particularities that would not covered by this generic test case.  
   *
   * @see com.mucommander.commons.file.FileURL
   * @author Maxence Bernard
   */
  public abstract class FileURLTestCase {
  
      ////////////////////
      // Helper methods //
      ////////////////////
  
      /**
       * Returns the scheme-specific version of the path.
       *
       * @param path a forward slash-separated path
       * @return the corresponding scheme-specific path
       */
      protected String getSchemePath(String path) {
          String separator = getPathSeparator();
  
          if(!separator.equals("/"))
              path = "/" + path.replace("/", separator);
  
          return path;
      }
  
      /**
       * Creates a URL from the given parts, ensuring that parsing it yields the given parts, and returns it.
       *
       * @param login login part, may be <code>null</code>
       * @param password password part, may be <code>null</code>
       * @param host host part, may be <code>null</code>
       * @param port port part, <code>-1</code> for none
       * @param path path part
       * @param query query part, may be <code>null</code>
       * @return a URL corresponding to the given parts
       * @throws MalformedURLException if the URL cannot be parsed
       */
      protected FileURL getURL(String login, String password, String host, int port, String path, String query) throws MalformedURLException {
          String scheme = getScheme();
          StringBuilder sb = new StringBuilder(scheme+"://");
  
          if(host!=null) {
              if(login!=null) {
                  sb.append(login);
  
                  if(password!=null) {
                      sb.append(':');
                      sb.append(password);
                  }
  
                  sb.append('@');
              }
  
              sb.append(host);
  
              if(port!=-1) {
                  sb.append(':');
                  sb.append(port);
              }
          }
  
          path = getSchemePath(path);
          sb.append(path);
  
          if(query!=null) {
              sb.append('?');
              sb.append(query);
          }
  
          // Assert that each of the url's parts match
  
          FileURL url = FileURL.getFileURL(sb.toString());
  
          assert scheme.equals(url.getScheme());
  
          if(host!=null) {
              if(login!=null) {
                  assert login.equals(url.getLogin());
                  assert url.containsCredentials();
  
                  if(password!=null)
                      assert password.equals(url.getPassword());
  
                  assert new Credentials(login, password).equals(url.getCredentials(), true);
              }
  
              assert host.equals(url.getHost());
              assert port == url.getPort();
          }
  
          if(query!=null && !isQueryParsed()) {
              assert url.getQuery() == null;
              path = path+"?"+query;
          }
          else if(query == null)
              assert url.getQuery() == null;
          else
              assert query.equals(url.getQuery());
  
          assertPathEquals(path, url);
  
          // Test the URL's string representation
          assert url.equals(FileURL.getFileURL(url.toString(true, false)));
          assert url.equals(FileURL.getFileURL(url.toString(false, false)), false, false);
  
          return url;
      }
  
      /**
       * Shorthand for {@link #getURL(String, String, String, int, String, String)} called with just a path.
       *
       * @param path the path part
       * @return a URL corresponding to the given part
       * @throws MalformedURLException if the URL cannot be parsed
       */
      protected FileURL getURL(String path) throws MalformedURLException {
          return getURL(null, null, null, -1, path, null);
      }
  
      /**
       * Shorthand for {@link #getURL(String, String, String, int, String, String)} called with just a host and path.
       *
       * @param host the host part, may be <code>null</code>  
       * @param path the path part
       * @return a URL corresponding to the given part
       * @throws MalformedURLException if the URL cannot be parsed
       */
      protected FileURL getURL(String host, String path) throws MalformedURLException {
          return getURL(null, null, host, -1, path, null);
      }
  
  
      /**
       * Returns the simplest FileURL instance possible using the scheme returned by {@link #getScheme()}, containing
       * only the scheme and '/' as a path. For instance, if <code>http</code> is the current scheme, a FileURL with
       * <code>http:///</code> as a string representation will be returned.
       *
       * @return a 'root' FileURL instance with the scheme returned by {@link # getScheme ()}
       * @throws MalformedURLException should never happen
       */
      protected FileURL getRootURL() throws MalformedURLException {
          return FileURL.getFileURL(getScheme()+":///");
      }
  
      /**
       * Attemps to parse the specified url using {@link FileURL#getFileURL(String)} and returns <code>true</code> if it
       * succeeded, <code>false</code> if it threw a <code>MalformedURLException</code>.
       *
       * @param url the URL to try and parse
       * @return <code>true</code> if the URL could be parsed, <code>false</code> if a <code>MalformedURLException</code> was thrown
       */
      protected boolean canParse(String url) {
          try {
              FileURL.getFileURL(url);
              return true;
          }
          catch(MalformedURLException e) {
              return false;
          }
      }
  
      /**
       * Asserts that both URLs are equal, and that their hashcodes are the same.
       *
       * @param url1 first url to test
       * @param url2 second url to test
       */
      protected void assertEquals(FileURL url1, FileURL url2) {
          assert url1.equals(url2);
          assert url2.equals(url1);
          assert url1.hashCode()==url2.hashCode();
      }
  
      /**
       * Asserts that both URLs are equal, comparing credentials and properties as requested. If both the
       * <code>compareCredentials</code> and <code>compareProperties</code> parameters are <code>true</code>, this method
       * asserts that the hashcode of both URLs are the same.
       *
       * @param url1 first url to test
       * @param url2 second url to test
       * @param compareCredentials if <code>true</code>, the login and password parts of both FileURL need to be
       * equal (case-sensitive) for the FileURL instances to be equal
       * @param compareProperties if <code>true</code>, all properties need to be equal (case-sensitive) in both
       * FileURL for them to be equal
       */
      protected void assertEquals(FileURL url1, FileURL url2, boolean compareCredentials, boolean compareProperties) {
          assert url1.equals(url2, compareCredentials, compareProperties);
          assert url2.equals(url1, compareCredentials, compareProperties);
  
          // Compare hash codes only if both flags are true.
          if(compareCredentials && compareProperties)
              assert url1.hashCode()==url2.hashCode();
      }
  
      /**
       * Asserts that both URLs are not equal.
       *
       * @param url1 first url to test
       * @param url2 second url to test
       */
      protected void assertNotEquals(FileURL url1, FileURL url2) {
          assert !url1.equals(url2);
          assert !url2.equals(url1);
      }
  
      /**
       * Asserts that both URLs are not equal, comparing credentials and properties as requested.
       *
       * @param url1 first url to test
       * @param url2 second url to test
       * @param compareCredentials if <code>true</code>, the login and password parts of both FileURL need to be
       * equal (case-sensitive) for the FileURL instances to be equal
       * @param compareProperties if <code>true</code>, all properties need to be equal (case-sensitive) in both
       * FileURL for them to be equal
       */
      protected void assertNotEquals(FileURL url1, FileURL url2, boolean compareCredentials, boolean compareProperties) {
          assert !url1.equals(url2, compareCredentials, compareProperties);
          assert !url2.equals(url1, compareCredentials, compareProperties);
      }
  
      /**
       * Asserts that the path of the given URL is equal to the given expected path.
       *
       * @param expectedPath the expected path
       * @param url URL to test
       */
      protected void assertPathEquals(String expectedPath, FileURL url) {
          assert expectedPath.equals(url.getPath());
      }
  
  
  
      //////////////////
      // Test methods //
      //////////////////
  
      /**
       * Ensures that the values returned by {@link FileURL#getStandardPort()} and {@link SchemeHandler#getStandardPort()}
       * match the expected one returned by {@link #getDefaultPort()}.
       *
       * @throws MalformedURLException should not happen
       */
      @Test
      public void testDefaultPort() throws MalformedURLException {
          FileURL url = getRootURL();
  