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.parser;
018    import junit.framework.TestCase;
019    
020    import org.apache.commons.net.ftp.FTPFileEntryParser;
021    
022    
023    public class DefaultFTPFileEntryParserFactoryTest extends TestCase
024    {
025        public void testDefaultParserFactory() throws Exception {
026            DefaultFTPFileEntryParserFactory factory =
027                new DefaultFTPFileEntryParserFactory();
028    
029            FTPFileEntryParser parser = factory.createFileEntryParser("unix");
030            assertTrue(parser instanceof UnixFTPEntryParser);
031    
032            parser = factory.createFileEntryParser("UNIX");
033            assertTrue(parser instanceof UnixFTPEntryParser);
034    
035            parser = factory.createFileEntryParser("Unix");
036            assertTrue(parser instanceof UnixFTPEntryParser);
037    
038            parser = factory.createFileEntryParser("EnterpriseUnix");
039            assertTrue(parser instanceof UnixFTPEntryParser);
040            assertFalse(parser instanceof EnterpriseUnixFTPEntryParser);
041    
042            // works because contains the expression "Unix"
043            parser = factory.createFileEntryParser("UnixFTPEntryParser");
044            assertTrue(parser instanceof UnixFTPEntryParser);
045    
046            try {
047                parser = factory.createFileEntryParser("NT");
048                fail("Exception should have been thrown. \"NT\" is not a recognized key");
049            } catch (ParserInitializationException pie) {
050                assertNull(pie.getRootCause());
051            }
052    
053            parser = factory.createFileEntryParser("WindowsNT");
054            assertTrue(parser instanceof CompositeFileEntryParser);
055    
056            parser = factory.createFileEntryParser("ThigaVMSaMaJig");
057            assertTrue(parser instanceof VMSFTPEntryParser);
058    
059            parser = factory.createFileEntryParser("OS/2");
060            assertTrue(parser instanceof OS2FTPEntryParser);
061    
062            parser = factory.createFileEntryParser("OS/400");
063            assertTrue(parser instanceof CompositeFileEntryParser);
064    
065            parser = factory.createFileEntryParser("AS/400");
066            assertTrue(parser instanceof CompositeFileEntryParser);
067    
068            // Added test to make sure it handles the Unix systems that were
069            // compiled with OS as "UNKNOWN". This test validates that the
070            // check is case-insensitive.
071            parser = factory.createFileEntryParser("UNKNOWN Type: L8");
072    
073            try {
074                parser = factory.createFileEntryParser("OS2FTPFileEntryParser");
075                fail("Exception should have been thrown. \"OS2FTPFileEntryParser\" is not a recognized key");
076            } catch (ParserInitializationException pie) {
077                assertNull(pie.getRootCause());
078            }
079    
080            parser = factory.createFileEntryParser(
081                "org.apache.commons.net.ftp.parser.OS2FTPEntryParser");
082            assertTrue(parser instanceof OS2FTPEntryParser);
083    
084            try {
085                parser = factory.createFileEntryParser(
086                    "org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory");
087                fail("Exception should have been thrown. \"DefaultFTPFileEntryParserFactory\" does not implement FTPFileEntryParser");
088            } catch (ParserInitializationException pie) {
089                Throwable root = pie.getRootCause();
090                assertTrue(root instanceof ClassCastException);
091            }
092        }
093    }
094