@Test
  public void testBasicWrite() throws Exception {
    Configuration conf = UTIL.getConfiguration();
    try (Connection connection = ConnectionFactory.createConnection(conf)) {
      // add some permissions
      addUserPermission(conf,
        new UserPermission("george",
          Permission.newBuilder(TEST_TABLE)
            .withActions(Permission.Action.READ, Permission.Action.WRITE).build()),
        connection.getTable(PermissionStorage.ACL_TABLE_NAME));
      addUserPermission(conf,
        new UserPermission("hubert",
          Permission.newBuilder(TEST_TABLE).withActions(Permission.Action.READ).build()),
        connection.getTable(PermissionStorage.ACL_TABLE_NAME));
      addUserPermission(conf,
        new UserPermission("humphrey",
          Permission.newBuilder(TEST_TABLE).withFamily(TEST_FAMILY).withQualifier(TEST_QUALIFIER)
            .withActions(Permission.Action.READ).build()),
        connection.getTable(PermissionStorage.ACL_TABLE_NAME));
    }
    // retrieve the same
    ListMultimap<String, UserPermission> perms =
      PermissionStorage.getTablePermissions(conf, TEST_TABLE);
    List<UserPermission> userPerms = perms.get("george");
    assertNotNull("Should have permissions for george", userPerms);
    assertEquals("Should have 1 permission for george", 1, userPerms.size());
    assertEquals(Permission.Scope.TABLE, userPerms.get(0).getAccessScope());
    TablePermission permission = (TablePermission) userPerms.get(0).getPermission();
    assertEquals("Permission should be for " + TEST_TABLE, TEST_TABLE, permission.getTableName());
    assertNull("Column family should be empty", permission.getFamily());

    // check actions
    assertNotNull(permission.getActions());
    assertEquals(2, permission.getActions().length);
    List<Permission.Action> actions = Arrays.asList(permission.getActions());
    assertTrue(actions.contains(TablePermission.Action.READ));
    assertTrue(actions.contains(TablePermission.Action.WRITE));

    userPerms = perms.get("hubert");
    assertNotNull("Should have permissions for hubert", userPerms);
    assertEquals("Should have 1 permission for hubert", 1, userPerms.size());
    assertEquals(Permission.Scope.TABLE, userPerms.get(0).getAccessScope());
    permission = (TablePermission) userPerms.get(0).getPermission();
    assertEquals("Permission should be for " + TEST_TABLE, TEST_TABLE, permission.getTableName());
    assertNull("Column family should be empty", permission.getFamily());

    // check actions
    assertNotNull(permission.getActions());
    assertEquals(1, permission.getActions().length);
    actions = Arrays.asList(permission.getActions());
    assertTrue(actions.contains(TablePermission.Action.READ));
    assertFalse(actions.contains(TablePermission.Action.WRITE));

    userPerms = perms.get("humphrey");
    assertNotNull("Should have permissions for humphrey", userPerms);
    assertEquals("Should have 1 permission for humphrey", 1, userPerms.size());
    assertEquals(Permission.Scope.TABLE, userPerms.get(0).getAccessScope());
    permission = (TablePermission) userPerms.get(0).getPermission();
    assertEquals("Permission should be for " + TEST_TABLE, TEST_TABLE, permission.getTableName());
    assertTrue("Permission should be for family " + Bytes.toString(TEST_FAMILY),
      Bytes.equals(TEST_FAMILY, permission.getFamily()));
    assertTrue("Permission should be for qualifier " + Bytes.toString(TEST_QUALIFIER),
      Bytes.equals(TEST_QUALIFIER, permission.getQualifier()));

    // check actions
    assertNotNull(permission.getActions());
    assertEquals(1, permission.getActions().length);
    actions = Arrays.asList(permission.getActions());
    assertTrue(actions.contains(TablePermission.Action.READ));
    assertFalse(actions.contains(TablePermission.Action.WRITE));

    // table 2 permissions
    try (Connection connection = ConnectionFactory.createConnection(conf);
      Table table = connection.getTable(PermissionStorage.ACL_TABLE_NAME)) {
      PermissionStorage.addUserPermission(conf,
        new UserPermission("hubert", Permission.newBuilder(TEST_TABLE2)
          .withActions(Permission.Action.READ, Permission.Action.WRITE).build()),
        table);
    }
    // check full load
    Map<byte[], ListMultimap<String, UserPermission>> allPerms = PermissionStorage.loadAll(conf);
    assertEquals("Full permission map should have entries for both test tables", 2,
      allPerms.size());

    userPerms = allPerms.get(TEST_TABLE.getName()).get("hubert");
    assertNotNull(userPerms);
    assertEquals(1, userPerms.size());
    assertEquals(Permission.Scope.TABLE, userPerms.get(0).getAccessScope());
    permission = (TablePermission) userPerms.get(0).getPermission();
    assertEquals(TEST_TABLE, permission.getTableName());
    assertEquals(1, permission.getActions().length);
    assertEquals(Permission.Action.READ, permission.getActions()[0]);

    userPerms = allPerms.get(TEST_TABLE2.getName()).get("hubert");
    assertNotNull(userPerms);
    assertEquals(1, userPerms.size());
    assertEquals(Permission.Scope.TABLE, userPerms.get(0).getAccessScope());
    permission = (TablePermission) userPerms.get(0).getPermission();
    assertEquals(TEST_TABLE2, permission.getTableName());
    assertEquals(2, permission.getActions().length);
    actions = Arrays.asList(permission.getActions());
    assertTrue(actions.contains(Permission.Action.READ));
    assertTrue(actions.contains(Permission.Action.WRITE));
  }
The PermissionStorage.addUserPermission may throw exception before closing the table.
hbase-server/src/test/java/org/apache/hadoop/hbase/security/access/TestTablePermissions.java