1   /*
2    * Copyright 2001-2005 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.net.ftp.parser;
17  
18  import java.util.Calendar;
19  
20  import junit.framework.TestSuite;
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 155429 2005-02-26 13:13:04Z dirkv $
27   */
28  public class NTFTPEntryParserTest extends CompositeFTPParseTestFramework
29  {
30  
31      private static final String [][] goodsamples = {
32      {
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      },
45      {
46              "-rw-r--r--   1 root     root       111325 Apr 27  2001 zxJDBC-2.0.1b1.tar.gz",
47              "-rw-r--r--   1 root     root       190144 Apr 27  2001 zxJDBC-2.0.1b1.zip",
48              "-rwxr-xr-x   2 500      500           166 Nov  2  2001 73131-testtes1.afp",
49              "-rw-r--r--   1 500      500           166 Nov  9  2001 73131-testtes1.AFP",
50          }
51      };
52  
53      private static final String[][] badsamples =
54          {
55              {
56                  "20-05-97  03:31PM                  681 .bash_history",
57                  "drwxr-xr-x   2 root     99           4096 Feb 23 30:01 zzplayer",
58                  "12-05-96  17:03         <DIR>          absoft2",
59                  "05-22-97  08:08                    828 AUTOEXEC.BAK",
60                  "     0           DIR   05-19-97   12:56  local",
61                  "     0           DIR   05-12-97   16:52  Maintenance Desktop",
62              },
63              {
64                  "20-05-97  03:31PM                  681 .bash_history",
65                  "drwxr-xr-x   2 root     99           4096Feb 23 30:01 zzplayer",
66                  "12-05-96  17:03         <DIR>          absoft2",
67                  "05-22-97  08:08                    828 AUTOEXEC.BAK",
68                  "     0           DIR   05-19-97   12:56  local",
69                  "     0           DIR   05-12-97   16:52  Maintenance Desktop",
70              }
71              };
72  
73      private static final String directoryBeginningWithNumber =
74          "12-03-96  06:38AM       <DIR>          123xyz";
75  
76  
77      /**
78       * @see junit.framework.TestCase#TestCase(String)
79       */
80      public NTFTPEntryParserTest (String name)
81      {
82          super(name);
83      }
84  
85      /**
86       * @see org.apache.commons.net.ftp.parser.CompositeFTPParseTestFramework#getGoodListings()
87       */
88      protected String[][] getGoodListings()
89      {
90          return goodsamples;
91      }
92  
93      /**
94       * @see org.apache.commons.net.ftp.parser.CompositeFTPParseTestFramework#getBadListings()
95       */
96      protected String[][] getBadListings()
97      {
98          return badsamples;
99      }
100 
101     /**
102      * @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#getParser()
103      */
104     protected FTPFileEntryParser getParser()
105     {
106        return new CompositeFileEntryParser(new FTPFileEntryParser[]
107         {
108             new NTFTPEntryParser(),
109             new UnixFTPEntryParser()
110 
111         });
112     }
113 
114     /**
115      * Method suite.
116      *
117      * @return TestSuite
118      */
119     public static TestSuite suite()
120     {
121         return(new TestSuite(NTFTPEntryParserTest.class));
122     }
123 
124     /**
125      * @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#testParseFieldsOnDirectory()
126      */
127     public void testParseFieldsOnDirectory() throws Exception
128     {
129         FTPFile dir = getParser().parseFTPEntry("12-05-96  05:03PM       <DIR>          absoft2");
130         assertNotNull("Could not parse entry.", dir);
131         assertEquals("Thu Dec 05 17:03:00 1996",
132                      df.format(dir.getTimestamp().getTime()));
133         assertTrue("Should have been a directory.",
134                    dir.isDirectory());
135         assertEquals("absoft2", dir.getName());
136         assertEquals(0, dir.getSize());
137 
138         dir = getParser().parseFTPEntry("12-03-96  06:38AM       <DIR>          123456");
139         assertNotNull("Could not parse entry.", dir);
140         assertTrue("Should have been a directory.",
141                 dir.isDirectory());
142         assertEquals("123456", dir.getName());
143         assertEquals(0, dir.getSize());
144 
145     }
146 
147     /**
148      * @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#testParseFieldsOnFile()
149      */
150     public void testParseFieldsOnFile() throws Exception
151     {
152         FTPFile f = getParser().parseFTPEntry("05-22-97  12:08AM                  5000000000 AUTOEXEC.BAK");
153         assertNotNull("Could not parse entry.", f);
154         assertEquals("Thu May 22 00:08:00 1997",
155                      df.format(f.getTimestamp().getTime()));
156         assertTrue("Should have been a file.",
157                    f.isFile());
158         assertEquals("AUTOEXEC.BAK", f.getName());
159         assertEquals(5000000000L, f.getSize());
160 
161         // test an NT-unix style listing that does NOT have a leading zero
162         // on the hour.
163 
164         f = getParser().parseFTPEntry(
165                 "-rw-rw-r--   1 mqm        mqm          17707 Mar 12  3:33 killmq.sh.log");
166         assertNotNull("Could not parse entry.", f);
167         Calendar cal = Calendar.getInstance();
168         cal.setTime(f.getTimestamp().getTime());
169         assertEquals("hour", 3, cal.get(Calendar.HOUR));
170         assertTrue("Should have been a file.",
171                 f.isFile());
172         assertEquals(17707, f.getSize());
173 
174 
175 
176 
177 
178     }
179 
180 
181     protected void doAdditionalGoodTests(String test, FTPFile f)
182     {
183         if (test.indexOf("<DIR>") >= 0)
184         {
185                     assertEquals("directory.type",
186                             FTPFile.DIRECTORY_TYPE, f.getType());
187                 }
188     }
189 
190     /**
191      * test condition reported as bug 20259.
192      * directory with name beginning with a numeric character
193      * was not parsing correctly
194      *
195      * @throws Exception
196      */
197     public void testDirectoryBeginningWithNumber() throws Exception
198     {
199         FTPFile f = getParser().parseFTPEntry(directoryBeginningWithNumber);
200         assertEquals("name", "123xyz", f.getName());
201     }
202 }