Clover coverage report -
Coverage timestamp: Thu Jun 22 2006 14:24:50 CEST
file stats: LOC: 209   Methods: 9
NCLOC: 98   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
CallbackLoggerAccessBase.java 50% 71.7% 66.7% 68.3%
coverage coverage
 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 703 2006-06-21 14:33:25Z studzine $
 23    * --------------------------------------------------------------------------
 24    */
 25    package org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.callbacklogger;
 26   
 27    import static java.util.Arrays.sort;
 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;
 33    import java.util.List;
 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    import org.objectweb.easybeans.tests.common.helper.ContextHelper;
 45   
 46   
 47    /**
 48    * The base that is used for manipulating CallbackLoggers.
 49    * @author Gisele Pinheiro Souza
 50    * @author Eduardo Studzinski Estima de Castro
 51    *
 52    */
 53    public class CallbackLoggerAccessBase{
 54   
 55    /**
 56    * Logger.
 57    */
 58    private static JLog logger = JLogFactory.getLog(CallbackLoggerAccessBase.class);
 59   
 60    /**
 61    * The Entity Manager Factory used during the tests.
 62    */
 63    @PersistenceUnit
 64    private EntityManagerFactory entityManagerFactory;
 65   
 66    /**
 67    * Waits 2 milliseconds and returns the current time.
 68    * @return the time.
 69    */
 70  162 protected long getTime(){
 71  162 try {
 72    // assures that the time diference between two insertion is at least
 73    // 2 miliseconds, and, consequently the test can registry the call
 74    // order.
 75  162 Thread.sleep(2);
 76    } catch (InterruptedException e) {
 77  0 throw new RuntimeException(e);
 78    }
 79   
 80  162 Date date = new Date();
 81  162 return date.getTime();
 82    }
 83   
 84    /**
 85    * Deletes a callback event from the database.
 86    * @param id the callback identifier.
 87    */
 88  0 public void deleteCallbackEvent(final int id) {
 89  0 EntityManager entityManager = entityManagerFactory.createEntityManager();
 90  0 CallbackLogger callbackLogger = entityManager.find(CallbackLogger.class, new Integer(id));
 91  0 if (callbackLogger != null) {
 92  0 entityManager.remove(callbackLogger);
 93    }
 94    }
 95   
 96    /**
 97    * Finds a callback event for a class.
 98    * @param className the class for each the callback was called.
 99    * @param callbackEvent the callback method that was called.
 100    * @return the list of results.
 101    */
 102  23 public List findCallbackEvent(final String className, final CallbackType callbackEvent) {
 103  23 EntityManager entityManager = entityManagerFactory.createEntityManager();
 104  23 Query query = entityManager.createNamedQuery("findLifecycleEvent");
 105  23 query.setParameter("className", className);
 106  23 query.setParameter("event", callbackEvent);
 107  23 return query.getResultList();
 108    }
 109   
 110    /**
 111    * Finds all callback events for a class.
 112    * @param className the class for each the callback was called.
 113    * @return the list of results.
 114    */
 115  0 public List findCallbackEvent(final String className) {
 116  0 EntityManager entityManager = entityManagerFactory.createEntityManager();
 117  0 Query query = entityManager.createNamedQuery("findLifecycleEventByClass");
 118  0 query.setParameter("className", className);
 119  0 return query.getResultList();
 120    }
 121   
 122    /**
 123    * Finds a callback event that was called for the class in the className
 124    * parameter and is defined in the class callbackClassName.
 125    * @param className the class for each the callback was called.
 126    * @param callbackEvent the callback method that was called.
 127    * @param callbackClassName the class taht the callback method is defined.
 128    * @return the list of results.
 129    */
 130  26 public List findCallbackEventByCallbackMethod(final String className, final CallbackType callbackEvent,
 131    final String callbackClassName) {
 132  26 EntityManager entityManager = entityManagerFactory.createEntityManager();
 133  26 Query query = entityManager.createNamedQuery("findLifecycleEventByCallbackMethod");
 134  26 query.setParameter("className", className);
 135  26 query.setParameter("event", callbackEvent);
 136  26 query.setParameter("callbackClassName", callbackClassName);
 137  26 return query.getResultList();
 138    }
 139   
 140    /**
 141    * Finds all callback events.
 142    * @return events
 143    */
 144  0 public List findAll(){
 145  0 EntityManager entityManager = entityManagerFactory.createEntityManager();
 146  0 Query query = entityManager.createNamedQuery("findAll");
 147  0 return query.getResultList();
 148    }
 149   
 150    /**
 151    * Deletes all callback events from the database.
 152    */
 153  65 public void deleteAll() {
 154  65 EntityManager entityManager = entityManagerFactory.createEntityManager();
 155  65 Query query = entityManager.createNamedQuery("findAll");
 156  65 List lstEvent = query.getResultList();
 157  65 for (Object obj : lstEvent) {
 158  178 CallbackLogger callbackLogger = (CallbackLogger) obj;
 159  178 if (callbackLogger != null) {
 160  178 entityManager.remove(callbackLogger);
 161    }
 162    }
 163    }
 164   
 165    /**
 166    * Verifies if the callback interceptor methods for a life cycle callback event are executed in
 167    * the correct order.
 168    * @param className the class where the interceptor must executes.
 169    * @param callbackEvent the lifecycle callback interceptor method type.
 170    * @param callbackClassNames the list of interceptors that must be called organized in
 171    * the correct order.
 172    */
 173  8 public void verifyCallbackOrder(final String className, final CallbackType callbackEvent,
 174    final String[] callbackClassNames) {
 175   
 176  8 List callbackList = findCallbackEvent(className, callbackEvent);
 177  8 logger.debug("Callback list: {0}", callbackList);
 178   
 179  8 assertTrue(callbackList.size() == callbackClassNames.length, "There is an error in callback interceptor invocation.");
 180   
 181    // sorts the events by date.
 182  5 if (callbackList.size() != 0) {
 183  5 CallbackLogger[] lstManager = new CallbackLogger[callbackList.size()];
 184  5 try {
 185  5 lstManager = convertListType(callbackList).toArray(lstManager);
 186    } catch (Exception e) {
 187  0 throw new RuntimeException(e);
 188    }
 189  5 sort(lstManager, new CallbackLoggerComparator<CallbackLogger>());
 190  5 for (int i = 0; i < lstManager.length; i++) {
 191  5 assertEquals(lstManager[i].getCallbackClassName(), callbackClassNames[i],
 192    "The callback methods were not called in the correct order. Expected = "
 193    + callbackClassNames[i] + ", Found = " + lstManager[i].toString());
 194    }
 195    }
 196    }
 197   
 198    /**
 199    * Verifies if the callback interceptor methods for a life cycle callback event are executed in
 200    * the correct order.
 201    * @param className the class where the interceptor must executes.
 202    * @param callbackEvent the lifecycle callback interceptor method type.
 203    * @param callbackClassNames the list of interceptors that must be called organized in
 204    * the correct order.
 205    */
 206  8 public void verifyCallbackOrder(final Class className, final CallbackType callbackEvent, final String[] callbackClassNames) {
 207  8 this.verifyCallbackOrder(className.getName(), callbackEvent, callbackClassNames);
 208    }
 209    }