RMAdminCLI extends HAAdmin { private int getGroups(String[] usernames) throws IOException { ResourceManagerAdministrationProtocol adminProtocol = createAdminProtocol(); if (usernames.length == 0) { usernames = new String[] { UserGroupInformation.getCurrentUser().getUserName() }; } for (String username : usernames) { StringBuilder sb = new StringBuilder(); sb.append(username + " :"); for (String group : adminProtocol.getGroupsForUser(username)) { sb.append(" "); sb.append(group); } System.out.println(sb); } return 0; } RMAdminCLI();  RMAdminCLI(Configuration conf); @Override int run(String[] args); @Override void setConf(Configuration conf); static void main(String[] args);  }
@Test public void testGetGroups() throws Exception { when(admin.getGroupsForUser(eq("admin"))).thenReturn( new String[] {"group1", "group2"}); PrintStream origOut = System.out; PrintStream out = mock(PrintStream.class); System.setOut(out); try { String[] args = { "-getGroups", "admin" }; assertEquals(0, rmAdminCLI.run(args)); verify(admin).getGroupsForUser(eq("admin")); verify(out).println(argThat(new ArgumentMatcher<StringBuilder>() { @Override public boolean matches(Object argument) { return ("" + argument).equals("admin : group1 group2"); } })); } finally { System.setOut(origOut); } }
@Test(timeout=500) public void testGetGroups() throws Exception { when(admin.getGroupsForUser(eq("admin"))).thenReturn( new String[] {"group1", "group2"}); PrintStream origOut = System.out; PrintStream out = mock(PrintStream.class); System.setOut(out); try { String[] args = { "-getGroups", "admin" }; assertEquals(0, rmAdminCLI.run(args)); verify(admin).getGroupsForUser(eq("admin")); verify(out).println(argThat(new ArgumentMatcher<StringBuilder>() { @Override public boolean matches(Object argument) { return ("" + argument).equals("admin : group1 group2"); } })); } finally { System.setOut(origOut); } }