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 java.util.Calendar;
20  
21  import org.apache.commons.net.ftp.FTPFile;
22  import org.apache.commons.net.ftp.FTPFileEntryParser;
23  
24  /**
25   * @author <a href="mailto:scohen@apache.org">Steve Cohen</a>
26   * @version $Id: NTFTPEntryParserTest.java 1299238 2012-03-10 17:12:28Z sebb $
27   */
28  public class NTFTPEntryParserTest extends CompositeFTPParseTestFramework
29  {
30  
31      private static final String [][] goodsamples = {
32      { // DOS-style tests
33              "05-26-95  10:57AM               143712 $LDR$",
34              "05-20-97  03:31PM                  681 .bash_history",
35              "12-05-96  05:03PM       <DIR>          absoft2",
36              "11-14-97  04:21PM                  953 AUDITOR3.INI",
37              "05-22-97  08:08AM                  828 AUTOEXEC.BAK",
38              "01-22-98  01:52PM                  795 AUTOEXEC.BAT",
39              "05-13-97  01:46PM                  828 AUTOEXEC.DOS",
40              "12-03-96  06:38AM                  403 AUTOTOOL.LOG",
41              "12-03-96  06:38AM       <DIR>          123xyz",
42              "01-20-97  03:48PM       <DIR>          bin",
43              "05-26-1995  10:57AM               143712 $LDR$",
44              // 24hr clock as used on Windows_CE
45              "12-05-96  17:03         <DIR>          absoft2",
46              "05-22-97  08:08                    828 AUTOEXEC.BAK",
47              "01-01-98  05:00       <DIR>          Network",
48              "01-01-98  05:00       <DIR>          StorageCard",
49              "09-13-10  20:08       <DIR>          Recycled",
50              "09-06-06  19:00                   69 desktop.ini",
51              "09-13-10  13:08                   23 Control Panel.lnk",
52              "09-13-10  13:08       <DIR>          My Documents",
53              "09-13-10  13:08       <DIR>          Program Files",
54              "09-13-10  13:08       <DIR>          Temp",
55              "09-13-10  13:08       <DIR>          Windows",
56      },
57      { // Unix-style tests
58              "-rw-r--r--   1 root     root       111325 Apr 27  2001 zxJDBC-2.0.1b1.tar.gz",
59              "-rw-r--r--   1 root     root       190144 Apr 27  2001 zxJDBC-2.0.1b1.zip",
60              "-rwxr-xr-x   2 500      500           166 Nov  2  2001 73131-testtes1.afp",
61              "-rw-r--r--   1 500      500           166 Nov  9  2001 73131-testtes1.AFP",
62              "drwx------ 4 maxm Domain Users 512 Oct 2 10:59 .metadata",
63          }
64      };
65  
66      private static final String[][] badsamples =
67          {
68              { // DOS-style tests
69                  "20-05-97  03:31PM                  681 .bash_history",
70                  "     0           DIR   05-19-97   12:56  local",
71                  "     0           DIR   05-12-97   16:52  Maintenance Desktop",
72              },
73              { // Unix-style tests
74                  "drwxr-xr-x   2 root     99           4096Feb 23 30:01 zzplayer",
75              }
76              };
77  
78      private static final String directoryBeginningWithNumber =
79          "12-03-96  06:38AM       <DIR>          123xyz";
80  
81  
82      /**
83       * @see junit.framework.TestCase#TestCase(String)
84       */
85      public NTFTPEntryParserTest (String name)
86      {
87          super(name);
88      }
89  
90      /**
91       * @see org.apache.commons.net.ftp.parser.CompositeFTPParseTestFramework#getGoodListings()
92       */
93      @Override
94      protected String[][] getGoodListings()
95      {
96          return goodsamples;
97      }
98  
99      /**
100      * @see org.apache.commons.net.ftp.parser.CompositeFTPParseTestFramework#getBadListings()
101      */
102     @Override
103     protected String[][] getBadListings()
104     {
105         return badsamples;
106     }
107 
108     /**
109      * @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#getParser()
110      */
111     @Override
112     protected FTPFileEntryParser getParser()
113     {
114        return new CompositeFileEntryParser(new FTPFileEntryParser[]
115         {
116             new NTFTPEntryParser(),
117             new UnixFTPEntryParser()
118 
119         });
120     }
121 
122     /**
123      * @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#testParseFieldsOnDirectory()
124      */
125     @Override
126     public void testParseFieldsOnDirectory() throws Exception
127     {
128         FTPFile dir = getParser().parseFTPEntry("12-05-96  05:03PM       <DIR>          absoft2");
129         assertNotNull("Could not parse entry.", dir);
130         assertEquals("Thu Dec 05 17:03:00 1996",
131                      df.format(dir.getTimestamp().getTime()));
132         assertTrue("Should have been a directory.",
133                    dir.isDirectory());
134         assertEquals("absoft2", dir.getName());
135         assertEquals(0, dir.getSize());
136 
137         dir = getParser().parseFTPEntry("12-03-96  06:38AM       <DIR>          123456");
138         assertNotNull("Could not parse entry.", dir);
139         assertTrue("Should have been a directory.",
140                 dir.isDirectory());
141         assertEquals("123456", dir.getName());
142         assertEquals(0, dir.getSize());
143 
144     }
145 
146     public void testParseLeadingDigits() {
147             FTPFile file = getParser().parseFTPEntry("05-22-97  12:08AM                  5000000000 10 years and under");
148             assertNotNull("Could not parse entry", file);
149             assertEquals("10 years and under", file.getName());
150             assertEquals(5000000000L, file.getSize());
151             Calendar timestamp = file.getTimestamp();
152             assertNotNull("Could not parse time",timestamp);
153             assertEquals("Thu May 22 00:08:00 1997",df.format(timestamp.getTime()));
154 
155             FTPFile dir = getParser().parseFTPEntry("12-03-96  06:38PM       <DIR>           10 years and under");
156             assertNotNull("Could not parse entry", dir);
157             assertEquals("10 years and under", dir.getName());
158             timestamp = dir.getTimestamp();
159             assertNotNull("Could not parse time",timestamp);
160             assertEquals("Tue Dec 03 18:38:00 1996",df.format(timestamp.getTime()));
161     }
162 
163     public void testNET339() { // TODO enable when NET-339 is fixed
164         FTPFile file = getParser().parseFTPEntry("05-22-97  12:08                  5000000000 10 years and under");
165         assertNotNull("Could not parse entry", file);
166         assertEquals("10 years and under", file.getName());
167         assertEquals(5000000000L, file.getSize());
168         Calendar timestamp = file.getTimestamp();
169         assertNotNull("Could not parse time",timestamp);
170         assertEquals("Thu May 22 12:08:00 1997",df.format(timestamp.getTime()));
171 
172         FTPFile dir = getParser().parseFTPEntry("12-03-96  06:38       <DIR>           10 years and under");
173         assertNotNull("Could not parse entry", dir);
174         assertEquals("10 years and under", dir.getName());
175         timestamp = dir.getTimestamp();
176         assertNotNull("Could not parse time",timestamp);
177         assertEquals("Tue Dec 03 06:38:00 1996",df.format(timestamp.getTime()));
178 }
179 
180     /**
181      * @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#testParseFieldsOnFile()
182      */
183     @Override
184     public void testParseFieldsOnFile() throws Exception
185     {
186         FTPFile f = getParser().parseFTPEntry("05-22-97  12:08AM                  5000000000 AUTOEXEC.BAK");
187         assertNotNull("Could not parse entry.", f);
188         assertEquals("Thu May 22 00:08:00 1997",
189                      df.format(f.getTimestamp().getTime()));
190         assertTrue("Should have been a file.",
191                    f.isFile());
192         assertEquals("AUTOEXEC.BAK", f.getName());
193         assertEquals(5000000000L, f.getSize());
194 
195         // test an NT-unix style listing that does NOT have a leading zero
196         // on the hour.
197 
198         f = getParser().parseFTPEntry(
199                 "-rw-rw-r--   1 mqm        mqm          17707 Mar 12  3:33 killmq.sh.log");
200         assertNotNull("Could not parse entry.", f);
201         Calendar cal = Calendar.getInstance();
202         cal.setTime(f.getTimestamp().getTime());
203         assertEquals("hour", 3, cal.get(Calendar.HOUR));
204         assertTrue("Should have been a file.",
205                 f.isFile());
206         assertEquals(17707, f.getSize());
207     }
208 
209 
210     @Override
211     protected void doAdditionalGoodTests(String test, FTPFile f)
212     {
213         if (test.indexOf("<DIR>") >= 0)
214         {
215                     assertEquals("directory.type",
216                             FTPFile.DIRECTORY_TYPE, f.getType());
217         }
218     }
219 
220     /**
221      * test condition reported as bug 20259 => NET-106.
222      * directory with name beginning with a numeric character
223      * was not parsing correctly
224      *
225      * @throws Exception
226      */
227     public void testDirectoryBeginningWithNumber() throws Exception
228     {
229         FTPFile f = getParser().parseFTPEntry(directoryBeginningWithNumber);
230         assertEquals("name", "123xyz", f.getName());
231     }
232 
233     public void testDirectoryBeginningWithNumberFollowedBySpaces() throws Exception
234     {
235         FTPFile f = getParser().parseFTPEntry("12-03-96  06:38AM       <DIR>          123 xyz");
236         assertEquals("name", "123 xyz", f.getName());
237         f = getParser().parseFTPEntry("12-03-96  06:38AM       <DIR>          123 abc xyz");
238         assertNotNull(f);
239         assertEquals("name", "123 abc xyz", f.getName());
240     }
241 
242     /**
243      * Test that group names with embedded spaces can be handled correctly
244      *
245      */
246     public void testGroupNameWithSpaces() {
247         FTPFile f = getParser().parseFTPEntry("drwx------ 4 maxm Domain Users 512 Oct 2 10:59 .metadata");
248         assertNotNull(f);
249         assertEquals("maxm", f.getUser());
250         assertEquals("Domain Users", f.getGroup());
251     }
252 
253 }