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.text.SimpleDateFormat;
019: import java.util.Calendar;
020: import java.util.Date;
021:
022: import junit.framework.TestCase;
023:
024: import org.apache.commons.net.ftp.FTPClientConfig;
025: import org.apache.commons.net.ftp.FTPFile;
026:
027: /**
028: * This is a simple TestCase that tests entry parsing using the new FTPClientConfig
029: * mechanism. The normal FTPClient cannot handle the different date formats in these
030: * entries, however using a configurable format, we can handle it easily.
031: *
032: * The original system presenting this issue was an AIX system - see bug #27437 for details.
033: *
034: * @version $Id$
035: */
036: public class FTPConfigEntryParserTest extends TestCase {
037:
038: private SimpleDateFormat df = new SimpleDateFormat();
039:
040: public void testParseFieldsOnAIX() {
041:
042: // Set a date format for this server type
043: FTPClientConfig config = new FTPClientConfig(
044: FTPClientConfig.SYST_UNIX);
045: config.setDefaultDateFormatStr("dd MMM HH:mm");
046:
047: UnixFTPEntryParser parser = new UnixFTPEntryParser();
048: parser.configure(config);
049:
050: FTPFile f = parser
051: .parseFTPEntry("-rw-r----- 1 ravensm sca 814 02 Mar 16:27 ZMIR2.m");
052:
053: assertNotNull("Could not parse entry.", f);
054: assertFalse("Is not a directory.", f.isDirectory());
055:
056: assertTrue("Should have user read permission.", f
057: .hasPermission(FTPFile.USER_ACCESS,
058: FTPFile.READ_PERMISSION));
059: assertTrue("Should have user write permission.", f
060: .hasPermission(FTPFile.USER_ACCESS,
061: FTPFile.WRITE_PERMISSION));
062: assertFalse("Should NOT have user execute permission.", f
063: .hasPermission(FTPFile.USER_ACCESS,
064: FTPFile.EXECUTE_PERMISSION));
065: assertTrue("Should have group read permission.", f
066: .hasPermission(FTPFile.GROUP_ACCESS,
067: FTPFile.READ_PERMISSION));
068: assertFalse("Should NOT have group write permission.", f
069: .hasPermission(FTPFile.GROUP_ACCESS,
070: FTPFile.WRITE_PERMISSION));
071: assertFalse("Should NOT have group execute permission.", f
072: .hasPermission(FTPFile.GROUP_ACCESS,
073: FTPFile.EXECUTE_PERMISSION));
074: assertFalse("Should NOT have world read permission.", f
075: .hasPermission(FTPFile.WORLD_ACCESS,
076: FTPFile.READ_PERMISSION));
077: assertFalse("Should NOT have world write permission.", f
078: .hasPermission(FTPFile.WORLD_ACCESS,
079: FTPFile.WRITE_PERMISSION));
080: assertFalse("Should NOT have world execute permission.", f
081: .hasPermission(FTPFile.WORLD_ACCESS,
082: FTPFile.EXECUTE_PERMISSION));
083:
084: assertEquals(1, f.getHardLinkCount());
085:
086: assertEquals("ravensm", f.getUser());
087: assertEquals("sca", f.getGroup());
088:
089: assertEquals("ZMIR2.m", f.getName());
090: assertEquals(814, f.getSize());
091:
092: Calendar cal = Calendar.getInstance();
093:
094: Date refDate = new Date();
095:
096: cal.set(Calendar.MONTH, Calendar.MARCH);
097: cal.set(Calendar.DATE, 2);
098: cal.set(Calendar.HOUR_OF_DAY, 16);
099: cal.set(Calendar.MINUTE, 27);
100: cal.set(Calendar.SECOND, 0);
101:
102: // With no year specified, it defaults to 1970
103: // TODO this is probably a bug - it should default to the current year
104: cal.set(Calendar.YEAR, 1970);
105:
106: assertEquals(df.format(cal.getTime()), df.format(f
107: .getTimestamp().getTime()));
108: }
109:
110: /**
111: * This is a new format reported on the mailing lists. Parsing this kind of
112: * entry necessitated changing the regex in the parser.
113: *
114: */
115: public void testParseEntryWithSymlink() {
116:
117: FTPClientConfig config = new FTPClientConfig(
118: FTPClientConfig.SYST_UNIX);
119: config.setDefaultDateFormatStr("yyyy-MM-dd HH:mm");
120:
121: UnixFTPEntryParser parser = new UnixFTPEntryParser();
122: parser.configure(config);
123:
124: FTPFile f = parser
125: .parseFTPEntry("lrwxrwxrwx 1 neeme neeme 23 2005-03-02 18:06 macros");
126:
127: assertNotNull("Could not parse entry.", f);
128: assertFalse("Is not a directory.", f.isDirectory());
129: assertTrue("Is a symbolic link", f.isSymbolicLink());
130:
131: assertTrue("Should have user read permission.", f
132: .hasPermission(FTPFile.USER_ACCESS,
133: FTPFile.READ_PERMISSION));
134: assertTrue("Should have user write permission.", f
135: .hasPermission(FTPFile.USER_ACCESS,
136: FTPFile.WRITE_PERMISSION));
137: assertTrue("Should have user execute permission.", f
138: .hasPermission(FTPFile.USER_ACCESS,
139: FTPFile.EXECUTE_PERMISSION));
140: assertTrue("Should have group read permission.", f
141: .hasPermission(FTPFile.GROUP_ACCESS,
142: FTPFile.READ_PERMISSION));
143: assertTrue("Should have group write permission.", f
144: .hasPermission(FTPFile.GROUP_ACCESS,
145: FTPFile.WRITE_PERMISSION));
146: assertTrue("Should have group execute permission.", f
147: .hasPermission(FTPFile.GROUP_ACCESS,
148: FTPFile.EXECUTE_PERMISSION));
149: assertTrue("Should have world read permission.", f
150: .hasPermission(FTPFile.WORLD_ACCESS,
151: FTPFile.READ_PERMISSION));
152: assertTrue("Should have world write permission.", f
153: .hasPermission(FTPFile.WORLD_ACCESS,
154: FTPFile.WRITE_PERMISSION));
155: assertTrue("Should have world execute permission.", f
156: .hasPermission(FTPFile.WORLD_ACCESS,
157: FTPFile.EXECUTE_PERMISSION));
158:
159: assertEquals(1, f.getHardLinkCount());
160:
161: assertEquals("neeme", f.getUser());
162: assertEquals("neeme", f.getGroup());
163:
164: assertEquals("macros", f.getName());
165: assertEquals(23, f.getSize());
166:
167: Calendar cal = Calendar.getInstance();
168:
169: Date refDate = new Date();
170:
171: cal.set(Calendar.MONTH, Calendar.MARCH);
172: cal.set(Calendar.DATE, 2);
173: cal.set(Calendar.HOUR_OF_DAY, 18);
174: cal.set(Calendar.MINUTE, 06);
175: cal.set(Calendar.SECOND, 0);
176: cal.set(Calendar.YEAR, 2005);
177:
178: assertEquals(df.format(cal.getTime()), df.format(f
179: .getTimestamp().getTime()));
180:
181: }
182:
183: }
|