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;
017:
018: import java.text.DateFormatSymbols;
019: import java.text.ParseException;
020: import java.text.SimpleDateFormat;
021: import java.util.Date;
022: import java.util.Locale;
023:
024: import junit.framework.TestCase;
025:
026: public class FTPClientConfigTest extends TestCase {
027:
028: /*
029: * Class under test for void FTPClientConfig(String)
030: */
031: public void testFTPClientConfigString() {
032: FTPClientConfig config = new FTPClientConfig(
033: FTPClientConfig.SYST_VMS);
034: assertEquals(FTPClientConfig.SYST_VMS, config
035: .getServerSystemKey());
036: assertNull(config.getDefaultDateFormatStr());
037: assertNull(config.getRecentDateFormatStr());
038: assertNull(config.getShortMonthNames());
039: assertNull(config.getServerTimeZoneId());
040: assertNull(config.getServerLanguageCode());
041: }
042:
043: String A = "A";
044: String B = "B";
045: String C = "C";
046: String D = "D";
047: String E = "E";
048: String F = "F";
049:
050: /*
051: * Class under test for void FTPClientConfig(String, String, String, String, String, String)
052: */
053: public void testFTPClientConfigStringStringStringStringStringString() {
054: FTPClientConfig conf = new FTPClientConfig(A, B, C, D, E, F);
055:
056: assertEquals("A", conf.getServerSystemKey());
057: assertEquals("B", conf.getDefaultDateFormatStr());
058: assertEquals("C", conf.getRecentDateFormatStr());
059: assertEquals("E", conf.getShortMonthNames());
060: assertEquals("F", conf.getServerTimeZoneId());
061: assertEquals("D", conf.getServerLanguageCode());
062: }
063:
064: String badDelim = "jan,feb,mar,apr,may,jun,jul,aug.sep,oct,nov,dec";
065: String tooLong = "jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec|jan";
066: String tooShort = "jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov";
067: String fakeLang = "abc|def|ghi|jkl|mno|pqr|stu|vwx|yza|bcd|efg|hij";
068:
069: public void testSetShortMonthNames() {
070: }
071:
072: public void testGetServerLanguageCode() {
073: }
074:
075: public void testLookupDateFormatSymbols() {
076: DateFormatSymbols dfs1 = null;
077: DateFormatSymbols dfs2 = null;
078: DateFormatSymbols dfs3 = null;
079: DateFormatSymbols dfs4 = null;
080:
081: try {
082: dfs1 = FTPClientConfig.lookupDateFormatSymbols("fr");
083: } catch (IllegalArgumentException e) {
084: fail("french");
085: }
086:
087: try {
088: dfs2 = FTPClientConfig.lookupDateFormatSymbols("sq");
089: } catch (IllegalArgumentException e) {
090: fail("albanian");
091: }
092:
093: try {
094: dfs3 = FTPClientConfig.lookupDateFormatSymbols("ru");
095: } catch (IllegalArgumentException e) {
096: fail("unusupported.default.to.en");
097: }
098: try {
099: dfs4 = FTPClientConfig.lookupDateFormatSymbols(fakeLang);
100: } catch (IllegalArgumentException e) {
101: fail("not.language.code.but.defaults");
102: }
103:
104: assertEquals(dfs3, dfs4);
105:
106: SimpleDateFormat sdf1 = new SimpleDateFormat("d MMM yyyy", dfs1);
107: SimpleDateFormat sdf2 = new SimpleDateFormat("MMM dd, yyyy",
108: dfs2);
109: SimpleDateFormat sdf3 = new SimpleDateFormat("MMM dd, yyyy",
110: dfs3);
111: Date d1 = null;
112: Date d2 = null;
113: Date d3 = null;
114: try {
115: d1 = sdf1.parse("31 d\u00e9c 2004");
116: } catch (ParseException px) {
117: fail("failed.to.parse.french");
118: }
119: try {
120: d2 = sdf2.parse("dhj 31, 2004");
121: } catch (ParseException px) {
122: fail("failed.to.parse.albanian");
123: }
124: try {
125: d3 = sdf3.parse("DEC 31, 2004");
126: } catch (ParseException px) {
127: fail("failed.to.parse.'russian'");
128: }
129: assertEquals("different.parser.same.date", d1, d2);
130: assertEquals("different.parser.same.date", d1, d3);
131:
132: }
133:
134: public void testGetDateFormatSymbols() {
135:
136: try {
137: FTPClientConfig.getDateFormatSymbols(badDelim);
138: fail("bad delimiter");
139: } catch (IllegalArgumentException e) {
140: // should have failed
141: }
142: try {
143: FTPClientConfig.getDateFormatSymbols(tooLong);
144: fail("more than 12 months");
145: } catch (IllegalArgumentException e) {
146: // should have failed
147: }
148: try {
149: FTPClientConfig.getDateFormatSymbols(tooShort);
150: fail("fewer than 12 months");
151: } catch (IllegalArgumentException e) {
152: // should have failed
153: }
154: DateFormatSymbols dfs2 = null;
155: try {
156: dfs2 = FTPClientConfig.getDateFormatSymbols(fakeLang);
157: } catch (Exception e) {
158: fail("rejected valid short month string");
159: }
160: SimpleDateFormat sdf1 = new SimpleDateFormat("MMM dd, yyyy",
161: Locale.ENGLISH);
162: SimpleDateFormat sdf2 = new SimpleDateFormat("MMM dd, yyyy",
163: dfs2);
164:
165: Date d1 = null;
166: Date d2 = null;
167: try {
168: d1 = sdf1.parse("dec 31, 2004");
169: } catch (ParseException px) {
170: fail("failed.to.parse.std");
171: }
172: try {
173: d2 = sdf2.parse("hij 31, 2004");
174: } catch (ParseException px) {
175: fail("failed.to.parse.weird");
176: }
177:
178: assertEquals("different.parser.same.date", d1, d2);
179:
180: try {
181: d2 = sdf1.parse("hij 31, 2004");
182: fail("should.have.failed.to.parse.weird");
183: } catch (ParseException px) {
184: }
185: try {
186: d2 = sdf2.parse("dec 31, 2004");
187: fail("should.have.failed.to.parse.standard");
188: } catch (ParseException px) {
189: }
190:
191: }
192:
193: }
|