AspectJInterceptorTests.java 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package sample.aspectj;
  2. import org.junit.After;
  3. import org.junit.Test;
  4. import org.junit.runner.RunWith;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.security.access.AccessDeniedException;
  7. import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
  8. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  9. import org.springframework.security.core.Authentication;
  10. import org.springframework.security.core.authority.AuthorityUtils;
  11. import org.springframework.security.core.context.SecurityContextHolder;
  12. import org.springframework.test.context.ContextConfiguration;
  13. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  14. @RunWith(SpringJUnit4ClassRunner.class)
  15. @ContextConfiguration(locations = "classpath:aspectj-context.xml")
  16. public class AspectJInterceptorTests {
  17. private Authentication admin = new UsernamePasswordAuthenticationToken("test", "xxx", AuthorityUtils.createAuthorityList("ROLE_ADMIN"));
  18. private Authentication user = new UsernamePasswordAuthenticationToken("test", "xxx", AuthorityUtils.createAuthorityList("ROLE_USER"));
  19. @Autowired
  20. private Service service;
  21. @Autowired
  22. private SecuredService securedService;
  23. @Test
  24. public void testPublicMethod() throws Exception {
  25. service.publicMethod();
  26. }
  27. @Test(expected = AuthenticationCredentialsNotFoundException.class)
  28. public void testSecuredMethodNotAuthenticated() throws Exception {
  29. service.secureMethod();
  30. }
  31. @Test(expected = AccessDeniedException.class)
  32. public void testSecuredMethodWrongRole() throws Exception {
  33. SecurityContextHolder.getContext().setAuthentication(admin);
  34. service.secureMethod();
  35. }
  36. @Test
  37. public void testSecuredMethodEverythingOk() throws Exception {
  38. SecurityContextHolder.getContext().setAuthentication(user);
  39. service.secureMethod();
  40. }
  41. @Test(expected = AuthenticationCredentialsNotFoundException.class)
  42. public void testSecuredClassNotAuthenticated() throws Exception {
  43. securedService.secureMethod();
  44. }
  45. @Test(expected = AccessDeniedException.class)
  46. public void testSecuredClassWrongRole() throws Exception {
  47. SecurityContextHolder.getContext().setAuthentication(admin);
  48. securedService.secureMethod();
  49. }
  50. @Test(expected = AccessDeniedException.class)
  51. public void testSecuredClassWrongRoleOnNewedInstance() throws Exception {
  52. SecurityContextHolder.getContext().setAuthentication(admin);
  53. new SecuredService().secureMethod();
  54. }
  55. @Test
  56. public void testSecuredClassEverythingOk() throws Exception {
  57. SecurityContextHolder.getContext().setAuthentication(user);
  58. securedService.secureMethod();
  59. new SecuredService().secureMethod();
  60. }
  61. @After
  62. public void tearDown() {
  63. SecurityContextHolder.clearContext();
  64. }
  65. }