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