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