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.support.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.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;
35 public class DatabaseUtilsTestBase {
37 private DataSource dataSource;
38 private PersistenceUnitDescription persistenceUnit;
39 private JpaBuilder builder;
40 private DatabaseUtils dbutils;
41 private IDatabaseTester dbtester;
44 public void setUp() throws Exception {
45 db = DatabaseBuilder.getDatabase();
46 dataSource = db.start();
48 persistenceUnit = new MyPersistenceUnit();
50 dbutils = new DatabaseUtils(dataSource, persistenceUnit.getTables());
52 dbutils.dropTables(JpaCustomizerBuilder.getCustomizer().getJpaTables());
54 builder = new JpaBuilder(dataSource, persistenceUnit);
57 dbtester = dbutils.createDbTester();
61 public void tearDown() {
67 public void testTablesCorrect() throws Exception {
68 String[] tables = dbutils.getTableNames();
69 assertEquals(1, tables.length);
70 assertEquals("XYZ_MYENTITY", tables[0]);
74 public void testDeleteTables() throws Exception {
75 String[] tables = dbutils.getTableNames();
76 assertEquals(1, tables.length);
77 assertEquals("XYZ_MYENTITY", tables[0]);
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");
88 // Verify one row is written (using Db unit)
89 ITable table = dbtester.getDataSet().getTable("XYZ_MYENTITY");
90 assertEquals(1, table.getRowCount());
93 dbutils.cleanDatabase();
94 table = dbtester.getDataSet().getTable("XYZ_MYENTITY");
95 assertEquals(0, table.getRowCount());
97 // Now drop the database
99 dbutils.dropTables(JpaCustomizerBuilder.getCustomizer().getJpaTables());
100 tables = dbutils.getTableNames();
101 assertEquals(0, tables.length);