001: /*
002: * Copyright 2001-2005 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.net.ftp.parser;
017:
018: import java.io.ByteArrayInputStream;
019: import java.io.IOException;
020:
021: import junit.framework.TestSuite;
022:
023: import org.apache.commons.net.ftp.FTPFile;
024: import org.apache.commons.net.ftp.FTPFileEntryParser;
025: import org.apache.commons.net.ftp.FTPListParseEngine;
026:
027: /**
028: * @author <a href="mailto:scohen@apache.org">Steve Cohen</a>
029: * @author <a href="sestegra@free.fr">Stephane ESTE-GRACIAS</a>
030: * @version $Id: VMSFTPEntryParserTest.java 155429 2005-02-26 13:13:04Z dirkv $
031: */
032: public class VMSFTPEntryParserTest extends FTPParseTestFramework {
033: private static final String[] badsamples = {
034:
035: "1-JUN.LIS;2 9/9 JUN-2-1998 07:32:04 [GROUP,OWNER] (RWED,RWED,RWED,)",
036: "1-JUN.LIS;2 a/9 2-JUN-98 07:32:04 [GROUP,OWNER] (RWED,RWED,RWED,)",
037: "DATA.DIR; 1 1/9 2-JUN-1998 07:32:04 [GROUP,OWNER] (,RWED,RWED,RE)",
038: "120196.TXT;1 118/126 14-APR-1997 12:45:27 PM [GROUP,OWNER] (RWED,,RWED,RE)",
039: "30CHARBAR.TXT;1 11/18 2-JUN-1998 08:38:42 [GROUP-1,OWNER] (RWED,RWED,RWED,RE)",
040: "A.;2 18/18 1-JUL-1998 08:43:20 [GROUP,OWNER] (RWED2,RWED,RWED,RE)",
041: "AA.;2 152/153 13-FED-1997 08:13:43 [GROUP,OWNER] (RWED,RWED,RWED,RE)",
042: "Directory USER1:[TEMP]\r\n\r\n", "\r\nTotal 14 files" };
043:
044: private static final String[] goodsamples = {
045: "1-JUN.LIS;1 9/9 2-JUN-1998 07:32:04 [GROUP,OWNER] (RWED,RWED,RWED,RE)",
046: "1-JUN.LIS;3 9/9 2-JUN-1998 07:32:04 [GROUP,OWNER] (RWED,RWED,RWED,)",
047: "1-JUN.LIS;2 9/9 2-JUN-1998 07:32:04 [GROUP,OWNER] (RWED,RWED,RWED,)",
048: "DATA.DIR;1 1/9 2-JUN-1998 07:32:04 [TRANSLATED] (,RWED,RWED,RE)",
049: "120196.TXT;1 118/126 14-APR-1997 12:45:27 [GROUP,OWNER] (RWED,,RWED,RE)",
050: "30CHARBAR.TXT;1 11/18 2-JUN-1998 08:38:42 [GROUP,OWNER] (RWED,RWED,RWED,RE)",
051: "A.;2 18/18 1-JUL-1998 08:43:20 [GROUP,OWNER] (RWED,RWED,RWED,RE)",
052: "AA.;2 152/153 13-FEB-1997 08:13:43 [GROUP,OWNER] (RWED,RWED,RWED,RE)",
053: "UCX$REXECD_STARTUP.LOG;1098\r\n 4/15 24-FEB-2003 13:17:24 [POSTWARE,LP] (RWED,RWED,RE,)",
054: "UNARCHIVE.COM;1 2/15 7-JUL-1997 16:37:45 [POSTWARE,LP] (RWE,RWE,RWE,RE)",
055: "UNXMERGE.COM;15 1/15 20-AUG-1996 13:59:50 [POSTWARE,LP] (RWE,RWE,RWE,RE)",
056: "UNXTEMP.COM;7 1/15 15-AUG-1996 14:10:38 [POSTWARE,LP] (RWE,RWE,RWE,RE)",
057: "UNZIP_AND_ATTACH_FILES.COM;12\r\n 14/15 24-JUL-2002 14:35:40 [TRANSLATED] (RWE,RWE,RWE,RE)",
058: "UNZIP_AND_ATTACH_FILES.SAV;1\r\n 14/15 17-JAN-2002 11:13:53 [POSTWARE,LP] (RWE,RWED,RWE,RE)",
059: "FREEWARE40.DIR;1 27/36 16-FEB-1999 10:01:46 [AP_HTTPD,APACHE$WWW (RWE,RWE,RE,RE)",
060: "1-JUN.LIS;1 9/9 2-jun-1998 07:32:04 [GROUP,OWNER] (RWED,RWED,RWED,RE)", };
061:
062: private static final String fullListing = "Directory USER1:[TEMP]\r\n\r\n"
063: + "1-JUN.LIS;1 9/9 2-JUN-1998 07:32:04 [GROUP,OWNER] (RWED,RWED,RWED,RE)\r\n"
064: + "2-JUN.LIS;1 9/9 2-JUN-1998 07:32:04 [GROUP,OWNER] (RWED,RWED,RWED,)\r\n"
065: + "3-JUN.LIS;1 9/9 3-JUN-1998 07:32:04 [GROUP,OWNER] (RWED,RWED,RWED,)\r\n"
066: + "3-JUN.LIS;4 9/9 7-JUN-1998 07:32:04 [GROUP,OWNER] (RWED,RWED,RWED,)\r\n"
067: + "3-JUN.LIS;2 9/9 4-JUN-1998 07:32:04 [GROUP,OWNER] (RWED,RWED,RWED,)\r\n"
068: + "3-JUN.LIS;3 9/9 6-JUN-1998 07:32:04 [GROUP,OWNER] (RWED,RWED,RWED,)\r\n"
069: + "\r\nTotal 6 files";
070:
071: /**
072: * @see junit.framework.TestCase#TestCase(String)
073: */
074: public VMSFTPEntryParserTest(String name) {
075: super (name);
076: }
077:
078: /**
079: * Test the parsing of the whole list.
080: * @throws IOException
081: */
082: public void testWholeListParse() throws IOException {
083: VMSFTPEntryParser parser = new VMSFTPEntryParser();
084: parser.configure(null);
085: FTPListParseEngine engine = new FTPListParseEngine(parser);
086: engine.readServerList(new ByteArrayInputStream(fullListing
087: .getBytes()));
088: FTPFile[] files = engine.getFiles();
089: assertEquals(6, files.length);
090: assertFileInListing(files, "2-JUN.LIS");
091: assertFileInListing(files, "3-JUN.LIS");
092: assertFileInListing(files, "1-JUN.LIS");
093: assertFileNotInListing(files, "1-JUN.LIS;1");
094:
095: }
096:
097: /**
098: * Test the parsing of the whole list.
099: * @throws IOException
100: */
101: public void testWholeListParseWithVersioning() throws IOException {
102:
103: VMSFTPEntryParser parser = new VMSVersioningFTPEntryParser();
104: parser.configure(null);
105: FTPListParseEngine engine = new FTPListParseEngine(parser);
106: engine.readServerList(new ByteArrayInputStream(fullListing
107: .getBytes()));
108: FTPFile[] files = engine.getFiles();
109: assertEquals(3, files.length);
110: assertFileInListing(files, "1-JUN.LIS;1");
111: assertFileInListing(files, "2-JUN.LIS;1");
112: assertFileInListing(files, "3-JUN.LIS;4");
113: assertFileNotInListing(files, "3-JUN.LIS;1");
114: assertFileNotInListing(files, "3-JUN.LIS");
115:
116: }
117:
118: public void assertFileInListing(FTPFile[] listing, String name) {
119: for (int i = 0; i < listing.length; i++) {
120: if (name.equals(listing[i].getName())) {
121: return;
122: }
123: }
124: fail("File " + name + " not found in supplied listing");
125: }
126:
127: public void assertFileNotInListing(FTPFile[] listing, String name) {
128: for (int i = 0; i < listing.length; i++) {
129: if (name.equals(listing[i].getName())) {
130: fail("Unexpected File " + name
131: + " found in supplied listing");
132: }
133: }
134: }
135:
136: /**
137: * @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#testParseFieldsOnDirectory()
138: */
139: public void testParseFieldsOnDirectory() throws Exception {
140:
141: FTPFile dir = getParser()
142: .parseFTPEntry(
143: "DATA.DIR;1 1/9 2-JUN-1998 07:32:04 [GROUP,OWNER] (RWED,RWED,RWED,RE)");
144: assertTrue("Should be a directory.", dir.isDirectory());
145: assertEquals("DATA.DIR", dir.getName());
146: assertEquals(512, dir.getSize());
147: assertEquals("Tue Jun 02 07:32:04 1998", df.format(dir
148: .getTimestamp().getTime()));
149: assertEquals("GROUP", dir.getGroup());
150: assertEquals("OWNER", dir.getUser());
151: checkPermisions(dir);
152:
153: dir = getParser()
154: .parseFTPEntry(
155: "DATA.DIR;1 1/9 2-JUN-1998 07:32:04 [TRANSLATED] (RWED,RWED,RWED,RE)");
156: assertTrue("Should be a directory.", dir.isDirectory());
157: assertEquals("DATA.DIR", dir.getName());
158: assertEquals(512, dir.getSize());
159: assertEquals("Tue Jun 02 07:32:04 1998", df.format(dir
160: .getTimestamp().getTime()));
161: assertEquals(null, dir.getGroup());
162: assertEquals("TRANSLATED", dir.getUser());
163: checkPermisions(dir);
164: }
165:
166: /**
167: * @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#testParseFieldsOnFile()
168: */
169: public void testParseFieldsOnFile() throws Exception {
170: FTPFile file = getParser()
171: .parseFTPEntry(
172: "1-JUN.LIS;1 9/9 2-JUN-1998 07:32:04 [GROUP,OWNER] (RWED,RWED,RWED,RE)");
173: assertTrue("Should be a file.", file.isFile());
174: assertEquals("1-JUN.LIS", file.getName());
175: assertEquals(9 * 512, file.getSize());
176: assertEquals("Tue Jun 02 07:32:04 1998", df.format(file
177: .getTimestamp().getTime()));
178: assertEquals("GROUP", file.getGroup());
179: assertEquals("OWNER", file.getUser());
180: checkPermisions(file);
181:
182: file = getParser()
183: .parseFTPEntry(
184: "1-JUN.LIS;1 9/9 2-JUN-1998 07:32:04 [TRANSLATED] (RWED,RWED,RWED,RE)");
185: assertTrue("Should be a file.", file.isFile());
186: assertEquals("1-JUN.LIS", file.getName());
187: assertEquals(9 * 512, file.getSize());
188: assertEquals("Tue Jun 02 07:32:04 1998", df.format(file
189: .getTimestamp().getTime()));
190: assertEquals(null, file.getGroup());
191: assertEquals("TRANSLATED", file.getUser());
192: checkPermisions(file);
193: }
194:
195: /**
196: * @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#getBadListing()
197: */
198: protected String[] getBadListing() {
199:
200: return (badsamples);
201: }
202:
203: /**
204: * @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#getGoodListing()
205: */
206: protected String[] getGoodListing() {
207:
208: return (goodsamples);
209: }
210:
211: /**
212: * @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#getParser()
213: */
214: protected FTPFileEntryParser getParser() {
215: ConfigurableFTPFileEntryParserImpl parser = new VMSFTPEntryParser();
216: parser.configure(null);
217: return parser;
218: }
219:
220: protected FTPFileEntryParser getVersioningParser() {
221: ConfigurableFTPFileEntryParserImpl parser = new VMSVersioningFTPEntryParser();
222: parser.configure(null);
223: return parser;
224: }
225:
226: /**
227: * Method checkPermisions.
228: * Verify that the VMS parser does NOT set the permissions.
229: * @param dir
230: */
231: private void checkPermisions(FTPFile dir) {
232: assertTrue("Owner should not have read permission.", !dir
233: .hasPermission(FTPFile.USER_ACCESS,
234: FTPFile.READ_PERMISSION));
235: assertTrue("Owner should not have write permission.", !dir
236: .hasPermission(FTPFile.USER_ACCESS,
237: FTPFile.WRITE_PERMISSION));
238: assertTrue("Owner should not have execute permission.", !dir
239: .hasPermission(FTPFile.USER_ACCESS,
240: FTPFile.EXECUTE_PERMISSION));
241: assertTrue("Group should not have read permission.", !dir
242: .hasPermission(FTPFile.GROUP_ACCESS,
243: FTPFile.READ_PERMISSION));
244: assertTrue("Group should not have write permission.", !dir
245: .hasPermission(FTPFile.GROUP_ACCESS,
246: FTPFile.WRITE_PERMISSION));
247: assertTrue("Group should not have execute permission.", !dir
248: .hasPermission(FTPFile.GROUP_ACCESS,
249: FTPFile.EXECUTE_PERMISSION));
250: assertTrue("World should not have read permission.", !dir
251: .hasPermission(FTPFile.WORLD_ACCESS,
252: FTPFile.READ_PERMISSION));
253: assertTrue("World should not have write permission.", !dir
254: .hasPermission(FTPFile.WORLD_ACCESS,
255: FTPFile.WRITE_PERMISSION));
256: assertTrue("World should not have execute permission.", !dir
257: .hasPermission(FTPFile.WORLD_ACCESS,
258: FTPFile.EXECUTE_PERMISSION));
259: }
260:
261: /**
262: * Method suite.
263: * @return TestSuite
264: */
265: public static TestSuite suite() {
266: return (new TestSuite(VMSFTPEntryParserTest.class));
267: }
268: }
|