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