View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.net.ftp;
18  
19  import java.text.DateFormatSymbols;
20  import java.text.ParseException;
21  import java.text.SimpleDateFormat;
22  import java.util.Date;
23  import java.util.Locale;
24  
25  import junit.framework.TestCase;
26  
27  public class FTPClientConfigTest extends TestCase {
28  
29      /*
30       * Class under test for void FTPClientConfig(String)
31       */
32      public void testFTPClientConfigString() {
33          FTPClientConfig config = new FTPClientConfig(FTPClientConfig.SYST_VMS);
34          assertEquals(FTPClientConfig.SYST_VMS, config.getServerSystemKey());
35          assertNull(config.getDefaultDateFormatStr());
36          assertNull(config.getRecentDateFormatStr());
37          assertNull(config.getShortMonthNames());
38          assertNull(config.getServerTimeZoneId());
39          assertNull(config.getServerLanguageCode());
40      }
41  
42      String A = "A";
43      String B = "B";
44      String C = "C";
45      String D = "D";
46      String E = "E";
47      String F = "F";
48  
49      /*
50       * Class under test for void FTPClientConfig(String, String, String, String, String, String)
51       */
52      public void testFTPClientConfigStringStringStringStringStringString() {
53          FTPClientConfig conf = new FTPClientConfig(A,B,C,D,E,F);
54  
55          assertEquals("A", conf.getServerSystemKey());
56          assertEquals("B", conf.getDefaultDateFormatStr());
57          assertEquals("C", conf.getRecentDateFormatStr());
58          assertEquals("E", conf.getShortMonthNames());
59          assertEquals("F", conf.getServerTimeZoneId());
60          assertEquals("D", conf.getServerLanguageCode());
61      }
62  
63  
64      String badDelim = "jan,feb,mar,apr,may,jun,jul,aug.sep,oct,nov,dec";
65      String tooLong =  "jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec|jan";
66      String tooShort = "jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov";
67      String fakeLang = "abc|def|ghi|jkl|mno|pqr|stu|vwx|yza|bcd|efg|hij";
68  
69      public void testSetShortMonthNames() {
70      }
71  
72      public void testGetServerLanguageCode() {
73      }
74  
75      public void testLookupDateFormatSymbols() {
76          DateFormatSymbols dfs1 = null;
77          DateFormatSymbols dfs2 = null;
78          DateFormatSymbols dfs3 = null;
79          DateFormatSymbols dfs4 = null;
80  
81  
82          try {
83              dfs1 = FTPClientConfig.lookupDateFormatSymbols("fr");
84          } catch (IllegalArgumentException e){
85              fail("french");
86          }
87  
88          try {
89              dfs2 = FTPClientConfig.lookupDateFormatSymbols("sq");
90          } catch (IllegalArgumentException e){
91              fail("albanian");
92          }
93  
94          try {
95              dfs3 = FTPClientConfig.lookupDateFormatSymbols("ru");
96          } catch (IllegalArgumentException e){
97              fail("unusupported.default.to.en");
98          }
99          try {
100             dfs4 = FTPClientConfig.lookupDateFormatSymbols(fakeLang);
101         } catch (IllegalArgumentException e){
102             fail("not.language.code.but.defaults");
103         }
104 
105         assertEquals(dfs3,dfs4);
106 
107         SimpleDateFormat sdf1 = new SimpleDateFormat("d MMM yyyy", dfs1);
108         SimpleDateFormat sdf2 = new SimpleDateFormat("MMM dd, yyyy", dfs2);
109         SimpleDateFormat sdf3 = new SimpleDateFormat("MMM dd, yyyy", dfs3);
110         Date d1 = null;
111         Date d2 = null;
112         Date d3 = null;
113         try {
114             d1 = sdf1.parse("31 d\u00e9c 2004");
115         } catch (ParseException px) {
116             fail("failed.to.parse.french");
117         }
118         try {
119             d2 = sdf2.parse("dhj 31, 2004");
120         } catch (ParseException px) {
121             fail("failed.to.parse.albanian");
122         }
123         try {
124             d3 = sdf3.parse("DEC 31, 2004");
125         } catch (ParseException px) {
126             fail("failed.to.parse.'russian'");
127         }
128         assertEquals("different.parser.same.date", d1, d2);
129         assertEquals("different.parser.same.date", d1, d3);
130 
131     }
132 
133     public void testGetDateFormatSymbols() {
134 
135         try {
136             FTPClientConfig.getDateFormatSymbols(badDelim);
137             fail("bad delimiter");
138         } catch (IllegalArgumentException e){
139             // should have failed
140         }
141         try {
142             FTPClientConfig.getDateFormatSymbols(tooLong);
143             fail("more than 12 months");
144         } catch (IllegalArgumentException e){
145             // should have failed
146         }
147         try {
148             FTPClientConfig.getDateFormatSymbols(tooShort);
149             fail("fewer than 12 months");
150         } catch (IllegalArgumentException e){
151             // should have failed
152         }
153         DateFormatSymbols dfs2 = null;
154         try {
155             dfs2 = FTPClientConfig.getDateFormatSymbols(fakeLang);
156         } catch (Exception e){
157             fail("rejected valid short month string");
158         }
159         SimpleDateFormat sdf1 =
160             new SimpleDateFormat("MMM dd, yyyy", Locale.ENGLISH);
161         SimpleDateFormat sdf2 = new SimpleDateFormat("MMM dd, yyyy", dfs2);
162 
163         Date d1 = null;
164         Date d2 = null;
165         try {
166             d1 = sdf1.parse("dec 31, 2004");
167         } catch (ParseException px) {
168             fail("failed.to.parse.std");
169         }
170         try {
171             d2 = sdf2.parse("hij 31, 2004");
172         } catch (ParseException px) {
173             fail("failed.to.parse.weird");
174         }
175 
176         assertEquals("different.parser.same.date",d1, d2);
177 
178         try {
179             d2 = sdf1.parse("hij 31, 2004");
180             fail("should.have.failed.to.parse.weird");
181         } catch (ParseException px) {
182         }
183         try {
184             d2 = sdf2.parse("dec 31, 2004");
185             fail("should.have.failed.to.parse.standard");
186         } catch (ParseException px) {
187         }
188 
189 
190     }
191 
192 }