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