    1   /*
    2    * Copyright 2001-2005 The Apache Software Foundation
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *     http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  package org.apache.commons.net.ftp.parser;
    17  import junit.framework.TestCase;
    18
    19  import java.text.SimpleDateFormat;
    20  import java.util.Locale;
    21  import org.apache.commons.net.ftp.FTPFile;
    22  import org.apache.commons.net.ftp.FTPFileEntryParser;
    23
    24  /***
    25   * @author <a href="mailto:scohen@apache.org">Steve Cohen</a>
    26   * @version $Id: FTPParseTestFramework.java 165675 2005-05-02 20:09:55Z rwinston $
    27   */
    28  public abstract class FTPParseTestFramework extends TestCase
    29  {
    30      private FTPFileEntryParser parser = null;
    31      protected SimpleDateFormat df = null;
    32
    33      /***
    34       * @see junit.framework.TestCase#TestCase(String)
    35       */
    36      public FTPParseTestFramework(String name)
    37      {
    38          super(name);
    39      }
    40
    41      /***
    42       * Method testBadListing.
    43       * Tests that parser provided failures actually fail.
    44       * @throws Exception
    45       */
    46      public void testBadListing() throws Exception
    47      {
    48
    49          String[] badsamples = getBadListing();
    50          for (int i = 0; i < badsamples.length; i++)
    51          {
    52
    53              String test = badsamples[i];
    54              FTPFile f = parser.parseFTPEntry(test);
    55              assertNull("Should have Failed to parse " + test,
    56                         f);
    57
    58              doAdditionalBadTests(test, f);
    59          }
    60      }
    61
    62      /***
    63       * Method testGoodListing.
    64       * Test that parser provided listings pass.
    65       * @throws Exception
    66       */
    67      public void testGoodListing() throws Exception
    68      {
    69
    70          String[] goodsamples = getGoodListing();
    71          for (int i = 0; i < goodsamples.length; i++)
    72          {
    73
    74              String test = goodsamples[i];
    75              FTPFile f = parser.parseFTPEntry(test);
    76              assertNotNull("Failed to parse " + test,
    77                            f);
    78
    79              doAdditionalGoodTests(test, f);
    80          }
    81      }
    82
    83      /***
    84       * during processing you could hook here to do additional tests
    85       *
    86       * @param test raw entry
    87       * @param f    parsed entry
    88       */
    89      protected void doAdditionalGoodTests(String test, FTPFile f)
    90      {
    91          }
    92
    93      /***
    94       * during processing you could hook here to do additional tests
    95       *
    96       * @param test raw entry
    97       * @param f    parsed entry
    98       */
    99      protected void doAdditionalBadTests(String test, FTPFile f)
    100     {
    101     }
    102
    103     /***
    104      * Method getBadListing.
    105      * Implementors must provide a listing that contains failures.
    106      * @return String[]
    107      */
    108     protected abstract String[] getBadListing();
    109
    110     /***
    111      * Method getGoodListing.
    112      * Implementors must provide a listing that passes.
    113      * @return String[]
    114      */
    115     protected abstract String[] getGoodListing();
    116
    117     /***
    118      * Method getParser.
    119      * Provide the parser to use for testing.
    120      * @return FTPFileEntryParser
    121      */
    122     protected abstract FTPFileEntryParser getParser();
    123
    124     /***
    125      * Method testParseFieldsOnDirectory.
    126      * Provide a test to show that fields on a directory entry are parsed correctly.
    127      * @throws Exception
    128      */
    129     public abstract void testParseFieldsOnDirectory() throws Exception;
    130
    131     /***
    132      * Method testParseFieldsOnFile.
    133      * Provide a test to show that fields on a file entry are parsed correctly.
    134      * @throws Exception
    135      */
    136     public abstract void testParseFieldsOnFile() throws Exception;
    137
    138     /***
    139      * @see junit.framework.TestCase#setUp()
    140      */
    141     protected void setUp() throws Exception
    142     {
    143         super.setUp();
    144         parser = getParser();
    145         df = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy", Locale.US);
    146     }
    147 }