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