SecurityContextUtil { public static String getUser() { SecurityContext sc = SecurityContextUtil.getSC(); if ((sc != null) && (sc.getUserPrincipal() != null)) { return sc.getUserPrincipal().getName(); } return null; }  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 getUserWithSetPrincipal() 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"); Assert.assertEquals("foobar", SecurityContextUtil.getUser()); }
@Test public void getUserWithNullContext() throws Exception { PowerMockito.spy(SecurityContextUtil.class); PowerMockito.doReturn(null).when(SecurityContextUtil.class, "getSC"); Assert.assertNull(SecurityContextUtil.getUser()); }
@Test public void getUserWithNullPrincipal() throws Exception { SecurityContext securityContext = PowerMockito.mock(SecurityContext.class); PowerMockito.when(securityContext.getUserPrincipal()).thenReturn(null); PowerMockito.spy(SecurityContextUtil.class); PowerMockito.doReturn(securityContext).when(SecurityContextUtil.class, "getSC"); Assert.assertNull(SecurityContextUtil.getUser()); }