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.ArrayList;
20  import java.util.Collections;
21  import java.util.List;
22  
23  import org.apache.commons.net.ftp.FTPFile;
24  import org.apache.commons.net.ftp.FTPFileEntryParser;
25  
26  /**
27   * Test suite addapted to new MVSFTPEntryParser.java.
28   */
29  public class MVSFTPEntryParserTest extends AbstractFTPParseTest {
30  
31      private static final String[] goodsamplesDatasetList = {
32              /* Note, if the string begins with SAVE, the parsed entry is stored in the List saveftpfiles */
33              // "Volume Unit Referred Ext Used Recfm Lrecl BlkSz Dsorg Dsname",
34              "SAVE00 3390   2004/06/23  1    1  FB     128  6144  PS    INCOMING.RPTBM023.D061704",
35              "SAVE01 3390   2004/06/23  1    1  FB     128  6144  PO    INCOMING.RPTBM024.D061704",
36              "SAVE02 3390   2004/06/23  1    1  FB     128  6144  PO-E  INCOMING.RPTBM025.D061704",
37              "PSMLC1 3390   2005/04/04  1    1  VB   27994 27998  PS    file3.I",
38              "PSMLB9 3390   2005/04/04  1    1  VB   27994 27998  PS    file4.I.BU",
39              "PSMLB6 3390   2005/04/05  1    1  VB   27994 27998  PS    file3.I.BU",
40              "PSMLC6 3390   2005/04/05  1    1  VB   27994 27998  PS    file6.I",
41              "PSMLB7 3390   2005/04/04  1    1  VB   27994 27998  PS    file7.O",
42              "PSMLC6 3390   2005/04/05  1    1  VB   27994 27998  PS    file7.O.BU",
43              "FPFS49 3390   2004/06/23  1    1  FB     128  6144  PO-E  INCOMING.RPTBM026.D061704",
44              "FPFS41 3390   2004/06/23  1    1  FB     128  6144  PS    INCOMING.RPTBM056.D061704",
45              "FPFS25 3390   2004/06/23  1    1  FB     128  6144  PS    INCOMING.WTM204.D061704",
46              "PEX26F 3390   2017/07/03  115807  FB   29600 29600  PS    INCOMING.FIN.D170630.T160630",
47              "VVVVVV 3390   2020/04/18  1   60   U   32760 32760  PO    NAME",
48              "TST017 3390   2023/09/08  2   59  FBA    133 32718  PO    NAME",
49              "TST063 3390   2023/09/11  1    1  VBA    133 27930  PS    NAME",
50              "Migrated                                                  NAME",
51              "ARCIVE Not Direct Access Device                           NAME"
52              };
53  
54      private static final String[] goodsamplesMemberList = {
55              /* Note, if the string begins with SAVE, the parsed entry is stored in the List saveftpfiles */
56              "Name      VV.MM   Created       Changed      Size  Init   Mod   Id", "SAVE03    01.03 2002/09/12 2002/10/11 09:37    11    11     0 KIL001",
57              "SAVE04                                                              ", // no statistics
58              "TBSHELF1  01.03 2002/09/12 2002/10/11 09:37    11    11     0 KIL001", "TBSHELF2  01.03 2002/09/12 2002/10/11 09:37    11    11     0 KIL001",
59              "TBSHELF3  01.03 2002/09/12 2002/10/11 09:37    11    11     0 KIL001", "TBSHELF4  01.03 2002/09/12 2002/10/11 09:37    11    11     0 KIL001", };
60  
61      private static final String[] goodsamplesJES1List = { /* no header for JES1 (JES Interface level 1) */
62              /* Note, if the string begins with SAVE, the parsed entry is stored in the List saveftpfiles */
63              "IBMUSER1  JOB01906  OUTPUT    3 Spool Files", };
64  
65      private static final String[] goodsamplesJES2List = { /* JES2 (JES Interface level 2) */
66              /* Note, if the string begins with SAVE, the parsed entry is stored in the List saveftpfiles */
67              // "JOBNAME JOBID OWNER STATUS CLASS",
68              "IBMUSER2 JOB01906 IBMUSER  OUTPUT A        RC=0000 3 spool files", "IBMUSER  TSU01830 IBMUSER  OUTPUT TSU      ABEND=522 3 spool files", };
69  
70      private static final String[] goodsamplesUnixList = { "total 1234", "-rwxr-xr-x   2 root     root         4096 Mar  2 15:13 zxbox",
71              "drwxr-xr-x   2 root     root         4096 Aug 24  2001 zxjdbc", };
72  
73      private static final String[] badsamples = { "MigratedP201.$FTXPBI1.$CF2ITB.$AAB0402.I", "PSMLC133902005/04/041VB2799427998PSfile1.I", "file2.O", };
74  
75      /**
76       * @see junit.framework.TestCase#TestCase(String)
77       */
78      public MVSFTPEntryParserTest(final String name) {
79          super(name);
80  
81      }
82  
83      @Override
84      public void doAdditionalGoodTests(final String test, final FTPFile f) {
85          assertNotNull("Could not parse raw listing in " + test, f.getRawListing());
86          assertNotNull("Could not parse name in " + test, f.getName());
87          // some tests don't produce any further details
88      }
89  
90      protected List<String[]> getAllGoodListings() {
91          final List<String[]> l = new ArrayList<>();
92          l.add(goodsamplesDatasetList);
93          l.add(goodsamplesMemberList);
94          l.add(goodsamplesJES1List);
95          l.add(goodsamplesJES2List);
96          l.add(goodsamplesUnixList);
97          return l;
98      }
99  
100     /*
101      * (non-Javadoc)
102      *
103      * @see org.apache.commons.net.ftp.parser.CompositeFTPParseTestFramework#getBadListings()
104      */
105     @Override
106     protected String[] getBadListing() {
107         return badsamples;
108     }
109 
110     /*
111      * (non-Javadoc)
112      *
113      * @see org.apache.commons.net.ftp.parser.CompositeFTPParseTestFramework#getGoodListings()
114      */
115     @Override
116     protected String[] getGoodListing() {
117         return goodsamplesDatasetList;
118     }
119 
120     /**
121      * @see org.apache.commons.net.ftp.parser.AbstractFTPParseTest#getParser()
122      */
123     @Override
124     protected FTPFileEntryParser getParser() {
125         return new MVSFTPEntryParser();
126     }
127 
128     @Override
129     public void testDefaultPrecision() {
130         // TODO Not sure what dates are parsed
131     }
132 
133     /*
134      * note the testGoodListing has to be the first test invoked, because some FTPFile entries are saved for the later tests
135      *
136      * (non-Javadoc)
137      *
138      * @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#testGoodListing()
139      */
140     @Override
141     public void testGoodListing() {
142         final String[] goodsamples = getGoodListing();
143         final MVSFTPEntryParser parser = new MVSFTPEntryParser();
144         parser.setType(MVSFTPEntryParser.FILE_LIST_TYPE);
145         parser.setRegex(MVSFTPEntryParser.FILE_LIST_REGEX);
146         for (final String test : goodsamples) {
147             final FTPFile f = parser.parseFTPEntry(test);
148             assertNotNull("Failed to parse " + test, f);
149             doAdditionalGoodTests(test, f);
150         }
151     }
152 
153     public void testJesLevel1Listing() {
154         final MVSFTPEntryParser parser = new MVSFTPEntryParser();
155         parser.setType(MVSFTPEntryParser.JES_LEVEL_1_LIST_TYPE);
156         parser.setRegex(MVSFTPEntryParser.JES_LEVEL_1_LIST_REGEX);
157         for (final String test : goodsamplesJES1List) {
158             final FTPFile f = parser.parseFTPEntry(test);
159             assertNotNull("Failed to parse " + test, f);
160             doAdditionalGoodTests(test, f);
161         }
162     }
163 
164     public void testJesLevel2Listing() {
165         final MVSFTPEntryParser parser = new MVSFTPEntryParser();
166         parser.setType(MVSFTPEntryParser.JES_LEVEL_2_LIST_TYPE);
167         parser.setRegex(MVSFTPEntryParser.JES_LEVEL_2_LIST_REGEX);
168         for (final String test : goodsamplesJES2List) {
169             final FTPFile f = parser.parseFTPEntry(test);
170             assertNotNull("Failed to parse " + test, f);
171             doAdditionalGoodTests(test, f);
172         }
173     }
174 
175     public void testMemberListing() {
176         final MVSFTPEntryParser parser = new MVSFTPEntryParser();
177         parser.setType(MVSFTPEntryParser.MEMBER_LIST_TYPE);
178         parser.setRegex(MVSFTPEntryParser.MEMBER_LIST_REGEX);
179         for (final String test : goodsamplesMemberList) {
180             final FTPFile f = parser.parseFTPEntry(test);
181             assertNotNull("Failed to parse " + test, f);
182             doAdditionalGoodTests(test, f);
183         }
184     }
185 
186     @Override
187     public void testParseFieldsOnDirectory() throws Exception {
188         final MVSFTPEntryParser parser = new MVSFTPEntryParser();
189         parser.setType(MVSFTPEntryParser.FILE_LIST_TYPE);
190         parser.setRegex(MVSFTPEntryParser.FILE_LIST_REGEX);
191 
192         FTPFile file = parser.parseFTPEntry("SAVE01 3390   2004/06/23  1    1  FB     128  6144  PO    INCOMING.RPTBM024.D061704");
193         assertNotNull("Could not parse entry.", file);
194         assertTrue("Should have been a directory.", file.isDirectory());
195         assertEquals("INCOMING.RPTBM024.D061704", file.getName());
196 
197         file = parser.parseFTPEntry("SAVE02 3390   2004/06/23  1    1  FB     128  6144  PO-E  INCOMING.RPTBM025.D061704");
198         assertNotNull("Could not parse entry.", file);
199         assertTrue("Should have been a directory.", file.isDirectory());
200         assertEquals("INCOMING.RPTBM025.D061704", file.getName());
201 
202     }
203 
204     /*
205      * (non-Javadoc)
206      *
207      * @see org.apache.commons.net.ftp.parser.FTPParseTestFramework#testParseFieldsOnFile()
208      */
209     @Override
210     public void testParseFieldsOnFile() throws Exception {
211         FTPFile file;
212 
213         final MVSFTPEntryParser parser = new MVSFTPEntryParser();
214 
215         parser.setRegex(MVSFTPEntryParser.FILE_LIST_REGEX);
216         parser.setType(MVSFTPEntryParser.FILE_LIST_TYPE);
217 
218         file = parser.parseFTPEntry("SAVE00 3390   2004/06/23  1    1  FB     128  6144  PS    INCOMING.RPTBM023.D061704");
219         assertNotNull("Could not parse entry.", file);
220         assertTrue("Should have been a file.", file.isFile());
221         assertEquals("INCOMING.RPTBM023.D061704", file.getName());
222         assertNull("Timestamp should not have been set.", file.getTimestamp());
223 
224         parser.setType(MVSFTPEntryParser.MEMBER_LIST_TYPE);
225         parser.setRegex(MVSFTPEntryParser.MEMBER_LIST_REGEX);
226 
227         file = parser.parseFTPEntry("SAVE03    01.03 2002/09/12 2002/10/11 09:37    11    11     0 KIL001");
228         assertNotNull("Could not parse entry.", file);
229         assertTrue("Should have been a file.", file.isFile());
230         assertEquals("SAVE03", file.getName());
231         assertNotNull("Timestamp should have been set.", file.getTimestamp());
232 
233         file = parser.parseFTPEntry("SAVE04                                                              ");
234         assertNotNull("Could not parse entry.", file);
235         assertTrue("Should have been a file.", file.isFile());
236         assertEquals("SAVE04", file.getName());
237         assertNull("Timestamp should not have been set.", file.getTimestamp());
238 
239     }
240 
241     @Override
242     public void testRecentPrecision() {
243         // TODO Auto-generated method stub
244     }
245 
246     public void testUnixListings() {
247         final MVSFTPEntryParser parser = new MVSFTPEntryParser();
248         final List<String> list = new ArrayList<>();
249         Collections.addAll(list, goodsamplesUnixList);
250         parser.preParse(list);
251         for (final String test : list) {
252             final FTPFile f = parser.parseFTPEntry(test);
253             assertNotNull("Failed to parse " + test, f);
254             assertNotNull("Failed to parse name " + test, f.getName());
255             assertNotNull("Failed to parse group " + test, f.getGroup());
256             assertNotNull("Failed to parse user " + test, f.getUser());
257         }
258     }
259 }