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.parser;
18  
19  import static org.junit.jupiter.api.Assertions.assertThrows;
20  
21  import org.apache.commons.net.ftp.FTPClientConfig;
22  import org.apache.commons.net.ftp.FTPFileEntryParser;
23  
24  import junit.framework.TestCase;
25  
26  public class DefaultFTPFileEntryParserFactoryTest extends TestCase {
27      private void checkParserClass(final FTPFileEntryParserFactory fact, final String key, final Class<?> expected) {
28          final FTPClientConfig config = key == null ? new FTPClientConfig() : new FTPClientConfig(key);
29          final FTPFileEntryParser parser = fact.createFileEntryParser(config);
30          assertNotNull(parser);
31          assertTrue("Expected " + expected.getCanonicalName() + " got " + parser.getClass().getCanonicalName(), expected.isInstance(parser));
32      }
33  
34      public void testDefaultParserFactory() {
35          final DefaultFTPFileEntryParserFactory factory = new DefaultFTPFileEntryParserFactory();
36  
37          FTPFileEntryParser parser = factory.createFileEntryParser("unix");
38          assertTrue(parser instanceof UnixFTPEntryParser);
39  
40          parser = factory.createFileEntryParser("UNIX");
41          assertTrue(parser instanceof UnixFTPEntryParser);
42          assertFalse(((UnixFTPEntryParser) parser).trimLeadingSpaces);
43  
44          parser = factory.createFileEntryParser("UNIX_LTRIM");
45          assertTrue(parser instanceof UnixFTPEntryParser);
46          assertTrue(((UnixFTPEntryParser) parser).trimLeadingSpaces);
47  
48          parser = factory.createFileEntryParser("Unix");
49          assertTrue(parser instanceof UnixFTPEntryParser);
50  
51          parser = factory.createFileEntryParser("EnterpriseUnix");
52          assertTrue(parser instanceof UnixFTPEntryParser);
53          assertFalse(parser instanceof EnterpriseUnixFTPEntryParser);
54  
55          // works because contains the expression "Unix"
56          parser = factory.createFileEntryParser("UnixFTPEntryParser");
57          assertTrue(parser instanceof UnixFTPEntryParser);
58  
59          try {
60              parser = factory.createFileEntryParser("NT");
61              fail("Exception should have been thrown. \"NT\" is not a recognized key");
62          } catch (final ParserInitializationException pie) {
63              assertNull(pie.getCause());
64              assertTrue(pie.getMessage() + "should contain 'Unknown parser type:'", pie.getMessage().contains("Unknown parser type:"));
65          }
66  
67          parser = factory.createFileEntryParser("WindowsNT");
68          assertTrue(parser instanceof CompositeFileEntryParser);
69  
70          parser = factory.createFileEntryParser("ThigaVMSaMaJig");
71          assertTrue(parser instanceof VMSFTPEntryParser);
72  
73          parser = factory.createFileEntryParser("OS/2");
74          assertTrue(parser instanceof OS2FTPEntryParser);
75  
76          parser = factory.createFileEntryParser("OS/400");
77          assertTrue(parser instanceof CompositeFileEntryParser);
78  
79          parser = factory.createFileEntryParser("AS/400");
80          assertTrue(parser instanceof CompositeFileEntryParser);
81  
82          // Added test to make sure it handles the Unix systems that were
83          // compiled with OS as "UNKNOWN". This test validates that the
84          // check is case-insensitive.
85          parser = factory.createFileEntryParser("UNKNOWN Type: L8");
86  
87          try {
88              parser = factory.createFileEntryParser("OS2FTPFileEntryParser");
89              fail("Exception should have been thrown. \"OS2FTPFileEntryParser\" is not a recognized key");
90          } catch (final ParserInitializationException pie) {
91              assertNull(pie.getCause());
92          }
93  
94          parser = factory.createFileEntryParser("org.apache.commons.net.ftp.parser.OS2FTPEntryParser");
95          assertTrue(parser instanceof OS2FTPEntryParser);
96  
97          try {
98              factory.createFileEntryParser("org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory");
99              fail("Exception should have been thrown. \"DefaultFTPFileEntryParserFactory\" does not implement FTPFileEntryParser");
100         } catch (final ParserInitializationException pie) {
101             assertTrue(pie.getCause() instanceof ClassCastException);
102         }
103 
104         try {
105             // Class exists, but is an interface
106             factory.createFileEntryParser("org.apache.commons.net.ftp.parser.FTPFileEntryParserFactory");
107             fail("ParserInitializationException should have been thrown.");
108         } catch (final ParserInitializationException pie) {
109             assertTrue(pie.getCause() instanceof InstantiationException);
110         }
111         try {
112             // Class exists, but is abstract
113             factory.createFileEntryParser("org.apache.commons.net.ftp.FTPFileEntryParserImpl");
114             fail("ParserInitializationException should have been thrown.");
115         } catch (final ParserInitializationException pie) {
116             assertTrue(pie.getCause() instanceof InstantiationException);
117         }
118     }
119 
120     public void testDefaultParserFactoryConfig() throws Exception {
121         final DefaultFTPFileEntryParserFactory factory = new DefaultFTPFileEntryParserFactory();
122 
123         assertThrows(NullPointerException.class, () -> factory.createFileEntryParser((FTPClientConfig) null));
124         checkParserClass(factory, null, UnixFTPEntryParser.class);
125 
126         checkParserClass(factory, FTPClientConfig.SYST_OS400, OS400FTPEntryParser.class);
127         checkParserClass(factory, FTPClientConfig.SYST_AS400, CompositeFileEntryParser.class);
128         checkParserClass(factory, FTPClientConfig.SYST_L8, UnixFTPEntryParser.class);
129         checkParserClass(factory, FTPClientConfig.SYST_MVS, MVSFTPEntryParser.class);
130         checkParserClass(factory, FTPClientConfig.SYST_NETWARE, NetwareFTPEntryParser.class);
131         checkParserClass(factory, FTPClientConfig.SYST_NT, NTFTPEntryParser.class);
132         checkParserClass(factory, FTPClientConfig.SYST_OS2, OS2FTPEntryParser.class);
133         checkParserClass(factory, FTPClientConfig.SYST_UNIX, UnixFTPEntryParser.class);
134         checkParserClass(factory, FTPClientConfig.SYST_VMS, VMSFTPEntryParser.class);
135         checkParserClass(factory, FTPClientConfig.SYST_MACOS_PETER, MacOsPeterFTPEntryParser.class);
136 
137         checkParserClass(factory, "WINDOWS", NTFTPEntryParser.class); // Same as SYST_NT
138         // This is the way it works at present; config matching is exact
139         checkParserClass(factory, "Windows", CompositeFileEntryParser.class);
140 
141         checkParserClass(factory, "OS/400", OS400FTPEntryParser.class); // Same as SYST_OS400
142         // This is the way it works at present; config matching is exact
143         checkParserClass(factory, "OS/400 v1", CompositeFileEntryParser.class);
144 
145         // Note: exact matching via config is the only way to generate NTFTPEntryParser and OS400FTPEntryParser
146         // using DefaultFTPFileEntryParserFactory
147     }
148 }