@SuppressWarnings("unchecked") // from Mockito mocks @Test public <T extends TokenIdentifier> void testUGITokens() throws Exception { UserGroupInformation ugi = UserGroupInformation.createUserForTesting("TheDoctor", new String[] {"TheTARDIS"}); Token<T> t1 = mock(Token.class); Token<T> t2 = mock(Token.class); ugi.addToken(t1); ugi.addToken(t2); Collection<Token<? extends TokenIdentifier>> z = ugi.getTokens(); assertTrue(z.contains(t1)); assertTrue(z.contains(t2)); assertEquals(2, z.size()); try { z.remove(t1); fail("Shouldn't be able to modify token collection from UGI"); } catch (UnsupportedOperationException uoe) { // Can't modify tokens } // ensure that the tokens are passed through doAs Collection<Token<? extends TokenIdentifier>> otherSet = ugi.doAs( new PrivilegedExceptionAction<Collection<Token<?>>>() { public Collection<Token<?>> run() throws IOException { return UserGroupInformation.getCurrentUser().getTokens(); } }); assertTrue(otherSet.contains(t1)); assertTrue(otherSet.contains(t2)); }