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