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