|
| 1 | +package org.mitre.oauth2.repository.impl; |
| 2 | + |
| 3 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import java.nio.file.Files; |
| 7 | +import java.nio.file.Paths; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +import javax.persistence.EntityManagerFactory; |
| 12 | +import javax.sql.DataSource; |
| 13 | + |
| 14 | +import org.springframework.beans.factory.FactoryBean; |
| 15 | +import org.springframework.beans.factory.annotation.Autowired; |
| 16 | +import org.springframework.context.annotation.Bean; |
| 17 | +import org.springframework.core.io.ByteArrayResource; |
| 18 | +import org.springframework.core.io.DefaultResourceLoader; |
| 19 | +import org.springframework.core.io.Resource; |
| 20 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; |
| 21 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; |
| 22 | +import org.springframework.orm.jpa.JpaTransactionManager; |
| 23 | +import org.springframework.orm.jpa.JpaVendorAdapter; |
| 24 | +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; |
| 25 | +import org.springframework.orm.jpa.vendor.Database; |
| 26 | +import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter; |
| 27 | +import org.springframework.transaction.PlatformTransactionManager; |
| 28 | + |
| 29 | +public class TestDatabaseConfiguration { |
| 30 | + |
| 31 | + @Autowired |
| 32 | + private JpaVendorAdapter jpaAdapter; |
| 33 | + |
| 34 | + @Autowired |
| 35 | + private DataSource dataSource; |
| 36 | + |
| 37 | + @Autowired |
| 38 | + private EntityManagerFactory entityManagerFactory; |
| 39 | + |
| 40 | + @Bean |
| 41 | + public JpaOAuth2TokenRepository repository() { |
| 42 | + return new JpaOAuth2TokenRepository(); |
| 43 | + } |
| 44 | + |
| 45 | + @Bean(name = "defaultPersistenceUnit") |
| 46 | + public FactoryBean<EntityManagerFactory> entityManagerFactory() { |
| 47 | + LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); |
| 48 | + factory.setPackagesToScan("org.mitre", "org.mitre"); |
| 49 | + factory.setPersistenceProviderClass(org.eclipse.persistence.jpa.PersistenceProvider.class); |
| 50 | + factory.setPersistenceUnitName("test" + System.currentTimeMillis()); |
| 51 | + factory.setDataSource(dataSource); |
| 52 | + factory.setJpaVendorAdapter(jpaAdapter); |
| 53 | + Map<String, Object> jpaProperties = new HashMap<String, Object>(); |
| 54 | + jpaProperties.put("eclipselink.weaving", "false"); |
| 55 | + jpaProperties.put("eclipselink.logging.level", "INFO"); |
| 56 | + jpaProperties.put("eclipselink.logging.level.sql", "INFO"); |
| 57 | + jpaProperties.put("eclipselink.cache.shared.default", "false"); |
| 58 | + factory.setJpaPropertyMap(jpaProperties); |
| 59 | + |
| 60 | + return factory; |
| 61 | + } |
| 62 | + |
| 63 | + @Bean |
| 64 | + public DataSource dataSource() { |
| 65 | + return new EmbeddedDatabaseBuilder(new DefaultResourceLoader() { |
| 66 | + @Override |
| 67 | + public Resource getResource(String location) { |
| 68 | + String sql; |
| 69 | + try { |
| 70 | + sql = new String(Files.readAllBytes(Paths.get("..", "openid-connect-server-webapp", "src", "main", |
| 71 | + "resources", "db", "hsql", location)), UTF_8); |
| 72 | + } catch (IOException e) { |
| 73 | + throw new RuntimeException("Failed to read sql-script " + location, e); |
| 74 | + } |
| 75 | + |
| 76 | + return new ByteArrayResource(sql.getBytes(UTF_8)); |
| 77 | + } |
| 78 | + }).generateUniqueName(true).setScriptEncoding(UTF_8.name()).setType(EmbeddedDatabaseType.HSQL) |
| 79 | + .addScripts("hsql_database_tables.sql").build(); |
| 80 | + } |
| 81 | + |
| 82 | + @Bean |
| 83 | + public JpaVendorAdapter jpaAdapter() { |
| 84 | + EclipseLinkJpaVendorAdapter adapter = new EclipseLinkJpaVendorAdapter(); |
| 85 | + adapter.setDatabase(Database.HSQL); |
| 86 | + adapter.setShowSql(true); |
| 87 | + return adapter; |
| 88 | + } |
| 89 | + |
| 90 | + @Bean |
| 91 | + public PlatformTransactionManager transactionManager() { |
| 92 | + JpaTransactionManager platformTransactionManager = new JpaTransactionManager(); |
| 93 | + platformTransactionManager.setEntityManagerFactory(entityManagerFactory); |
| 94 | + return platformTransactionManager; |
| 95 | + } |
| 96 | +} |
0 commit comments