Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.bexcomponents.wup.onptest;
- import org.junit.After;
- import org.junit.Before;
- import org.junit.runner.RunWith;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.config.ConfigurableBeanFactory;
- import org.springframework.context.ApplicationContext;
- import org.springframework.mock.web.MockHttpServletRequest;
- import org.springframework.security.GrantedAuthorityImpl;
- import org.springframework.security.context.SecurityContextHolder;
- import org.springframework.security.providers.UsernamePasswordAuthenticationToken;
- import org.springframework.test.context.ContextConfiguration;
- import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
- import org.springframework.web.context.request.RequestContextHolder;
- import org.springframework.web.context.request.RequestScope;
- import org.springframework.web.context.request.ServletRequestAttributes;
- import org.springframework.web.context.request.SessionScope;
- import com.bexcomponents.common.security.BexUser;
- import com.bexcomponents.common.security.BexUserNamePassword;
- import com.bexcomponents.common.security.UserNamePasswordPerThread;
- import com.bexcomponents.wup.dao.IMandantDAO;
- import com.bexcomponents.wup.model.persistence.Mandant;
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations =
- {
- //@formatter:off
- "file:src/main/webapp/WEB-INF/propertyPlaceholder.xml",
- "file:src/main/webapp/WEB-INF/springCacheContext.xml",
- "file:src/main/webapp/WEB-INF/interceptorContext.xml",
- "file:src/main/webapp/WEB-INF/updateContext.xml",
- "file:src/main/webapp/WEB-INF/springBatchContext.xml",
- "file:src/main/webapp/WEB-INF/applicationContext.xml",
- "file:src/main/webapp/WEB-INF/hibernateContext.xml",
- "file:src/main/webapp/WEB-INF/dataSource.xml",
- "file:src/main/webapp/WEB-INF/acegi.xml",
- "file:src/main/webapp/WEB-INF/acegiAuthenticationProvider.xml",
- "file:src/test/resources/com/bexcomponents/wup/onptest/webserviceUrl.xml",
- "file:src/main/webapp/WEB-INF/quartz.xml",
- "file:src/main/webapp/WEB-INF/springSchedulerContext.xml",
- "file:src/main/webapp/WEB-INF/cxf-servlet.xml",
- "file:src/main/webapp/WEB-INF/web.xml"
- //@formatter:on
- })
- public class AbstractOnPAutoTesting
- {
- private String username = "1";
- private String password = "1";
- @Autowired
- private ApplicationContext applicationContext;
- @Before
- public void authenticate()
- {
- IMandantDAO mandantDAO = applicationContext.getBean(IMandantDAO.class);
- Mandant mandant = mandantDAO.getById(1L);
- BexUser user = new BexUser(username, username, password, true, false, true, true, new GrantedAuthorityImpl[]
- { new GrantedAuthorityImpl("admin") }); //$NON-NLS-1$
- user.setLanguageCode("DE");
- user.setLanguageId(1L);
- user.setMandantId(mandant.getId());
- user.setMandant(mandant);
- SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_INHERITABLETHREADLOCAL);
- UserNamePasswordPerThread.setUserNamePassword(new BexUserNamePassword(username, password, "/wup/cxf/wupService"));
- UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(user, password, new GrantedAuthorityImpl[0]);
- SecurityContextHolder.getContext().setAuthentication(auth);
- registerScopes();
- }
- private void registerScopes()
- {
- ConfigurableBeanFactory configurableBeanFactory = (ConfigurableBeanFactory) applicationContext.getAutowireCapableBeanFactory();
- configurableBeanFactory.registerScope("session", new SessionScope());
- configurableBeanFactory.registerScope("request", new RequestScope());
- MockHttpServletRequest request = new MockHttpServletRequest();
- ServletRequestAttributes attributes = new ServletRequestAttributes(request);
- RequestContextHolder.setRequestAttributes(attributes);
- }
- @After
- public void clear()
- {
- SecurityContextHolder.clearContext();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement