1 25 package org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.listeners; 26 27 import static org.testng.Assert.assertEquals; 28 import static org.testng.Assert.assertTrue; 29 30 import java.util.Arrays ; 31 import java.util.List ; 32 33 import javax.ejb.EJB ; 34 35 import org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.CallbackType; 36 import org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.ListenerLogger; 37 import org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.callbacklogger.CallbackLoggerComparator; 38 import org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.callbacklogger.ItfListenerLoggerAccess; 39 import org.objectweb.easybeans.tests.common.helper.ListHelper; 40 41 50 public abstract class ListenerTesterBase implements ItfListenerTester { 51 52 55 @EJB 56 private ItfListenerLoggerAccess bean; 57 58 66 public void verifySameCallbackMethodOrder(final CallbackType lifecycle, final String [] listenersId, final String formType) { 67 68 List callbackList = bean.findCallbackEvent(formType, lifecycle); 69 70 assertTrue(callbackList.size() == listenersId.length, "The container did not call all listeners"); 71 72 if (callbackList.size() != 0) { 74 ListenerLogger[] lstManager = new ListenerLogger[callbackList.size()]; 75 try { 76 lstManager = ListHelper.convertListType(callbackList).toArray(lstManager); 77 } catch (Exception e) { 78 throw new RuntimeException (e); 79 } 80 Arrays.sort(lstManager, new CallbackLoggerComparator<ListenerLogger>()); 81 for (int i = 0; i < lstManager.length; i++) { 82 assertEquals(lstManager[i].getCallbackClassName(), listenersId[i], 83 "The callback methods were not called in the correct order."); 84 } 85 } 86 } 87 88 99 public void verifyDifferentCallbackMethodOrder(final CallbackType lifecycle1, final CallbackType lifecycle2, 100 final String listenerId, final String formType) { 101 ListenerLogger lstManCallback1 = null; 102 ListenerLogger lstManCallback2 = null; 103 104 List lstLifecycle1 = bean.findCallbackEventByCallbackMethod(formType, lifecycle1, listenerId); 105 List lstLifecycle2 = bean.findCallbackEventByCallbackMethod(formType, lifecycle2, listenerId); 106 107 lstManCallback1 = (ListenerLogger) lstLifecycle1.get(0); 108 lstManCallback2 = (ListenerLogger) lstLifecycle2.get(0); 109 110 assertTrue(lstManCallback1.getInsertionDate() < lstManCallback2.getInsertionDate(), 111 "The callback methods are not called in the correct order"); 112 } 113 114 117 protected abstract void createEntity(); 118 119 122 protected abstract void createAndRemoveEntity(); 123 124 127 protected abstract void createAndModifyEntity(); 128 129 132 protected abstract void createAndRefreshEntity(); 133 134 139 protected abstract String [] createListPersistListeners(); 140 141 146 protected abstract String [] createListRemoveListeners(); 147 148 153 protected abstract String [] createListUpdateListeners(); 154 155 160 protected abstract String [] createListLoadListeners(); 161 162 166 protected abstract String getFormName(); 167 168 177 private void verifyOrder(final String [] listeners, final CallbackType lifecycletype1, final CallbackType lifecycletype2) { 178 try { 179 verifySameCallbackMethodOrder(lifecycletype1, listeners, getFormName()); 181 verifySameCallbackMethodOrder(lifecycletype2, listeners, getFormName()); 183 if (listeners.length > 0) { 186 for (String strListener : listeners) { 187 verifyDifferentCallbackMethodOrder(lifecycletype1, lifecycletype2, strListener, getFormName()); 188 } 189 } 190 } finally { 191 bean.deleteAll(); 192 } 193 } 194 195 200 public void testPersistCallbackMethods() { 201 bean.deleteAll(); 203 createEntity(); 204 String [] strListeners = createListPersistListeners(); 205 verifyOrder(strListeners, CallbackType.PRE_PERSIST, CallbackType.POST_PERSIST); 206 } 207 208 213 public void testRemoveCallbackMethods() { 214 bean.deleteAll(); 216 createAndRemoveEntity(); 217 String [] strListeners = createListRemoveListeners(); 218 verifyOrder(strListeners, CallbackType.PRE_REMOVE, CallbackType.POST_REMOVE); 219 } 220 221 226 public void testUpdateCallbackMethods() { 227 bean.deleteAll(); 229 createAndModifyEntity(); 230 String [] strListeners = createListUpdateListeners(); 231 verifyOrder(strListeners, CallbackType.PRE_UPDATE, CallbackType.POST_UPDATE); 232 } 233 234 238 public void testLoadCallbackMethods() { 239 bean.deleteAll(); 241 createAndRefreshEntity(); 242 String [] strListeners = createListLoadListeners(); 243 try { 244 verifySameCallbackMethodOrder(CallbackType.POST_LOAD, strListeners, getFormName()); 246 } finally { 247 bean.deleteAll(); 248 } 249 } 250 251 } 252 | Popular Tags |