KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > common > ejbs > stateless > containermanaged > callbacklogger > CallbackLoggerAccessBase


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: CallbackLoggerAccessBase.java 915 2006-07-25 07:51:27Z studzine $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.callbacklogger;
26
27 import static java.util.Arrays.sort JavaDoc;
28 import static org.objectweb.easybeans.tests.common.helper.ListHelper.convertListType;
29 import static org.testng.Assert.assertEquals;
30 import static org.testng.Assert.assertTrue;
31
32 import java.util.Date JavaDoc;
33 import java.util.List JavaDoc;
34
35 import javax.persistence.EntityManager;
36 import javax.persistence.EntityManagerFactory;
37 import javax.persistence.PersistenceUnit;
38 import javax.persistence.Query;
39
40 import org.objectweb.easybeans.log.JLog;
41 import org.objectweb.easybeans.log.JLogFactory;
42 import org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.CallbackLogger;
43 import org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.CallbackType;
44
45
46 /**
47  * The base that is used for manipulating CallbackLoggers.
48  * @author Gisele Pinheiro Souza
49  * @author Eduardo Studzinski Estima de Castro
50  *
51  */

52 public class CallbackLoggerAccessBase{
53
54     /**
55      * Logger.
56      */

57     private static JLog logger = JLogFactory.getLog(CallbackLoggerAccessBase.class);
58
59     /**
60      * The Entity Manager Factory used during the tests.
61      */

62     @PersistenceUnit
63     private EntityManagerFactory entityManagerFactory;
64
65     /**
66      * Sleep time to wait callback operations.
67      */

68     public static final int WAIT = 1000;
69
70     /**
71      * Waits 2 milliseconds and returns the current time.
72      * @return the time.
73      */

74     protected long getTime(){
75         try {
76             // assures that the time diference between two insertion is at least
77
// 2 miliseconds, and, consequently the test can registry the call
78
// order.
79
Thread.sleep(2);
80         } catch (InterruptedException JavaDoc e) {
81             throw new RuntimeException JavaDoc(e);
82         }
83
84         Date JavaDoc date = new Date JavaDoc();
85         return date.getTime();
86     }
87
88    /**
89      * Deletes a callback event from the database.
90      * @param id the callback identifier.
91      */

92     public void deleteCallbackEvent(final int id) {
93         EntityManager entityManager = entityManagerFactory.createEntityManager();
94         CallbackLogger callbackLogger = entityManager.find(CallbackLogger.class, new Integer JavaDoc(id));
95         if (callbackLogger != null) {
96             entityManager.remove(callbackLogger);
97         }
98     }
99
100     /**
101      * Finds a callback event for a class.
102      * @param className the class for each the callback was called.
103      * @param callbackEvent the callback method that was called.
104      * @return the list of results.
105      */

106     public List JavaDoc findCallbackEvent(final String JavaDoc className, final CallbackType callbackEvent) {
107         //All logs
108
List JavaDoc arAll = findAll();
109         for(int i = 0; i < arAll.size(); i++){
110             logger.debug("Callback: {0}", arAll.get(i).toString());
111         }
112
113         logger.debug("Finding: className={0}, callbackEvent={1}", className, callbackEvent);
114
115         EntityManager entityManager = entityManagerFactory.createEntityManager();
116         Query query = entityManager.createNamedQuery("findLifecycleEvent");
117         query.setParameter("className", className);
118         query.setParameter("event", callbackEvent);
119
120
121         List JavaDoc arFound = query.getResultList();
122         for(int i = 0; i < arFound.size(); i++){
123             logger.debug("Found callback: {0}", arFound.get(i).toString());
124         }
125
126         return arFound;
127     }
128
129     /**
130      * Finds all callback events for a class.
131      * @param className the class for each the callback was called.
132      * @return the list of results.
133      */

134     public List JavaDoc findCallbackEvent(final String JavaDoc className) {
135         logger.debug("Finding: className={0}", className);
136
137         EntityManager entityManager = entityManagerFactory.createEntityManager();
138         Query query = entityManager.createNamedQuery("findLifecycleEventByClass");
139         query.setParameter("className", className);
140         return query.getResultList();
141     }
142
143     /**
144      * Finds a callback event that was called for the class in the className
145      * parameter and is defined in the class callbackClassName.
146      * @param className the class for each the callback was called.
147      * @param callbackEvent the callback method that was called.
148      * @param callbackClassName the class taht the callback method is defined.
149      * @return the list of results.
150      */

151     public List JavaDoc findCallbackEventByCallbackMethod(final String JavaDoc className, final CallbackType callbackEvent,
152             final String JavaDoc callbackClassName) {
153         EntityManager entityManager = entityManagerFactory.createEntityManager();
154         Query query = entityManager.createNamedQuery("findLifecycleEventByCallbackMethod");
155         query.setParameter("className", className);
156         query.setParameter("event", callbackEvent);
157         query.setParameter("callbackClassName", callbackClassName);
158         return query.getResultList();
159     }
160
161     /**
162      * Finds all callback events.
163      * @return events
164      */

165     public List JavaDoc findAll(){
166         EntityManager entityManager = entityManagerFactory.createEntityManager();
167         Query query = entityManager.createNamedQuery("findAll");
168         return query.getResultList();
169     }
170
171     /**
172      * Deletes all callback events from the database.
173      */

174     public void deleteAll() {
175         EntityManager entityManager = entityManagerFactory.createEntityManager();
176         Query query = entityManager.createNamedQuery("findAll");
177         List JavaDoc lstEvent = query.getResultList();
178         for (Object JavaDoc obj : lstEvent) {
179             CallbackLogger callbackLogger = (CallbackLogger) obj;
180             if (callbackLogger != null) {
181                 entityManager.remove(callbackLogger);
182             }
183         }
184     }
185
186     /**
187      * Verifies if the callback interceptor methods for a life cycle callback event are executed in
188      * the correct order.
189      * @param className the class where the interceptor must executes.
190      * @param callbackEvent the lifecycle callback interceptor method type.
191      * @param callbackClassNames the list of interceptors that must be called organized in
192      * the correct order.
193      */

194     public void verifyCallbackOrder(final String JavaDoc className, final CallbackType callbackEvent,
195             final String JavaDoc[] callbackClassNames) {
196
197         try {
198             Thread.sleep(WAIT);
199         } catch (InterruptedException JavaDoc e1) {
200             e1.printStackTrace();
201         }
202
203         List JavaDoc callbackList = findCallbackEvent(className, callbackEvent);
204         logger.debug("Callback list: {0}", callbackList);
205
206         assertTrue(callbackList.size() == callbackClassNames.length, "There is an error in callback interceptor invocation.");
207
208         // sorts the events by date.
209
if (callbackList.size() != 0) {
210             CallbackLogger[] lstManager = new CallbackLogger[callbackList.size()];
211             try {
212                 lstManager = convertListType(callbackList).toArray(lstManager);
213             } catch (Exception JavaDoc e) {
214                 throw new RuntimeException JavaDoc(e);
215             }
216             sort(lstManager, new CallbackLoggerComparator<CallbackLogger>());
217             for (int i = 0; i < lstManager.length; i++) {
218                 assertEquals(lstManager[i].getCallbackClassName(), callbackClassNames[i],
219                         "The callback methods were not called in the correct order. Expected = "
220                         + callbackClassNames[i] + ", Found = " + lstManager[i].toString());
221             }
222         }
223     }
224
225     /**
226      * Verifies if the callback interceptor methods for a life cycle callback event are executed in
227      * the correct order.
228      * @param className the class where the interceptor must executes.
229      * @param callbackEvent the lifecycle callback interceptor method type.
230      * @param callbackClassNames the list of interceptors that must be called organized in
231      * the correct order.
232      */

233     public void verifyCallbackOrder(final Class JavaDoc className, final CallbackType callbackEvent, final String JavaDoc[] callbackClassNames) {
234         this.verifyCallbackOrder(className.getName(), callbackEvent, callbackClassNames);
235     }
236 }
237
Popular Tags