TokenAuthentication implements Authentication { @Nullable @Override public Principal getUserPrincipal() { if (tokenInfo == null) { throw new IllegalStateException("Token info can only be retrieved after successful authentication."); } if (tokenInfo instanceof TokenProviderImpl.TokenInfoImpl) { return ((TokenProviderImpl.TokenInfoImpl) tokenInfo).getPrincipal(); } else { return null; } } TokenAuthentication(@NotNull TokenProvider tokenProvider); @Override boolean authenticate(@Nullable Credentials credentials); @Nullable @Override String getUserId(); @Nullable @Override Principal getUserPrincipal();  }
@Test(expected = IllegalStateException.class) public void testGetUserPrincipalBeforeLogin() { authentication.getUserPrincipal(); }
@Test public void testGetUserPrincipal() throws Exception { TokenInfo info = createTokenInfo(tokenProvider, userId); assertTrue(authentication.authenticate(new TokenCredentials(info.getToken()))); assertEquals(getTestUser().getPrincipal(), authentication.getUserPrincipal()); }