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