### Question:
User { public abstract <T> T runAs(PrivilegedAction<T> action);  UserGroupInformation getUGI(); String getName(); String[] getGroupNames(); abstract String getShortName(); abstract T runAs(PrivilegedAction<T> action); abstract T runAs(PrivilegedExceptionAction<T> action); abstract void obtainAuthTokenForJob(Configuration conf, Job job); abstract void obtainAuthTokenForJob(JobConf job); String toString(); static User getCurrent(); static User create(UserGroupInformation ugi); static User createUserForTesting(Configuration conf,
      String name, String[] groups); static void login(Configuration conf, String fileConfKey,
      String principalConfKey, String localhost); static boolean isSecurityEnabled(); static boolean isHBaseSecurityEnabled(Configuration conf); static final String HBASE_SECURITY_CONF_KEY; }### Answer:
@Test public void testRunAs() throws Exception { Configuration conf = HBaseConfiguration.create(); final User user = User.createUserForTesting(conf, "testuser", new String[]{"foo"}); final PrivilegedExceptionAction<String> action = new PrivilegedExceptionAction<String>(){ public String run() throws IOException { User u = User.getCurrent(); return u.getName(); } }; String username = user.runAs(action); assertEquals("Current user within runAs() should match", "testuser", username); User user2 = User.createUserForTesting(conf, "testuser2", new String[]{"foo"}); String username2 = user2.runAs(action); assertEquals("Second username should match second user", "testuser2", username2); username = user.runAs(new PrivilegedExceptionAction<String>(){ public String run() throws Exception { return User.getCurrent().getName(); } }); assertEquals("User name in runAs() should match", "testuser", username); user2.runAs(new PrivilegedExceptionAction(){ public Object run() throws IOException, InterruptedException{ String nestedName = user.runAs(action); assertEquals("Nest name should match nested user", "testuser", nestedName); assertEquals("Current name should match current user", "testuser2", User.getCurrent().getName()); return null; } }); }