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.FTPClientConfig;
22  import org.apache.commons.net.ftp.FTPFile;
23  import org.apache.commons.net.ftp.FTPFileEntryParser;
24  
25  /**
26   */
27  
28  public class OS400FTPEntryParserTest extends CompositeFTPParseTestFramework {
29      private static final String[][] badsamples = {
30              { "PEP              4019 04/03/18 18:58:16 STMF       einladung.zip", "PEP               422 03/24 14:06:26 *STMF      readme",
31                      "PEP              6409 04/03/24 30:06:29 *STMF      build.xml", "PEP USR         36864 04/03/24 14:06:34 *DIR       dir1/",
32                      "PEP             3686404/03/24 14:06:47 *DIR       zdir2/" },
33  
34              { "----rwxr-x   1PEP       0           4019 Mar 18 18:58 einladung.zip", "----rwxr-x   1 PEP      0  xx        422 Mar 24 14:06 readme",
35                      "----rwxr-x   1 PEP      0           8492 Apr 07 30:13 build.xml", "d---rwxr-x   2 PEP      0          45056Mar 24 14:06 zdir2" } };
36  
37      private static final String[][] goodsamples = {
38              { "PEP              4019 04/03/18 18:58:16 *STMF      einladung.zip", "PEP               422 04/03/24 14:06:26 *STMF      readme",
39                      "PEP              6409 04/03/24 14:06:29 *STMF      build.xml", "PEP             36864 04/03/24 14:06:34 *DIR       dir1/",
40                      "PEP             36864 04/03/24 14:06:47 *DIR       zdir2/" },
41              { "----rwxr-x   1 PEP      0           4019 Mar 18 18:58 einladung.zip", "----rwxr-x   1 PEP      0            422 Mar 24 14:06 readme",
42                      "----rwxr-x   1 PEP      0           8492 Apr 07 07:13 build.xml", "d---rwxr-x   2 PEP      0          45056 Mar 24 14:06 dir1",
43                      "d---rwxr-x   2 PEP      0          45056 Mar 24 14:06 zdir2" } };
44  
45      /**
46       * @see junit.framework.TestCase#TestCase(String)
47       */
48      public OS400FTPEntryParserTest(final String name) {
49          super(name);
50      }
51  
52      @Override
53      protected void doAdditionalGoodTests(final String test, final FTPFile f) {
54          if (test.startsWith("d")) {
55              assertEquals("directory.type", FTPFile.DIRECTORY_TYPE, f.getType());
56          }
57      }
58  
59      /**
60       * @see AbstractFTPParseTest#getBadListing()
61       */
62      @Override
63      protected String[][] getBadListings() {
64          return badsamples;
65      }
66  
67      /**
68       * @see AbstractFTPParseTest#getGoodListing()
69       */
70      @Override
71      protected String[][] getGoodListings() {
72          return goodsamples;
73      }
74  
75      /**
76       * @see AbstractFTPParseTest#getParser()
77       */
78      @Override
79      protected FTPFileEntryParser getParser() {
80          return new CompositeFileEntryParser(new FTPFileEntryParser[] { new OS400FTPEntryParser(), new UnixFTPEntryParser() });
81      }
82  
83      @Override
84      public void testDefaultPrecision() {
85          testPrecision("PEP              4019 04/03/18 18:58:16 *STMF      einladung.zip", CalendarUnit.SECOND);
86      }
87  
88      public void testNET573() {
89          final FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_AS400);
90          conf.setDefaultDateFormatStr("MM/dd/yy HH:mm:ss");
91          final FTPFileEntryParser parser = new OS400FTPEntryParser(conf);
92  
93          final FTPFile f = parser.parseFTPEntry("ZFTPDEV 9069 05/20/15 15:36:52 *STMF /DRV/AUDWRKSHET/AUDWRK0204232015114625.PDF");
94          assertNotNull("Could not parse entry.", f);
95          assertNotNull("Could not parse timestamp.", f.getTimestamp());
96          assertFalse("Should not have been a directory.", f.isDirectory());
97          assertEquals("ZFTPDEV", f.getUser());
98          assertEquals("AUDWRK0204232015114625.PDF", f.getName());
99          assertEquals(9069, f.getSize());
100 
101         final Calendar cal = Calendar.getInstance();
102         cal.set(Calendar.YEAR, 2015);
103         cal.set(Calendar.MONTH, Calendar.MAY);
104         cal.set(Calendar.DAY_OF_MONTH, 20);
105         cal.set(Calendar.HOUR_OF_DAY, 15);
106         cal.set(Calendar.MINUTE, 36);
107         cal.set(Calendar.SECOND, 52);
108 
109         assertEquals(df.format(cal.getTime()), df.format(f.getTimestamp().getTime()));
110     }
111 
112     /**
113      * @see AbstractFTPParseTest#testParseFieldsOnDirectory()
114      */
115     @Override
116     public void testParseFieldsOnDirectory() throws Exception {
117         final FTPFile f = getParser().parseFTPEntry("PEP             36864 04/03/24 14:06:34 *DIR       dir1/");
118         assertNotNull("Could not parse entry.", f);
119         assertTrue("Should have been a directory.", f.isDirectory());
120         assertEquals("PEP", f.getUser());
121         assertEquals("dir1", f.getName());
122         assertEquals(36864, f.getSize());
123 
124         final Calendar cal = Calendar.getInstance();
125         cal.set(Calendar.MONTH, Calendar.MARCH);
126 
127         cal.set(Calendar.YEAR, 2004);
128         cal.set(Calendar.DAY_OF_MONTH, 24);
129         cal.set(Calendar.HOUR_OF_DAY, 14);
130         cal.set(Calendar.MINUTE, 6);
131         cal.set(Calendar.SECOND, 34);
132 
133         assertEquals(df.format(cal.getTime()), df.format(f.getTimestamp().getTime()));
134     }
135 
136     /**
137      * @see AbstractFTPParseTest#testParseFieldsOnFile()
138      */
139     @Override
140     public void testParseFieldsOnFile() throws Exception {
141         final FTPFile f = getParser().parseFTPEntry("PEP              5000000000 04/03/24 14:06:29 *STMF      build.xml");
142         assertNotNull("Could not parse entry.", f);
143         assertTrue("Should have been a file.", f.isFile());
144         assertEquals("PEP", f.getUser());
145         assertEquals("build.xml", f.getName());
146         assertEquals(5000000000L, f.getSize());
147 
148         final Calendar cal = Calendar.getInstance();
149 
150         cal.set(Calendar.DAY_OF_MONTH, 24);
151         cal.set(Calendar.MONTH, Calendar.MARCH);
152         cal.set(Calendar.YEAR, 2004);
153         cal.set(Calendar.HOUR_OF_DAY, 14);
154         cal.set(Calendar.MINUTE, 6);
155         cal.set(Calendar.SECOND, 29);
156         assertEquals(df.format(cal.getTime()), df.format(f.getTimestamp().getTime()));
157     }
158 
159     /**
160      * Test file names with spaces.
161      */
162     public void testParseFileNameWithSpaces() {
163         final FTPFile f = getParser().parseFTPEntry("MYUSER              3 06/12/21 12:00:00 *STMF      file with space.txt");
164         assertNotNull("Could not parse entry.", f);
165         assertTrue("Should have been a file.", f.isFile());
166         assertEquals("file with space.txt", f.getName());
167     }
168 
169     @Override
170     public void testRecentPrecision() {
171         testPrecision("----rwxr-x   1 PEP      0           4019 Mar 18 18:58 einladung.zip", CalendarUnit.MINUTE);
172     }
173 
174 }