001: package com.tctest;
002:
003: import net.sf.ehcache.CacheManager;
004:
005: import org.hibernate.Session;
006: import org.terracotta.modules.hibernate_3_1_2.util.HibernateUtil;
007:
008: import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
009:
010: import com.tc.object.config.ConfigVisitor;
011: import com.tc.object.config.DSOClientConfigHelper;
012: import com.tc.object.config.spec.CyclicBarrierSpec;
013: import com.tc.simulator.app.ApplicationConfig;
014: import com.tc.simulator.listener.ListenerProvider;
015: import com.tc.test.HSqlDBServer;
016: import com.tc.util.Assert;
017: import com.tc.util.TIMUtil;
018: import com.tctest.domain.Account;
019: import com.tctest.domain.AccountIntf;
020: import com.tctest.domain.Customer;
021: import com.tctest.domain.Gifts;
022: import com.tctest.domain.Product;
023: import com.tctest.domain.Promotion;
024: import com.tctest.domain.PromotionId;
025: import com.tctest.runner.AbstractTransparentApp;
026:
027: import java.sql.Connection;
028: import java.sql.PreparedStatement;
029: import java.util.ArrayList;
030: import java.util.HashSet;
031: import java.util.List;
032: import java.util.Set;
033:
034: public class HibernateSimpleTestApp extends AbstractTransparentApp {
035: private CyclicBarrier barrier;
036:
037: private Customer cus;
038: private Customer cus2;
039: private Promotion promotion;
040: private HSqlDBServer dbServer = null;
041:
042: public HibernateSimpleTestApp(String appId, ApplicationConfig cfg,
043: ListenerProvider listenerProvider) {
044: super (appId, cfg, listenerProvider);
045: barrier = new CyclicBarrier(getParticipantCount());
046: }
047:
048: public void run() {
049: try {
050: int index = barrier.barrier();
051:
052: try {
053:
054: setupDatabaseResource(index);
055:
056: barrier.barrier();
057:
058: insertData(index);
059:
060: singleNodeDeleteTest(index);
061:
062: insertData(index);
063:
064: singleNodeModifyTest(index);
065:
066: multiNodesShareTest(index);
067:
068: multiNodesLazyLoadingTest(index);
069:
070: compositeIdTest(index);
071: } finally {
072: if (index == 0) {
073: shutdownDatabase();
074: CacheManager.getInstance().shutdown(); // This is a total hack to shutdown the Ehcache provider before the TC
075: // server
076: // shutdown. If cache other than EhCache is used, we may need to modify this.
077: }
078: }
079:
080: } catch (Throwable e) {
081: notifyError(e);
082: }
083:
084: }
085:
086: private void singleNodeDeleteTest(int index) throws Exception {
087: Customer cus1 = (Customer) loadByIdentifier(Customer.class, 1);
088: Set products = cus1.getProducts();
089: int productSize = products.size();
090: Assert.assertEquals(2, productSize);
091:
092: assertEqualCustomer(cus1);
093:
094: commitTransaction();
095:
096: barrier.barrier();
097:
098: if (index == 0) {
099: cus1 = (Customer) loadByIdentifier(Customer.class, 1);
100: delete(cus1);
101: }
102: barrier.barrier();
103:
104: cus1 = (Customer) getByIdentifier(Customer.class, 1);
105: Assert.assertNull(cus1);
106:
107: barrier.barrier();
108: }
109:
110: private void singleNodeModifyTest(int index) throws Exception {
111: Customer cus1 = (Customer) loadByIdentifier(Customer.class, 2);
112:
113: assertEqualCustomer(cus1);
114:
115: commitTransaction();
116:
117: barrier.barrier();
118:
119: if (index == 0) {
120: cus1 = (Customer) loadByIdentifier(Customer.class, 2);
121: synchronized (cus1) {
122: cus1.setEmailAddress("asi@gmail.com");
123: }
124: saveOrUpdate(cus1);
125: }
126:
127: barrier.barrier();
128:
129: cus1 = (Customer) loadByIdentifier(Customer.class, 2);
130: assertEqualNewEmailCustomer(cus1);
131:
132: barrier.barrier();
133: }
134:
135: private void multiNodesShareTest(int index) throws Exception {
136: Assert.assertNull(cus);
137:
138: barrier.barrier();
139:
140: if (index == 0) {
141: cus = (Customer) loadByIdentifier(Customer.class, 2);
142: }
143: barrier.barrier();
144:
145: assertEqualNewEmailCustomer(cus);
146:
147: barrier.barrier();
148:
149: if (index == 1) {
150: // commitTransaction();
151: synchronized (cus) {
152: cus.setEmailAddress("asi@yahoo.com");
153: }
154: saveOrUpdate(cus);
155: }
156:
157: barrier.barrier();
158:
159: assertEqualCustomer(cus);
160:
161: barrier.barrier();
162: }
163:
164: private void multiNodesLazyLoadingTest(int index) throws Exception {
165: if (index == 0) {
166: cus2 = (Customer) loadByIdentifier(Customer.class, 2);
167: }
168: barrier.barrier();
169:
170: if (index == 1) {
171: AccountIntf account = cus2.getAccount();
172: startTransaction();
173: Assert.assertEquals("ASI-001", account.getNumber());
174: }
175:
176: barrier.barrier();
177: }
178:
179: private void compositeIdTest(int index) throws Exception {
180: if (index == 0) {
181: Promotion promotion1 = (Promotion) getFirstPromotion();
182: promotion = promotion1;
183: }
184:
185: barrier.barrier();
186:
187: assertEqualPromotion(promotion);
188:
189: barrier.barrier();
190:
191: }
192:
193: private Object getFirstPromotion() throws Exception {
194: Session session = HibernateUtil.getSessionFactory()
195: .getCurrentSession();
196: session.beginTransaction();
197: List promotions = session.createQuery("from Promotion").list();
198: return promotions.get(0);
199: }
200:
201: private Object loadByIdentifier(Class clazz, int id)
202: throws Exception {
203: Session session = HibernateUtil.getSessionFactory()
204: .getCurrentSession();
205: session.beginTransaction();
206: return session.load(clazz, new Integer(id));
207: }
208:
209: private Object getByIdentifier(Class clazz, int id) {
210: Session session = HibernateUtil.getSessionFactory()
211: .getCurrentSession();
212: session.beginTransaction();
213: return session.get(clazz, new Integer(id));
214: }
215:
216: private void delete(Object o) throws Exception {
217: Session session = HibernateUtil.getSessionFactory()
218: .getCurrentSession();
219: session.beginTransaction();
220: session.delete(o);
221:
222: session.getTransaction().commit();
223: }
224:
225: private void save(Object o) throws Exception {
226: Session session = HibernateUtil.getSessionFactory()
227: .getCurrentSession();
228: session.beginTransaction();
229: session.save(o);
230: session.getTransaction().commit();
231: }
232:
233: private void saveOrUpdate(Object o) throws Exception {
234: Session session = HibernateUtil.getSessionFactory()
235: .getCurrentSession();
236: session.beginTransaction();
237: session.saveOrUpdate(o);
238: session.getTransaction().commit();
239: }
240:
241: private void startTransaction() throws Exception {
242: Session session = HibernateUtil.getSessionFactory()
243: .getCurrentSession();
244: session.beginTransaction();
245: }
246:
247: private void commitTransaction() throws Exception {
248: Session session = HibernateUtil.getSessionFactory()
249: .getCurrentSession();
250: session.getTransaction().commit();
251: }
252:
253: private void assertEqualPromotion(Promotion promotion) {
254: PromotionId pId = promotion.getId();
255: Assert.assertEquals(2, pId.getCustomerId().intValue());
256: Assert.assertEquals(2, pId.getGiftId().intValue());
257: Assert.assertEquals("holiday", promotion.getReason());
258: }
259:
260: private void assertEqualCustomer(Customer customer) {
261: Account acc = customer.getAccount();
262: Assert.assertEquals("ASI-001", acc.getNumber());
263:
264: Assert
265: .assertEquals("asi@yahoo.com", customer
266: .getEmailAddress());
267: Assert.assertEquals("Antonio", customer.getFirstName());
268: Assert.assertEquals("Si", customer.getLastName());
269: }
270:
271: private void assertEqualNewEmailCustomer(Customer customer) {
272: Account acc = customer.getAccount();
273: Assert.assertEquals("ASI-001", acc.getNumber());
274:
275: Assert
276: .assertEquals("asi@gmail.com", customer
277: .getEmailAddress());
278: Assert.assertEquals("Antonio", customer.getFirstName());
279: Assert.assertEquals("Si", customer.getLastName());
280: Assert.assertEquals(2, customer.getProducts().size());
281: }
282:
283: private void insertData(int index) throws Exception {
284: if (index == 0) {
285: deleteAllData();
286:
287: Account acc = new Account();
288: acc.setNumber("ASI-001");
289: List accounts = new ArrayList();
290: accounts.add(acc);
291:
292: Customer cus = new Customer();
293:
294: Set products = new HashSet();
295: Product prod = new Product();
296: prod.setNumber("prod-001");
297: prod.setCustomer(cus);
298: products.add(prod);
299: prod = new Product();
300: prod.setNumber("prod-002");
301: prod.setCustomer(cus);
302: products.add(prod);
303:
304: cus.setEmailAddress("asi@yahoo.com");
305: cus.setFirstName("Antonio");
306: cus.setLastName("Si");
307: cus.setAccount(acc);
308: cus.setProducts(products);
309: save(cus);
310:
311: Gifts gift = new Gifts();
312: gift.setName("lego");
313: gift.setCategory("toy");
314: save(gift);
315:
316: PromotionId pId = new PromotionId(new Long(cus.getId()),
317: new Long(gift.getId()));
318: Promotion p = new Promotion();
319: p.setId(pId);
320: p.setReason("holiday");
321: save(p);
322: }
323: barrier.barrier();
324: }
325:
326: private void deleteAllData() throws Exception {
327: Session session = HibernateUtil.getSessionFactory()
328: .getCurrentSession();
329:
330: session.beginTransaction();
331:
332: Connection conn = session.connection();
333:
334: PreparedStatement stmt = conn
335: .prepareStatement("delete from PROMOTION ");
336: stmt.execute();
337:
338: stmt = conn.prepareStatement("delete from GIFTS ");
339: stmt.execute();
340:
341: stmt = conn.prepareStatement("delete from CUSTOMER ");
342: stmt.execute();
343:
344: stmt = conn.prepareStatement("delete from ACCOUNT ");
345: stmt.execute();
346:
347: stmt = conn.prepareStatement("delete from PRODUCT ");
348: stmt.execute();
349:
350: stmt = conn.prepareStatement("delete from Customer_Products ");
351: stmt.execute();
352:
353: session.getTransaction().commit();
354: }
355:
356: private void setupDatabaseResource(int index) throws Exception {
357: if (index == 0) {
358: connectDatabase();
359: }
360: barrier.barrier();
361:
362: Session session = HibernateUtil.getSessionFactory()
363: .getCurrentSession();
364:
365: if (index == 0) {
366: session.beginTransaction();
367:
368: Connection conn = session.connection();
369: PreparedStatement stmt = conn
370: .prepareStatement("create table ACCOUNT (ACC_ID integer generated by default as identity (start with 1), ACC_NUMBER varchar(255), primary key (ACC_ID))");
371: stmt.execute();
372:
373: stmt = conn
374: .prepareStatement("create table CUSTOMER (CUS_ID integer generated by default as identity (start with 1), CUS_FIRST_NAME varchar(255), CUS_LAST_NAME varchar(255), CUS_EMAIL varchar(255), acc_id integer, primary key (CUS_ID), unique (acc_id))");
375: stmt.execute();
376:
377: stmt = conn
378: .prepareStatement("alter table CUSTOMER add constraint FK52C76FDE76EB25DC foreign key (acc_id) references ACCOUNT");
379: stmt.execute();
380:
381: stmt = conn
382: .prepareStatement("create table PRODUCT (PROD_ID integer generated by default as identity (start with 1), PROD_NUMBER varchar(255), primary key (PROD_ID))");
383: stmt.execute();
384:
385: stmt = conn
386: .prepareStatement("create table Customer_Products (PROD_ID integer not null, CUS_ID integer not null, primary key (PROD_ID, CUS_ID))");
387: stmt.execute();
388:
389: stmt = conn
390: .prepareStatement("alter table Customer_Products add constraint FKEFFD20A5FFC78061 foreign key (CUS_ID) references CUSTOMER");
391: stmt.execute();
392:
393: stmt = conn
394: .prepareStatement("alter table Customer_Products add constraint FKEFFD20A5ED0B4608 foreign key (PROD_ID) references PRODUCT");
395: stmt.execute();
396:
397: stmt = conn
398: .prepareStatement("create table GIFTS (GIFT_ID integer generated by default as identity (start with 1), GIFT_NAME varchar(255), GIFT_CATEGORY varchar(255), primary key (GIFT_ID))");
399: stmt.execute();
400:
401: stmt = conn
402: .prepareStatement("create table PROMOTION (CUST_ID integer, GIFT_ID integer, PROMOTION_REASON varchar(255), primary key (CUST_ID, GIFT_ID))");
403: stmt.execute();
404:
405: session.getTransaction().commit();
406: }
407: }
408:
409: private void connectDatabase() throws Exception {
410: dbServer = new HSqlDBServer();
411: dbServer.start();
412: }
413:
414: private void shutdownDatabase() throws Exception {
415: dbServer.stop();
416: }
417:
418: public static void visitL1DSOConfig(ConfigVisitor visitor,
419: DSOClientConfigHelper config) {
420: String testClass = HibernateSimpleTestApp.class.getName();
421:
422: config.getOrCreateSpec(testClass).addRoot("barrier", "barrier")
423: .addRoot("cus", "cus").addRoot("cus2", "cus2").addRoot(
424: "promotion", "promotion");
425:
426: config.addWriteAutolock("* " + testClass + "*.*(..)");
427: config.addIncludePattern("com.tctest.domain.Account");
428: config.addIncludePattern("com.tctest.domain.Customer");
429: config.addIncludePattern("com.tctest.domain.Product");
430: config.addIncludePattern("com.tctest.domain.Promotion");
431: config.addIncludePattern("com.tctest.domain.Gifts");
432: config.addIncludePattern("com.tctest.domain.PromotionId");
433: new CyclicBarrierSpec().visit(visitor, config);
434:
435: config.addModule(TIMUtil.HIBERNATE_3_1_2, TIMUtil
436: .getVersion(TIMUtil.HIBERNATE_3_1_2));
437: config.addModule(TIMUtil.EHCACHE_1_2_4, TIMUtil
438: .getVersion(TIMUtil.EHCACHE_1_2_4));
439:
440: // transient stuff
441: config.addModule(TIMUtil.CGLIB_2_1_3, TIMUtil
442: .getVersion(TIMUtil.CGLIB_2_1_3));
443: config.addModule(TIMUtil.COMMONS_COLLECTIONS_3_1, TIMUtil
444: .getVersion(TIMUtil.COMMONS_COLLECTIONS_3_1));
445: }
446:
447: }
|