KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > common > ejbs > stateless > containermanaged > listeners > ListenerTesterBase


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: ListenerTesterBase.java 692 2006-06-20 09:17:57Z studzine $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.listeners;
26
27 import static org.testng.Assert.assertEquals;
28 import static org.testng.Assert.assertTrue;
29
30 import java.util.Arrays JavaDoc;
31 import java.util.List JavaDoc;
32
33 import javax.ejb.EJB JavaDoc;
34
35 import org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.CallbackType;
36 import org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.ListenerLogger;
37 import org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.callbacklogger.CallbackLoggerComparator;
38 import org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.callbacklogger.ItfListenerLoggerAccess;
39 import org.objectweb.easybeans.tests.common.helper.ListHelper;
40
41 /**
42  * Create the base for doing the tests that verifies if the lifecycle callback
43  * methods works properly. Creates, modifies and removes an intance of an entity
44  * and verifies if all callback methods for each operation was called. The
45  * entities used to test are in the geometricforms package and each one has a
46  * different definition of listeners.
47  * @author Gisele Pinheiro Souza
48  * @author Eduardo Studzinski Estima de Castro
49  */

50 public abstract class ListenerTesterBase implements ItfListenerTester {
51
52     /**
53      * The bean used to register the lifecycle calls in the database.
54      */

55     @EJB JavaDoc
56     private ItfListenerLoggerAccess bean;
57
58     /**
59      * Verifies if the callback methods that have the same type are executed in
60      * the correct order.
61      * @param lifecycle the lifecycle callback method.
62      * @param listenersId the list of listeners that must be called organized in
63      * the correct order.
64      * @param formType the form for which the the callback was called.
65      */

66     public void verifySameCallbackMethodOrder(final CallbackType lifecycle, final String JavaDoc[] listenersId, final String JavaDoc formType) {
67
68         List JavaDoc callbackList = bean.findCallbackEvent(formType, lifecycle);
69
70         assertTrue(callbackList.size() == listenersId.length, "The container did not call all listeners");
71
72         // sorts the events by date.
73
if (callbackList.size() != 0) {
74             ListenerLogger[] lstManager = new ListenerLogger[callbackList.size()];
75             try {
76                 lstManager = ListHelper.convertListType(callbackList).toArray(lstManager);
77             } catch (Exception JavaDoc e) {
78                 throw new RuntimeException JavaDoc(e);
79             }
80             Arrays.sort(lstManager, new CallbackLoggerComparator<ListenerLogger>());
81             for (int i = 0; i < lstManager.length; i++) {
82                 assertEquals(lstManager[i].getCallbackClassName(), listenersId[i],
83                         "The callback methods were not called in the correct order.");
84             }
85         }
86     }
87
88     /**
89      * Verifies if two callback methods that are executed before and after an
90      * operation were called in order. For example, verifies if the PrePersist
91      * was called before the PostPersist.
92      * @param lifecycle1 the callback method which should be called before the
93      * operation.
94      * @param lifecycle2 the callback method which should be called after the
95      * operation.
96      * @param listenerId the listener where the callback method is.
97      * @param formType the form for which the callback method was called.
98      */

99     public void verifyDifferentCallbackMethodOrder(final CallbackType lifecycle1, final CallbackType lifecycle2,
100             final String JavaDoc listenerId, final String JavaDoc formType) {
101         ListenerLogger lstManCallback1 = null;
102         ListenerLogger lstManCallback2 = null;
103
104         List JavaDoc lstLifecycle1 = bean.findCallbackEventByCallbackMethod(formType, lifecycle1, listenerId);
105         List JavaDoc lstLifecycle2 = bean.findCallbackEventByCallbackMethod(formType, lifecycle2, listenerId);
106
107         lstManCallback1 = (ListenerLogger) lstLifecycle1.get(0);
108         lstManCallback2 = (ListenerLogger) lstLifecycle2.get(0);
109
110         assertTrue(lstManCallback1.getInsertionDate() < lstManCallback2.getInsertionDate(),
111                 "The callback methods are not called in the correct order");
112     }
113
114     /**
115      * Creates an instance of the entity under test.
116      */

117     protected abstract void createEntity();
118
119     /**
120      * Creates an entity and after that removes it.
121      */

122     protected abstract void createAndRemoveEntity();
123
124     /**
125      * Creates an entity and after that modifies it.
126      */

127     protected abstract void createAndModifyEntity();
128
129     /**
130      * Creates an entity, modifies and after that refreshes it.
131      */

132     protected abstract void createAndRefreshEntity();
133
134     /**
135      * Create a list with all listeners that must be called for the persist
136      * operation. The list is ordered by the callback invocation order.
137      * @return the list of listeners executed in order.
138      */

139     protected abstract String JavaDoc[] createListPersistListeners();
140
141     /**
142      * Create a list with all listeners that must be called for the remove
143      * operation. The list is ordered by the callback invocation order.
144      * @return the list of listeners executed in order.
145      */

146     protected abstract String JavaDoc[] createListRemoveListeners();
147
148     /**
149      * Create a list with all listners that must be called for the update
150      * operation. The list is ordered by the callback invocation order.
151      * @return the list of listeners executed in order.
152      */

153     protected abstract String JavaDoc[] createListUpdateListeners();
154
155     /**
156      * Create a list with all listners that must be called for the load
157      * operation. The list is ordered by the callback invocation order.
158      * @return the list of listeners executed in order.
159      */

160     protected abstract String JavaDoc[] createListLoadListeners();
161
162     /**
163      * Gets the name of the enitty created that extends form.
164      * @return the form name.
165      */

166     protected abstract String JavaDoc getFormName();
167
168     /**
169      * Verifies if the same type of callback methods are called in the correct
170      * order, as well as verifies if the callback methods that are executed
171      * before and after an operation were called in order.
172      * @param listeners a list of the listeners called ordered by invocation
173      * order.
174      * @param lifecycletype1 the callback method executed before the operation.
175      * @param lifecycletype2 the callback method executed afeter the operation.
176      */

177     private void verifyOrder(final String JavaDoc[] listeners, final CallbackType lifecycletype1, final CallbackType lifecycletype2) {
178         try {
179             // verifies if the callback methods are called in the correct order.
180
verifySameCallbackMethodOrder(lifecycletype1, listeners, getFormName());
181             // verifies if the callback methods are called in the correct order.
182
verifySameCallbackMethodOrder(lifecycletype2, listeners, getFormName());
183             // verifies if each lifecycletype1 is called before the
184
// lifecycletype2 respective
185
if (listeners.length > 0) {
186                 for (String JavaDoc strListener : listeners) {
187                     verifyDifferentCallbackMethodOrder(lifecycletype1, lifecycletype2, strListener, getFormName());
188                 }
189             }
190         } finally {
191             bean.deleteAll();
192         }
193     }
194
195     /**
196      * Verifies if the PrePersist and PostPersist are called before and after a
197      * persist operation, respectively. Also, verifies if all listeners
198      * specified for the entity class were called.
199      */

200     public void testPersistCallbackMethods() {
201         //deletes all callback event from the database.
202
bean.deleteAll();
203         createEntity();
204         String JavaDoc[] strListeners = createListPersistListeners();
205         verifyOrder(strListeners, CallbackType.PRE_PERSIST, CallbackType.POST_PERSIST);
206     }
207
208     /**
209      * Verifies if the PreRemove and PostRemove are called before and after a
210      * remove operation, respectively. Also, verifies if all listeners specified
211      * for the entity class were called.
212      */

213     public void testRemoveCallbackMethods() {
214         //deletes all callback event from the database.
215
bean.deleteAll();
216         createAndRemoveEntity();
217         String JavaDoc[] strListeners = createListRemoveListeners();
218         verifyOrder(strListeners, CallbackType.PRE_REMOVE, CallbackType.POST_REMOVE);
219     }
220
221     /**
222      * Verifies if the PreUpdate and PostUpdate are called before and after a
223      * update operation, respectively. Also, verifies if all listeners specified
224      * for the entity class were called.
225      */

226     public void testUpdateCallbackMethods() {
227         //deletes all callback event from the database.
228
bean.deleteAll();
229         createAndModifyEntity();
230         String JavaDoc[] strListeners = createListUpdateListeners();
231         verifyOrder(strListeners, CallbackType.PRE_UPDATE, CallbackType.POST_UPDATE);
232     }
233
234     /**
235      * Verifies if the PostLoad is called after the refresh operation.lso,
236      * verifies if all listeners specified for the entity class were called.
237      */

238     public void testLoadCallbackMethods() {
239         //deletes all callback event from the database.
240
bean.deleteAll();
241         createAndRefreshEntity();
242         String JavaDoc[] strListeners = createListLoadListeners();
243         try {
244             // verifies if the PostLoad methods are called in the correct order.
245
verifySameCallbackMethodOrder(CallbackType.POST_LOAD, strListeners, getFormName());
246         } finally {
247             bean.deleteAll();
248         }
249     }
250
251 }
252
Popular Tags