001: package com.bm.testsuite;
002:
003: import java.util.ArrayList;
004: import java.util.List;
005:
006: import javax.persistence.EntityManager;
007: import javax.persistence.EntityTransaction;
008: import javax.persistence.Query;
009: import javax.sql.DataSource;
010:
011: import com.bm.cfg.Ejb3UnitCfg;
012: import com.bm.jndi.Ejb3UnitJndiBinder;
013: import com.bm.testsuite.dataloader.EntityInitialDataSet;
014: import com.bm.testsuite.dataloader.InitialDataSet;
015: import com.bm.utils.BasicDataSource;
016:
017: /**
018: * Supports entity manager and flat file db injection for pojos.
019: *
020: * @author Daniel Wiese
021: * @param <T> -
022: * the type of the service
023: * @since 12.11.2005
024: */
025: public abstract class PoJoFixture extends BaseTest {
026:
027: private static final org.apache.log4j.Logger log = org.apache.log4j.Logger
028: .getLogger(PoJoFixture.class);
029:
030: private final Ejb3UnitJndiBinder jndiBinder;
031:
032: private InitialDataSet[] initalDataSet = null;
033:
034: private final Ejb3UnitCfg configuration;
035:
036: private EntityManager entityManager;
037:
038: /**
039: * Constructor.
040: *
041: * @param usedEntityBeans -
042: * the used entity bens
043: */
044: public PoJoFixture(Class[] usedEntityBeans) {
045: super ();
046: this .jndiBinder = new Ejb3UnitJndiBinder(usedEntityBeans);
047: final List<Class<? extends Object>> usedEntityBeansC = new ArrayList<Class<? extends Object>>();
048:
049: for (Class<? extends Object> akt : usedEntityBeans) {
050: usedEntityBeansC.add(akt);
051: }
052: Ejb3UnitCfg.addEntytiesToTest(usedEntityBeansC);
053: this .configuration = Ejb3UnitCfg.getConfiguration();
054:
055: }
056:
057: /**
058: * Constructor.
059: *
060: * @param usedEntityBeans -
061: * the used entity beans
062: * @param initialData -
063: * the inital data to create in the db
064: */
065: public PoJoFixture(Class[] usedEntityBeans,
066: InitialDataSet... initialData) {
067: this (usedEntityBeans);
068: this .initalDataSet = initialData;
069: }
070:
071: /**
072: * @author Daniel Wiese
073: * @since 16.10.2005
074: * @see junit.framework.TestCase#setUp()
075: */
076: @Override
077: protected void setUp() throws Exception {
078: super .setUp();
079: this .jndiBinder.bind();
080: log.info("Creating entity manager instance for POJO test");
081: entityManager = this .configuration.getEntityManagerFactory()
082: .createEntityManager();
083:
084: if (this .initalDataSet != null) {
085: EntityManager em = this .getEntityManager();
086:
087: for (InitialDataSet current : this .initalDataSet) {
088: // insert entity manager
089: if (current instanceof EntityInitialDataSet) {
090: EntityInitialDataSet curentEntDs = (EntityInitialDataSet) current;
091: curentEntDs.setEntityManager(em);
092: EntityTransaction tx = em.getTransaction();
093: tx.begin();
094: current.create();
095: tx.commit();
096: } else {
097: current.create();
098: }
099: }
100: }
101: }
102:
103: /**
104: * Find all rows of the given class in the database.
105: *
106: * @param <T>
107: * the tyme of the persistent object
108: * @param clazz
109: * the class of of the persistent object
110: * @return all DB instances
111: */
112: @SuppressWarnings("unchecked")
113: protected <T> List<T> findAll(Class<T> clazz) {
114: EntityManager manager = this .getEntityManager();
115: Query query = manager.createQuery("select c from "
116: + clazz.getName() + " c");
117: return (List<T>) query.getResultList();
118: }
119:
120: /**
121: * Deletes all rows of the given class in the database.
122: *
123: * @param <T>
124: * the tyme of the persistent object
125: * @param clazz
126: * the class of of the persistent object
127: * @return all DB instances
128: */
129: @SuppressWarnings("unchecked")
130: protected <T> void deleteAll(Class<T> clazz) {
131: EntityManager manager = this .getEntityManager();
132: EntityTransaction tx = manager.getTransaction();
133: tx.begin();
134: Query query = manager.createQuery("delete from "
135: + clazz.getName());
136: query.executeUpdate();
137: tx.commit();
138: }
139:
140: /**
141: * Persists all objects in the database.
142: *
143: * @param <T>
144: * the tyme of the persistent object
145: * @param complexObjectGraph
146: * th egraph to persist
147: * @return the persisted objects
148: */
149: protected <T> List<T> persist(List<T> complexObjectGraph) {
150: EntityManager manager = this .getEntityManager();
151: EntityTransaction tx = manager.getTransaction();
152: tx.begin();
153: final List<T> persited = new ArrayList<T>();
154: try {
155: for (T order : complexObjectGraph) {
156: persited.add(manager.merge(order));
157: }
158: } finally {
159: tx.commit();
160: }
161:
162: return persited;
163: }
164:
165: /**
166: * @author Daniel Wiese
167: * @since 16.10.2005
168: * @see junit.framework.TestCase#tearDown()
169: */
170: @Override
171: protected void tearDown() throws Exception {
172: if (this .initalDataSet != null) {
173: for (InitialDataSet current : this .initalDataSet) {
174: current.cleanup(entityManager);
175: }
176: }
177: this .entityManager = null;
178: super .tearDown();
179:
180: }
181:
182: /**
183: * Liefert die datasource.
184: *
185: * @return die data source.
186: */
187: public DataSource getDataSource() {
188: return new BasicDataSource(this .configuration);
189: }
190:
191: /**
192: * Returns a isntance of a EntityManager.
193: *
194: * @author Daniel Wiese
195: * @since 12.11.2005
196: * @return - a instance of an entity manager
197: */
198: public EntityManager getEntityManager() {
199: return this.entityManager;
200: }
201:
202: }
|