|
1 | 1 | package org.baeldung.test; |
2 | 2 |
|
3 | | -import java.util.Properties; |
4 | | - |
5 | | -import javax.sql.DataSource; |
6 | | - |
7 | | -import org.springframework.beans.factory.annotation.Autowired; |
| 3 | +import org.baeldung.spring.PersistenceJPAConfig; |
8 | 4 | import org.springframework.context.annotation.Bean; |
9 | 5 | import org.springframework.context.annotation.ComponentScan; |
10 | | -import org.springframework.context.annotation.Configuration; |
11 | | -import org.springframework.context.annotation.PropertySource; |
12 | | -import org.springframework.core.env.Environment; |
13 | | -import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor; |
14 | | -import org.springframework.data.jpa.repository.config.EnableJpaRepositories; |
15 | | -import org.springframework.jdbc.datasource.DriverManagerDataSource; |
16 | | -import org.springframework.orm.jpa.JpaTransactionManager; |
17 | | -import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; |
18 | | -import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; |
19 | | -import org.springframework.transaction.annotation.EnableTransactionManagement; |
20 | | - |
21 | | -@Configuration |
22 | | -@EnableTransactionManagement |
23 | | -@PropertySource({ "classpath:persistence.properties" }) |
24 | | -@ComponentScan({ "org.baeldung.persistence.model","org.baeldung.persistence.dao" }) |
25 | | -@EnableJpaRepositories(basePackages = "org.baeldung.persistence.dao") |
26 | | -public class TestConfig { |
27 | | - @Autowired |
28 | | - private Environment env; |
29 | | - |
30 | | - public TestConfig() { |
31 | | - super(); |
32 | | - } |
33 | | - |
34 | | - @Bean |
35 | | - public LocalContainerEntityManagerFactoryBean entityManagerFactory() { |
36 | | - final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); |
37 | | - em.setDataSource(dataSource()); |
38 | | - em.setPackagesToScan(new String[] { "org.baeldung.persistence.model" }); |
39 | | - final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); |
40 | | - em.setJpaVendorAdapter(vendorAdapter); |
41 | | - em.setJpaProperties(additionalProperties()); |
42 | | - return em; |
43 | | - } |
| 6 | +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; |
| 7 | +import org.springframework.security.crypto.password.PasswordEncoder; |
44 | 8 |
|
45 | | - @Bean |
46 | | - public DataSource dataSource() { |
47 | | - final DriverManagerDataSource dataSource = new DriverManagerDataSource(); |
48 | | - dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName")); |
49 | | - dataSource.setUrl(env.getProperty("jdbc.url")); |
50 | | - dataSource.setUsername(env.getProperty("jdbc.user")); |
51 | | - dataSource.setPassword(env.getProperty("jdbc.pass")); |
52 | | - return dataSource; |
53 | | - } |
| 9 | +@ComponentScan({ "org.baeldung.persistence.dao" }) |
| 10 | +public class TestConfig extends PersistenceJPAConfig { |
54 | 11 |
|
55 | 12 | @Bean |
56 | | - public JpaTransactionManager transactionManager() { |
57 | | - JpaTransactionManager transactionManager = new JpaTransactionManager(); |
58 | | - transactionManager.setEntityManagerFactory(entityManagerFactory().getObject()); |
59 | | - return transactionManager; |
| 13 | + public PasswordEncoder encoder() { |
| 14 | + return new BCryptPasswordEncoder(11); |
60 | 15 | } |
61 | 16 |
|
62 | | - @Bean |
63 | | - public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { |
64 | | - return new PersistenceExceptionTranslationPostProcessor(); |
65 | | - } |
66 | | - |
67 | | - final Properties additionalProperties() { |
68 | | - final Properties hibernateProperties = new Properties(); |
69 | | - hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto")); |
70 | | - hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect")); |
71 | | - return hibernateProperties; |
72 | | - } |
73 | 17 | } |
0 commit comments