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.assertTrue; 30 31 import java.util.List ; 32 33 import javax.ejb.Remote ; 34 import javax.ejb.Stateless ; 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.tests.common.ejbs.entity.callbacklogger.CallbackLogger; 41 import org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.CallbackType; 42 import org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.OperationLogger; 43 import org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType; 44 45 50 @Stateless (name = "SLSBOperationLoggerAccess") 51 @Remote (ItfOperationLoggerAccess.class) 52 public class SLSBOperationLoggerAccess extends CallbackLoggerAccessBase implements ItfOperationLoggerAccess { 53 54 57 @PersistenceUnit 58 private EntityManagerFactory entityManagerFactory; 59 60 63 private static final String UNDEFINED = "Undefined."; 64 65 74 public void insertOperationLogger(final String className, final CallbackType event, final String eventClassName, 75 final OperationType operationType, final String description) { 76 EntityManager entityManager = entityManagerFactory.createEntityManager(); 77 78 OperationLogger logger = new OperationLogger(); 79 logger.setCallbackClassName(eventClassName); 80 logger.setCallbackEvent(event); 81 logger.setClassName(className); 82 logger.setInsertionDate(getTime()); 83 logger.setOperationType(operationType); 84 logger.setDescription(description); 85 entityManager.persist(logger); 86 entityManager.flush(); 87 } 88 89 97 public void insertOperationLogger(final String className, final CallbackType event, final String eventClassName, 98 final OperationType operationType) { 99 this.insertOperationLogger(className, event, eventClassName, operationType, UNDEFINED); 100 } 101 102 111 public void verifyOperation(final String className, final CallbackType event, final String [] eventClassNames, 112 final OperationType operationType) { 113 114 this.verifyOperation(className, event, eventClassNames, operationType, 115 getArrayUndefined(eventClassNames.length)); 116 } 117 118 127 public void verifyOperation(final Class className, final CallbackType event, final String [] eventClassNames, 128 final OperationType operationType) { 129 this.verifyOperation(className.getName(), event, eventClassNames, operationType, 130 getArrayUndefined(eventClassNames.length)); 131 } 132 133 143 public void verifyOperation(final Class className, final CallbackType event, final String [] eventClassNames, 144 final OperationType operationType, final String [] descriptions) { 145 this.verifyOperation(className.getName(), event, eventClassNames, operationType, descriptions); 146 } 147 148 158 public void verifyOperation(final String className, final CallbackType event, final String [] eventClassNames, 159 final OperationType operationType, final String [] descriptions) { 160 161 try { 162 Thread.sleep(WAIT); 163 } catch (InterruptedException e1) { 164 e1.printStackTrace(); 165 } 166 167 assertTrue(descriptions.length == eventClassNames.length, 169 "The length of descriptions and event class names must be equal."); 170 171 EntityManager entityManager = entityManagerFactory.createEntityManager(); 172 Query query = entityManager.createQuery("SELECT e FROM OperationLogger " 173 + "e WHERE e.className = :className AND e.callbackEvent= :event AND e.operationType = :operationType"); 174 query.setParameter("className", className); 175 query.setParameter("event", event); 176 query.setParameter("operationType", operationType); 177 List callbackList = query.getResultList(); 178 179 assertTrue(callbackList.size() == eventClassNames.length, 180 "The length of operations expected is different from the length of operations found."); 181 182 if (callbackList.size() != 0) { 184 OperationLogger[] lstManager = new OperationLogger[callbackList.size()]; 185 try { 186 lstManager = convertListType(callbackList).toArray(lstManager); 187 } catch (Exception e) { 188 throw new RuntimeException (e); 189 } 190 sort(lstManager, new CallbackLoggerComparator<CallbackLogger>()); 191 for (int i = 0; i < lstManager.length; i++) { 192 if (!(lstManager[i].getCallbackClassName().equals(eventClassNames[i]) & lstManager[i].getDescription() 193 .equals(descriptions[i]))) { 194 throw new IllegalStateException ("The operation was not called. Expected = " + eventClassNames[i] 197 + ", Found = " + lstManager[i].toString()); 198 } 199 } 200 } 201 } 202 203 208 private String [] getArrayUndefined(final int length) { 209 String [] list = new String [length]; 210 211 for (int i = 0; i < length; i++) { 212 list[i] = UNDEFINED; 213 } 214 215 return list; 216 } 217 } 218 | Popular Tags |