/** * Utility method that allows you to run a block of code as the given user. Regardless of success or exception * situation, the original session and authentication will be restored once your block of code is finished executing, * i.e. the given user will apply only to your {@link Callable}, then the system environment will return to the user * present prior to you calling this method. * * @param <T> the return type of your operation, specify this type as <code>T</code> * @param principalName the user under whom you wish to run a section of code * @param callable {@link Callable#call()} contains the code you wish to run as the given user * @return the value returned by your implementation of {@link Callable#call()} * @throws Exception * @see {@link Callable} */ @Override public <T> T runAsUser( final String principalName, final Callable<T> callable ) throws Exception { return runAsUser( principalName, null, callable ); }
@Test @SuppressWarnings( "unchecked" ) public void runAsUserTest() throws Exception { Callable<String> callable = (Callable<String>) mock( Callable.class ); when( callable.call() ).thenReturn( CALLABLE_RETURNED_VALUE_OK ); String runningResult = getAuthorizedSecurityHelper().runAsUser( DEF_USERNAME, callable ); assertEquals( CALLABLE_RETURNED_VALUE_OK, runningResult ); }