001: package com.bm.testsuite;
002:
003: import javax.persistence.EntityManager;
004: import javax.persistence.EntityTransaction;
005:
006: import com.bm.creators.SessionBeanFactory;
007: import com.bm.introspectors.AbstractIntrospector;
008: import com.bm.introspectors.SessionBeanIntrospector;
009: import com.bm.jndi.Ejb3UnitJndiBinder;
010: import com.bm.testsuite.dataloader.EntityInitialDataSet;
011: import com.bm.testsuite.dataloader.InitialDataSet;
012:
013: /**
014: * This is the base class for all JUnit test - testing stateless/statefull
015: * SessionBeans (EJB 3.0 Specification conform).
016: *
017: * @author Daniel Wiese
018: * @param <T> -
019: * the type of the session bean to test
020: * @since 16.10.2005
021: */
022: public abstract class BaseSessionBeanFixture<T> extends BaseTest {
023:
024: private final SessionBeanFactory<T> sbFactory;
025:
026: private final Ejb3UnitJndiBinder jndiBinder;
027:
028: private final Class<T> beanClass;
029:
030: private T beanToTest = null;
031:
032: private InitialDataSet[] initalDataSets = null;
033:
034: /**
035: * Constructor.
036: *
037: * @param sessionBeanToTest -
038: * the class of the session bean to test
039: * @param usedEntityBeans -
040: * the used entity bens
041: */
042: public BaseSessionBeanFixture(Class<T> sessionBeanToTest,
043: Class[] usedEntityBeans) {
044: super ();
045: AbstractIntrospector<T> intro = new SessionBeanIntrospector<T>(
046: sessionBeanToTest);
047: this .sbFactory = new SessionBeanFactory<T>(intro,
048: usedEntityBeans);
049: this .beanClass = sessionBeanToTest;
050: this .jndiBinder = new Ejb3UnitJndiBinder(usedEntityBeans);
051:
052: }
053:
054: /**
055: * Constructor.
056: *
057: * @param sessionBeanToTest -
058: * the class of the session bean to test
059: * @param usedEntityBeans -
060: * the used entity beans
061: * @param initialData -
062: * the inital data to create in the db
063: */
064: public BaseSessionBeanFixture(Class<T> sessionBeanToTest,
065: Class[] usedEntityBeans, InitialDataSet... initialData) {
066: this (sessionBeanToTest, usedEntityBeans);
067: this .initalDataSets = initialData;
068: }
069:
070: /**
071: * Protected Constructor> possible to pass another introspector
072: *
073: * @param sessionBeanToTest -
074: * the class of the session bean to test
075: * @param intro -
076: * the introspector
077: * @param usedEntityBeans -
078: * the used entity bens
079: * @param initialData -
080: * the inital data to create in the db
081: */
082: protected BaseSessionBeanFixture(Class<T> sessionBeanToTest,
083: AbstractIntrospector<T> intro, Class[] usedEntityBeans,
084: InitialDataSet... initialData) {
085: super ();
086: this .sbFactory = new SessionBeanFactory<T>(intro,
087: usedEntityBeans);
088: this .beanClass = sessionBeanToTest;
089: this .initalDataSets = initialData;
090: this .jndiBinder = new Ejb3UnitJndiBinder(usedEntityBeans);
091: }
092:
093: /**
094: * Sets a CSV inital data set to prepopylate the database with data.
095: *
096: * @param initalDataSets
097: * the array of initial data sets
098: */
099: public void setInitalDataSets(InitialDataSet... initalDataSets) {
100: this .initalDataSets = initalDataSets;
101: }
102:
103: /**
104: * @author Daniel Wiese
105: * @since 16.10.2005
106: * @see junit.framework.TestCase#setUp()
107: */
108: @Override
109: protected void setUp() throws Exception {
110: super .setUp();
111: this .jndiBinder.bind();
112: // the creation process is expensive, do it once per test
113: if (this .beanToTest == null) {
114: this .beanToTest = this .sbFactory
115: .createSessionBean(this .beanClass);
116: }
117:
118: if (this .initalDataSets != null) {
119: EntityManager em = this .getEntityManager();
120:
121: for (InitialDataSet current : this .initalDataSets) {
122: // insert entity manager
123: if (current instanceof EntityInitialDataSet) {
124: EntityInitialDataSet curentEntDs = (EntityInitialDataSet) current;
125: curentEntDs.setEntityManager(em);
126: EntityTransaction tx = em.getTransaction();
127: tx.begin();
128: current.create();
129: tx.commit();
130: } else {
131: current.create();
132: }
133: }
134: }
135: }
136:
137: /**
138: * @author Daniel Wiese
139: * @since 16.10.2005
140: * @see junit.framework.TestCase#tearDown()
141: */
142: @Override
143: protected void tearDown() throws Exception {
144: super .tearDown();
145:
146: // delete all objects (faster than shutdown and restart everything)
147: final EntityManager em = this .getEntityManager();
148: if (this .initalDataSets != null) {
149: // tear down in reverse order, otherwise foreign key constraints may be violated
150: for (int i = this .initalDataSets.length - 1; i >= 0; i--) {
151: this .initalDataSets[i].cleanup(em);
152: }
153: }
154:
155: }
156:
157: /**
158: * Returns the beanClass.
159: *
160: * @return Returns the beanClass.
161: */
162: public Class<T> getBeanClass() {
163: return this .beanClass;
164: }
165:
166: /**
167: * Returns the beanToTest.
168: *
169: * @return Returns the beanToTest.
170: */
171: public T getBeanToTest() {
172: return this .beanToTest;
173: }
174:
175: /**
176: * Returns a isntance of a EntityManager.
177: *
178: * @author Daniel Wiese
179: * @since 12.11.2005
180: * @return - a instance of an entity manager
181: */
182: public EntityManager getEntityManager() {
183: return this .sbFactory.getEntityManager();
184: }
185:
186: /**
187: * Returns the sbFactory.
188: *
189: * @return Returns the sbFactory.
190: */
191: public SessionBeanFactory<T> getSbFactory() {
192: return this.sbFactory;
193: }
194:
195: }
|