KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > entitycallback > unit > EntityCallbackUnitTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

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 /**
37  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
38  * @version $Revision: 58110 $
39  */

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 JavaDoc entity;
51       String JavaDoc 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 JavaDoc name)
76    {
77       super(name);
78    }
79
80    public void test() throws Exception JavaDoc
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 JavaDoc 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       //Order of parameters for check method is:
95
//preCreate, postCreate, preRemove, postRemove, preUpdate, postUpdate, postLoad
96
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       //Order of parameters for check method is:
111
//preCreate, postCreate, preRemove, postRemove, preUpdate, postUpdate, postLoad
112
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       //Order of parameters for check method is:
127
//preCreate, postCreate, preRemove, postRemove, preUpdate, postUpdate, postLoad
128
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       //Order of parameters for check method is:
143
//preCreate, postCreate, preRemove, postRemove, preUpdate, postUpdate, postLoad
144
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       //Order of parameters for check method is:
159
//preCreate, postCreate, preRemove, postRemove, preUpdate, postUpdate, postLoad
160
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       //Order of parameters for check method is:
175
//preCreate, postCreate, preRemove, postRemove, preUpdate, postUpdate, postLoad
176
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       //Order of parameters for check method is:
191
//preCreate, postCreate, preRemove, postRemove, preUpdate, postUpdate, postLoad
192
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       //Order of parameters for check method is:
207
//preCreate, postCreate, preRemove, postRemove, preUpdate, postUpdate, postLoad
208
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       //Order of parameters for check method is:
224
//preCreate, postCreate, preRemove, postRemove, preUpdate, postUpdate, postLoad
225
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 JavaDoc
232    {
233       return getDeploySetup(EntityCallbackUnitTestCase.class, "entitycallback-test.jar");
234    }
235
236    private CallbackCounts getCallbackCounts(String JavaDoc test, String JavaDoc entity)throws Exception JavaDoc
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