001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.net.ftp;
018    
019    import java.text.DateFormatSymbols;
020    import java.text.ParseException;
021    import java.text.SimpleDateFormat;
022    import java.util.Date;
023    import java.util.Locale;
024    
025    import junit.framework.TestCase;
026    
027    public class FTPClientConfigTest extends TestCase {
028    
029        /*
030         * Class under test for void FTPClientConfig(String)
031         */
032        public void testFTPClientConfigString() {
033            FTPClientConfig config = new FTPClientConfig(FTPClientConfig.SYST_VMS);
034            assertEquals(FTPClientConfig.SYST_VMS, config.getServerSystemKey());
035            assertNull(config.getDefaultDateFormatStr());
036            assertNull(config.getRecentDateFormatStr());
037            assertNull(config.getShortMonthNames());
038            assertNull(config.getServerTimeZoneId());
039            assertNull(config.getServerLanguageCode());
040        }
041    
042        String A = "A";
043        String B = "B";
044        String C = "C";
045        String D = "D";
046        String E = "E";
047        String F = "F";
048    
049        /*
050         * Class under test for void FTPClientConfig(String, String, String, String, String, String)
051         */
052        public void testFTPClientConfigStringStringStringStringStringString() {
053            FTPClientConfig conf = new FTPClientConfig(A,B,C,D,E,F);
054    
055            assertEquals("A", conf.getServerSystemKey());
056            assertEquals("B", conf.getDefaultDateFormatStr());
057            assertEquals("C", conf.getRecentDateFormatStr());
058            assertEquals("E", conf.getShortMonthNames());
059            assertEquals("F", conf.getServerTimeZoneId());
060            assertEquals("D", conf.getServerLanguageCode());
061        }
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            
082            try {
083                dfs1 = FTPClientConfig.lookupDateFormatSymbols("fr");
084            } catch (IllegalArgumentException e){
085                fail("french");
086            }
087            
088            try {
089                dfs2 = FTPClientConfig.lookupDateFormatSymbols("sq");
090            } catch (IllegalArgumentException e){
091                fail("albanian");
092            }
093            
094            try {
095                dfs3 = FTPClientConfig.lookupDateFormatSymbols("ru");
096            } catch (IllegalArgumentException e){
097                fail("unusupported.default.to.en");
098            }
099            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    }