View Javadoc
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  
19  import org.apache.commons.net.ftp.FTPFile;
20  import org.apache.commons.net.ftp.FTPFileEntryParser;
21  
22  /**
23   */
24  public abstract class CompositeFTPParseTestFramework extends AbstractFTPParseTest {
25      public CompositeFTPParseTestFramework(final String name) {
26          super(name);
27      }
28  
29      @Override
30      protected String[] getBadListing() {
31          return getBadListings()[0];
32      }
33  
34      /**
35       * Method getBadListing. Implementors must provide multiple listing that contains failures and must force the composite parser to switch the FtpEntryParser
36       *
37       * @return String[]
38       */
39      protected abstract String[][] getBadListings();
40  
41      @Override
42      protected String[] getGoodListing() {
43          return getGoodListings()[0];
44      }
45  
46      /**
47       * Method getGoodListing. Implementors must provide multiple listing that passes and must force the composite parser to switch the FtpEntryParser
48       *
49       * @return String[]
50       */
51      protected abstract String[][] getGoodListings();
52  
53      /*
54       * (non-Javadoc)
55       *
56       * @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#testGoodListing()
57       */
58      @Override
59      public void testBadListing() {
60          final String badsamples[][] = getBadListings();
61  
62          for (final String[] badsample : badsamples) {
63              final FTPFileEntryParser parser = getParser();
64              for (final String test : badsample) {
65                  final FTPFile f = parser.parseFTPEntry(test);
66                  assertNull("Should have Failed to parse " + test, nullFileOrNullDate(f));
67  
68                  doAdditionalBadTests(test, f);
69              }
70          }
71      }
72  
73      /*
74       * (non-Javadoc)
75       *
76       * @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#testGoodListing()
77       */
78      public void testConsistentListing() {
79          final String goodsamples[][] = getGoodListings();
80  
81          for (final String[] goodsample : goodsamples) {
82              final FTPFileEntryParser parser = getParser();
83              for (final String test : goodsample) {
84                  final FTPFile f = parser.parseFTPEntry(test);
85                  assertNotNull("Failed to parse " + test, f);
86  
87                  doAdditionalGoodTests(test, f);
88              }
89          }
90      }
91  
92      // even though all these listings are good using one parser
93      // or the other, this tests that a parser that has succeeded
94      // on one format will fail if another format is substituted.
95      public void testInconsistentListing() {
96          final String goodsamples[][] = getGoodListings();
97  
98          final FTPFileEntryParser parser = getParser();
99  
100         for (int i = 0; i < goodsamples.length; i++) {
101             final String test = goodsamples[i][0];
102             final FTPFile f = parser.parseFTPEntry(test);
103 
104             switch (i) {
105             case 0:
106                 assertNotNull("Failed to parse " + test, f);
107                 break;
108             case 1:
109                 assertNull("Should have failed to parse " + test, f);
110                 break;
111             }
112         }
113     }
114 }