9012dbd575c5d0bcb193054ba39eb98e2b982d0c
[utils] / test / enterprise / src / test / java / org / wamblee / support / persistence / DatabaseUtilsTestBase.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */ 
16 package org.wamblee.support.persistence;
17
18 import static junit.framework.Assert.assertEquals;
19
20 import javax.persistence.EntityManager;
21 import javax.sql.DataSource;
22
23 import org.dbunit.IDatabaseTester;
24 import org.dbunit.dataset.ITable;
25 import org.junit.After;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.wamblee.support.persistence.Database;
29 import org.wamblee.support.persistence.DatabaseBuilder;
30 import org.wamblee.support.persistence.DatabaseUtils;
31 import org.wamblee.support.persistence.JpaBuilder;
32 import org.wamblee.support.persistence.PersistenceUnitDescription;
33 import org.wamblee.support.persistence.JpaBuilder.JpaUnitOfWork;
34
35 public class DatabaseUtilsTestBase {
36     private Database db;
37     private DataSource dataSource;
38     private PersistenceUnitDescription persistenceUnit;
39     private JpaBuilder builder;
40     private DatabaseUtils dbutils;
41     private IDatabaseTester dbtester;
42
43     @Before
44     public void setUp() throws Exception {
45         db = DatabaseBuilder.getDatabase();
46         dataSource = db.start();
47
48         persistenceUnit = new MyPersistenceUnit();
49
50         dbutils = new DatabaseUtils(dataSource, persistenceUnit.getTables());
51         dbutils.dropTables();
52         dbutils.dropTables(JpaCustomizerBuilder.getCustomizer().getJpaTables());
53
54         builder = new JpaBuilder(dataSource, persistenceUnit);
55         builder.start();
56
57         dbtester = dbutils.createDbTester();
58     }
59
60     @After
61     public void tearDown() {
62         builder.stop();
63         db.stop();
64     }
65
66     @Test
67     public void testTablesCorrect() throws Exception {
68         String[] tables = dbutils.getTableNames();
69         assertEquals(1, tables.length);
70         assertEquals("XYZ_MYENTITY", tables[0]);
71     }
72
73     @Test
74     public void testDeleteTables() throws Exception {
75         String[] tables = dbutils.getTableNames();
76         assertEquals(1, tables.length);
77         assertEquals("XYZ_MYENTITY", tables[0]);
78
79         // Put some data in the database.
80         builder.execute(new JpaUnitOfWork<Void>() {
81             public Void execute(EntityManager aEm) {
82                 MyEntity entity = new MyEntity("a", "b");
83                 aEm.persist(entity);
84                 return null;
85             }
86         });
87
88         // Verify one row is written (using Db unit)
89         ITable table = dbtester.getDataSet().getTable("XYZ_MYENTITY");
90         assertEquals(1, table.getRowCount());
91
92         // Clean the database
93         dbutils.cleanDatabase();
94         table = dbtester.getDataSet().getTable("XYZ_MYENTITY");
95         assertEquals(0, table.getRowCount());
96
97         // Now drop the database
98         dbutils.dropTables();
99         dbutils.dropTables(JpaCustomizerBuilder.getCustomizer().getJpaTables());
100         tables = dbutils.getTableNames();
101         assertEquals(0, tables.length);
102     }
103
104 }