@ -16,6 +16,10 @@ import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Mockito.doAnswer ;
import static org.mockito.Mockito.doAnswer ;
import static org.mockito.Mockito.verify ;
import static org.mockito.Mockito.verify ;
import java.util.concurrent.ExecutorService ;
import java.util.concurrent.Executors ;
import java.util.concurrent.Future ;
import org.junit.After ;
import org.junit.After ;
import org.junit.Before ;
import org.junit.Before ;
import org.junit.Test ;
import org.junit.Test ;
@ -24,6 +28,8 @@ import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock ;
import org.mockito.invocation.InvocationOnMock ;
import org.mockito.runners.MockitoJUnitRunner ;
import org.mockito.runners.MockitoJUnitRunner ;
import org.mockito.stubbing.Answer ;
import org.mockito.stubbing.Answer ;
import org.springframework.core.task.SyncTaskExecutor ;
import org.springframework.core.task.support.ExecutorServiceAdapter ;
import org.springframework.security.core.context.SecurityContext ;
import org.springframework.security.core.context.SecurityContext ;
import org.springframework.security.core.context.SecurityContextHolder ;
import org.springframework.security.core.context.SecurityContextHolder ;
@ -43,6 +49,8 @@ public class DelegatingSecurityContextRunnableTests {
private Runnable runnable ;
private Runnable runnable ;
private ExecutorService executor ;
@Before
@Before
public void setUp ( ) throws Exception {
public void setUp ( ) throws Exception {
doAnswer ( new Answer < Object > ( ) {
doAnswer ( new Answer < Object > ( ) {
@ -51,6 +59,8 @@ public class DelegatingSecurityContextRunnableTests {
return null ;
return null ;
}
}
} ) . when ( delegate ) . run ( ) ;
} ) . when ( delegate ) . run ( ) ;
executor = Executors . newFixedThreadPool ( 1 ) ;
}
}
@After
@After
@ -85,8 +95,7 @@ public class DelegatingSecurityContextRunnableTests {
@Test
@Test
public void call ( ) throws Exception {
public void call ( ) throws Exception {
runnable = new DelegatingSecurityContextRunnable ( delegate , securityContext ) ;
runnable = new DelegatingSecurityContextRunnable ( delegate , securityContext ) ;
runnable . run ( ) ;
assertWrapped ( runnable ) ;
assertWrapped ( ) ;
}
}
@Test
@Test
@ -95,8 +104,26 @@ public class DelegatingSecurityContextRunnableTests {
runnable = new DelegatingSecurityContextRunnable ( delegate ) ;
runnable = new DelegatingSecurityContextRunnable ( delegate ) ;
SecurityContextHolder . clearContext ( ) ; // ensure runnable is what sets up the
SecurityContextHolder . clearContext ( ) ; // ensure runnable is what sets up the
// SecurityContextHolder
// SecurityContextHolder
runnable . run ( ) ;
assertWrapped ( runnable ) ;
assertWrapped ( ) ;
}
// SEC-3031
@Test
public void callOnSameThread ( ) throws Exception {
executor = synchronousExecutor ( ) ;
runnable = new DelegatingSecurityContextRunnable ( delegate ,
securityContext ) ;
securityContext = SecurityContextHolder . createEmptyContext ( ) ;
assertWrapped ( runnable ) ;
}
@Test
public void callOnSameThreadExplicitlyEnabled ( ) throws Exception {
executor = synchronousExecutor ( ) ;
DelegatingSecurityContextRunnable runnable = new DelegatingSecurityContextRunnable ( delegate ,
securityContext ) ;
runnable . setEnableOnOriginalThread ( true ) ;
assertWrapped ( runnable ) ;
}
}
// --- create ---
// --- create ---
@ -112,20 +139,18 @@ public class DelegatingSecurityContextRunnableTests {
}
}
@Test
@Test
public void createNullSecurityContext ( ) {
public void createNullSecurityContext ( ) throws Exception {
SecurityContextHolder . setContext ( securityContext ) ;
SecurityContextHolder . setContext ( securityContext ) ;
runnable = DelegatingSecurityContextRunnable . create ( delegate , null ) ;
runnable = DelegatingSecurityContextRunnable . create ( delegate , null ) ;
SecurityContextHolder . clearContext ( ) ; // ensure runnable is what sets up the
SecurityContextHolder . clearContext ( ) ; // ensure runnable is what sets up the
// SecurityContextHolder
// SecurityContextHolder
runnable . run ( ) ;
assertWrapped ( runnable ) ;
assertWrapped ( ) ;
}
}
@Test
@Test
public void create ( ) {
public void create ( ) throws Exception {
runnable = DelegatingSecurityContextRunnable . create ( delegate , securityContext ) ;
runnable = DelegatingSecurityContextRunnable . create ( delegate , securityContext ) ;
runnable . run ( ) ;
assertWrapped ( runnable ) ;
assertWrapped ( ) ;
}
}
// --- toString
// --- toString
@ -137,9 +162,15 @@ public class DelegatingSecurityContextRunnableTests {
assertThat ( runnable . toString ( ) ) . isEqualTo ( delegate . toString ( ) ) ;
assertThat ( runnable . toString ( ) ) . isEqualTo ( delegate . toString ( ) ) ;
}
}
private void assertWrapped ( ) {
private void assertWrapped ( Runnable runnable ) throws Exception {
Future < ? > submit = executor . submit ( runnable ) ;
submit . get ( ) ;
verify ( delegate ) . run ( ) ;
verify ( delegate ) . run ( ) ;
assertThat ( SecurityContextHolder . getContext ( ) ) . isEqualTo (
assertThat ( SecurityContextHolder . getContext ( ) ) . isEqualTo (
SecurityContextHolder . createEmptyContext ( ) ) ;
SecurityContextHolder . createEmptyContext ( ) ) ;
}
}
private static ExecutorService synchronousExecutor ( ) {
return new ExecutorServiceAdapter ( new SyncTaskExecutor ( ) ) ;
}
}
}