2 * Copyright 2005-2010 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.test.persistence;
18 import static junit.framework.Assert.assertEquals;
20 import javax.persistence.EntityManager;
21 import javax.sql.DataSource;
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.test.persistence.Database;
29 import org.wamblee.test.persistence.DatabaseBuilder;
30 import org.wamblee.test.persistence.DatabaseUtils;
31 import org.wamblee.test.persistence.JpaBuilder;
32 import org.wamblee.test.persistence.JpaCustomizerBuilder;
33 import org.wamblee.test.persistence.PersistenceUnitDescription;
34 import org.wamblee.test.persistence.JpaBuilder.JpaUnitOfWork;
36 public class DatabaseUtilsTestBase {
38 private DataSource dataSource;
39 private PersistenceUnitDescription persistenceUnit;
40 private JpaBuilder builder;
41 private DatabaseUtils dbutils;
42 private IDatabaseTester dbtester;
45 public void setUp() throws Exception {
46 db = DatabaseBuilder.getDatabase();
47 dataSource = db.start();
49 persistenceUnit = new MyPersistenceUnit();
51 dbutils = new DatabaseUtils(dataSource);
52 dbutils.dropTables(new MyTables());
53 dbutils.dropTables(JpaCustomizerBuilder.getCustomizer().getJpaTables());
55 builder = new JpaBuilder(db.getJdbcUrl(), db.getUsername(), db.getPassword(), persistenceUnit);
58 assertEquals(0, db.getActiveConnections());
59 dbtester = dbutils.createDbTester(new MyTables());
63 public void tearDown() throws Exception {
64 dbtester.getConnection().close();
67 assertEquals(0, db.getActiveConnections());
72 public void testTablesCorrect() throws Exception {
73 String[] tables = dbutils.getTableNames(new MyTables());
74 assertEquals(1, tables.length);
75 assertEquals("XYZ_MYENTITY", tables[0]);
79 public void testDeleteTables() throws Exception {
80 String[] tables = dbutils.getTableNames(new MyTables());
81 assertEquals(1, tables.length);
82 assertEquals("XYZ_MYENTITY", tables[0]);
84 // Put some data in the database.
85 builder.execute(new JpaUnitOfWork<Void>() {
86 public Void execute(EntityManager aEm) {
87 MyEntity entity = new MyEntity("a", "b");
93 // Verify one row is written (using Db unit)
94 ITable table = dbtester.getDataSet().getTable("XYZ_MYENTITY");
95 assertEquals(1, table.getRowCount());
98 dbutils.cleanDatabase(new MyTables());
99 table = dbtester.getDataSet().getTable("XYZ_MYENTITY");
100 assertEquals(0, table.getRowCount());
102 // Now drop the database
103 dbutils.dropTables(new MyTables());
104 dbutils.dropTables(JpaCustomizerBuilder.getCustomizer().getJpaTables());
105 tables = dbutils.getTableNames(new MyTables());
106 assertEquals(0, tables.length);