code style improvements.
[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 static junit.framework.Assert.*;
19
20 import javax.persistence.EntityManager;
21
22 import org.dbunit.IDatabaseTester;
23 import org.dbunit.dataset.ITable;
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.wamblee.support.persistence.JpaBuilder.JpaUnitOfWork;
28
29 /**
30  * This class shows an example of how to test an entity using jpa.
31  */
32 public class MyEntityExampleTestBase {
33
34     // This is the magical object that does all the test setup.
35     private JpaTester jpaTester;
36
37     // The jpa tester initializes a lot for us....
38
39     // A JPA builder that provides a transaction scoped entity manager for us.
40     private JpaBuilder builder;
41
42     // The database tester for dbunit which is appropriately configured for our
43     // persistence unit.
44     private IDatabaseTester dbtester;
45
46     // Database utilities with some additional functionality for working with
47     // the databse
48     // such as dropping tables, cleaning tables, etc.
49     private DatabaseUtils dbutils;
50
51     @Before
52     public void setUp() throws Exception {
53
54         // First we create the JpaTester by telling us which persistence unit we
55         // are going to test
56         jpaTester = new JpaTester(new MyPersistenceUnit());
57         jpaTester.start();
58
59         // Retrieve some useful objects fromt he jpa tester. It also provides
60         // direct access to the datasource
61         // but we don't need it. We can use datbase utils if we want to execute
62         // straight JDBC calls.
63         builder = jpaTester.getJpaBuilder();
64         dbtester = jpaTester.getDbUtils().createDbTester(new MyTables());
65         dbutils = jpaTester.getDbUtils();
66     }
67
68     @After
69     public void tearDown() {
70         dbutils.stop();
71         jpaTester.stop();
72     }
73
74     @Test
75     public void testEntityPersistence() throws Exception {
76
77         // Use the JPA builder to create a transaction scoped entity manager for
78         // as and execute the
79         // unit of work.
80         builder.execute(new JpaUnitOfWork<Void>() {
81             public Void execute(EntityManager aEm) {
82                 MyEntity entity = new MyEntity("a", "b");
83                 aEm.persist(entity);
84                 return null;
85             }
86         });
87
88         // Verify one row is written (using Db unit)
89         ITable table = dbtester.getDataSet().getTable("XYZ_MYENTITY");
90         assertEquals(1, table.getRowCount());
91
92         assertEquals("a", table.getValue(0, "SLEUTELTJE"));
93         assertEquals("b", table.getValue(0, "VALUE"));
94
95         // For this simple test, it can also be done through DatabaseUtils
96         assertEquals(1, dbutils.getTableSize("XYZ_MYENTITY"));
97
98     }
99
100 }