(no commit message)
[utils] / test / enterprise / src / main / java / org / wamblee / support / persistence / JpaTester.java
1 package org.wamblee.support.persistence;
2
3 import javax.sql.DataSource;
4
5 import org.dbunit.IDatabaseTester;
6
7 /**
8  * This class is the entry point for JPA tests. Test code should construct a JpaTester in the 
9  * <code>@Before</code> method and call {@link #start()} on it in that method. Also, test code should 
10  * call {@link #stop()} on it in the <code>@After</code> method.  
11  * 
12  * This class is constructed with a description of the persistence unit to be tested. The principle is that
13  * an existing <code>persistence.xml</code> can be tested without change in unit test code. 
14  *  
15  * It then takes care of the following: 
16  * <ul> 
17  *   <li>  Creating an inmemory database for testing (default) or connecting to an external database. 
18  *        See {@link DatabaseBuilder} for more information on how a databse is obtained. 
19  *   </li>
20  *   <li> Drop all database tables that are related to the persistence unit under test, including JPA provider
21  *      specific tables. 
22  *   </li>
23  *   <li>  Creating a datasource for the database and make the datasource available through JNDI. 
24  *   </li>
25  *   <li> Creating the entity manager factory for JPA and configuring it in such a way that schema creation 
26  *      happens. (Typically, schema creation will be disabled in the persistence.xml but this utility enables it 
27  *      for unit test). 
28  *   </li>
29  *   <li> Creating a DBUnit database tester which is appropriately configured for the persistence unit under test. 
30  *   </li>
31  * </ul>
32  * 
33  * The main entry point for all this functionality is the {@link PersistenceUnitDescription} which describes the 
34  * persistence unit and must be provided at construction of the <code>JpaTester</code>
35  * 
36  * NOTE: Persistence XML files should be explicitly configured with the classes that are part of the persistence unit
37  * since scanning of classes does not work correctly in a unit test environment. This is currently the only limitation.
38  */
39 public class JpaTester {
40
41         private PersistenceUnitDescription persistenceUnit;
42         private Database db;
43         private DataSource dataSource;
44         private DatabaseUtils dbUtils;
45         private JpaBuilder jpaBuilder;
46         private IDatabaseTester dbTester;
47
48         /**
49          * Constructs the tester.
50          * @param aPersistenceUnit Persistence unit under test. 
51          */
52         public JpaTester(PersistenceUnitDescription aPersistenceUnit) {
53                 persistenceUnit = aPersistenceUnit;
54         }
55
56         /**
57          * Starts the tester. This must be called prior to running the test. 
58          * @throws Exception
59          */
60         public void start() throws Exception {
61                 db = DatabaseBuilder.getDatabase();
62                 dataSource = db.start();
63
64                 dbUtils = new DatabaseUtils(dataSource, persistenceUnit.getTables());
65                 dbUtils.dropTables();
66                 dbUtils.dropTables(JpaCustomizerBuilder.getCustomizer().getJpaTables());
67
68                 jpaBuilder = new JpaBuilder(dataSource, persistenceUnit);
69                 jpaBuilder.start();
70
71                 // db tester should be created after Jpa builder because jpa builder
72                 // creates the
73                 // tables that the tester looks at when it is initialized.
74                 dbTester = dbUtils.createDbTester();
75         }
76
77         /**
78          * Stops the tester. This must be called after the test. 
79          */
80         public void stop() {
81                 if (jpaBuilder != null) {
82                         jpaBuilder.stop();
83                 }
84                 if (db != null) {
85                         db.stop();
86                 }
87         }
88
89         public Database getDb() {
90                 return db;
91         }
92
93         public DataSource getDataSource() {
94                 return dataSource;
95         }
96
97         public IDatabaseTester getDbTester() {
98                 return dbTester;
99         }
100
101         public DatabaseUtils getDbUtils() {
102                 return dbUtils;
103         }
104
105         public JpaBuilder getJpaBuilder() {
106                 return jpaBuilder;
107         }
108
109         public PersistenceUnitDescription getPersistenceUnit() {
110                 return persistenceUnit;
111         }
112
113 }