Advertisement
Guest User

Untitled

a guest
Dec 14th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. package com.bexcomponents.wup.onptest;
  2.  
  3. import org.junit.After;
  4. import org.junit.Before;
  5. import org.junit.runner.RunWith;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.beans.factory.config.ConfigurableBeanFactory;
  8. import org.springframework.context.ApplicationContext;
  9. import org.springframework.mock.web.MockHttpServletRequest;
  10. import org.springframework.security.GrantedAuthorityImpl;
  11. import org.springframework.security.context.SecurityContextHolder;
  12. import org.springframework.security.providers.UsernamePasswordAuthenticationToken;
  13. import org.springframework.test.context.ContextConfiguration;
  14. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  15. import org.springframework.web.context.request.RequestContextHolder;
  16. import org.springframework.web.context.request.RequestScope;
  17. import org.springframework.web.context.request.ServletRequestAttributes;
  18. import org.springframework.web.context.request.SessionScope;
  19.  
  20. import com.bexcomponents.common.security.BexUser;
  21. import com.bexcomponents.common.security.BexUserNamePassword;
  22. import com.bexcomponents.common.security.UserNamePasswordPerThread;
  23. import com.bexcomponents.wup.dao.IMandantDAO;
  24. import com.bexcomponents.wup.model.persistence.Mandant;
  25.  
  26. @RunWith(SpringJUnit4ClassRunner.class)
  27. @ContextConfiguration(locations =
  28. {
  29.  
  30. //@formatter:off
  31. "file:src/main/webapp/WEB-INF/propertyPlaceholder.xml",
  32. "file:src/main/webapp/WEB-INF/springCacheContext.xml",
  33. "file:src/main/webapp/WEB-INF/interceptorContext.xml",
  34. "file:src/main/webapp/WEB-INF/updateContext.xml",
  35. "file:src/main/webapp/WEB-INF/springBatchContext.xml",
  36. "file:src/main/webapp/WEB-INF/applicationContext.xml",
  37. "file:src/main/webapp/WEB-INF/hibernateContext.xml",
  38. "file:src/main/webapp/WEB-INF/dataSource.xml",
  39. "file:src/main/webapp/WEB-INF/acegi.xml",
  40. "file:src/main/webapp/WEB-INF/acegiAuthenticationProvider.xml",
  41. "file:src/test/resources/com/bexcomponents/wup/onptest/webserviceUrl.xml",
  42. "file:src/main/webapp/WEB-INF/quartz.xml",
  43. "file:src/main/webapp/WEB-INF/springSchedulerContext.xml",
  44. "file:src/main/webapp/WEB-INF/cxf-servlet.xml",
  45. "file:src/main/webapp/WEB-INF/web.xml"
  46. //@formatter:on
  47.  
  48. })
  49. public class AbstractOnPAutoTesting
  50. {
  51. private String username = "1";
  52. private String password = "1";
  53.  
  54. @Autowired
  55. private ApplicationContext applicationContext;
  56.  
  57. @Before
  58. public void authenticate()
  59. {
  60. IMandantDAO mandantDAO = applicationContext.getBean(IMandantDAO.class);
  61. Mandant mandant = mandantDAO.getById(1L);
  62.  
  63. BexUser user = new BexUser(username, username, password, true, false, true, true, new GrantedAuthorityImpl[]
  64. { new GrantedAuthorityImpl("admin") }); //$NON-NLS-1$
  65.  
  66. user.setLanguageCode("DE");
  67. user.setLanguageId(1L);
  68. user.setMandantId(mandant.getId());
  69. user.setMandant(mandant);
  70.  
  71. SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_INHERITABLETHREADLOCAL);
  72.  
  73. UserNamePasswordPerThread.setUserNamePassword(new BexUserNamePassword(username, password, "/wup/cxf/wupService"));
  74. UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(user, password, new GrantedAuthorityImpl[0]);
  75. SecurityContextHolder.getContext().setAuthentication(auth);
  76.  
  77. registerScopes();
  78.  
  79. }
  80.  
  81. private void registerScopes()
  82. {
  83. ConfigurableBeanFactory configurableBeanFactory = (ConfigurableBeanFactory) applicationContext.getAutowireCapableBeanFactory();
  84. configurableBeanFactory.registerScope("session", new SessionScope());
  85. configurableBeanFactory.registerScope("request", new RequestScope());
  86. MockHttpServletRequest request = new MockHttpServletRequest();
  87. ServletRequestAttributes attributes = new ServletRequestAttributes(request);
  88. RequestContextHolder.setRequestAttributes(attributes);
  89. }
  90.  
  91. @After
  92. public void clear()
  93. {
  94. SecurityContextHolder.clearContext();
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement