001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: CallbackLoggerAccessBase.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.tests.common.ejbs.stateless.containermanaged.callbacklogger;
025:
026: import static java.util.Arrays.sort;
027: import static org.ow2.easybeans.tests.common.helper.ListHelper.convertListType;
028: import static org.testng.Assert.assertEquals;
029: import static org.testng.Assert.assertTrue;
030:
031: import java.util.Date;
032: import java.util.List;
033:
034: import javax.interceptor.ExcludeClassInterceptors;
035: import javax.interceptor.ExcludeDefaultInterceptors;
036: import javax.persistence.EntityManager;
037: import javax.persistence.EntityManagerFactory;
038: import javax.persistence.PersistenceUnit;
039: import javax.persistence.Query;
040:
041: import org.ow2.easybeans.tests.common.ejbs.entity.callbacklogger.CallbackLogger;
042: import org.ow2.easybeans.tests.common.ejbs.entity.callbacklogger.CallbackType;
043: import org.ow2.util.log.Log;
044: import org.ow2.util.log.LogFactory;
045:
046: /**
047: * The base that is used for manipulating CallbackLoggers.
048: * @author Gisele Pinheiro Souza
049: * @author Eduardo Studzinski Estima de Castro
050: *
051: */
052: public class CallbackLoggerAccessBase {
053:
054: /**
055: * Logger.
056: */
057: private static Log logger = LogFactory
058: .getLog(CallbackLoggerAccessBase.class);
059:
060: /**
061: * The Entity Manager Factory used during the tests.
062: */
063: @PersistenceUnit
064: private EntityManagerFactory entityManagerFactory;
065:
066: /**
067: * Sleep time to wait callback operations.
068: */
069: public static final int WAIT = 1000;
070:
071: /**
072: * Waits 2 milliseconds and returns the current time.
073: * @return the time.
074: */
075: protected long getTime() {
076: try {
077: // assures that the time diference between two insertion is at least
078: // 2 miliseconds, and, consequently the test can registry the call
079: // order.
080: Thread.sleep(2);
081: } catch (InterruptedException e) {
082: throw new RuntimeException(e);
083: }
084:
085: Date date = new Date();
086: return date.getTime();
087: }
088:
089: /**
090: * Deletes a callback event from the database.
091: * @param id the callback identifier.
092: */
093: public void deleteCallbackEvent(final int id) {
094: EntityManager entityManager = entityManagerFactory
095: .createEntityManager();
096: CallbackLogger callbackLogger = entityManager.find(
097: CallbackLogger.class, new Integer(id));
098: if (callbackLogger != null) {
099: entityManager.remove(callbackLogger);
100: }
101: }
102:
103: /**
104: * Finds a callback event for a class.
105: * @param className the class for each the callback was called.
106: * @param callbackEvent the callback method that was called.
107: * @return the list of results.
108: */
109: public List findCallbackEvent(final String className,
110: final CallbackType callbackEvent) {
111: //All logs
112: List arAll = findAll();
113: for (int i = 0; i < arAll.size(); i++) {
114: logger.debug("Callback: {0}", arAll.get(i).toString());
115: }
116:
117: logger.debug("Finding: className={0}, callbackEvent={1}",
118: className, callbackEvent);
119:
120: EntityManager entityManager = entityManagerFactory
121: .createEntityManager();
122: Query query = entityManager
123: .createNamedQuery("findLifecycleEvent");
124: query.setParameter("className", className);
125: query.setParameter("event", callbackEvent);
126:
127: List arFound = query.getResultList();
128: for (int i = 0; i < arFound.size(); i++) {
129: logger.debug("Found callback: {0}", arFound.get(i)
130: .toString());
131: }
132:
133: return arFound;
134: }
135:
136: /**
137: * Finds all callback events for a class.
138: * @param className the class for each the callback was called.
139: * @return the list of results.
140: */
141: public List findCallbackEvent(final String className) {
142: logger.debug("Finding: className={0}", className);
143:
144: EntityManager entityManager = entityManagerFactory
145: .createEntityManager();
146: Query query = entityManager
147: .createNamedQuery("findLifecycleEventByClass");
148: query.setParameter("className", className);
149: return query.getResultList();
150: }
151:
152: /**
153: * Finds a callback event that was called for the class in the className
154: * parameter and is defined in the class callbackClassName.
155: * @param className the class for each the callback was called.
156: * @param callbackEvent the callback method that was called.
157: * @param callbackClassName the class taht the callback method is defined.
158: * @return the list of results.
159: */
160: public List findCallbackEventByCallbackMethod(
161: final String className, final CallbackType callbackEvent,
162: final String callbackClassName) {
163: EntityManager entityManager = entityManagerFactory
164: .createEntityManager();
165: Query query = entityManager
166: .createNamedQuery("findLifecycleEventByCallbackMethod");
167: query.setParameter("className", className);
168: query.setParameter("event", callbackEvent);
169: query.setParameter("callbackClassName", callbackClassName);
170: return query.getResultList();
171: }
172:
173: /**
174: * Finds all callback events.
175: * @return events
176: */
177: public List findAll() {
178: EntityManager entityManager = entityManagerFactory
179: .createEntityManager();
180: Query query = entityManager.createNamedQuery("findAll");
181: return query.getResultList();
182: }
183:
184: /**
185: * Deletes all callback events from the database.
186: */
187: @ExcludeDefaultInterceptors
188: @ExcludeClassInterceptors
189: public void deleteAll() {
190: EntityManager entityManager = entityManagerFactory
191: .createEntityManager();
192: Query query = entityManager.createNamedQuery("findAll");
193: List lstEvent = query.getResultList();
194: for (Object obj : lstEvent) {
195: CallbackLogger callbackLogger = (CallbackLogger) obj;
196: if (callbackLogger != null) {
197: entityManager.remove(callbackLogger);
198: }
199: }
200: }
201:
202: /**
203: * Verifies if the callback interceptor methods for a life cycle callback event are executed in
204: * the correct order.
205: * @param className the class where the interceptor must executes.
206: * @param callbackEvent the lifecycle callback interceptor method type.
207: * @param callbackClassNames the list of interceptors that must be called organized in
208: * the correct order.
209: */
210: public void verifyCallbackOrder(final String className,
211: final CallbackType callbackEvent,
212: final String[] callbackClassNames) {
213:
214: try {
215: Thread.sleep(WAIT);
216: } catch (InterruptedException e1) {
217: e1.printStackTrace();
218: }
219:
220: List callbackList = findCallbackEvent(className, callbackEvent);
221: logger.debug("Callback list: {0}", callbackList);
222:
223: assertTrue(callbackList.size() == callbackClassNames.length,
224: "There is an error in callback interceptor invocation.");
225:
226: // sorts the events by date.
227: if (callbackList.size() != 0) {
228: CallbackLogger[] lstManager = new CallbackLogger[callbackList
229: .size()];
230: try {
231: lstManager = convertListType(callbackList).toArray(
232: lstManager);
233: } catch (Exception e) {
234: throw new RuntimeException(e);
235: }
236: sort(lstManager,
237: new CallbackLoggerComparator<CallbackLogger>());
238: for (int i = 0; i < lstManager.length; i++) {
239: assertEquals(lstManager[i].getCallbackClassName(),
240: callbackClassNames[i],
241: "The callback methods were not called in the correct order. Expected = "
242: + callbackClassNames[i] + ", Found = "
243: + lstManager[i].toString());
244: }
245: }
246: }
247:
248: /**
249: * Verifies if the callback interceptor methods for a life cycle callback event are executed in
250: * the correct order.
251: * @param className the class where the interceptor must executes.
252: * @param callbackEvent the lifecycle callback interceptor method type.
253: * @param callbackClassNames the list of interceptors that must be called organized in
254: * the correct order.
255: */
256: public void verifyCallbackOrder(final Class className,
257: final CallbackType callbackEvent,
258: final String[] callbackClassNames) {
259: this.verifyCallbackOrder(className.getName(), callbackEvent,
260: callbackClassNames);
261: }
262: }
|