1 22 package org.jboss.ejb3.test.entitycallback.unit; 23 24 import javax.persistence.PostLoad; 25 import javax.persistence.PostPersist; 26 import javax.persistence.PostRemove; 27 import javax.persistence.PostUpdate; 28 import javax.persistence.PrePersist; 29 import javax.persistence.PreRemove; 30 import javax.persistence.PreUpdate; 31 import org.jboss.ejb3.test.entitycallback.CallbackCounter; 32 import org.jboss.ejb3.test.entitycallback.EntityCallbackTest; 33 import org.jboss.test.JBossTestCase; 34 import junit.framework.Test; 35 36 40 public class EntityCallbackUnitTestCase 41 extends JBossTestCase 42 { 43 org.jboss.logging.Logger log = getLog(); 44 45 static boolean deployed = false; 46 static int test = 0; 47 48 class CallbackCounts 49 { 50 String entity; 51 String test; 52 53 int preCreate; 54 int postCreate; 55 int preRemove; 56 int postRemove; 57 int preUpdate; 58 int postUpdate; 59 int postLoad; 60 61 void checkCallbackCounts(int preCreate, int postCreate, int preRemove, int postRemove, int preUpdate, int postUpdate, int postLoad) 62 { 63 assertEquals("Wrong number of preCreate for " + entity + " for test '" + test + "'", preCreate, this.preCreate); 64 assertEquals("Wrong number of postCreate for " + entity + " for test '" + test + "'", postCreate, this.postCreate); 65 assertEquals("Wrong number of preRemove for " + entity + " for test '" + test + "'", preRemove, this.preRemove); 66 assertEquals("Wrong number of postRemove for " + entity + " for test '" + test + "'", postRemove, this.postRemove); 67 assertEquals("Wrong number of preUpdate for " + entity + " for test '" + test + "'", preUpdate, this.preUpdate); 68 assertEquals("Wrong number of postUpdate for " + entity + " for test '" + test + "'", postUpdate, this.postUpdate); 69 assertEquals("Wrong number of postLoad for " + entity + " for test '" + test + "'", postLoad, this.postLoad); 70 } 71 } 72 73 74 75 public EntityCallbackUnitTestCase(String name) 76 { 77 super(name); 78 } 79 80 public void test() throws Exception 81 { 82 System.out.println("testing insertion"); 83 EntityCallbackTest test = (EntityCallbackTest) this.getInitialContext().lookup("EntityCallbackTestBean/remote"); 84 85 86 System.out.println("***** testing createCustomer() *****"); 87 String current = "createCustomer"; 88 test.createCustomer(); 89 CallbackCounts customerCounts = getCallbackCounts(current, "Customer"); 90 CallbackCounts journeyCounts = getCallbackCounts(current, "Journey"); 91 CallbackCounts trainJourneyCounts = getCallbackCounts(current, "TrainJourney"); 92 CallbackCounts busJourneyCounts = getCallbackCounts(current, "BusJourney"); 93 94 customerCounts.checkCallbackCounts(1, 1, 0, 0, 0, 0, 0); 97 journeyCounts.checkCallbackCounts(0, 0, 0, 0, 0, 0, 0); 98 trainJourneyCounts.checkCallbackCounts(0, 0, 0, 0, 0, 0, 0); 99 busJourneyCounts.checkCallbackCounts(0, 0, 0, 0, 0, 0, 0); 100 101 102 System.out.println("***** testing addJourneysToCustomer() *****"); 103 current = "addJourneysToCustomer"; 104 test.addJourneysToCustomer(); 105 customerCounts = getCallbackCounts(current, "Customer"); 106 journeyCounts = getCallbackCounts(current, "Journey"); 107 trainJourneyCounts = getCallbackCounts(current, "TrainJourney"); 108 busJourneyCounts = getCallbackCounts(current, "BusJourney"); 109 110 customerCounts.checkCallbackCounts(0, 0, 0, 0, 0, 0, 1); 113 journeyCounts.checkCallbackCounts(5, 5, 0, 0, 0, 0, 0); 114 trainJourneyCounts.checkCallbackCounts(2, 2, 0, 0, 0, 0, 0); 115 busJourneyCounts.checkCallbackCounts(0, 0, 0, 0, 0, 0, 0); 116 117 118 System.out.println("***** testing updateCustomer() *****"); 119 current = "updateCustomer"; 120 test.updateCustomer(); 121 customerCounts = getCallbackCounts(current, "Customer"); 122 journeyCounts = getCallbackCounts(current, "Journey"); 123 trainJourneyCounts = getCallbackCounts(current, "TrainJourney"); 124 busJourneyCounts = getCallbackCounts(current, "BusJourney"); 125 126 customerCounts.checkCallbackCounts(0, 0, 0, 0, 1, 1, 1); 129 journeyCounts.checkCallbackCounts(0, 0, 0, 0, 0, 0, 5); 130 trainJourneyCounts.checkCallbackCounts(0, 0, 0, 0, 0, 0, 2); 131 busJourneyCounts.checkCallbackCounts(0, 0, 0, 0, 0, 0, 0); 132 133 134 System.out.println("***** testing updateOneTrainJourney() *****"); 135 current = "updateOneTrainJourney"; 136 test.updateOneTrainJourney(); 137 customerCounts = getCallbackCounts(current, "Customer"); 138 journeyCounts = getCallbackCounts(current, "Journey"); 139 trainJourneyCounts = getCallbackCounts(current, "TrainJourney"); 140 busJourneyCounts = getCallbackCounts(current, "BusJourney"); 141 142 customerCounts.checkCallbackCounts(0, 0, 0, 0, 0, 0, 1); 145 journeyCounts.checkCallbackCounts(0, 0, 0, 0, 1, 1, 5); 146 trainJourneyCounts.checkCallbackCounts(0, 0, 0, 0, 1, 1, 2); 147 busJourneyCounts.checkCallbackCounts(0, 0, 0, 0, 0, 0, 0); 148 149 150 System.out.println("***** testing updateAllBusJourneys() *****"); 151 current = "updateAllBusJourneys"; 152 test.updateAllBusJourneys(); 153 customerCounts = getCallbackCounts(current, "Customer"); 154 journeyCounts = getCallbackCounts(current, "Journey"); 155 trainJourneyCounts = getCallbackCounts(current, "TrainJourney"); 156 busJourneyCounts = getCallbackCounts(current, "BusJourney"); 157 158 customerCounts.checkCallbackCounts(0, 0, 0, 0, 0, 0, 1); 161 journeyCounts.checkCallbackCounts(0, 0, 0, 0, 3, 3, 5); 162 trainJourneyCounts.checkCallbackCounts(0, 0, 0, 0, 0, 0, 2); 163 busJourneyCounts.checkCallbackCounts(0, 0, 0, 0, 0, 0, 0); 164 165 166 System.out.println("***** testing updateEverything() *****"); 167 current = "updateEverything"; 168 test.updateEverything(); 169 customerCounts = getCallbackCounts(current, "Customer"); 170 journeyCounts = getCallbackCounts(current, "Journey"); 171 trainJourneyCounts = getCallbackCounts(current, "TrainJourney"); 172 busJourneyCounts = getCallbackCounts(current, "BusJourney"); 173 174 customerCounts.checkCallbackCounts(0, 0, 0, 0, 1, 1, 1); 177 journeyCounts.checkCallbackCounts(0, 0, 0, 0, 5, 5, 5); 178 trainJourneyCounts.checkCallbackCounts(0, 0, 0, 0, 2, 2, 2); 179 busJourneyCounts.checkCallbackCounts(0, 0, 0, 0, 0, 0, 0); 180 181 182 System.out.println("***** testing createAndDeleteCustomer() *****"); 183 current = "createAndDeleteCustomer"; 184 test.createAndDeleteCustomer(); 185 customerCounts = getCallbackCounts(current, "Customer"); 186 journeyCounts = getCallbackCounts(current, "Journey"); 187 trainJourneyCounts = getCallbackCounts(current, "TrainJourney"); 188 busJourneyCounts = getCallbackCounts(current, "BusJourney"); 189 190 customerCounts.checkCallbackCounts(1, 1, 1, 1, 0, 0, 0); 193 journeyCounts.checkCallbackCounts(0, 0, 0, 0, 0, 0, 0); 194 trainJourneyCounts.checkCallbackCounts(0, 0, 0, 0, 0, 0, 0); 195 busJourneyCounts.checkCallbackCounts(0, 0, 0, 0, 0, 0, 0); 196 197 198 System.out.println("***** testing deleteSomeJourneys() *****"); 199 current = "deleteSomeJourneys"; 200 test.deleteSomeJourneys(); 201 customerCounts = getCallbackCounts(current, "Customer"); 202 journeyCounts = getCallbackCounts(current, "Journey"); 203 trainJourneyCounts = getCallbackCounts(current, "TrainJourney"); 204 busJourneyCounts = getCallbackCounts(current, "BusJourney"); 205 206 customerCounts.checkCallbackCounts(0, 0, 0, 0, 0, 0, 1); 209 journeyCounts.checkCallbackCounts(0, 0, 2, 2, 0, 0, 5); 210 trainJourneyCounts.checkCallbackCounts(0, 0, 1, 1, 0, 0, 2); 211 busJourneyCounts.checkCallbackCounts(0, 0, 0, 0, 0, 0, 0); 212 213 214 215 System.out.println("***** testing deleteCustomerAndJourneys() *****"); 216 current = "deleteCustomerAndJourneys"; 217 test.deleteCustomerAndJourneys(); 218 customerCounts = getCallbackCounts(current, "Customer"); 219 journeyCounts = getCallbackCounts(current, "Journey"); 220 trainJourneyCounts = getCallbackCounts(current, "TrainJourney"); 221 busJourneyCounts = getCallbackCounts(current, "BusJourney"); 222 223 customerCounts.checkCallbackCounts(0, 0, 1, 1, 0, 0, 1); 226 journeyCounts.checkCallbackCounts(0, 0, 3, 3, 0, 0, 3); 227 trainJourneyCounts.checkCallbackCounts(0, 0, 1, 1, 0, 0, 1); 228 busJourneyCounts.checkCallbackCounts(0, 0, 0, 0, 0, 0, 0); 229 } 230 231 public static Test suite() throws Exception 232 { 233 return getDeploySetup(EntityCallbackUnitTestCase.class, "entitycallback-test.jar"); 234 } 235 236 private CallbackCounts getCallbackCounts(String test, String entity)throws Exception 237 { 238 CallbackCounter callbackCounter = (CallbackCounter)this.getInitialContext().lookup("CallbackCounterBean/remote"); 239 CallbackCounts counts = new CallbackCounts(); 240 counts.entity = entity; 241 counts.test = test; 242 counts.preCreate = callbackCounter.getCallbacks(entity, PrePersist.class); 243 counts.postCreate = callbackCounter.getCallbacks(entity, PostPersist.class); 244 counts.preRemove = callbackCounter.getCallbacks(entity, PreRemove.class); 245 counts.postRemove = callbackCounter.getCallbacks(entity, PostRemove.class); 246 counts.preUpdate = callbackCounter.getCallbacks(entity, PreUpdate.class); 247 counts.postUpdate = callbackCounter.getCallbacks(entity, PostUpdate.class); 248 counts.postLoad = callbackCounter.getCallbacks(entity, PostLoad.class); 249 250 return counts; 251 } 252 } 253 | Popular Tags |