1 package org.wamblee.support.persistence;
3 import javax.sql.DataSource;
5 import org.dbunit.IDatabaseTester;
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.
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.
15 * It then takes care of the following:
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.
20 * <li> Drop all database tables that are related to the persistence unit under test, including JPA provider
23 * <li> Creating a datasource for the database and make the datasource available through JNDI.
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
29 * <li> Creating a DBUnit database tester which is appropriately configured for the persistence unit under test.
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>
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.
39 public class JpaTester {
41 private PersistenceUnitDescription persistenceUnit;
43 private DataSource dataSource;
44 private DatabaseUtils dbUtils;
45 private JpaBuilder jpaBuilder;
46 private IDatabaseTester dbTester;
49 * Constructs the tester.
50 * @param aPersistenceUnit Persistence unit under test.
52 public JpaTester(PersistenceUnitDescription aPersistenceUnit) {
53 persistenceUnit = aPersistenceUnit;
57 * Starts the tester. This must be called prior to running the test.
60 public void start() throws Exception {
61 db = DatabaseBuilder.getDatabase();
62 dataSource = db.start();
64 dbUtils = new DatabaseUtils(dataSource, persistenceUnit.getTables());
66 dbUtils.dropTables(JpaCustomizerBuilder.getCustomizer().getJpaTables());
68 jpaBuilder = new JpaBuilder(dataSource, persistenceUnit);
71 // db tester should be created after Jpa builder because jpa builder
73 // tables that the tester looks at when it is initialized.
74 dbTester = dbUtils.createDbTester();
78 * Stops the tester. This must be called after the test.
81 if (jpaBuilder != null) {
89 public Database getDb() {
93 public DataSource getDataSource() {
97 public IDatabaseTester getDbTester() {
101 public DatabaseUtils getDbUtils() {
105 public JpaBuilder getJpaBuilder() {
109 public PersistenceUnitDescription getPersistenceUnit() {
110 return persistenceUnit;