2929import org .apache .hadoop .fs .FsConstants ;
3030import org .apache .hadoop .fs .Path ;
3131import org .apache .hadoop .fs .contract .ContractTestUtils ;
32+ import org .apache .hadoop .fs .permission .FsPermission ;
3233import org .apache .hadoop .io .DataInputBuffer ;
3334import org .apache .hadoop .io .DataOutputBuffer ;
3435import org .apache .hadoop .test .GenericTestUtils ;
36+ import org .junit .After ;
3537import org .junit .AfterClass ;
38+ import org .junit .Before ;
3639import org .junit .Test ;
3740import org .mockito .Mockito ;
3841
@@ -48,6 +51,17 @@ public class TestViewfsFileStatus {
4851 private static final File TEST_DIR = GenericTestUtils .getTestDir (
4952 TestViewfsFileStatus .class .getSimpleName ());
5053
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+
5165 @ Test
5266 public void testFileStatusSerialziation ()
5367 throws IOException , URISyntaxException {
@@ -56,38 +70,90 @@ public void testFileStatusSerialziation()
5670 File infile = new File (TEST_DIR , testfilename );
5771 final byte [] content = "dingos" .getBytes ();
5872
59- FileOutputStream fos = null ;
60- try {
61- fos = new FileOutputStream (infile );
73+ try (FileOutputStream fos = new FileOutputStream (infile )) {
6274 fos .write (content );
63- } finally {
64- if (fos != null ) {
65- fos .close ();
66- }
6775 }
6876 assertEquals ((long )content .length , infile .length ());
6977
7078 Configuration conf = new Configuration ();
7179 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+ }
91157 }
92158
93159 // Tests that ViewFileSystem.getFileChecksum calls res.targetFileSystem
0 commit comments