001: /*
002: * Copyright 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.ParseException;
019: import java.text.SimpleDateFormat;
020: import java.util.Calendar;
021: import java.util.Date;
022: import java.util.TimeZone;
023:
024: import org.apache.commons.net.ftp.FTPClientConfig;
025:
026: import junit.framework.TestCase;
027: import junit.framework.TestSuite;
028:
029: /**
030: * @author scohen
031: *
032: * To change the template for this generated type comment go to
033: * Window - Preferences - Java - Code Generation - Code and Comments
034: */
035: public class FTPTimestampParserImplTest extends TestCase {
036:
037: private static final int TWO_HOURS_OF_MILLISECONDS = 2 * 60 * 60 * 1000;
038:
039: public void testParseTimestamp() {
040: Calendar cal = Calendar.getInstance();
041: int timeZoneOffset = cal.getTimeZone().getRawOffset();
042: cal.add(Calendar.HOUR_OF_DAY, 1);
043: cal.set(Calendar.SECOND, 0);
044: cal.set(Calendar.MILLISECOND, 0);
045: Date anHourFromNow = cal.getTime();
046: FTPTimestampParserImpl parser = new FTPTimestampParserImpl();
047: SimpleDateFormat sdf = new SimpleDateFormat(parser
048: .getRecentDateFormatString());
049: String fmtTime = sdf.format(anHourFromNow);
050: try {
051: Calendar parsed = parser.parseTimestamp(fmtTime);
052: // since the timestamp is ahead of now (by one hour),
053: // this must mean the file's date refers to a year ago.
054: assertEquals("test.roll.back.year", 1, cal
055: .get(Calendar.YEAR)
056: - parsed.get(Calendar.YEAR));
057: } catch (ParseException e) {
058: fail("Unable to parse");
059: }
060: }
061:
062: public void testParseTimestampAcrossTimeZones() {
063:
064: Calendar cal = Calendar.getInstance();
065: cal.set(Calendar.SECOND, 0);
066: cal.set(Calendar.MILLISECOND, 0);
067:
068: cal.add(Calendar.HOUR_OF_DAY, 1);
069: Date anHourFromNow = cal.getTime();
070:
071: cal.add(Calendar.HOUR_OF_DAY, 2);
072: Date threeHoursFromNow = cal.getTime();
073: cal.add(Calendar.HOUR_OF_DAY, -2);
074:
075: FTPTimestampParserImpl parser = new FTPTimestampParserImpl();
076:
077: // assume we are FTPing a server in Chicago, two hours ahead of
078: // L. A.
079: FTPClientConfig config = new FTPClientConfig(
080: FTPClientConfig.SYST_UNIX);
081: config.setDefaultDateFormatStr(FTPTimestampParser.DEFAULT_SDF);
082: config
083: .setRecentDateFormatStr(FTPTimestampParser.DEFAULT_RECENT_SDF);
084: // 2 hours difference
085: config.setServerTimeZoneId("America/Chicago");
086: parser.configure(config);
087:
088: SimpleDateFormat sdf = (SimpleDateFormat) parser
089: .getRecentDateFormat().clone();
090:
091: // assume we're in the US Pacific Time Zone
092: TimeZone tzla = TimeZone.getTimeZone("America/Los_Angeles");
093: sdf.setTimeZone(tzla);
094:
095: // get formatted versions of time in L.A.
096: String fmtTimePlusOneHour = sdf.format(anHourFromNow);
097: String fmtTimePlusThreeHours = sdf.format(threeHoursFromNow);
098:
099: try {
100: Calendar parsed = parser.parseTimestamp(fmtTimePlusOneHour);
101: // the only difference should be the two hours
102: // difference, no rolling back a year should occur.
103: assertEquals("no.rollback.because.of.time.zones",
104: (long) TWO_HOURS_OF_MILLISECONDS, cal.getTime()
105: .getTime()
106: - parsed.getTime().getTime());
107: } catch (ParseException e) {
108: fail("Unable to parse " + fmtTimePlusOneHour);
109: }
110:
111: //but if the file's timestamp is THREE hours ahead of now, that should
112: //cause a rollover even taking the time zone difference into account.
113: //Since that time is still later than ours, it is parsed as occurring
114: //on this date last year.
115: try {
116: Calendar parsed = parser
117: .parseTimestamp(fmtTimePlusThreeHours);
118: // rollback should occur here.
119: assertEquals("rollback.even.with.time.zones", 1, cal
120: .get(Calendar.YEAR)
121: - parsed.get(Calendar.YEAR));
122: } catch (ParseException e) {
123: fail("Unable to parse" + fmtTimePlusThreeHours);
124: }
125: }
126:
127: public void testParser() {
128: FTPTimestampParserImpl parser = new FTPTimestampParserImpl();
129: try {
130: parser.parseTimestamp("feb 22 2002");
131: } catch (ParseException e) {
132: fail("failed.to.parse.default");
133: }
134: try {
135: parser.parseTimestamp("f\u00e9v 22 2002");
136: fail("should.have.failed.to.parse.default");
137: } catch (ParseException e) {
138: // this is the success case
139: }
140:
141: FTPClientConfig config = new FTPClientConfig();
142: config.setDefaultDateFormatStr("d MMM yyyy");
143: config.setRecentDateFormatStr("d MMM HH:mm");
144: config.setServerLanguageCode("fr");
145: parser.configure(config);
146: try {
147: parser.parseTimestamp("d\u00e9c 22 2002");
148: fail("incorrect.field.order");
149: } catch (ParseException e) {
150: // this is the success case
151: }
152: try {
153: parser.parseTimestamp("22 d\u00e9c 2002");
154: } catch (ParseException e) {
155: fail("failed.to.parse.french");
156: }
157:
158: try {
159: parser.parseTimestamp("22 dec 2002");
160: fail("incorrect.language");
161: } catch (ParseException e) {
162: // this is the success case
163: }
164: try {
165: parser.parseTimestamp("29 f\u00e9v 2002");
166: fail("nonexistent.date");
167: } catch (ParseException e) {
168: // this is the success case
169: }
170:
171: try {
172: parser.parseTimestamp("22 ao\u00fb 30:02");
173: fail("bad.hour");
174: } catch (ParseException e) {
175: // this is the success case
176: }
177:
178: try {
179: parser.parseTimestamp("22 ao\u00fb 20:74");
180: fail("bad.minute");
181: } catch (ParseException e) {
182: // this is the success case
183: }
184: try {
185: parser.parseTimestamp("28 ao\u00fb 20:02");
186: } catch (ParseException e) {
187: fail("failed.to.parse.french.recent");
188: }
189: }
190:
191: /**
192: * Method suite.
193: *
194: * @return TestSuite
195: */
196: public static TestSuite suite() {
197: return (new TestSuite(FTPTimestampParserImplTest.class));
198: }
199:
200: }
|