Source Code Cross Referenced for FTPTimestampParserImplTest.java in  » Net » Apache-commons-net-1.4.1 » org » apache » commons » net » ftp » parser » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » Apache commons net 1.4.1 » org.apache.commons.net.ftp.parser 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.