SecurityContextUtil { public static void assertLoggedIn() { SecurityContextUtil.assertSC(); }  static SecurityContext getSC(); static void assertSC(); static void assertLoggedIn(); static String getUser(); static IUser getUserObject(); static boolean hasRole(String role); static String requestId(); static boolean isLoggedIn();  }
@Test public void assertLoggedInWithNullContext() throws Exception { try { PowerMockito.spy(SecurityContextUtil.class); PowerMockito.doReturn(null).when(SecurityContextUtil.class, "getSC"); SecurityContextUtil.assertLoggedIn(); Assert.fail("Should not get past assertLoggedIn"); } catch (Exception e) { Assert.assertEquals(NotAuthorizedException.class, e.getClass()); } }
@Test public void assertLoggedInWithNullPrincipal() throws Exception { try { SecurityContext securityContext = PowerMockito.mock(SecurityContext.class); PowerMockito.when(securityContext.getUserPrincipal()).thenReturn(null); PowerMockito.spy(SecurityContextUtil.class); PowerMockito.doReturn(securityContext).when(SecurityContextUtil.class, "getSC"); SecurityContextUtil.assertLoggedIn(); Assert.fail("Should not get past assertLoggedIn"); } catch (Exception e) { Assert.assertEquals(NotAuthorizedException.class, e.getClass()); } }
@Test public void assertLoggedInWithSetPrincipal() throws Exception { SecurityContext securityContext = PowerMockito.mock(SecurityContext.class); PowerMockito.when(securityContext.getUserPrincipal()).thenReturn(new SimplePrincipal("foobar")); PowerMockito.spy(SecurityContextUtil.class); PowerMockito.doReturn(securityContext).when(SecurityContextUtil.class, "getSC"); SecurityContextUtil.assertLoggedIn(); }