code style improvements.
[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);
51         dbutils.dropTables(new MyTables());
52         dbutils.dropTables(JpaCustomizerBuilder.getCustomizer().getJpaTables());
53
54         builder = new JpaBuilder(db.getJdbcUrl(), db.getUsername(), db.getPassword(), persistenceUnit);
55         builder.start();
56
57         assertEquals(0, db.getActiveConnections());
58         dbtester = dbutils.createDbTester(new MyTables());
59     }
60
61     @After
62     public void tearDown() throws Exception {
63         dbtester.getConnection().close();
64         builder.stop();
65         dbutils.stop();
66         assertEquals(0, db.getActiveConnections());
67         db.stop();
68     }
69
70     @Test
71     public void testTablesCorrect() throws Exception {
72         String[] tables = dbutils.getTableNames(new MyTables());
73         assertEquals(1, tables.length);
74         assertEquals("XYZ_MYENTITY", tables[0]);
75     }
76
77     @Test
78     public void testDeleteTables() throws Exception {
79         String[] tables = dbutils.getTableNames(new MyTables());
80         assertEquals(1, tables.length);
81         assertEquals("XYZ_MYENTITY", tables[0]);
82
83         // Put some data in the database.
84         builder.execute(new JpaUnitOfWork<Void>() {
85             public Void execute(EntityManager aEm) {
86                 MyEntity entity = new MyEntity("a", "b");
87                 aEm.persist(entity);
88                 return null;
89             }
90         });
91
92         // Verify one row is written (using Db unit)
93         ITable table = dbtester.getDataSet().getTable("XYZ_MYENTITY");
94         assertEquals(1, table.getRowCount());
95
96         // Clean the database
97         dbutils.cleanDatabase(new MyTables());
98         table = dbtester.getDataSet().getTable("XYZ_MYENTITY");
99         assertEquals(0, table.getRowCount());
100
101         // Now drop the database
102         dbutils.dropTables(new MyTables());
103         dbutils.dropTables(JpaCustomizerBuilder.getCustomizer().getJpaTables());
104         tables = dbutils.getTableNames(new MyTables());
105         assertEquals(0, tables.length);
106     }
107
108 }