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 static org.junit.jupiter.api.Assertions.assertThrows;
20  
21  import java.text.DateFormatSymbols;
22  import java.text.ParseException;
23  import java.text.SimpleDateFormat;
24  import java.util.Date;
25  import java.util.Locale;
26  
27  import junit.framework.TestCase;
28  
29  public class FTPClientConfigTest extends TestCase {
30  
31      private static final String A = "A";
32  
33      private static final String B = "B";
34      private static final String C = "C";
35      private static final String D = "D";
36      private static final String E = "E";
37      private static final String F = "F";
38      private static final String badDelim = "jan,feb,mar,apr,may,jun,jul,aug.sep,oct,nov,dec";
39  
40      private static final String tooLong = "jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec|jan";
41  
42      private static final String tooShort = "jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov";
43      private static final String fakeLang = "abc|def|ghi|jkl|mno|pqr|stu|vwx|yza|bcd|efg|hij";
44  
45      /*
46       * Class under test for void FTPClientConfig(String)
47       */
48      public void testFTPClientConfigString() {
49          final FTPClientConfig config = new FTPClientConfig(FTPClientConfig.SYST_VMS);
50          assertEquals(FTPClientConfig.SYST_VMS, config.getServerSystemKey());
51          assertNull(config.getDefaultDateFormatStr());
52          assertNull(config.getRecentDateFormatStr());
53          assertNull(config.getShortMonthNames());
54          assertNull(config.getServerTimeZoneId());
55          assertNull(config.getServerLanguageCode());
56      }
57  
58      /*
59       * Class under test for void FTPClientConfig(String, String, String, String, String, String)
60       */
61      public void testFTPClientConfigStringStringStringStringStringString() {
62          final FTPClientConfig conf = new FTPClientConfig(A, B, C, D, E, F);
63  
64          assertEquals("A", conf.getServerSystemKey());
65          assertEquals("B", conf.getDefaultDateFormatStr());
66          assertEquals("C", conf.getRecentDateFormatStr());
67          assertEquals("E", conf.getShortMonthNames());
68          assertEquals("F", conf.getServerTimeZoneId());
69          assertEquals("D", conf.getServerLanguageCode());
70      }
71  
72      public void testGetDateFormatSymbols() {
73  
74          assertThrows(IllegalArgumentException.class, () -> FTPClientConfig.getDateFormatSymbols(badDelim), "bad delimiter");
75          assertThrows(IllegalArgumentException.class, () -> FTPClientConfig.getDateFormatSymbols(tooLong), "more than 12 months");
76          assertThrows(IllegalArgumentException.class, () -> FTPClientConfig.getDateFormatSymbols(tooShort), "fewer than 12 months");
77          DateFormatSymbols dfs2 = null;
78          try {
79              dfs2 = FTPClientConfig.getDateFormatSymbols(fakeLang);
80          } catch (final Exception e) {
81              fail("rejected valid short month string");
82          }
83          final SimpleDateFormat sdf1 = new SimpleDateFormat("MMM dd, yyyy", Locale.ENGLISH);
84          final SimpleDateFormat sdf2 = new SimpleDateFormat("MMM dd, yyyy", dfs2);
85  
86          Date d1 = null;
87          Date d2 = null;
88          try {
89              d1 = sdf1.parse("dec 31, 2004");
90          } catch (final ParseException px) {
91              fail("failed.to.parse.std");
92          }
93          try {
94              d2 = sdf2.parse("hij 31, 2004");
95          } catch (final ParseException px) {
96              fail("failed.to.parse.weird");
97          }
98  
99          assertEquals("different.parser.same.date", d1, d2);
100 
101         assertThrows(ParseException.class, () -> sdf1.parse("hij 31, 2004"));
102         assertThrows(ParseException.class, () -> sdf2.parse("dec 31, 2004"));
103     }
104 
105     public void testGetServerLanguageCode() {
106     }
107 
108     public void testLookupDateFormatSymbols() {
109         DateFormatSymbols dfs1 = null;
110         DateFormatSymbols dfs2 = null;
111         DateFormatSymbols dfs3 = null;
112         DateFormatSymbols dfs4 = null;
113 
114         try {
115             dfs1 = FTPClientConfig.lookupDateFormatSymbols("fr");
116         } catch (final IllegalArgumentException e) {
117             fail("french");
118         }
119 
120         try {
121             dfs2 = FTPClientConfig.lookupDateFormatSymbols("sq");
122         } catch (final IllegalArgumentException e) {
123             fail("albanian");
124         }
125 
126         try {
127             dfs3 = FTPClientConfig.lookupDateFormatSymbols("ru");
128         } catch (final IllegalArgumentException e) {
129             fail("unusupported.default.to.en");
130         }
131         try {
132             dfs4 = FTPClientConfig.lookupDateFormatSymbols(fakeLang);
133         } catch (final IllegalArgumentException e) {
134             fail("not.language.code.but.defaults");
135         }
136 
137         assertEquals(dfs3, dfs4);
138 
139         final SimpleDateFormat sdf1 = new SimpleDateFormat("d MMM yyyy", dfs1);
140         final SimpleDateFormat sdf2 = new SimpleDateFormat("MMM dd, yyyy", dfs2);
141         final SimpleDateFormat sdf3 = new SimpleDateFormat("MMM dd, yyyy", dfs3);
142         Date d1 = null;
143         Date d2 = null;
144         Date d3 = null;
145         try {
146             d1 = sdf1.parse("31 d\u00e9c 2004");
147         } catch (final ParseException px) {
148             fail("failed.to.parse.french");
149         }
150         try {
151             d2 = sdf2.parse("dhj 31, 2004");
152         } catch (final ParseException px) {
153             fail("failed.to.parse.albanian");
154         }
155         try {
156             d3 = sdf3.parse("DEC 31, 2004");
157         } catch (final ParseException px) {
158             fail("failed.to.parse.'russian'");
159         }
160         assertEquals("different.parser.same.date", d1, d2);
161         assertEquals("different.parser.same.date", d1, d3);
162 
163     }
164 
165     public void testSetShortMonthNames() {
166     }
167 
168 }