Integration-Test for envers really generates the SQL
[hibernate4-maven-plugin] / src / it / hibernate4-maven-plugin-envers-sample / src / test / java / org / bitbucket / fbascheper / tutorial / envers / AbstractIntegrationTest.java
1 /**
2  * Copyright 2013 F.B.A. Scheper.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. *
15  */
16 package org.bitbucket.fbascheper.tutorial.envers;
17
18 import org.bitbucket.fbascheper.tutorial.envers.util.InitScriptRunner;
19 import org.junit.Before;
20 import org.junit.runner.RunWith;
21 import org.springframework.beans.factory.annotation.Value;
22 import org.springframework.test.context.ContextConfiguration;
23 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
24 import org.springframework.test.context.support.DelegatingSmartContextLoader;
25 import org.springframework.transaction.PlatformTransactionManager;
26 import org.springframework.transaction.support.TransactionTemplate;
27
28 import javax.annotation.Resource;
29 import javax.inject.Inject;
30 import javax.persistence.EntityManager;
31 import javax.persistence.PersistenceContext;
32 import javax.sql.DataSource;
33 import java.io.IOException;
34
35 import static org.hamcrest.CoreMatchers.notNullValue;
36 import static org.junit.Assert.assertThat;
37
38 /**
39  * Base class for the integration tests using Spring, Hibernate and JTA.
40  *
41  * @author Erik-Berndt Scheper
42  * @since 11-09-2013
43  */
44 @RunWith(SpringJUnit4ClassRunner.class)
45 @ContextConfiguration(
46         loader = DelegatingSmartContextLoader.class,
47         locations = {"classpath:/hhv-test-datamodel-domain-context.xml", "classpath:/spring-persistence-context.xml"})
48 public abstract class AbstractIntegrationTest {
49
50     @PersistenceContext
51     private EntityManager entityManager;
52
53     @Inject
54     private PlatformTransactionManager transactionManager;
55
56     @Value(value = "${hibernate.dialect}")
57     private String hibernateDialect;
58
59     @Resource(name = "dataSource")
60     private DataSource dataSource;
61
62     @Inject
63     private InitScriptRunner initScriptRunner;
64
65     @Before
66     public void initIntegrationTest() throws IOException {
67         assertThat(transactionManager, notNullValue());
68         assertThat(entityManager, notNullValue());
69         assertThat(hibernateDialect, notNullValue());
70         assertThat(dataSource, notNullValue());
71
72         // re-run the database creation script for each test
73         initScriptRunner.runScript();
74
75     }
76
77     protected EntityManager getEntityManager() {
78         return entityManager;
79     }
80
81     protected TransactionTemplate getTransactionTemplate() {
82
83         TransactionTemplate template = new TransactionTemplate(transactionManager);
84         return template;
85     }
86
87 }