f6da97e80a108f55089240054a89ede85cdf4f18
[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.getDbTester();
65         dbutils = jpaTester.getDbUtils();
66         dbutils.start();
67     }
68
69     @After
70     public void tearDown() {
71         dbutils.stop();
72         jpaTester.stop();
73     }
74
75     @Test
76     public void testEntityPersistence() throws Exception {
77
78         // Use the JPA builder to create a transaction scoped entity manager for
79         // as and execute the
80         // unit of work.
81         builder.execute(new JpaUnitOfWork<Void>() {
82             public Void execute(EntityManager aEm) {
83                 MyEntity entity = new MyEntity("a", "b");
84                 aEm.persist(entity);
85                 return null;
86             }
87         });
88
89         // Verify one row is written (using Db unit)
90         ITable table = dbtester.getDataSet().getTable("XYZ_MYENTITY");
91         assertEquals(1, table.getRowCount());
92
93         assertEquals("a", table.getValue(0, "SLEUTELTJE"));
94         assertEquals("b", table.getValue(0, "VALUE"));
95
96         // For this simple test, it can also be done through DatabaseUtils
97         assertEquals(1, dbutils.getTableSize("XYZ_MYENTITY"));
98
99     }
100
101 }