@Test
  public void testImpersonation() throws Exception {
    assertTrue("Test setup failed. MiniHS2 is not initialized",
        miniHS2 != null && miniHS2.isStarted());

    Class.forName(MiniHS2.getJdbcDriverName());

    // Create two tables one as user "foo" and other as user "bar"
    Connection hs2Conn = DriverManager.getConnection(miniHS2.getJdbcURL()+";retries=3", "foo", null);
    Statement stmt = hs2Conn.createStatement();

    String tableName = "foo_table";
    stmt.execute("drop table if exists " + tableName);
    stmt.execute("create table " + tableName + " (value string)");

    stmt.close();
    hs2Conn.close();

    hs2Conn = DriverManager.getConnection(miniHS2.getJdbcURL()+";retries=3", "bar", null);
    stmt = hs2Conn.createStatement();

    tableName = "bar_table";
    stmt.execute("drop table if exists " + tableName);
    stmt.execute("create table " + tableName + " (value string)");

    stmt.close();
    hs2Conn.close();

    MiniDFSShim dfs = miniHS2.getDfs();
    FileSystem fs = dfs.getFileSystem();

    FileStatus[] files = fs.listStatus(miniHS2.getWareHouseDir());
    boolean fooTableValidated = false;
    boolean barTableValidated = false;
    for(FileStatus file : files) {
      final String name = file.getPath().getName();
      final String owner = file.getOwner();
      if (name.equals("foo_table")) {
        fooTableValidated = owner.equals("foo");
        assertTrue(String.format("User 'foo' table has wrong ownership '%s'", owner),
            fooTableValidated);
      } else if (name.equals("bar_table")) {
        barTableValidated = owner.equals("bar");
        assertTrue(String.format("User 'bar' table has wrong ownership '%s'", owner),
            barTableValidated);
      } else {
        fail(String.format("Unexpected table directory '%s' in warehouse", name));
      }

      System.out.println(String.format("File: %s, Owner: %s", name, owner));
    }

    assertTrue("User 'foo' table not found in warehouse", fooTableValidated);
    assertTrue("User 'bar' table not found in warehouse", barTableValidated);
  }
Test HiveServer2 sends correct user name to remote MetaStore server for user impersonation.
itests/hive-unit/src/test/java/org/apache/hive/service/TestHS2ImpersonationWithRemoteMS.java