29
29
import org .apache .hadoop .fs .FsConstants ;
30
30
import org .apache .hadoop .fs .Path ;
31
31
import org .apache .hadoop .fs .contract .ContractTestUtils ;
32
+ import org .apache .hadoop .fs .permission .FsPermission ;
32
33
import org .apache .hadoop .io .DataInputBuffer ;
33
34
import org .apache .hadoop .io .DataOutputBuffer ;
34
35
import org .apache .hadoop .test .GenericTestUtils ;
36
+ import org .junit .After ;
35
37
import org .junit .AfterClass ;
38
+ import org .junit .Before ;
36
39
import org .junit .Test ;
37
40
import org .mockito .Mockito ;
38
41
@@ -48,6 +51,17 @@ public class TestViewfsFileStatus {
48
51
private static final File TEST_DIR = GenericTestUtils .getTestDir (
49
52
TestViewfsFileStatus .class .getSimpleName ());
50
53
54
+ @ Before
55
+ public void setUp () {
56
+ FileUtil .fullyDelete (TEST_DIR );
57
+ assertTrue (TEST_DIR .mkdirs ());
58
+ }
59
+
60
+ @ After
61
+ public void tearDown () throws IOException {
62
+ FileUtil .fullyDelete (TEST_DIR );
63
+ }
64
+
51
65
@ Test
52
66
public void testFileStatusSerialziation ()
53
67
throws IOException , URISyntaxException {
@@ -56,38 +70,90 @@ public void testFileStatusSerialziation()
56
70
File infile = new File (TEST_DIR , testfilename );
57
71
final byte [] content = "dingos" .getBytes ();
58
72
59
- FileOutputStream fos = null ;
60
- try {
61
- fos = new FileOutputStream (infile );
73
+ try (FileOutputStream fos = new FileOutputStream (infile )) {
62
74
fos .write (content );
63
- } finally {
64
- if (fos != null ) {
65
- fos .close ();
66
- }
67
75
}
68
76
assertEquals ((long )content .length , infile .length ());
69
77
70
78
Configuration conf = new Configuration ();
71
79
ConfigUtil .addLink (conf , "/foo/bar/baz" , TEST_DIR .toURI ());
72
- FileSystem vfs = FileSystem .get (FsConstants .VIEWFS_URI , conf );
73
- assertEquals (ViewFileSystem .class , vfs .getClass ());
74
- Path path = new Path ("/foo/bar/baz" , testfilename );
75
- FileStatus stat = vfs .getFileStatus (path );
76
- assertEquals (content .length , stat .getLen ());
77
- ContractTestUtils .assertNotErasureCoded (vfs , path );
78
- assertTrue (path + " should have erasure coding unset in " +
79
- "FileStatus#toString(): " + stat ,
80
- stat .toString ().contains ("isErasureCoded=false" ));
81
-
82
- // check serialization/deserialization
83
- DataOutputBuffer dob = new DataOutputBuffer ();
84
- stat .write (dob );
85
- DataInputBuffer dib = new DataInputBuffer ();
86
- dib .reset (dob .getData (), 0 , dob .getLength ());
87
- FileStatus deSer = new FileStatus ();
88
- deSer .readFields (dib );
89
- assertEquals (content .length , deSer .getLen ());
90
- assertFalse (deSer .isErasureCoded ());
80
+ try (FileSystem vfs = FileSystem .get (FsConstants .VIEWFS_URI , conf )) {
81
+ assertEquals (ViewFileSystem .class , vfs .getClass ());
82
+ Path path = new Path ("/foo/bar/baz" , testfilename );
83
+ FileStatus stat = vfs .getFileStatus (path );
84
+ assertEquals (content .length , stat .getLen ());
85
+ ContractTestUtils .assertNotErasureCoded (vfs , path );
86
+ assertTrue (path + " should have erasure coding unset in " +
87
+ "FileStatus#toString(): " + stat ,
88
+ stat .toString ().contains ("isErasureCoded=false" ));
89
+
90
+ // check serialization/deserialization
91
+ DataOutputBuffer dob = new DataOutputBuffer ();
92
+ stat .write (dob );
93
+ DataInputBuffer dib = new DataInputBuffer ();
94
+ dib .reset (dob .getData (), 0 , dob .getLength ());
95
+ FileStatus deSer = new FileStatus ();
96
+ deSer .readFields (dib );
97
+ assertEquals (content .length , deSer .getLen ());
98
+ assertFalse (deSer .isErasureCoded ());
99
+ }
100
+ }
101
+
102
+ /**
103
+ * Tests the ACL returned from getFileStatus for directories and files.
104
+ * @throws IOException
105
+ */
106
+ @ Test
107
+ public void testListStatusACL () throws IOException {
108
+ String testfilename = "testFileACL" ;
109
+ String childDirectoryName = "testDirectoryACL" ;
110
+ TEST_DIR .mkdirs ();
111
+ File infile = new File (TEST_DIR , testfilename );
112
+ final byte [] content = "dingos" .getBytes ();
113
+
114
+ try (FileOutputStream fos = new FileOutputStream (infile )) {
115
+ fos .write (content );
116
+ }
117
+ assertEquals (content .length , infile .length ());
118
+ File childDir = new File (TEST_DIR , childDirectoryName );
119
+ childDir .mkdirs ();
120
+
121
+ Configuration conf = new Configuration ();
122
+ ConfigUtil .addLink (conf , "/file" , infile .toURI ());
123
+ ConfigUtil .addLink (conf , "/dir" , childDir .toURI ());
124
+
125
+ try (FileSystem vfs = FileSystem .get (FsConstants .VIEWFS_URI , conf )) {
126
+ assertEquals (ViewFileSystem .class , vfs .getClass ());
127
+ FileStatus [] statuses = vfs .listStatus (new Path ("/" ));
128
+
129
+ FileSystem localFs = FileSystem .getLocal (conf );
130
+ FileStatus fileStat = localFs .getFileStatus (new Path (infile .getPath ()));
131
+ FileStatus dirStat = localFs .getFileStatus (new Path (childDir .getPath ()));
132
+
133
+ for (FileStatus status : statuses ) {
134
+ if (status .getPath ().getName ().equals ("file" )) {
135
+ assertEquals (fileStat .getPermission (), status .getPermission ());
136
+ } else {
137
+ assertEquals (dirStat .getPermission (), status .getPermission ());
138
+ }
139
+ }
140
+
141
+ localFs .setPermission (new Path (infile .getPath ()),
142
+ FsPermission .valueOf ("-rwxr--r--" ));
143
+ localFs .setPermission (new Path (childDir .getPath ()),
144
+ FsPermission .valueOf ("-r--rwxr--" ));
145
+
146
+ statuses = vfs .listStatus (new Path ("/" ));
147
+ for (FileStatus status : statuses ) {
148
+ if (status .getPath ().getName ().equals ("file" )) {
149
+ assertEquals (FsPermission .valueOf ("-rwxr--r--" ),
150
+ status .getPermission ());
151
+ } else {
152
+ assertEquals (FsPermission .valueOf ("-r--rwxr--" ),
153
+ status .getPermission ());
154
+ }
155
+ }
156
+ }
91
157
}
92
158
93
159
// Tests that ViewFileSystem.getFileChecksum calls res.targetFileSystem
0 commit comments