Merge pull request #4721 from amit2103/BAEL-4461-3

[BAEL-7620] - Fixed integration tests in spring-hibernate4 module by …
This commit is contained in:
Loredana Crusoveanu 2018-07-15 20:55:26 +03:00 committed by GitHub
commit 4fface3898
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 265 additions and 56 deletions

View File

@ -127,6 +127,12 @@
<version>${hsqldb.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
@ -153,6 +159,7 @@
<tomcat-dbcp.version>8.5.8</tomcat-dbcp.version>
<jta.version>1.1</jta.version>
<hsqldb.version>2.3.4</hsqldb.version>
<h2.version>1.4.193</h2.version>
<!-- various -->
<hibernate-validator.version>5.3.3.Final</hibernate-validator.version>

View File

@ -16,7 +16,7 @@ public class UserEager implements Serializable {
@Column(name = "USER_ID")
private Long userId;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "user")
@OneToMany(fetch = FetchType.EAGER)
private Set<OrderDetail> orderDetail = new HashSet();
public UserEager() {

View File

@ -16,7 +16,7 @@ public class UserLazy implements Serializable {
@Column(name = "USER_ID")
private Long userId;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
@OneToMany(fetch = FetchType.LAZY)
private Set<OrderDetail> orderDetail = new HashSet();
public UserLazy() {

View File

@ -20,7 +20,7 @@ public class FetchingAppView {
public Set<OrderDetail> lazyLoaded() {
final Session sessionLazy = HibernateUtil.getHibernateSession("lazy");
List<UserLazy> users = sessionLazy.createQuery("From UserLazy").list();
UserLazy userLazyLoaded = users.get(3);
UserLazy userLazyLoaded = users.get(0);
// since data is lazyloaded so data won't be initialized
return (userLazyLoaded.getOrderDetail());
}
@ -31,7 +31,7 @@ public class FetchingAppView {
// data should be loaded in the following line
// also note the queries generated
List<UserEager> user = sessionEager.createQuery("From UserEager").list();
UserEager userEagerLoaded = user.get(3);
UserEager userEagerLoaded = user.get(0);
return userEagerLoaded.getOrderDetail();
}

View File

@ -5,11 +5,6 @@ import static org.junit.Assert.assertNotNull;
import java.util.List;
import com.baeldung.persistence.model.Foo;
import com.baeldung.persistence.service.IBarAuditableService;
import com.baeldung.persistence.service.IFooAuditableService;
import com.baeldung.spring.PersistenceConfig;
import com.baeldung.persistence.model.Bar;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
@ -26,8 +21,14 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import com.baeldung.persistence.model.Bar;
import com.baeldung.persistence.model.Foo;
import com.baeldung.persistence.service.IBarAuditableService;
import com.baeldung.persistence.service.IFooAuditableService;
import com.baeldung.spring.config.PersistenceTestConfig;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
@ContextConfiguration(classes = { PersistenceTestConfig.class }, loader = AnnotationConfigContextLoader.class)
public class EnversFooBarAuditIntegrationTest {
private static Logger logger = LoggerFactory.getLogger(EnversFooBarAuditIntegrationTest.class);

View File

@ -7,10 +7,6 @@ import static org.junit.Assert.assertTrue;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import com.baeldung.persistence.model.Bar;
import com.baeldung.persistence.model.Bar.OPERATION;
import com.baeldung.persistence.service.IBarService;
import com.baeldung.spring.PersistenceConfig;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
@ -25,8 +21,13 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import com.baeldung.persistence.model.Bar;
import com.baeldung.persistence.model.Bar.OPERATION;
import com.baeldung.persistence.service.IBarService;
import com.baeldung.spring.config.PersistenceTestConfig;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
@ContextConfiguration(classes = { PersistenceTestConfig.class }, loader = AnnotationConfigContextLoader.class)
public class JPABarAuditIntegrationTest {
private static Logger logger = LoggerFactory.getLogger(JPABarAuditIntegrationTest.class);

View File

@ -6,9 +6,6 @@ import static org.junit.Assert.assertTrue;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import com.baeldung.persistence.model.Bar;
import com.baeldung.persistence.service.IBarService;
import com.baeldung.spring.PersistenceConfig;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
@ -24,8 +21,12 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import com.baeldung.persistence.model.Bar;
import com.baeldung.persistence.service.IBarService;
import com.baeldung.spring.config.PersistenceTestConfig;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
@ContextConfiguration(classes = { PersistenceTestConfig.class }, loader = AnnotationConfigContextLoader.class)
public class SpringDataJPABarAuditIntegrationTest {
private static Logger logger = LoggerFactory.getLogger(SpringDataJPABarAuditIntegrationTest.class);

View File

@ -7,9 +7,6 @@ import static org.junit.Assert.assertThat;
import java.util.List;
import com.baeldung.persistence.model.Foo;
import com.baeldung.persistence.service.IFooService;
import com.baeldung.spring.PersistenceConfig;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.ScrollMode;
@ -26,10 +23,13 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import com.baeldung.persistence.model.Foo;
import com.baeldung.persistence.service.IFooService;
import com.baeldung.spring.config.PersistenceTestConfig;
import com.google.common.collect.Lists;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
@ContextConfiguration(classes = { PersistenceTestConfig.class }, loader = AnnotationConfigContextLoader.class)
public class FooPaginationPersistenceIntegrationTest {
@Autowired

View File

@ -5,9 +5,6 @@ import static org.junit.Assert.assertNull;
import java.util.List;
import java.util.Set;
import com.baeldung.persistence.model.Bar;
import com.baeldung.persistence.model.Foo;
import com.baeldung.spring.PersistenceConfig;
import org.hibernate.Criteria;
import org.hibernate.NullPrecedence;
import org.hibernate.Query;
@ -23,8 +20,12 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import com.baeldung.persistence.model.Bar;
import com.baeldung.persistence.model.Foo;
import com.baeldung.spring.config.PersistenceTestConfig;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
@ContextConfiguration(classes = { PersistenceTestConfig.class }, loader = AnnotationConfigContextLoader.class)
@SuppressWarnings("unchecked")
public class FooSortingPersistenceIntegrationTest {

View File

@ -2,8 +2,6 @@ package com.baeldung.persistence.service;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
import com.baeldung.persistence.model.Foo;
import com.baeldung.spring.PersistenceConfig;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
@ -15,8 +13,11 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import com.baeldung.persistence.model.Foo;
import com.baeldung.spring.config.PersistenceTestConfig;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
@ContextConfiguration(classes = { PersistenceTestConfig.class }, loader = AnnotationConfigContextLoader.class)
public class FooServiceBasicPersistenceIntegrationTest {
@Autowired

View File

@ -2,8 +2,6 @@ package com.baeldung.persistence.service;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
import com.baeldung.persistence.model.Foo;
import com.baeldung.spring.PersistenceConfig;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -16,8 +14,11 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import com.baeldung.persistence.model.Foo;
import com.baeldung.spring.config.PersistenceTestConfig;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
@ContextConfiguration(classes = { PersistenceTestConfig.class }, loader = AnnotationConfigContextLoader.class)
public class FooServicePersistenceIntegrationTest {
@Autowired

View File

@ -26,9 +26,9 @@ import com.baeldung.spring.PersistenceConfig;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
public class FooStoredProceduresIntegrationTest {
public class FooStoredProceduresLiveTest {
private static final Logger LOGGER = LoggerFactory.getLogger(FooStoredProceduresIntegrationTest.class);
private static final Logger LOGGER = LoggerFactory.getLogger(FooStoredProceduresLiveTest.class);
@Autowired
private SessionFactory sessionFactory;

View File

@ -1,8 +1,5 @@
package com.baeldung.persistence.service;
import com.baeldung.persistence.model.Child;
import com.baeldung.persistence.model.Parent;
import com.baeldung.spring.PersistenceConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@ -11,8 +8,12 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import com.baeldung.persistence.model.Child;
import com.baeldung.persistence.model.Parent;
import com.baeldung.spring.config.PersistenceTestConfig;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
@ContextConfiguration(classes = { PersistenceTestConfig.class }, loader = AnnotationConfigContextLoader.class)
public class ParentServicePersistenceIntegrationTest {
@Autowired

View File

@ -0,0 +1,179 @@
package com.baeldung.spring.config;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;
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.EnableJpaAuditing;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.baeldung.persistence.dao.IBarAuditableDao;
import com.baeldung.persistence.dao.IBarDao;
import com.baeldung.persistence.dao.IFooAuditableDao;
import com.baeldung.persistence.dao.IFooDao;
import com.baeldung.persistence.dao.impl.BarAuditableDao;
import com.baeldung.persistence.dao.impl.BarDao;
import com.baeldung.persistence.dao.impl.BarJpaDao;
import com.baeldung.persistence.dao.impl.FooAuditableDao;
import com.baeldung.persistence.dao.impl.FooDao;
import com.baeldung.persistence.service.IBarAuditableService;
import com.baeldung.persistence.service.IBarService;
import com.baeldung.persistence.service.IFooAuditableService;
import com.baeldung.persistence.service.IFooService;
import com.baeldung.persistence.service.impl.BarAuditableService;
import com.baeldung.persistence.service.impl.BarJpaService;
import com.baeldung.persistence.service.impl.BarSpringDataJpaService;
import com.baeldung.persistence.service.impl.FooAuditableService;
import com.baeldung.persistence.service.impl.FooService;
import com.google.common.base.Preconditions;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = { "com.baeldung.persistence" }, transactionManagerRef = "jpaTransactionManager")
@EnableJpaAuditing
@PropertySource({ "classpath:persistence-h2.properties" })
@ComponentScan({ "com.baeldung.persistence" })
public class PersistenceTestConfig {
@Autowired
private Environment env;
public PersistenceTestConfig() {
super();
}
@Bean
public LocalSessionFactoryBean sessionFactory() {
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(restDataSource());
sessionFactory.setPackagesToScan(new String[] { "com.baeldung.persistence.model" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
final LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
emf.setDataSource(restDataSource());
emf.setPackagesToScan(new String[] { "com.baeldung.persistence.model" });
final JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
emf.setJpaVendorAdapter(vendorAdapter);
emf.setJpaProperties(hibernateProperties());
return emf;
}
@Bean
public DataSource restDataSource() {
final BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user")));
dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass")));
return dataSource;
}
@Bean
public PlatformTransactionManager hibernateTransactionManager() {
final HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory().getObject());
return transactionManager;
}
@Bean
public PlatformTransactionManager jpaTransactionManager() {
final JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
return transactionManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
@Bean
public IBarService barJpaService() {
return new BarJpaService();
}
@Bean
public IBarService barSpringDataJpaService() {
return new BarSpringDataJpaService();
}
@Bean
public IFooService fooHibernateService() {
return new FooService();
}
@Bean
public IBarAuditableService barHibernateAuditableService() {
return new BarAuditableService();
}
@Bean
public IFooAuditableService fooHibernateAuditableService() {
return new FooAuditableService();
}
@Bean
public IBarDao barJpaDao() {
return new BarJpaDao();
}
@Bean
public IBarDao barHibernateDao() {
return new BarDao();
}
@Bean
public IBarAuditableDao barHibernateAuditableDao() {
return new BarAuditableDao();
}
@Bean
public IFooDao fooHibernateDao() {
return new FooDao();
}
@Bean
public IFooAuditableDao fooHibernateAuditableDao() {
return new FooAuditableDao();
}
private final Properties hibernateProperties() {
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", "true");
// hibernateProperties.setProperty("hibernate.format_sql", "true");
// hibernateProperties.setProperty("hibernate.globally_quoted_identifiers", "true");
// Envers properties
hibernateProperties.setProperty("org.hibernate.envers.audit_table_suffix", env.getProperty("envers.audit_table_suffix"));
return hibernateProperties;
}
}

View File

@ -5,11 +5,12 @@
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">iamtheking</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">org.h2.Driver</property>
<property name="hibernate.connection.url">jdbc:h2:mem:testdb</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<mapping resource="com/baeldung/hibernate/criteria/model/Item.hbm.xml" />
</session-factory>

View File

@ -5,14 +5,15 @@
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">iamtheking</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">org.h2.Driver</property>
<property name="hibernate.connection.url">jdbc:h2:mem:testdb</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<mapping class="com.baeldung.hibernate.fetching.model.UserEager" />
<mapping class="com.baeldung.hibernate.fetching.model.OrderDetailEager" />
<mapping class="com.baeldung.hibernate.fetching.model.OrderDetail" />
</session-factory>
</hibernate-configuration>

View File

@ -5,14 +5,15 @@
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">iamtheking</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">org.h2.Driver</property>
<property name="hibernate.connection.url">jdbc:h2:mem:testdb</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<mapping class="com.baeldung.hibernate.fetching.model.UserLazy" />
<mapping class="com.baeldung.hibernate.fetching.model.OrderDetail" />
<mapping class="com.baeldung.hibernate.fetching.model.OrderDetail" />
</session-factory>
</hibernate-configuration>

View File

@ -0,0 +1,13 @@
# jdbc.X
jdbc.driverClassName=org.h2.Driver
jdbc.url=jdbc:h2:mem:test
jdbc.user=sa
jdbc.pass=
# hibernate.X
hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.show_sql=false
hibernate.hbm2ddl.auto=create-drop
# envers.X
envers.audit_table_suffix=_audit_log