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