1 16 package org.apache.commons.net.ftp.parser; 17 18 import java.text.ParseException ; 19 import java.text.SimpleDateFormat ; 20 import java.util.Calendar ; 21 import java.util.Date ; 22 import java.util.TimeZone ; 23 24 import org.apache.commons.net.ftp.FTPClientConfig; 25 26 import junit.framework.TestCase; 27 import junit.framework.TestSuite; 28 29 35 public class FTPTimestampParserImplTest extends TestCase { 36 37 private static final int TWO_HOURS_OF_MILLISECONDS = 2 * 60 * 60 * 1000; 38 39 public void testParseTimestamp() { 40 Calendar cal = Calendar.getInstance(); 41 int timeZoneOffset = cal.getTimeZone().getRawOffset(); 42 cal.add(Calendar.HOUR_OF_DAY, 1); 43 cal.set(Calendar.SECOND,0); 44 cal.set(Calendar.MILLISECOND,0); 45 Date anHourFromNow = cal.getTime(); 46 FTPTimestampParserImpl parser = new FTPTimestampParserImpl(); 47 SimpleDateFormat sdf = 48 new SimpleDateFormat (parser.getRecentDateFormatString()); 49 String fmtTime = sdf.format(anHourFromNow); 50 try { 51 Calendar parsed = parser.parseTimestamp(fmtTime); 52 assertEquals("test.roll.back.year", 1, cal.get(Calendar.YEAR) - parsed.get(Calendar.YEAR)); 55 } catch (ParseException e) { 56 fail("Unable to parse"); 57 } 58 } 59 60 public void testParseTimestampAcrossTimeZones() { 61 62 63 Calendar cal = Calendar.getInstance(); 64 cal.set(Calendar.SECOND,0); 65 cal.set(Calendar.MILLISECOND,0); 66 67 cal.add(Calendar.HOUR_OF_DAY, 1); 68 Date anHourFromNow = cal.getTime(); 69 70 cal.add(Calendar.HOUR_OF_DAY, 2); 71 Date threeHoursFromNow = cal.getTime(); 72 cal.add(Calendar.HOUR_OF_DAY, -2); 73 74 FTPTimestampParserImpl parser = new FTPTimestampParserImpl(); 75 76 FTPClientConfig config = 79 new FTPClientConfig(FTPClientConfig.SYST_UNIX); 80 config.setDefaultDateFormatStr(FTPTimestampParser.DEFAULT_SDF); 81 config.setRecentDateFormatStr(FTPTimestampParser.DEFAULT_RECENT_SDF); 82 config.setServerTimeZoneId("America/Chicago"); 84 parser.configure(config); 85 86 SimpleDateFormat sdf = (SimpleDateFormat ) 87 parser.getRecentDateFormat().clone(); 88 89 TimeZone tzla = TimeZone.getTimeZone("America/Los_Angeles"); 91 sdf.setTimeZone(tzla); 92 93 String fmtTimePlusOneHour = sdf.format(anHourFromNow); 95 String fmtTimePlusThreeHours = sdf.format(threeHoursFromNow); 96 97 98 try { 99 Calendar parsed = parser.parseTimestamp(fmtTimePlusOneHour); 100 assertEquals("no.rollback.because.of.time.zones", 103 (long)TWO_HOURS_OF_MILLISECONDS, 104 cal.getTime().getTime() - parsed.getTime().getTime()); 105 } catch (ParseException e){ 106 fail("Unable to parse " + fmtTimePlusOneHour); 107 } 108 109 try { 114 Calendar parsed = parser.parseTimestamp(fmtTimePlusThreeHours); 115 assertEquals("rollback.even.with.time.zones", 117 1, cal.get(Calendar.YEAR) - parsed.get(Calendar.YEAR)); 118 } catch (ParseException e){ 119 fail("Unable to parse" + fmtTimePlusThreeHours); 120 } 121 } 122 123 124 public void testParser() { 125 FTPTimestampParserImpl parser = new FTPTimestampParserImpl(); 126 try { 127 parser.parseTimestamp("feb 22 2002"); 128 } catch (ParseException e) { 129 fail("failed.to.parse.default"); 130 } 131 try { 132 parser.parseTimestamp("f\u00e9v 22 2002"); 133 fail("should.have.failed.to.parse.default"); 134 } catch (ParseException e) { 135 } 137 138 FTPClientConfig config = new FTPClientConfig(); 139 config.setDefaultDateFormatStr("d MMM yyyy"); 140 config.setRecentDateFormatStr("d MMM HH:mm"); 141 config.setServerLanguageCode("fr"); 142 parser.configure(config); 143 try { 144 parser.parseTimestamp("d\u00e9c 22 2002"); 145 fail("incorrect.field.order"); 146 } catch (ParseException e) { 147 } 149 try { 150 parser.parseTimestamp("22 d\u00e9c 2002"); 151 } catch (ParseException e) { 152 fail("failed.to.parse.french"); 153 } 154 155 try { 156 parser.parseTimestamp("22 dec 2002"); 157 fail("incorrect.language"); 158 } catch (ParseException e) { 159 } 161 try { 162 parser.parseTimestamp("29 f\u00e9v 2002"); 163 fail("nonexistent.date"); 164 } catch (ParseException e) { 165 } 167 168 try { 169 parser.parseTimestamp("22 ao\u00fb 30:02"); 170 fail("bad.hour"); 171 } catch (ParseException e) { 172 } 174 175 try { 176 parser.parseTimestamp("22 ao\u00fb 20:74"); 177 fail("bad.minute"); 178 } catch (ParseException e) { 179 } 181 try { 182 parser.parseTimestamp("28 ao\u00fb 20:02"); 183 } catch (ParseException e) { 184 fail("failed.to.parse.french.recent"); 185 } 186 } 187 188 193 public static TestSuite suite() 194 { 195 return(new TestSuite(FTPTimestampParserImplTest.class)); 196 } 197 198 199 200 } 201 | Popular Tags |