01: package org.emforge.tests;
02:
03: import static org.junit.Assert.assertNotNull;
04:
05: import org.acegisecurity.Authentication;
06: import org.acegisecurity.AuthenticationManager;
07: import org.acegisecurity.context.SecurityContextHolder;
08: import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
09: import org.junit.Test;
10: import org.junit.runner.RunWith;
11: import org.springframework.beans.factory.annotation.Autowired;
12: import org.springframework.test.context.ContextConfiguration;
13: import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
14: import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
15: import org.springframework.test.context.transaction.TransactionConfiguration;
16: import org.springframework.transaction.annotation.Propagation;
17: import org.springframework.transaction.annotation.Transactional;
18:
19: /** Basic class for all unit-tests, used in EmForge
20: *
21: */
22: @RunWith(SpringJUnit4ClassRunner.class)
23: @ContextConfiguration(locations={"classpath:/META-INF/emforge-config-test.xml","classpath:/META-INF/acegi-security.xml","classpath:/META-INF/emforge-security-test.xml","classpath:/META-INF/emforge-ds-test.xml","classpath:/META-INF/emforge-db.xml","classpath:/META-INF/emforge-jbpm.xml","classpath:/META-INF/emforge-wiki.xml","classpath:/META-INF/emforge-vc.xml","classpath:/META-INF/emforge-projectmanager.xml","classpath:/META-INF/emforge-jsf.xml","classpath:/META-INF/emforge-comments.xml","classpath:/META-INF/emforge-reports.xml"})
24: @TransactionConfiguration(transactionManager="txManager",defaultRollback=true)
25: @Transactional(propagation=Propagation.REQUIRES_NEW)
26: public class BaseUnitTest extends
27: AbstractTransactionalJUnit4SpringContextTests {
28: @Autowired
29: private AuthenticationManager m_authManager;
30:
31: /** Performs authentication for specified user
32: *
33: * @param i_userName
34: * @param i_password
35: */
36: protected void authenticateUser(String i_userName, String i_password) {
37: // authenticate user
38: Authentication authentication = new UsernamePasswordAuthenticationToken(
39: i_userName, i_password);
40: authentication = m_authManager.authenticate(authentication);
41: assertNotNull(authentication);
42:
43: SecurityContextHolder.getContext().setAuthentication(
44: authentication);
45: }
46:
47: /** returns information about currently logged user
48: *
49: * @return
50: */
51: protected Authentication getCurrentUser() {
52: return SecurityContextHolder.getContext().getAuthentication();
53: }
54:
55: @Test
56: public void testAuthentication() {
57: authenticateUser("admin", "admin");
58: }
59: }
|