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.FTPFile;
22  import org.apache.commons.net.ftp.FTPFileEntryParser;
23  
24  public class MacOsPeterFTPEntryParserTest extends AbstractFTPParseTest {
25  
26      private static final String[] badsamples = { "drwxr-xr-x    123       folder        0 Jan  4 14:49 Steak", };
27  
28      private static final String[] goodsamples = { "-rw-r--r--    54149       27826    81975 Jul 22  2010 09.jpg",
29              "drwxr-xr-x               folder        0 Jan  4 14:51 Alias_to_Steak",
30              "-rw-r--r--    78440       49231   127671 Jul 22  2010 Filename with whitespace.jpg",
31              "-rw-r--r--    78440       49231   127671 Jul 22 14:51 Filename with whitespace.jpg",
32              "-rw-r--r--        0      108767   108767 Jul 22  2010 presentation03.jpg",
33              "-rw-r--r--    58679       60393   119072 Jul 22  2010 presentation04.jpg",
34              "-rw-r--r--    82543       51433   133976 Jul 22  2010 presentation06.jpg",
35              "-rw-r--r--    83616     1430976  1514592 Jul 22  2010 presentation10.jpg",
36              "-rw-r--r--        0       66990    66990 Jul 22  2010 presentation11.jpg", "drwxr-xr-x               folder        0 Jan  4 14:49 Steak",
37              "-rwx------        0       12713    12713 Jul  8  2009 Twitter_Avatar.png", };
38  
39      public MacOsPeterFTPEntryParserTest(final String name) {
40          super(name);
41      }
42  
43      /**
44       * Method checkPermissions. Verify that the persmissions were properly set.
45       *
46       * @param f
47       */
48      private void checkPermissions(final FTPFile f) {
49          assertTrue("Should have user read permission.", f.hasPermission(FTPFile.USER_ACCESS, FTPFile.READ_PERMISSION));
50          assertTrue("Should have user write permission.", f.hasPermission(FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION));
51          assertTrue("Should have user execute permission.", f.hasPermission(FTPFile.USER_ACCESS, FTPFile.EXECUTE_PERMISSION));
52          assertTrue("Should have group read permission.", f.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.READ_PERMISSION));
53          assertFalse("Should NOT have group write permission.", f.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.WRITE_PERMISSION));
54          assertTrue("Should have group execute permission.", f.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.EXECUTE_PERMISSION));
55          assertTrue("Should have world read permission.", f.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.READ_PERMISSION));
56          assertFalse("Should NOT have world write permission.", f.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.WRITE_PERMISSION));
57          assertTrue("Should have world execute permission.", f.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.EXECUTE_PERMISSION));
58      }
59  
60      @Override
61      protected String[] getBadListing() {
62          return badsamples;
63      }
64  
65      @Override
66      protected String[] getGoodListing() {
67          return goodsamples;
68      }
69  
70      @Override
71      protected FTPFileEntryParser getParser() {
72          return new MacOsPeterFTPEntryParser();
73      }
74  
75      @Override
76      public void testDefaultPrecision() {
77          testPrecision("-rw-r--r--    78440       49231   127671 Jul 22  2010 Filename with whitespace.jpg", CalendarUnit.DAY_OF_MONTH);
78      }
79  
80      @Override
81      public void testParseFieldsOnDirectory() throws Exception {
82          final FTPFile f = getParser().parseFTPEntry("drwxr-xr-x               folder        0 Mar  2 15:13 Alias_to_Steak");
83          assertNotNull("Could not parse entry.", f);
84          assertTrue("Should have been a directory.", f.isDirectory());
85          checkPermissions(f);
86          assertEquals(0, f.getHardLinkCount());
87          assertNull(f.getUser());
88          assertNull(f.getGroup());
89          assertEquals(0, f.getSize());
90          assertEquals("Alias_to_Steak", f.getName());
91  
92          final Calendar cal = Calendar.getInstance();
93          cal.set(Calendar.MONTH, Calendar.MARCH);
94  
95          cal.set(Calendar.DAY_OF_MONTH, 1);
96          cal.set(Calendar.HOUR_OF_DAY, 0);
97          cal.set(Calendar.MINUTE, 0);
98          cal.set(Calendar.SECOND, 0);
99          if (f.getTimestamp().getTime().before(cal.getTime())) {
100             cal.add(Calendar.YEAR, -1);
101         }
102         cal.set(Calendar.DAY_OF_MONTH, 2);
103         cal.set(Calendar.HOUR_OF_DAY, 15);
104         cal.set(Calendar.MINUTE, 13);
105 
106         assertEquals(df.format(cal.getTime()), df.format(f.getTimestamp().getTime()));
107     }
108 
109     @Override
110     public void testParseFieldsOnFile() throws Exception {
111         final FTPFile f = getParser().parseFTPEntry("-rwxr-xr-x    78440       49231   127671 Jul  2 14:51 Filename with whitespace.jpg");
112         assertNotNull("Could not parse entry.", f);
113         assertTrue("Should have been a file.", f.isFile());
114         checkPermissions(f);
115         assertEquals(0, f.getHardLinkCount());
116         assertNull(f.getUser());
117         assertNull(f.getGroup());
118         assertEquals("Filename with whitespace.jpg", f.getName());
119         assertEquals(127671L, f.getSize());
120 
121         final Calendar cal = Calendar.getInstance();
122         cal.set(Calendar.MONTH, Calendar.JULY);
123 
124         cal.set(Calendar.DAY_OF_MONTH, 1);
125         cal.set(Calendar.HOUR_OF_DAY, 0);
126         cal.set(Calendar.MINUTE, 0);
127         cal.set(Calendar.SECOND, 0);
128         if (f.getTimestamp().getTime().before(cal.getTime())) {
129             cal.add(Calendar.YEAR, -1);
130         }
131         cal.set(Calendar.DAY_OF_MONTH, 2);
132         cal.set(Calendar.HOUR_OF_DAY, 14);
133         cal.set(Calendar.MINUTE, 51);
134         assertEquals(df.format(cal.getTime()), df.format(f.getTimestamp().getTime()));
135     }
136 
137     @Override
138     public void testRecentPrecision() {
139         testPrecision("-rw-r--r--    78440       49231   127671 Jul 22 14:51 Filename with whitespace.jpg", CalendarUnit.MINUTE);
140     }
141 
142 }