e55f13413e8e84161c3d13b6c6f6b6409d1aa53c
[utils] /
1 package org.wamblee.support.persistence;
2
3 import static junit.framework.Assert.assertEquals;
4
5 import javax.persistence.EntityManager;
6 import javax.sql.DataSource;
7
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;
19
20 public class DatabaseUtilsTestBase {
21     private Database db;
22     private DataSource dataSource;
23     private PersistenceUnitDescription persistenceUnit;
24     private JpaBuilder builder;
25     private DatabaseUtils dbutils;
26     private IDatabaseTester dbtester;
27
28     @Before
29     public void setUp() throws Exception {
30         db = DatabaseBuilder.getDatabase();
31         dataSource = db.start();
32
33         persistenceUnit = new MyPersistenceUnit();
34
35         dbutils = new DatabaseUtils(dataSource, persistenceUnit.getTables());
36         dbutils.dropTables();
37         dbutils.dropTables(JpaCustomizerBuilder.getCustomizer().getJpaTables());
38
39         builder = new JpaBuilder(dataSource, persistenceUnit);
40         builder.start();
41         
42         dbtester = dbutils.createDbTester();
43     }
44
45     @After
46     public void tearDown() {
47         builder.stop();
48         db.stop();
49     }
50
51     @Test
52     public void testTablesCorrect() throws Exception {
53         String[] tables = dbutils.getTableNames();
54         assertEquals(1, tables.length);
55         assertEquals("XYZ_MYENTITY", tables[0]);
56     }
57
58     @Test
59     public void testDeleteTables() throws Exception {
60         String[] tables = dbutils.getTableNames();
61         assertEquals(1, tables.length);
62         assertEquals("XYZ_MYENTITY", tables[0]);
63
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");
68                 em.persist(entity);
69                 return null;
70             }
71         });
72
73         // Verify one row is written (using Db unit)
74         ITable table = dbtester.getDataSet().getTable("XYZ_MYENTITY");
75         assertEquals(1, table.getRowCount());
76
77         // Clean the database
78         dbutils.cleanDatabase();
79         table = dbtester.getDataSet().getTable("XYZ_MYENTITY");
80         assertEquals(0, table.getRowCount());
81
82         // Now drop the database
83         dbutils.dropTables();
84         dbutils.dropTables(JpaCustomizerBuilder.getCustomizer().getJpaTables());
85         tables = dbutils.getTableNames();
86         assertEquals(0, tables.length);
87     }
88
89 }