1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.net.ftp.parser;
18
19 import static org.junit.jupiter.api.Assertions.assertFalse;
20 import static org.junit.jupiter.api.Assertions.assertNotNull;
21 import static org.junit.jupiter.api.Assertions.assertNull;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23
24 import java.text.SimpleDateFormat;
25 import java.time.Instant;
26 import java.util.Calendar;
27 import java.util.Locale;
28
29 import org.apache.commons.net.ftp.FTPFile;
30 import org.apache.commons.net.ftp.FTPFileEntryParser;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33
34
35
36 public abstract class AbstractFTPParseTest {
37
38
39
40 protected enum CalendarUnit {
41 MILLISECOND(Calendar.MILLISECOND), SECOND(Calendar.SECOND), MINUTE(Calendar.MINUTE), HOUR_OF_DAY(Calendar.HOUR_OF_DAY),
42 DAY_OF_MONTH(Calendar.DAY_OF_MONTH), MONTH(Calendar.MONTH), YEAR(Calendar.YEAR);
43
44 final int unit;
45
46 CalendarUnit(final int calUnit) {
47 unit = calUnit;
48 }
49 }
50
51 private FTPFileEntryParser parser;
52
53 protected SimpleDateFormat df;
54
55
56
57
58
59
60
61 protected void doAdditionalBadTests(final String test, final FTPFile f) {
62 }
63
64
65
66
67
68
69
70 protected void doAdditionalGoodTests(final String test, final FTPFile f) {
71 }
72
73
74
75
76
77
78 protected abstract String[] getBadListing();
79
80
81
82
83
84
85 protected abstract String[] getGoodListing();
86
87
88
89
90
91
92 protected abstract FTPFileEntryParser getParser();
93
94
95
96
97
98
99
100 protected FTPFile nullFileOrNullDate(final FTPFile f) {
101 if (f == null || f.getTimestamp() == null) {
102 return null;
103 }
104 return f;
105 }
106
107 @BeforeEach
108 protected void setUp() throws Exception {
109 parser = getParser();
110 df = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy", Locale.US);
111 }
112
113 @Test
114 void testBadListing() {
115
116 final String[] badsamples = getBadListing();
117 for (final String test : badsamples) {
118
119 final FTPFile f = parser.parseFTPEntry(test);
120 assertNull(nullFileOrNullDate(f), "Should have Failed to parse <" + test + ">");
121
122 doAdditionalBadTests(test, f);
123 }
124 }
125
126
127 abstract void testDefaultPrecision();
128
129 @Test
130 void testGoodListing() {
131
132 final String[] goodsamples = getGoodListing();
133 for (final String test : goodsamples) {
134
135 final FTPFile f = parser.parseFTPEntry(test);
136 assertNotNull(f, "Failed to parse " + test);
137
138 doAdditionalGoodTests(test, f);
139 }
140 }
141
142
143
144
145
146
147 abstract void testParseFieldsOnDirectory() throws Exception;
148
149
150
151
152
153
154 abstract void testParseFieldsOnFile() throws Exception;
155
156 protected void testPrecision(final String listEntry, final CalendarUnit expectedPrecision) {
157 final FTPFile file = getParser().parseFTPEntry(listEntry);
158 assertNotNull(file, "Could not parse " + listEntry);
159 final Calendar stamp = file.getTimestamp();
160 assertNotNull(stamp, "Failed to parse time in " + listEntry);
161 final Instant instant = file.getTimestampInstant();
162 assertNotNull(instant, "Failed to parse time in " + listEntry);
163 final int ordinal = expectedPrecision.ordinal();
164 final CalendarUnit[] values = CalendarUnit.values();
165
166
167 for (int i = ordinal; i < values.length; i++) {
168 final CalendarUnit unit = values[i];
169 assertTrue(stamp.isSet(unit.unit), "Expected set " + unit + " in " + listEntry);
170 }
171
172
173 if (ordinal > 0) {
174 final CalendarUnit prevUnit = values[ordinal - 1];
175 assertFalse(stamp.isSet(prevUnit.unit), "Expected not set " + prevUnit + " in " + listEntry);
176 }
177 }
178
179 abstract void testRecentPrecision();
180 }