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.parser;
18  import junit.framework.TestCase;
19  
20  import org.apache.commons.net.ftp.FTPFileEntryParser;
21  
22  
23  public class DefaultFTPFileEntryParserFactoryTest extends TestCase
24  {
25      public void testDefaultParserFactory() throws Exception {
26          DefaultFTPFileEntryParserFactory factory =
27              new DefaultFTPFileEntryParserFactory();
28  
29          FTPFileEntryParser parser = factory.createFileEntryParser("unix");
30          assertTrue(parser instanceof UnixFTPEntryParser);
31  
32          parser = factory.createFileEntryParser("UNIX");
33          assertTrue(parser instanceof UnixFTPEntryParser);
34  
35          parser = factory.createFileEntryParser("Unix");
36          assertTrue(parser instanceof UnixFTPEntryParser);
37  
38          parser = factory.createFileEntryParser("EnterpriseUnix");
39          assertTrue(parser instanceof UnixFTPEntryParser);
40          assertFalse(parser instanceof EnterpriseUnixFTPEntryParser);
41  
42          // works because contains the expression "Unix"
43          parser = factory.createFileEntryParser("UnixFTPEntryParser");
44          assertTrue(parser instanceof UnixFTPEntryParser);
45  
46          try {
47              parser = factory.createFileEntryParser("NT");
48              fail("Exception should have been thrown. \"NT\" is not a recognized key");
49          } catch (ParserInitializationException pie) {
50              assertNull(pie.getRootCause());
51          }
52  
53          parser = factory.createFileEntryParser("WindowsNT");
54          assertTrue(parser instanceof CompositeFileEntryParser);
55  
56          parser = factory.createFileEntryParser("ThigaVMSaMaJig");
57          assertTrue(parser instanceof VMSFTPEntryParser);
58  
59          parser = factory.createFileEntryParser("OS/2");
60          assertTrue(parser instanceof OS2FTPEntryParser);
61  
62          parser = factory.createFileEntryParser("OS/400");
63          assertTrue(parser instanceof CompositeFileEntryParser);
64  
65          parser = factory.createFileEntryParser("AS/400");
66          assertTrue(parser instanceof CompositeFileEntryParser);
67  
68          // Added test to make sure it handles the Unix systems that were
69          // compiled with OS as "UNKNOWN". This test validates that the
70          // check is case-insensitive.
71          parser = factory.createFileEntryParser("UNKNOWN Type: L8");
72  
73          try {
74              parser = factory.createFileEntryParser("OS2FTPFileEntryParser");
75              fail("Exception should have been thrown. \"OS2FTPFileEntryParser\" is not a recognized key");
76          } catch (ParserInitializationException pie) {
77              assertNull(pie.getRootCause());
78          }
79  
80          parser = factory.createFileEntryParser(
81              "org.apache.commons.net.ftp.parser.OS2FTPEntryParser");
82          assertTrue(parser instanceof OS2FTPEntryParser);
83  
84          try {
85              parser = factory.createFileEntryParser(
86                  "org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory");
87              fail("Exception should have been thrown. \"DefaultFTPFileEntryParserFactory\" does not implement FTPFileEntryParser");
88          } catch (ParserInitializationException pie) {
89              Throwable root = pie.getRootCause();
90              assertTrue(root instanceof ClassCastException);
91          }
92      }
93  }
94