1 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 45 46 52 public class CallbackLoggerAccessBase{ 53 54 57 private static JLog logger = JLogFactory.getLog(CallbackLoggerAccessBase.class); 58 59 62 @PersistenceUnit 63 private EntityManagerFactory entityManagerFactory; 64 65 68 public static final int WAIT = 1000; 69 70 74 protected long getTime(){ 75 try { 76 Thread.sleep(2); 80 } catch (InterruptedException e) { 81 throw new RuntimeException (e); 82 } 83 84 Date date = new Date (); 85 return date.getTime(); 86 } 87 88 92 public void deleteCallbackEvent(final int id) { 93 EntityManager entityManager = entityManagerFactory.createEntityManager(); 94 CallbackLogger callbackLogger = entityManager.find(CallbackLogger.class, new Integer (id)); 95 if (callbackLogger != null) { 96 entityManager.remove(callbackLogger); 97 } 98 } 99 100 106 public List findCallbackEvent(final String className, final CallbackType callbackEvent) { 107 List 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 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 134 public List findCallbackEvent(final String 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 151 public List findCallbackEventByCallbackMethod(final String className, final CallbackType callbackEvent, 152 final String 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 165 public List findAll(){ 166 EntityManager entityManager = entityManagerFactory.createEntityManager(); 167 Query query = entityManager.createNamedQuery("findAll"); 168 return query.getResultList(); 169 } 170 171 174 public void deleteAll() { 175 EntityManager entityManager = entityManagerFactory.createEntityManager(); 176 Query query = entityManager.createNamedQuery("findAll"); 177 List lstEvent = query.getResultList(); 178 for (Object obj : lstEvent) { 179 CallbackLogger callbackLogger = (CallbackLogger) obj; 180 if (callbackLogger != null) { 181 entityManager.remove(callbackLogger); 182 } 183 } 184 } 185 186 194 public void verifyCallbackOrder(final String className, final CallbackType callbackEvent, 195 final String [] callbackClassNames) { 196 197 try { 198 Thread.sleep(WAIT); 199 } catch (InterruptedException e1) { 200 e1.printStackTrace(); 201 } 202 203 List 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 if (callbackList.size() != 0) { 210 CallbackLogger[] lstManager = new CallbackLogger[callbackList.size()]; 211 try { 212 lstManager = convertListType(callbackList).toArray(lstManager); 213 } catch (Exception e) { 214 throw new RuntimeException (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 233 public void verifyCallbackOrder(final Class className, final CallbackType callbackEvent, final String [] callbackClassNames) { 234 this.verifyCallbackOrder(className.getName(), callbackEvent, callbackClassNames); 235 } 236 } 237 | Popular Tags |