checkstyle
[utils] / test / enterprise / src / test / java / org / wamblee / support / persistence / MyEntityExampleTestBase.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 javax.persistence.EntityManager;
19 import javax.persistence.Persistence;
20 import javax.sql.DataSource;
21
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;
34
35 import static junit.framework.Assert.*;
36
37 /**
38  * This class shows an example of how to test an entity using jpa.
39  */
40 public class MyEntityExampleTestBase {
41
42     // This is the magical object that does all the test setup.
43     private JpaTester jpaTester;
44
45     // The jpa tester initializes a lot for us....
46
47     // A JPA builder that provides a transaction scoped entity manager for us.
48     private JpaBuilder builder;
49
50     // The database tester for dbunit which is appropriately configured for our
51     // persistence unit.
52     private IDatabaseTester dbtester;
53
54     // Database utilities with some additional functionality for working with
55     // the databse
56     // such as dropping tables, cleaning tables, etc.
57     private DatabaseUtils dbutils;
58
59     @Before
60     public void setUp() throws Exception {
61
62         // First we create the JpaTester by telling us which persistence unit we
63         // are going to test
64         jpaTester = new JpaTester(new MyPersistenceUnit());
65         jpaTester.start();
66
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();
74     }
75
76     @After
77     public void tearDown() {
78         jpaTester.stop();
79     }
80
81     @Test
82     public void testEntityPersistence() throws Exception {
83
84         // Use the JPA builder to create a transaction scoped entity manager for
85         // as and execute the
86         // unit of work.
87         builder.execute(new JpaUnitOfWork<Void>() {
88             public Void execute(EntityManager aEm) {
89                 MyEntity entity = new MyEntity("a", "b");
90                 aEm.persist(entity);
91                 return null;
92             }
93         });
94
95         // Verify one row is written (using Db unit)
96         ITable table = dbtester.getDataSet().getTable("XYZ_MYENTITY");
97         assertEquals(1, table.getRowCount());
98
99         assertEquals("a", table.getValue(0, "SLEUTELTJE"));
100         assertEquals("b", table.getValue(0, "VALUE"));
101
102         // For this simple test, it can also be done through DatabaseUtils
103         assertEquals(1, dbutils.getTableSize("XYZ_MYENTITY"));
104
105     }
106
107 }