commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sco...@apache.org
Subject cvs commit: jakarta-commons/net/src/test/org/apache/commons/net/ftp/parser DefaultFTPFileEntryParserFactoryTest.java
Date Fri, 02 Jan 2004 22:16:41 GMT
scohen      2004/01/02 14:16:41

  Modified:    net/src/java/org/apache/commons/net/ftp FTPClient.java
  Added:       net/src/java/org/apache/commons/net/ftp/parser
                        DefaultFTPFileEntryParserFactory.java
                        FTPFileEntryParserFactory.java
                        ParserInitializationException.java
               net/src/test/org/apache/commons/net/ftp/parser
                        DefaultFTPFileEntryParserFactoryTest.java
  Log:
  Implementation of a Parser Factory for creating FTPFileEntryParser entries.
  
  Revision  Changes    Path
  1.18      +142 -0    jakarta-commons/net/src/java/org/apache/commons/net/ftp/FTPClient.java
  
  Index: FTPClient.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/net/src/java/org/apache/commons/net/ftp/FTPClient.java,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- FTPClient.java	2 Jan 2004 03:39:04 -0000	1.17
  +++ FTPClient.java	2 Jan 2004 22:16:41 -0000	1.18
  @@ -69,6 +69,8 @@
   import org.apache.commons.net.io.ToNetASCIIOutputStream;
   import org.apache.commons.net.io.Util;
   import org.apache.commons.net.MalformedServerReplyException;
  +import org.apache.commons.net.ftp.parser.FTPFileEntryParserFactory;
  +import org.apache.commons.net.ftp.parser.ParserInitializationException;
   
   /***
    * FTPClient encapsulates all the functionality necessary to store and
  @@ -2212,6 +2214,146 @@
               return getReplyString();
           return null;
       }
  +    /**
  +     * Using the supplied key, and the pluggable parser factory defined by
  +     * the system property <code>ftp.entry.parser.factory</code> obtain a list
  +     * of file information for the current working directory or for just a 
  +     * single file.
  +     * <p>
  +     * Under the DefaultFTPFileEntryParserFactory, which is used unless a 
  +     * different factory has been specified in the system properties, the key 
  +     * can be either a recognized System type for which a parser has been 
  +     * defined, or the fully qualified class name of a class that implements
  +     * org.apache.commons.net.ftp.FTPFileEntryParser.
  +     * <p>
  +     * This information is obtained through the LIST command.  The contents of
  +     * the returned array is determined by the<code> FTPFileEntryParser </code>

  +     * used.
  +     * <p>
  +     * Since the server may or may not expand glob expressions, using them 
  +     * is not recommended and may well cause this method to fail.
  +     * <p>
  +     * 
  +     * @param parserKey This is a "handle" which the parser factory used
  +     *                  must be able to resolve into a class implementing
  +     *                  FTPFileEntryParser.
  +     *                  
  +     *                  In the DefaultFTPFileEntryParserFactory, this
  +     *                  may either be a specific key identifying a server type,
  +     *                  which is used to identify a parser type,
  +     *                  or the fully qualified class name of the parser.  See
  +     *                  DefaultFTPFileEntryParserFactory.createFileEntryParser
  +     *                  for full details.
  +     * @param pathname  The file or directory to list.
  +     * 
  +     * @return The list of file information contained in the given path in
  +     *         the format determined by the parser represented by the
  +     *         <code> parserKey </code> parameter.
  +     * @exception FTPConnectionClosedException
  +     *                   If the FTP server prematurely closes the connection
  +     *                   as a result of the client being idle or some other
  +     *                   reason causing the server to send FTP reply code 421.
  +     *                   This exception may be caught either as an IOException
  +     *                   or independently as itself.
  +     * @exception IOException
  +     *                   If an I/O error occurs while either sending a
  +     *                   command to the server or receiving a reply
  +     *                   from the server.
  +     * @exception ParserInitializationException
  +     *                   Thrown if the parserKey parameter cannot be
  +     *                   resolved by the selected parser factory.
  +     *                   In the DefaultFTPEntryParserFactory, this will
  +     *                   happen when parserKey is neither
  +     *                   the fully qualified class name of a class
  +     *                   implementing the interface
  +     *                   org.apache.commons.net.ftp.FTPFileEntryParser
  +     *                   nor a string containing one of the recognized keys
  +     *                   mapping to such a parser or if class loader
  +     *                   security issues prevent its being loaded.
  +     * @see org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory
  +     * @see org.apache.commons.net.ftp.parser.FTPFileEntryParserFactory
  +     * @see org.apache.commons.net.ftp.FTPFileEntryParser
  +     */
  +    public FTPFile[] getFileList(String parserKey, String pathname)
  +    throws IOException, ParserInitializationException
  +    {
  +        String factoryName = System.getProperty("ftp.entry.parser.factory",
  +           "org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory");
  +        FTPFileEntryParserFactory factory = null;
  +        try {
  +            factory = (FTPFileEntryParserFactory) 
  +                Class.forName(factoryName).newInstance();
  +        } catch (Exception e) {
  +            throw new ParserInitializationException(
  +                "Unable to instantiate parser factory", e);
  +        }
   
  +        FTPFileEntryParser parser = 
  +            factory.createFileEntryParser(parserKey);
  +        FTPFileList list = createFileList(pathname, parser);
  +        return list.getFiles();
  +    }
   
  +
  +    /**
  +     * Using the supplied key, and the pluggable parser factory defined by
  +     * the system property <code>ftp.entry.parser.factory</code>, obtain a

  +     * list of file information for the current working directory.
  +     * <p>
  +     * Under the DefaultFTPFileEntryParserFactory, which is used unless a 
  +     * different factory has been specified in the system properties, the key 
  +     * can be either a recognized System type for which a parser has been 
  +     * defined, or the fully qualified class name of a class that implements
  +     * org.apache.commons.net.ftp.FTPFileEntryParser.
  +     * <p>
  +     * This information is obtained through the LIST command.  The contents of
  +     * the returned array is determined by the<code> FTPFileEntryParser </code>

  +     * used.
  +     * <p>
  +     * 
  +     * @param parserKey This is a "handle" which the parser factory used
  +     *                  must be able to resolve into a class implementing
  +     *                  FTPFileEntryParser.
  +     *                  
  +     *                  In the DefaultFTPFileEntryParserFactory, this
  +     *                  may either be a specific key identifying a server type,
  +     *                  which is used to identify a parser type,
  +     *                  or the fully qualified class name of the parser.  See
  +     *                  DefaultFTPFileEntryParserFactory.createFileEntryParser
  +     *                  for full details.
  +     * @param pathname  The file or directory to list.
  +     * 
  +     * @return The list of file information contained in the given path in
  +     *         the format determined by the parser represented by the
  +     *         <code> parserKey </code> parameter.
  +     * @exception FTPConnectionClosedException
  +     *                   If the FTP server prematurely closes the connection
  +     *                   as a result of the client being idle or some other
  +     *                   reason causing the server to send FTP reply code 421.
  +     *                   This exception may be caught either as an IOException
  +     *                   or independently as itself.
  +     * @exception IOException
  +     *                   If an I/O error occurs while either sending a
  +     *                   command to the server or receiving a reply
  +     *                   from the server.
  +     * @exception ParserInitializationException
  +     *                   Thrown if the parserKey parameter cannot be
  +     *                   resolved by the selected parser factory.
  +     *                   In the DefaultFTPEntryParserFactory, this will
  +     *                   happen when parserKey is neither
  +     *                   the fully qualified class name of a class
  +     *                   implementing the interface
  +     *                   org.apache.commons.net.ftp.FTPFileEntryParser
  +     *                   nor a string containing one of the recognized keys
  +     *                   mapping to such a parser or if class loader
  +     *                   security issues prevent its being loaded.
  +     * @see org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory
  +     * @see org.apache.commons.net.ftp.parser.FTPFileEntryParserFactory
  +     * @see org.apache.commons.net.ftp.FTPFileEntryParser
  +     */
  +    public FTPFile[] getFileList(String parserKey) 
  +    throws IOException, ParserInitializationException
  +    {
  +        return getFileList(parserKey, "");
  +    }
   }
  
  
  
  1.1                  jakarta-commons/net/src/java/org/apache/commons/net/ftp/parser/DefaultFTPFileEntryParserFactory.java
  
  Index: DefaultFTPFileEntryParserFactory.java
  ===================================================================
  package org.apache.commons.net.ftp.parser;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2004 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Commons" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import org.apache.commons.net.ftp.FTPFileEntryParser;
  
  /**
   * This is the default implementation of the 
   * FTPFileEntryParserFactory interface.  This is the 
   * implementation that will be used by
   * org.apache.commons.net.ftp.FTPClient.getFileList()
   * if no other implementation is specified as a system
   * property.
   * 
   * @see org.apache.commons.net.ftp.FTPClient.getFileList()
   */
  public class DefaultFTPFileEntryParserFactory 
  implements FTPFileEntryParserFactory 
  {
      /**
       * This default implementation of the FTPFileEntryParserFactory
       * interface works according to the following logic:
       * <p>
       * First it attempts to interpret the supplied key as a fully
       * qualified classname of a class implementing the
       * FTPFileEntryParser interface.  If that succeeds, a parser 
       * object of this class is instantiated and is returned.
       * <p/>
       * If <code>key</code> is not recognized as a fully qualified
       * classname known to the system, this method will then attempt
       * to see whether it <b>contains</b> a string identifying one of
       * the known parsers.  This comparison is <b>case-insensitive</b>.
       * The intent here is where possible, to select as keys strings
       * which are returned by the SYST command on the systems which
       * the corresponding parser successfully parses.  This will 
       * enable this factory to be used in a future auto-detection
       * system. 
       * <p/>
       * @param key    should be a fully qualified classname corresponding to
       *               a class implementing the FTPFileEntryParser interface<br/>
       *               OR<br/>
       *               a string containing (case-insensitively) one of the
       *               following keywords:
       *               <ul>
       *               <li><code>unix</code></li>
       *               <li><code>windows</code></li>
       *               <li><code>os/2</code></li>
       *               <li><code>vms</code></li>
       *               </ul>
       * 
       * @return the FTPFileEntryParser corresponding to the supplied key.
       * @exception ParserInitializationException
       *                   thrown if for any reason the factory cannot resolve 
       *                   the supplied key into an FTPFileEntryParser.
       * @see FTPFileEntryParser
       */
      public FTPFileEntryParser createFileEntryParser(String key) 
      throws ParserInitializationException 
      {
          Class parserClass = null;
          try {
              parserClass = Class.forName(key);
              return (FTPFileEntryParser) parserClass.newInstance();
          } catch (ClassNotFoundException e) {
              String ukey = null;
              if (null != key) {
                  ukey = key.toUpperCase();
              }
              if (ukey.indexOf("UNIX") >= 0) {
                  return new UnixFTPEntryParser();
              } else if (ukey.indexOf("VMS") >= 0) {
                  return new VMSFTPEntryParser();
              } else if (ukey.indexOf("WINDOWS") >= 0) {
                  return new NTFTPEntryParser();
              } else if (ukey.indexOf("OS/2") >= 0) {
                  return new OS2FTPEntryParser();
              } else {
                  throw new ParserInitializationException(
                      "Unknown parser type: " + key);
              }
          } catch (ClassCastException e) {
              throw new ParserInitializationException(
                  parserClass.getName() 
                  + " does not implement the interface " 
                  + "org.apache.commons.net.ftp.FTPFileEntryParser.", e);
          } catch (Throwable e) {
              throw new ParserInitializationException(
                  "Error initializing parser", e);
          }
  
      }
  }
  
  
  
  
  1.1                  jakarta-commons/net/src/java/org/apache/commons/net/ftp/parser/FTPFileEntryParserFactory.java
  
  Index: FTPFileEntryParserFactory.java
  ===================================================================
  package org.apache.commons.net.ftp.parser;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2004 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Commons" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  import org.apache.commons.net.ftp.FTPFileEntryParser;
  
  /**
   * The interface describes a factory for creating FTPFileEntryParsers
   */
  public interface FTPFileEntryParserFactory {
      /**
       * Implementation should be a method that decodes the
       * supplied key and creates an object implementing the
       * interface FTPFileEntryParser.
       * 
       * @param key    A string that somehow identifies an
       *               FTPFileEntryParser to be created.
       * 
       * @return the FTPFileEntryParser created.
       * @exception ParserInitializationException
       *                   Thrown on any exception in instantiation
       * @see org.apache.commons.net.ftp.parser.ParserInitializationException
       */
      public FTPFileEntryParser createFileEntryParser(String key) 
          throws ParserInitializationException;
  
  
  }
  
  
  
  1.1                  jakarta-commons/net/src/java/org/apache/commons/net/ftp/parser/ParserInitializationException.java
  
  Index: ParserInitializationException.java
  ===================================================================
  package org.apache.commons.net.ftp.parser;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2004 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Commons" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  /**
   * This class encapsulates all errors that may be thrown by
   * the process of an FTPFileEntryParserFactory creating and
   * instantiating an FTPFileEntryParser.
   */ 
  public class ParserInitializationException extends Exception {
      
      /**
       * Root exception that caused this to be thrown
       */
      private final Throwable rootCause;
  
      /**
       * Constucts a ParserInitializationException with just a message
       * 
       * @param message Exception message
       */
      public ParserInitializationException(String message) {
          super(message);
          this.rootCause = null;
      }
  
      /**
       * Constucts a ParserInitializationException with a message 
       * and a root cause.
       * 
       * @param message   Exception message
       * @param rootCause root cause throwable that caused 
       * this to be thrown
       */
      public ParserInitializationException(String message, Throwable rootCause) {
          super(message);
          this.rootCause = rootCause;
      }
  
      /**
       * returns the root cause of this exception or null 
       * if no root cause was specified.
       * 
       * @return the root cause of this exception being thrown
       */
      public Throwable getRootCause() {
          return this.rootCause;
      }
  
  }
  
  
  
  1.1                  jakarta-commons/net/src/test/org/apache/commons/net/ftp/parser/DefaultFTPFileEntryParserFactoryTest.java
  
  Index: DefaultFTPFileEntryParserFactoryTest.java
  ===================================================================
  package org.apache.commons.net.ftp.parser;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2004 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Commons" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  import junit.framework.TestCase;
  
  import org.apache.commons.net.ftp.FTPFile;
  import org.apache.commons.net.ftp.FTPFileEntryParser;
  
  
  public class DefaultFTPFileEntryParserFactoryTest extends TestCase
  {
      public void testDefaultParserFactory() throws Exception {
          DefaultFTPFileEntryParserFactory factory = 
              new DefaultFTPFileEntryParserFactory();
  
          FTPFileEntryParser parser = factory.createFileEntryParser("unix");
          assertTrue(parser instanceof UnixFTPEntryParser);
          
          parser = factory.createFileEntryParser("UNIX");
          assertTrue(parser instanceof UnixFTPEntryParser);
  
          parser = factory.createFileEntryParser("Unix");
          assertTrue(parser instanceof UnixFTPEntryParser);
          
          parser = factory.createFileEntryParser("EnterpriseUnix");
          assertTrue(parser instanceof UnixFTPEntryParser);
          assertFalse(parser instanceof EnterpriseUnixFTPEntryParser);
  
          // works because contains the expression "Unix"
          parser = factory.createFileEntryParser("UnixFTPEntryParser");
          assertTrue(parser instanceof UnixFTPEntryParser);
          
          try {
              parser = factory.createFileEntryParser("NT");
              fail("Exception should have been thrown. \"NT\" is not a recognized key");
          } catch (ParserInitializationException pie) {
              assertNull(pie.getRootCause());
          }
  
          parser = factory.createFileEntryParser("WindowsNT");
          assertTrue(parser instanceof NTFTPEntryParser);
          
          parser = factory.createFileEntryParser("ThigaVMSaMaJig");
          assertTrue(parser instanceof VMSFTPEntryParser);
  
          parser = factory.createFileEntryParser("OS/2");
          assertTrue(parser instanceof OS2FTPEntryParser);
  
          try {
              parser = factory.createFileEntryParser("OS2FTPFileEntryParser");
              fail("Exception should have been thrown. \"OS2FTPFileEntryParser\" is not a
recognized key");
          } catch (ParserInitializationException pie) {
              assertNull(pie.getRootCause());
          }
          
          parser = factory.createFileEntryParser(
              "org.apache.commons.net.ftp.parser.OS2FTPEntryParser");
          assertTrue(parser instanceof OS2FTPEntryParser);
          
          try {
              parser = factory.createFileEntryParser(
                  "org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory");
              fail("Exception should have been thrown. \"DefaultFTPFileEntryParserFactory\"
does not implement FTPFileEntryParser");
          } catch (ParserInitializationException pie) {
              Throwable root = pie.getRootCause();
              assertTrue(root instanceof ClassCastException);
          }
      }
  }
  
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message