Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id F0E09200B7C for ; Thu, 8 Sep 2016 16:57:41 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id EFB0F160AD4; Thu, 8 Sep 2016 14:57:41 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id F1F96160ABD for ; Thu, 8 Sep 2016 16:57:40 +0200 (CEST) Received: (qmail 96471 invoked by uid 500); 8 Sep 2016 14:57:31 -0000 Mailing-List: contact common-commits-help@hadoop.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Delivered-To: mailing list common-commits@hadoop.apache.org Received: (qmail 91003 invoked by uid 99); 8 Sep 2016 14:57:28 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 08 Sep 2016 14:57:28 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id E5663EF9A3; Thu, 8 Sep 2016 14:57:27 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: aw@apache.org To: common-commits@hadoop.apache.org Date: Thu, 08 Sep 2016 14:58:00 -0000 Message-Id: In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [35/48] hadoop git commit: HADOOP-13388. Clean up TestLocalFileSystemPermission. Contributed by Andras Bokor. archived-at: Thu, 08 Sep 2016 14:57:42 -0000 HADOOP-13388. Clean up TestLocalFileSystemPermission. Contributed by Andras Bokor. Project: http://git-wip-us.apache.org/repos/asf/hadoop/repo Commit: http://git-wip-us.apache.org/repos/asf/hadoop/commit/f414d5e1 Tree: http://git-wip-us.apache.org/repos/asf/hadoop/tree/f414d5e1 Diff: http://git-wip-us.apache.org/repos/asf/hadoop/diff/f414d5e1 Branch: refs/heads/HADOOP-13341 Commit: f414d5e118940cb98015c0b66e11102a9704a505 Parents: 7fdfcd8 Author: Anu Engineer Authored: Wed Sep 7 11:09:41 2016 -0700 Committer: Anu Engineer Committed: Wed Sep 7 11:09:41 2016 -0700 ---------------------------------------------------------------------- .../fs/TestLocalFileSystemPermission.java | 149 ++++++++----------- 1 file changed, 60 insertions(+), 89 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/hadoop/blob/f414d5e1/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestLocalFileSystemPermission.java ---------------------------------------------------------------------- diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestLocalFileSystemPermission.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestLocalFileSystemPermission.java index 817285c..1478111 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestLocalFileSystemPermission.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestLocalFileSystemPermission.java @@ -18,24 +18,23 @@ package org.apache.hadoop.fs; import org.apache.hadoop.conf.Configuration; -import org.apache.hadoop.fs.permission.*; +import org.apache.hadoop.fs.permission.FsPermission; import org.apache.hadoop.test.GenericTestUtils; -import org.apache.log4j.Level; import org.apache.hadoop.util.Shell; - -import java.io.*; -import java.util.*; - +import org.apache.log4j.Level; import org.junit.Test; -import static org.apache.hadoop.test.PlatformAssumptions.assumeNotWindows; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.StringTokenizer; + +import static org.apache.hadoop.test.PlatformAssumptions.assumeNotWindows; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.not; -import static org.junit.Assert.assertThat; +import static org.junit.Assert.*; /** * This class tests the local file system via the FileSystem abstraction. @@ -60,18 +59,22 @@ public class TestLocalFileSystemPermission { return f; } - private Path writeFile(FileSystem fs, String name, FsPermission perm) throws IOException { + private Path writeFile(FileSystem fs, String name, FsPermission perm) + throws IOException { Path f = new Path(TEST_PATH_PREFIX + name); - FSDataOutputStream stm = fs.create(f, perm, true, 2048, (short)1, 32 * 1024 * 1024, null); + FSDataOutputStream stm = fs.create(f, perm, true, 2048, (short)1, + 32 * 1024 * 1024, null); stm.writeBytes("42\n"); stm.close(); return f; } private void cleanup(FileSystem fs, Path name) throws IOException { - assertTrue(fs.exists(name)); - fs.delete(name, true); - assertTrue(!fs.exists(name)); + if (name!=null) { + assertTrue(fs.exists(name)); + fs.delete(name, true); + assertFalse(fs.exists(name)); + } } @Test @@ -82,39 +85,33 @@ public class TestLocalFileSystemPermission { conf.set(CommonConfigurationKeys.FS_PERMISSIONS_UMASK_KEY, "044"); Path dir = new Path(TEST_PATH_PREFIX + "dir"); localfs.mkdirs(dir); + Path dir1 = new Path(TEST_PATH_PREFIX + "dir1"); + Path dir2 = new Path(TEST_PATH_PREFIX + "dir2"); + try { FsPermission initialPermission = getPermission(localfs, dir); assertEquals( - FsPermission.getDirDefault().applyUMask(FsPermission.getUMask(conf)), + FsPermission.getDirDefault() + .applyUMask(FsPermission.getUMask(conf)), initialPermission); - } catch(Exception e) { - LOGGER.error("Cannot run test", e); - return; - } - FsPermission perm = new FsPermission((short)0755); - Path dir1 = new Path(TEST_PATH_PREFIX + "dir1"); - localfs.mkdirs(dir1, perm); - try { - FsPermission initialPermission = getPermission(localfs, dir1); - assertEquals(perm.applyUMask(FsPermission.getUMask(conf)), initialPermission); - } catch(Exception e) { - LOGGER.error("Cannot run test", e); - return; - } + FsPermission perm = new FsPermission((short)0755); - Path dir2 = new Path(TEST_PATH_PREFIX + "dir2"); - localfs.mkdirs(dir2); - try { - FsPermission initialPermission = getPermission(localfs, dir2); + localfs.mkdirs(dir1, perm); + + initialPermission = getPermission(localfs, dir1); + assertEquals(perm.applyUMask(FsPermission.getUMask(conf)), + initialPermission); + + localfs.mkdirs(dir2); + + initialPermission = getPermission(localfs, dir2); Path copyPath = new Path(TEST_PATH_PREFIX + "dir_copy"); localfs.rename(dir2, copyPath); FsPermission copyPermission = getPermission(localfs, copyPath); - assertEquals(copyPermission, initialPermission); + assertEquals(initialPermission, copyPermission); dir2 = copyPath; - } catch (Exception e) { - LOGGER.error("Cannot run test", e); - return; + } finally { cleanup(localfs, dir); cleanup(localfs, dir1); @@ -124,52 +121,42 @@ public class TestLocalFileSystemPermission { } } - /** Test LocalFileSystem.setPermission */ + /** Test LocalFileSystem.setPermission. */ @Test public void testLocalFSsetPermission() throws IOException { assumeNotWindows(); Configuration conf = new Configuration(); conf.set(CommonConfigurationKeys.FS_PERMISSIONS_UMASK_KEY, "044"); LocalFileSystem localfs = FileSystem.getLocal(conf); + Path f = null; + Path f1 = null; + Path f2 = null; String filename = "foo"; - Path f = writeFile(localfs, filename); + String filename1 = "foo1"; + String filename2 = "foo2"; + FsPermission perm = new FsPermission((short)0755); + try { + f = writeFile(localfs, filename); + f1 = writeFile(localfs, filename1, perm); + f2 = writeFile(localfs, filename2); + FsPermission initialPermission = getPermission(localfs, f); assertEquals( FsPermission.getFileDefault().applyUMask(FsPermission.getUMask(conf)), initialPermission); - } catch(Exception e) { - LOGGER.error("Cannot run test", e); - return; - } - String filename1 = "foo1"; - FsPermission perm = new FsPermission((short)0755); - Path f1 = writeFile(localfs, filename1, perm); - try { - FsPermission initialPermission = getPermission(localfs, f1); + initialPermission = getPermission(localfs, f1); assertEquals( perm.applyUMask(FsPermission.getUMask(conf)), initialPermission); - } catch(Exception e) { - LOGGER.error("Cannot run test", e); - return; - } - String filename2 = "foo2"; - Path f2 = writeFile(localfs, filename2); - try { - FsPermission initialPermission = getPermission(localfs, f2); + initialPermission = getPermission(localfs, f2); Path copyPath = new Path(TEST_PATH_PREFIX + "/foo_copy"); localfs.rename(f2, copyPath); FsPermission copyPermission = getPermission(localfs, copyPath); assertEquals(copyPermission, initialPermission); f2 = copyPath; - } catch (Exception e) { - LOGGER.error("Cannot run test", e); - return; - } - try { // create files and manipulate them. FsPermission all = new FsPermission((short)0777); FsPermission none = new FsPermission((short)0); @@ -179,8 +166,7 @@ public class TestLocalFileSystemPermission { localfs.setPermission(f, all); assertEquals(all, getPermission(localfs, f)); - } - finally { + } finally { cleanup(localfs, f); cleanup(localfs, f1); if (localfs.exists(f2)) { @@ -196,33 +182,19 @@ public class TestLocalFileSystemPermission { /** Test LocalFileSystem.setOwner. */ @Test public void testLocalFSsetOwner() throws IOException { - if (Path.WINDOWS) { - LOGGER.info("Cannot run test for Windows"); - return; - } + assumeNotWindows(); Configuration conf = new Configuration(); conf.set(CommonConfigurationKeys.FS_PERMISSIONS_UMASK_KEY, "044"); LocalFileSystem localfs = FileSystem.getLocal(conf); String filename = "bar"; Path f = writeFile(localfs, filename); - List groups = null; + List groups; try { groups = getGroups(); LOGGER.info("{}: {}", filename, getPermission(localfs, f)); - } - catch(IOException e) { - LOGGER.error("Cannot run test", e); - return; - } - if (groups == null || groups.size() < 1) { - LOGGER.error("Cannot run test: need at least one group. groups={}", - groups); - return; - } - // create files and manipulate them. - try { + // create files and manipulate them. String g0 = groups.get(0); localfs.setOwner(f, null, g0); assertEquals(g0, getGroup(localfs, f)); @@ -235,8 +207,9 @@ public class TestLocalFileSystemPermission { LOGGER.info("Not testing changing the group since user " + "belongs to only one group."); } - } - finally {cleanup(localfs, f);} + } finally { + cleanup(localfs, f); + } } /** @@ -250,10 +223,7 @@ public class TestLocalFileSystemPermission { */ @Test public void testSetUmaskInRealTime() throws Exception { - if (Path.WINDOWS) { - LOGGER.info("Cannot run test for Windows"); - return; - } + assumeNotWindows(); LocalFileSystem localfs = FileSystem.getLocal(new Configuration()); Configuration conf = localfs.getConf(); @@ -289,9 +259,10 @@ public class TestLocalFileSystemPermission { } static List getGroups() throws IOException { - List a = new ArrayList(); + List a = new ArrayList<>(); String s = Shell.execCommand(Shell.getGroupsCommand()); - for(StringTokenizer t = new StringTokenizer(s); t.hasMoreTokens(); ) { + StringTokenizer t = new StringTokenizer(s); + while (t.hasMoreTokens()) { a.add(t.nextToken()); } return a; --------------------------------------------------------------------- To unsubscribe, e-mail: common-commits-unsubscribe@hadoop.apache.org For additional commands, e-mail: common-commits-help@hadoop.apache.org