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