in memory test (#1476)

* in memory test

* update dependencies

* rename db

* remove create db

* update version
This commit is contained in:
lor6 2017-03-25 23:12:17 +02:00 committed by KevinGilmore
parent de29cd5632
commit 052c149b0e
7 changed files with 176 additions and 4 deletions

View File

@ -196,14 +196,14 @@
<properties> <properties>
<!-- Spring --> <!-- Spring -->
<org.springframework.version>4.3.4.RELEASE</org.springframework.version> <org.springframework.version>4.3.7.RELEASE</org.springframework.version>
<javassist.version>3.21.0-GA</javassist.version> <javassist.version>3.21.0-GA</javassist.version>
<!-- persistence --> <!-- persistence -->
<hibernate.version>5.2.5.Final</hibernate.version> <hibernate.version>5.2.9.Final</hibernate.version>
<mysql-connector-java.version>5.1.40</mysql-connector-java.version> <mysql-connector-java.version>5.1.40</mysql-connector-java.version>
<spring-data-jpa.version>1.10.5.RELEASE</spring-data-jpa.version> <spring-data-jpa.version>1.11.1.RELEASE</spring-data-jpa.version>
<h2.version>1.4.193</h2.version> <h2.version>1.4.194</h2.version>
<!-- web --> <!-- web -->
<javax.servlet.jstl.version>1.2</javax.servlet.jstl.version> <javax.servlet.jstl.version>1.2</javax.servlet.jstl.version>

View File

@ -0,0 +1,68 @@
package org.baeldung.config;
import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
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
@EnableJpaRepositories(basePackages = "org.baeldung.persistence.dao")
@PropertySource("persistence-student.properties")
@EnableTransactionManagement
public class StudentJpaConfig {
@Autowired
private Environment env;
@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 LocalContainerEntityManagerFactoryBean entityManagerFactory() {
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });
em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
em.setJpaProperties(additionalProperties());
return em;
}
@Bean
JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory);
return transactionManager;
}
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"));
hibernateProperties.setProperty("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
hibernateProperties.setProperty("hibernate.cache.use_second_level_cache", env.getProperty("hibernate.cache.use_second_level_cache"));
hibernateProperties.setProperty("hibernate.cache.use_query_cache", env.getProperty("hibernate.cache.use_query_cache"));
return hibernateProperties;
}
}

View File

@ -0,0 +1,8 @@
package org.baeldung.persistence.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import org.baeldung.persistence.model.Student;
public interface StudentRepository extends JpaRepository<Student, Long> {
}

View File

@ -0,0 +1,38 @@
package org.baeldung.persistence.model;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Student {
@Id
private long id;
private String name;
public Student() {
}
public Student(long id, String name) {
super();
this.id = id;
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,11 @@
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/myDb
jdbc.user=tutorialuser
jdbc.pass=tutorialpass
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=create-drop
hibernate.cache.use_second_level_cache=false
hibernate.cache.use_query_cache=false

View File

@ -0,0 +1,38 @@
package org.baeldung.persistence.repository;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import org.springframework.transaction.annotation.Transactional;
import org.baeldung.config.StudentJpaConfig;
import org.baeldung.persistence.model.Student;
import org.baeldung.persistence.dao.StudentRepository;
import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { StudentJpaConfig.class }, loader = AnnotationConfigContextLoader.class)
@Transactional
public class InMemoryDBTest {
@Resource
private StudentRepository studentRepository;
private static final long ID = 1;
private static final String NAME="john";
@Test
public void givenStudent_whenSave_thenGetOk(){
Student student = new Student(ID, NAME);
studentRepository.save(student);
Student student2 = studentRepository.findOne(ID);
assertEquals("name incorrect", NAME, student2.getName());
}
}

View File

@ -0,0 +1,9 @@
jdbc.driverClassName=org.h2.Driver
jdbc.url=jdbc:h2:mem:myDb;DB_CLOSE_DELAY=-1
hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=create-drop
hibernate.cache.use_second_level_cache=false
hibernate.cache.use_query_cache=false