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
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.
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.
17 * It then takes care of the following:
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
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>
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>
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.
41 public class JpaTester {
43 private PersistenceUnitDescription persistenceUnit;
45 private DataSource dataSource;
46 private DatabaseUtils dbUtils;
47 private JpaBuilder jpaBuilder;
48 private IDatabaseTester dbTester;
51 * Constructs the tester.
53 * @param aPersistenceUnit
54 * Persistence unit under test.
56 public JpaTester(PersistenceUnitDescription aPersistenceUnit) {
57 persistenceUnit = aPersistenceUnit;
61 * Starts the tester. This must be called prior to running the test.
65 public void start() throws Exception {
66 db = DatabaseBuilder.getDatabase();
67 dataSource = db.start();
69 dbUtils = new DatabaseUtils(dataSource, persistenceUnit.getTables());
71 dbUtils.dropTables(JpaCustomizerBuilder.getCustomizer().getJpaTables());
73 jpaBuilder = new JpaBuilder(dataSource, persistenceUnit);
76 // db tester should be created after Jpa builder because jpa builder
78 // tables that the tester looks at when it is initialized.
79 dbTester = dbUtils.createDbTester();
83 * Stops the tester. This must be called after the test.
86 if (jpaBuilder != null) {
94 public Database getDb() {
98 public DataSource getDataSource() {
102 public IDatabaseTester getDbTester() {
106 public DatabaseUtils getDbUtils() {
110 public JpaBuilder getJpaBuilder() {
114 public PersistenceUnitDescription getPersistenceUnit() {
115 return persistenceUnit;