hbase-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From apurt...@apache.org
Subject [5/6] hbase git commit: HBASE-13332 Fix the usage of doAs/runAs in Visibility Controller tests (Srikanth Srungarapu)
Date Fri, 27 Mar 2015 04:54:49 GMT
http://git-wip-us.apache.org/repos/asf/hbase/blob/b6b1e3b8/hbase-server/src/test/java/org/apache/hadoop/hbase/security/visibility/TestVisibilityLablesWithGroups.java
----------------------------------------------------------------------
diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/security/visibility/TestVisibilityLablesWithGroups.java
b/hbase-server/src/test/java/org/apache/hadoop/hbase/security/visibility/TestVisibilityLablesWithGroups.java
index 9e122c9..f0881fd 100644
--- a/hbase-server/src/test/java/org/apache/hadoop/hbase/security/visibility/TestVisibilityLablesWithGroups.java
+++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/security/visibility/TestVisibilityLablesWithGroups.java
@@ -112,12 +112,13 @@ public class TestVisibilityLablesWithGroups {
   @Test
   public void testGroupAuths() throws Exception {
     final TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
-
-    // create the table and put data.
+    // create the table
+    TEST_UTIL.createTable(tableName, CF);
+    // put the data.
     SUPERUSER.runAs(new PrivilegedExceptionAction<Void>() {
       public Void run() throws Exception {
-        Table table = TEST_UTIL.createTable(tableName, CF);
-        try {
+        try (Connection connection = ConnectionFactory.createConnection(conf);
+             Table table = connection.getTable(tableName)) {
           Put put = new Put(ROW_1);
           put.add(CF, Q1, HConstants.LATEST_TIMESTAMP, value1);
           put.setCellVisibility(new CellVisibility(SECRET));
@@ -129,8 +130,6 @@ public class TestVisibilityLablesWithGroups {
           put = new Put(ROW_1);
           put.add(CF, Q3, HConstants.LATEST_TIMESTAMP, value3);
           table.put(put);
-        } finally {
-          table.close();
         }
         return null;
       }
@@ -139,9 +138,8 @@ public class TestVisibilityLablesWithGroups {
     // 'admin' user is part of 'supergroup', thus can see all the cells.
     SUPERUSER.runAs(new PrivilegedExceptionAction<Void>() {
       public Void run() throws Exception {
-        Connection connection = ConnectionFactory.createConnection(conf);
-        Table table = connection.getTable(tableName);
-        try {
+        try (Connection connection = ConnectionFactory.createConnection(conf);
+             Table table = connection.getTable(tableName)) {
           Scan s = new Scan();
           ResultScanner scanner = table.getScanner(s);
           Result[] next = scanner.next(1);
@@ -167,10 +165,6 @@ public class TestVisibilityLablesWithGroups {
               current.getRowLength(), ROW_1, 0, ROW_1.length));
           assertTrue(Bytes.equals(current.getQualifier(), Q3));
           assertTrue(Bytes.equals(current.getValue(), value3));
-
-        } finally {
-          table.close();
-          connection.close();
         }
         return null;
       }
@@ -198,9 +192,8 @@ public class TestVisibilityLablesWithGroups {
     // Test that test user can see what 'testgroup' has been authorized to.
     TESTUSER.runAs(new PrivilegedExceptionAction<Void>() {
       public Void run() throws Exception {
-        Connection connection = ConnectionFactory.createConnection(conf);
-        Table table = connection.getTable(tableName);
-        try {
+        try (Connection connection = ConnectionFactory.createConnection(conf);
+             Table table = connection.getTable(tableName)) {
           // Test scan with no auth attribute
           Scan s = new Scan();
           ResultScanner scanner = table.getScanner(s);
@@ -265,9 +258,6 @@ public class TestVisibilityLablesWithGroups {
           assertTrue(Bytes.equals(current2.getValue(), value3));
 
           assertFalse(cellScanner2.advance());
-        } finally {
-          table.close();
-          connection.close();
         }
         return null;
       }
@@ -307,9 +297,8 @@ public class TestVisibilityLablesWithGroups {
     // Test that test user cannot see the cells with the labels anymore.
     TESTUSER.runAs(new PrivilegedExceptionAction<Void>() {
       public Void run() throws Exception {
-        Connection connection = ConnectionFactory.createConnection(conf);
-        Table table = connection.getTable(tableName);
-        try {
+        try (Connection connection = ConnectionFactory.createConnection(conf);
+             Table table = connection.getTable(tableName)) {
           Scan s1 = new Scan();
           // test user is not entitled to 'CONFIDENTIAL' anymore since we dropped
           // testgroup's label.  test user has no auth labels now.
@@ -329,9 +318,6 @@ public class TestVisibilityLablesWithGroups {
           assertTrue(Bytes.equals(current1.getValue(), value3));
 
           assertFalse(cellScanner1.advance());
-        } finally {
-          table.close();
-          connection.close();
         }
         return null;
       }

http://git-wip-us.apache.org/repos/asf/hbase/blob/b6b1e3b8/hbase-server/src/test/java/org/apache/hadoop/hbase/security/visibility/TestVisibilityWithCheckAuths.java
----------------------------------------------------------------------
diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/security/visibility/TestVisibilityWithCheckAuths.java
b/hbase-server/src/test/java/org/apache/hadoop/hbase/security/visibility/TestVisibilityWithCheckAuths.java
index 457d2eb..67d9c63 100644
--- a/hbase-server/src/test/java/org/apache/hadoop/hbase/security/visibility/TestVisibilityWithCheckAuths.java
+++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/security/visibility/TestVisibilityWithCheckAuths.java
@@ -33,7 +33,6 @@ import org.apache.hadoop.hbase.client.Admin;
 import org.apache.hadoop.hbase.client.Append;
 import org.apache.hadoop.hbase.client.Connection;
 import org.apache.hadoop.hbase.client.ConnectionFactory;
-import org.apache.hadoop.hbase.client.HTable;
 import org.apache.hadoop.hbase.client.Put;
 import org.apache.hadoop.hbase.client.Table;
 import org.apache.hadoop.hbase.protobuf.generated.VisibilityLabelsProtos.VisibilityLabelsResponse;
@@ -129,15 +128,13 @@ public class TestVisibilityWithCheckAuths {
     HTableDescriptor desc = new HTableDescriptor(tableName);
     desc.addFamily(colDesc);
     hBaseAdmin.createTable(desc);
-    Table table = null;
     try {
       TEST_UTIL.getHBaseAdmin().flush(tableName);
       PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>()
{
         @Override
         public Void run() throws Exception {
-          Table table = null;
-          try {
-            table = TEST_UTIL.getConnection().getTable(tableName);
+          try (Connection connection = ConnectionFactory.createConnection(conf);
+               Table table = connection.getTable(tableName)) {
             Put p = new Put(row1);
             p.setCellVisibility(new CellVisibility(PUBLIC + "&" + TOPSECRET));
             p.add(fam, qual, 125l, value);
@@ -145,8 +142,6 @@ public class TestVisibilityWithCheckAuths {
             Assert.fail("Testcase should fail with AccesDeniedException");
           } catch (Throwable t) {
             assertTrue(t.getMessage().contains("AccessDeniedException"));
-          } finally {
-            table.close();
           }
           return null;
         }
@@ -173,25 +168,18 @@ public class TestVisibilityWithCheckAuths {
     };
     SUPERUSER.runAs(action);
     final TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
-    Table table = null;
-    try {
-      table = TEST_UTIL.createTable(tableName, fam);
+    try (Table table = TEST_UTIL.createTable(tableName, fam)) {
       final byte[] row1 = Bytes.toBytes("row1");
       final byte[] val = Bytes.toBytes("a");
       PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>()
{
         @Override
         public Void run() throws Exception {
-          Connection connection = ConnectionFactory.createConnection(conf);
-          Table table = null;
-          try {
-            table = connection.getTable(tableName);
+          try (Connection connection = ConnectionFactory.createConnection(conf);
+               Table table = connection.getTable(tableName)) {
             Put put = new Put(row1);
             put.add(fam, qual, HConstants.LATEST_TIMESTAMP, val);
             put.setCellVisibility(new CellVisibility(TOPSECRET));
             table.put(put);
-          } finally {
-            table.close();
-            connection.close();
           }
           return null;
         }
@@ -200,16 +188,11 @@ public class TestVisibilityWithCheckAuths {
       actiona = new PrivilegedExceptionAction<Void>() {
         @Override
         public Void run() throws Exception {
-          Connection connection = ConnectionFactory.createConnection(conf);
-          Table table = null;
-          try {
-            table = TEST_UTIL.getConnection().getTable(tableName);
+          try (Connection connection = ConnectionFactory.createConnection(conf);
+               Table table = connection.getTable(tableName)) {
             Append append = new Append(row1);
             append.add(fam, qual, Bytes.toBytes("b"));
             table.append(append);
-          } finally {
-            table.close();
-            connection.close();
           }
           return null;
         }
@@ -218,11 +201,8 @@ public class TestVisibilityWithCheckAuths {
       actiona = new PrivilegedExceptionAction<Void>() {
         @Override
         public Void run() throws Exception {
-          Table table = null;
-          Connection connection = null;
-          try {
-            connection = ConnectionFactory.createConnection(conf);
-            table = connection.getTable(tableName);
+          try (Connection connection = ConnectionFactory.createConnection(conf);
+               Table table = connection.getTable(tableName)) {
             Append append = new Append(row1);
             append.add(fam, qual, Bytes.toBytes("c"));
             append.setCellVisibility(new CellVisibility(PUBLIC));
@@ -230,22 +210,11 @@ public class TestVisibilityWithCheckAuths {
             Assert.fail("Testcase should fail with AccesDeniedException");
           } catch (Throwable t) {
             assertTrue(t.getMessage().contains("AccessDeniedException"));
-          } finally {
-            if (table != null) {
-              table.close();
-            }
-            if (connection != null) {
-              connection.close();
-            }
           }
           return null;
         }
       };
       USER.runAs(actiona);
-    } finally {
-      if (table != null) {
-        table.close();
-      }
     }
   }
 }


Mime
View raw message