@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; } }); }
public abstract <T> T runAs(PrivilegedAction<T> action);
User { public abstract <T> T runAs(PrivilegedAction<T> action); }
User { public abstract <T> T runAs(PrivilegedAction<T> action);  }
User { public abstract <T> T runAs(PrivilegedAction<T> action);  String getName(); abstract String getShortName(); abstract T runAs(PrivilegedAction<T> action); abstract T runAs(PrivilegedExceptionAction<T> action); String toString(); static User getCurrent(); static User createUserForTesting(Configuration conf,
      String name, String[] groups); static void login(Configuration conf, String fileConfKey,
      String principalConfKey, String localhost); }
User { public abstract <T> T runAs(PrivilegedAction<T> action);  String getName(); abstract String getShortName(); abstract T runAs(PrivilegedAction<T> action); abstract T runAs(PrivilegedExceptionAction<T> action); String toString(); static User getCurrent(); static User createUserForTesting(Configuration conf,
      String name, String[] groups); static void login(Configuration conf, String fileConfKey,
      String principalConfKey, String localhost);  }