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