Merge pull request #113 from Doha2012/master

add test config
This commit is contained in:
Eugen 2015-01-09 21:26:06 +02:00
commit e5b7f52b5c
6 changed files with 142 additions and 41 deletions

View File

@ -4,5 +4,6 @@ import org.baeldung.persistence.model.Privilege;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
public interface PrivilegeRepository extends JpaRepository<Privilege, Long> { public interface PrivilegeRepository extends JpaRepository<Privilege, Long> {
Privilege findByName(String name); public Privilege findByName(String name);
public void delete(Privilege privilege);
} }

View File

@ -5,4 +5,5 @@ import org.springframework.data.jpa.repository.JpaRepository;
public interface RoleRepository extends JpaRepository<Role, Long> { public interface RoleRepository extends JpaRepository<Role, Long> {
public Role findByName(String name); public Role findByName(String name);
public void delete(Role role);
} }

View File

@ -2,7 +2,6 @@ package org.baeldung.persistence.model;
import java.util.Collection; import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType; import javax.persistence.GenerationType;
@ -21,7 +20,7 @@ public class Role {
@ManyToMany(mappedBy = "roles") @ManyToMany(mappedBy = "roles")
private Collection<User> users; private Collection<User> users;
@ManyToMany(cascade = CascadeType.ALL) @ManyToMany
@JoinTable(name = "roles_privileges", joinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "privilege_id", referencedColumnName = "id")) @JoinTable(name = "roles_privileges", joinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "privilege_id", referencedColumnName = "id"))
private Collection<Privilege> privileges; private Collection<Privilege> privileges;

View File

@ -3,7 +3,6 @@ package org.baeldung.persistence.model;
import java.util.Collection; import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType; import javax.persistence.GenerationType;
@ -31,7 +30,7 @@ public class User {
private boolean tokenExpired; private boolean tokenExpired;
@ManyToMany(cascade = CascadeType.ALL) @ManyToMany
@JoinTable( @JoinTable(
name = "users_roles", name = "users_roles",
joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"), joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),

View File

@ -1,5 +1,9 @@
package org.baeldung.test; package org.baeldung.test;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import org.baeldung.persistence.dao.PrivilegeRepository; import org.baeldung.persistence.dao.PrivilegeRepository;
@ -8,12 +12,6 @@ import org.baeldung.persistence.dao.UserRepository;
import org.baeldung.persistence.model.Privilege; import org.baeldung.persistence.model.Privilege;
import org.baeldung.persistence.model.Role; import org.baeldung.persistence.model.Role;
import org.baeldung.persistence.model.User; import org.baeldung.persistence.model.User;
import org.baeldung.spring.AppConfig;
import org.baeldung.spring.MvcConfig;
import org.baeldung.spring.PersistenceJPAConfig;
import org.baeldung.spring.SecSecurityConfig;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -22,13 +20,13 @@ import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration; import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.test.context.support.AnnotationConfigContextLoader; import org.springframework.test.context.support.AnnotationConfigContextLoader;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { AppConfig.class, MvcConfig.class, PersistenceJPAConfig.class, SecSecurityConfig.class}) @ContextConfiguration(classes = { TestConfig.class }, loader = AnnotationConfigContextLoader.class)
@Transactional
@TransactionConfiguration
public class SpringSecurityRolesTest { public class SpringSecurityRolesTest {
@Autowired @Autowired
@ -44,11 +42,8 @@ public class SpringSecurityRolesTest {
private Role role; private Role role;
private Privilege privilege; private Privilege privilege;
@Before @Test
public void init(){ public void testDeleteUser() {
privilege = new Privilege("TEST_PRIVILEGE");
privilegeRepository.save(privilege);
role = new Role("TEST_ROLE"); role = new Role("TEST_ROLE");
roleRepository.save(role); roleRepository.save(role);
@ -61,31 +56,64 @@ public class SpringSecurityRolesTest {
user.setRoles(Arrays.asList(role)); user.setRoles(Arrays.asList(role));
user.setEnabled(true); user.setEnabled(true);
userRepository.save(user); userRepository.save(user);
}
@After assertNotNull(userRepository.findByEmail(user.getEmail()));
public void cleanUp(){ assertNotNull(roleRepository.findByName(role.getName()));
privilegeRepository.delete(privilege); user.setRoles(null);
roleRepository.delete(role);
userRepository.delete(user); userRepository.delete(user);
assertNull(userRepository.findByEmail(user.getEmail()));
assertNotNull(roleRepository.findByName(role.getName()));
} }
@Test @Test
public void testDeleteUser(){ public void testDeleteRole() {
userRepository.delete(user); privilege = new Privilege("TEST_PRIVILEGE");
System.out.println(roleRepository.findByName(role.getName())); privilegeRepository.save(privilege);
}
@Test role = new Role("TEST_ROLE");
public void testDeleteRole(){ role.setPrivileges(Arrays.asList(privilege));
roleRepository.save(role);
user = new User();
user.setFirstName("John");
user.setLastName("Doe");
PasswordEncoder encoder = new BCryptPasswordEncoder();
user.setPassword(encoder.encode("123"));
user.setEmail("john@doe.com");
user.setRoles(Arrays.asList(role));
user.setEnabled(true);
userRepository.save(user);
assertNotNull(privilegeRepository.findByName(privilege.getName()));
assertNotNull(userRepository.findByEmail(user.getEmail()));
assertNotNull(roleRepository.findByName(role.getName()));
user.setRoles(new ArrayList<Role>());
role.setPrivileges(new ArrayList<Privilege>());
roleRepository.delete(role); roleRepository.delete(role);
System.out.println(privilegeRepository.findByName(privilege.getName()));
System.out.println(userRepository.findByEmail(user.getEmail())); assertNull(roleRepository.findByName(role.getName()));
assertNotNull(privilegeRepository.findByName(privilege.getName()));
assertNotNull(userRepository.findByEmail(user.getEmail()));
} }
@Test @Test
public void testDeletePrivilege(){ public void testDeletePrivilege() {
privilege = new Privilege("TEST_PRIVILEGE");
privilegeRepository.save(privilege);
role = new Role("TEST_ROLE");
role.setPrivileges(Arrays.asList(privilege));
roleRepository.save(role);
assertNotNull(roleRepository.findByName(role.getName()));
assertNotNull(privilegeRepository.findByName(privilege.getName()));
role.setPrivileges(new ArrayList<Privilege>());
privilegeRepository.delete(privilege); privilegeRepository.delete(privilege);
System.out.println(roleRepository.findByName(role.getName()));
assertNull(privilegeRepository.findByName(privilege.getName()));
assertNotNull(roleRepository.findByName(role.getName()));
} }
} }

View File

@ -0,0 +1,73 @@
package org.baeldung.test;
import java.util.Properties;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableTransactionManagement
@PropertySource({ "classpath:persistence.properties" })
@ComponentScan({ "org.baeldung.persistence.model","org.baeldung.persistence.dao" })
@EnableJpaRepositories(basePackages = "org.baeldung.persistence.dao")
public class TestConfig {
@Autowired
private Environment env;
public TestConfig() {
super();
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalProperties());
return em;
}
@Bean
public DataSource dataSource() {
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
dataSource.setUrl(env.getProperty("jdbc.url"));
dataSource.setUsername(env.getProperty("jdbc.user"));
dataSource.setPassword(env.getProperty("jdbc.pass"));
return dataSource;
}
@Bean
public JpaTransactionManager transactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
return transactionManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
final Properties additionalProperties() {
final Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
return hibernateProperties;
}
}