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