1 package org.wamblee.support.persistence;
3 import static junit.framework.Assert.assertEquals;
5 import javax.persistence.EntityManager;
6 import javax.sql.DataSource;
8 import org.dbunit.IDatabaseTester;
9 import org.dbunit.dataset.ITable;
10 import org.junit.After;
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.wamblee.support.persistence.Database;
14 import org.wamblee.support.persistence.DatabaseBuilder;
15 import org.wamblee.support.persistence.DatabaseUtils;
16 import org.wamblee.support.persistence.JpaBuilder;
17 import org.wamblee.support.persistence.PersistenceUnitDescription;
18 import org.wamblee.support.persistence.JpaBuilder.JpaUnitOfWork;
20 public class DatabaseUtilsTestBase {
22 private DataSource dataSource;
23 private PersistenceUnitDescription persistenceUnit;
24 private JpaBuilder builder;
25 private DatabaseUtils dbutils;
26 private IDatabaseTester dbtester;
29 public void setUp() throws Exception {
30 db = DatabaseBuilder.getDatabase();
31 dataSource = db.start();
33 persistenceUnit = new MyPersistenceUnit();
35 dbutils = new DatabaseUtils(dataSource, persistenceUnit.getTables());
37 dbutils.dropTables(JpaCustomizerBuilder.getCustomizer().getJpaTables());
39 builder = new JpaBuilder(dataSource, persistenceUnit);
42 dbtester = dbutils.createDbTester();
46 public void tearDown() {
52 public void testTablesCorrect() throws Exception {
53 String[] tables = dbutils.getTableNames();
54 assertEquals(1, tables.length);
55 assertEquals("XYZ_MYENTITY", tables[0]);
59 public void testDeleteTables() throws Exception {
60 String[] tables = dbutils.getTableNames();
61 assertEquals(1, tables.length);
62 assertEquals("XYZ_MYENTITY", tables[0]);
64 // Put some data in the database.
65 builder.execute(new JpaUnitOfWork<Void>() {
66 public Void execute(EntityManager em) {
67 MyEntity entity = new MyEntity("a", "b");
73 // Verify one row is written (using Db unit)
74 ITable table = dbtester.getDataSet().getTable("XYZ_MYENTITY");
75 assertEquals(1, table.getRowCount());
78 dbutils.cleanDatabase();
79 table = dbtester.getDataSet().getTable("XYZ_MYENTITY");
80 assertEquals(0, table.getRowCount());
82 // Now drop the database
84 dbutils.dropTables(JpaCustomizerBuilder.getCustomizer().getJpaTables());
85 tables = dbutils.getTableNames();
86 assertEquals(0, tables.length);