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.util.Calendar;
019:
020: import junit.framework.TestSuite;
021: import org.apache.commons.net.ftp.FTPFile;
022: import org.apache.commons.net.ftp.FTPFileEntryParser;
023:
024: /**
025: * @author <a href="mailto:scohen@apache.org">Steve Cohen</a>
026: * @version $Id: NTFTPEntryParserTest.java 155429 2005-02-26 13:13:04Z dirkv $
027: */
028: public class NTFTPEntryParserTest extends
029: CompositeFTPParseTestFramework {
030:
031: private static final String[][] goodsamples = {
032: {
033: "05-26-95 10:57AM 143712 $LDR$",
034: "05-20-97 03:31PM 681 .bash_history",
035: "12-05-96 05:03PM <DIR> absoft2",
036: "11-14-97 04:21PM 953 AUDITOR3.INI",
037: "05-22-97 08:08AM 828 AUTOEXEC.BAK",
038: "01-22-98 01:52PM 795 AUTOEXEC.BAT",
039: "05-13-97 01:46PM 828 AUTOEXEC.DOS",
040: "12-03-96 06:38AM 403 AUTOTOOL.LOG",
041: "12-03-96 06:38AM <DIR> 123xyz",
042: "01-20-97 03:48PM <DIR> bin",
043: "05-26-1995 10:57AM 143712 $LDR$", },
044: {
045: "-rw-r--r-- 1 root root 111325 Apr 27 2001 zxJDBC-2.0.1b1.tar.gz",
046: "-rw-r--r-- 1 root root 190144 Apr 27 2001 zxJDBC-2.0.1b1.zip",
047: "-rwxr-xr-x 2 500 500 166 Nov 2 2001 73131-testtes1.afp",
048: "-rw-r--r-- 1 500 500 166 Nov 9 2001 73131-testtes1.AFP", } };
049:
050: private static final String[][] badsamples = {
051: {
052: "20-05-97 03:31PM 681 .bash_history",
053: "drwxr-xr-x 2 root 99 4096 Feb 23 30:01 zzplayer",
054: "12-05-96 17:03 <DIR> absoft2",
055: "05-22-97 08:08 828 AUTOEXEC.BAK",
056: " 0 DIR 05-19-97 12:56 local",
057: " 0 DIR 05-12-97 16:52 Maintenance Desktop", },
058: {
059: "20-05-97 03:31PM 681 .bash_history",
060: "drwxr-xr-x 2 root 99 4096Feb 23 30:01 zzplayer",
061: "12-05-96 17:03 <DIR> absoft2",
062: "05-22-97 08:08 828 AUTOEXEC.BAK",
063: " 0 DIR 05-19-97 12:56 local",
064: " 0 DIR 05-12-97 16:52 Maintenance Desktop", } };
065:
066: private static final String directoryBeginningWithNumber = "12-03-96 06:38AM <DIR> 123xyz";
067:
068: /**
069: * @see junit.framework.TestCase#TestCase(String)
070: */
071: public NTFTPEntryParserTest(String name) {
072: super (name);
073: }
074:
075: /**
076: * @see org.apache.commons.net.ftp.parser.CompositeFTPParseTestFramework#getGoodListings()
077: */
078: protected String[][] getGoodListings() {
079: return goodsamples;
080: }
081:
082: /**
083: * @see org.apache.commons.net.ftp.parser.CompositeFTPParseTestFramework#getBadListings()
084: */
085: protected String[][] getBadListings() {
086: return badsamples;
087: }
088:
089: /**
090: * @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#getParser()
091: */
092: protected FTPFileEntryParser getParser() {
093: return new CompositeFileEntryParser(new FTPFileEntryParser[] {
094: new NTFTPEntryParser(), new UnixFTPEntryParser()
095:
096: });
097: }
098:
099: /**
100: * Method suite.
101: *
102: * @return TestSuite
103: */
104: public static TestSuite suite() {
105: return (new TestSuite(NTFTPEntryParserTest.class));
106: }
107:
108: /**
109: * @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#testParseFieldsOnDirectory()
110: */
111: public void testParseFieldsOnDirectory() throws Exception {
112: FTPFile dir = getParser().parseFTPEntry(
113: "12-05-96 05:03PM <DIR> absoft2");
114: assertNotNull("Could not parse entry.", dir);
115: assertEquals("Thu Dec 05 17:03:00 1996", df.format(dir
116: .getTimestamp().getTime()));
117: assertTrue("Should have been a directory.", dir.isDirectory());
118: assertEquals("absoft2", dir.getName());
119: assertEquals(0, dir.getSize());
120:
121: dir = getParser().parseFTPEntry(
122: "12-03-96 06:38AM <DIR> 123456");
123: assertNotNull("Could not parse entry.", dir);
124: assertTrue("Should have been a directory.", dir.isDirectory());
125: assertEquals("123456", dir.getName());
126: assertEquals(0, dir.getSize());
127:
128: }
129:
130: /**
131: * @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#testParseFieldsOnFile()
132: */
133: public void testParseFieldsOnFile() throws Exception {
134: FTPFile f = getParser()
135: .parseFTPEntry(
136: "05-22-97 12:08AM 5000000000 AUTOEXEC.BAK");
137: assertNotNull("Could not parse entry.", f);
138: assertEquals("Thu May 22 00:08:00 1997", df.format(f
139: .getTimestamp().getTime()));
140: assertTrue("Should have been a file.", f.isFile());
141: assertEquals("AUTOEXEC.BAK", f.getName());
142: assertEquals(5000000000L, f.getSize());
143:
144: // test an NT-unix style listing that does NOT have a leading zero
145: // on the hour.
146:
147: f = getParser()
148: .parseFTPEntry(
149: "-rw-rw-r-- 1 mqm mqm 17707 Mar 12 3:33 killmq.sh.log");
150: assertNotNull("Could not parse entry.", f);
151: Calendar cal = Calendar.getInstance();
152: cal.setTime(f.getTimestamp().getTime());
153: assertEquals("hour", 3, cal.get(Calendar.HOUR));
154: assertTrue("Should have been a file.", f.isFile());
155: assertEquals(17707, f.getSize());
156:
157: }
158:
159: protected void doAdditionalGoodTests(String test, FTPFile f) {
160: if (test.indexOf("<DIR>") >= 0) {
161: assertEquals("directory.type", FTPFile.DIRECTORY_TYPE, f
162: .getType());
163: }
164: }
165:
166: /**
167: * test condition reported as bug 20259.
168: * directory with name beginning with a numeric character
169: * was not parsing correctly
170: *
171: * @throws Exception
172: */
173: public void testDirectoryBeginningWithNumber() throws Exception {
174: FTPFile f = getParser().parseFTPEntry(
175: directoryBeginningWithNumber);
176: assertEquals("name", "123xyz", f.getName());
177: }
178: }
|