Minor refactoring of aspects tests.

This commit is contained in:
Luke Taylor 2011-07-20 17:42:05 +01:00
parent 8740efc0f5
commit 7e44580c75

View File

@ -16,6 +16,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:aspectj-context.xml") @ContextConfiguration(locations = "classpath:aspectj-context.xml")
public class AspectJInterceptorTests { public class AspectJInterceptorTests {
private Authentication admin = new UsernamePasswordAuthenticationToken("test", "xxx", AuthorityUtils.createAuthorityList("ROLE_ADMIN"));
private Authentication user = new UsernamePasswordAuthenticationToken("test", "xxx", AuthorityUtils.createAuthorityList("ROLE_USER"));
@Autowired @Autowired
private Service service; private Service service;
@ -35,17 +37,13 @@ public class AspectJInterceptorTests {
@Test(expected = AccessDeniedException.class) @Test(expected = AccessDeniedException.class)
public void testSecuredMethodWrongRole() throws Exception { public void testSecuredMethodWrongRole() throws Exception {
Authentication token = new UsernamePasswordAuthenticationToken("test", "xxx", AuthorityUtils SecurityContextHolder.getContext().setAuthentication(admin);
.createAuthorityList("ROLE_ADMIN"));
SecurityContextHolder.getContext().setAuthentication(token);
service.secureMethod(); service.secureMethod();
} }
@Test @Test
public void testSecuredMethodEverythingOk() throws Exception { public void testSecuredMethodEverythingOk() throws Exception {
Authentication token = new UsernamePasswordAuthenticationToken("test", "xxx", AuthorityUtils SecurityContextHolder.getContext().setAuthentication(user);
.createAuthorityList("ROLE_USER"));
SecurityContextHolder.getContext().setAuthentication(token);
service.secureMethod(); service.secureMethod();
} }
@ -56,18 +54,21 @@ public class AspectJInterceptorTests {
@Test(expected = AccessDeniedException.class) @Test(expected = AccessDeniedException.class)
public void testSecuredClassWrongRole() throws Exception { public void testSecuredClassWrongRole() throws Exception {
Authentication token = new UsernamePasswordAuthenticationToken("test", "xxx", AuthorityUtils SecurityContextHolder.getContext().setAuthentication(admin);
.createAuthorityList("ROLE_ADMIN"));
SecurityContextHolder.getContext().setAuthentication(token);
securedService.secureMethod(); securedService.secureMethod();
} }
@Test(expected = AccessDeniedException.class)
public void testSecuredClassWrongRoleOnNewedInstance() throws Exception {
SecurityContextHolder.getContext().setAuthentication(admin);
new SecuredService().secureMethod();
}
@Test @Test
public void testSecuredClassEverythingOk() throws Exception { public void testSecuredClassEverythingOk() throws Exception {
Authentication token = new UsernamePasswordAuthenticationToken("test", "xxx", AuthorityUtils SecurityContextHolder.getContext().setAuthentication(user);
.createAuthorityList("ROLE_USER"));
SecurityContextHolder.getContext().setAuthentication(token);
securedService.secureMethod(); securedService.secureMethod();
new SecuredService().secureMethod();
} }
@After @After