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 javax.persistence.EntityManager;
19 import javax.persistence.Persistence;
20 import javax.sql.DataSource;
22 import org.dbunit.DataSourceDatabaseTester;
23 import org.dbunit.DatabaseTestCase;
24 import org.dbunit.IDatabaseTester;
25 import org.dbunit.dataset.ITable;
26 import org.dbunit.dataset.filter.ITableFilterSimple;
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.wamblee.support.persistence.DatabaseUtils;
31 import org.wamblee.support.persistence.JpaBuilder;
32 import org.wamblee.support.persistence.JpaTester;
33 import org.wamblee.support.persistence.JpaBuilder.JpaUnitOfWork;
35 import static junit.framework.Assert.*;
38 * This class shows an example of how to test an entity using jpa.
40 public class MyEntityExampleTestBase {
42 // This is the magical object that does all the test setup.
43 private JpaTester jpaTester;
45 // The jpa tester initializes a lot for us....
47 // A JPA builder that provides a transaction scoped entity manager for us.
48 private JpaBuilder builder;
50 // The database tester for dbunit which is appropriately configured for our
52 private IDatabaseTester dbtester;
54 // Database utilities with some additional functionality for working with
56 // such as dropping tables, cleaning tables, etc.
57 private DatabaseUtils dbutils;
60 public void setUp() throws Exception {
62 // First we create the JpaTester by telling us which persistence unit we
64 jpaTester = new JpaTester(new MyPersistenceUnit());
67 // Retrieve some useful objects fromt he jpa tester. It also provides
68 // direct access to the datasource
69 // but we don't need it. We can use datbase utils if we want to execute
70 // straight JDBC calls.
71 builder = jpaTester.getJpaBuilder();
72 dbtester = jpaTester.getDbTester();
73 dbutils = jpaTester.getDbUtils();
77 public void tearDown() {
82 public void testEntityPersistence() throws Exception {
84 // Use the JPA builder to create a transaction scoped entity manager for
87 builder.execute(new JpaUnitOfWork<Void>() {
88 public Void execute(EntityManager aEm) {
89 MyEntity entity = new MyEntity("a", "b");
95 // Verify one row is written (using Db unit)
96 ITable table = dbtester.getDataSet().getTable("XYZ_MYENTITY");
97 assertEquals(1, table.getRowCount());
99 assertEquals("a", table.getValue(0, "SLEUTELTJE"));
100 assertEquals("b", table.getValue(0, "VALUE"));
102 // For this simple test, it can also be done through DatabaseUtils
103 assertEquals(1, dbutils.getTableSize("XYZ_MYENTITY"));