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