001: package com.tctest;
002:
003: import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
004:
005: import com.ibatis.common.resources.Resources;
006: import com.ibatis.dao.client.Dao;
007: import com.ibatis.dao.client.DaoException;
008: import com.ibatis.dao.client.DaoManager;
009: import com.ibatis.dao.client.DaoManagerBuilder;
010: import com.ibatis.dao.engine.transaction.ConnectionDaoTransaction;
011: import com.tc.object.config.ConfigVisitor;
012: import com.tc.object.config.DSOClientConfigHelper;
013: import com.tc.object.config.spec.CyclicBarrierSpec;
014: import com.tc.simulator.app.ApplicationConfig;
015: import com.tc.simulator.listener.ListenerProvider;
016: import com.tc.test.HSqlDBServer;
017: import com.tc.util.Assert;
018: import com.tc.util.TIMUtil;
019: import com.tctest.domain.Account;
020: import com.tctest.domain.AccountDAO;
021: import com.tctest.domain.Customer;
022: import com.tctest.domain.CustomerDAO;
023: import com.tctest.runner.AbstractTransparentApp;
024:
025: import java.io.Reader;
026: import java.sql.Connection;
027: import java.sql.PreparedStatement;
028:
029: public class IBatisSimpleDaoTestApp extends AbstractTransparentApp {
030: private CyclicBarrier barrier;
031:
032: private AccountDAO accountDAO, dao;
033:
034: private CustomerDAO customerDAO;
035:
036: private Customer cus;
037:
038: private HSqlDBServer dbServer = null;
039:
040: public IBatisSimpleDaoTestApp(String appId, ApplicationConfig cfg,
041: ListenerProvider listenerProvider) {
042: super (appId, cfg, listenerProvider);
043: barrier = new CyclicBarrier(getParticipantCount());
044: }
045:
046: public void run() {
047: try {
048: int id = barrier.barrier();
049:
050: try {
051:
052: if (id == 0) {
053: setupDatabase();
054:
055: synchronized (accountDAO) {
056: Account acc = new Account();
057: acc.setNumber("ASI-001");
058: accountDAO.insertAccount(acc);
059: Customer cus = new Customer();
060: cus.setEmailAddress("asi@yahoo.com");
061: cus.setFirstName("Antonio");
062: cus.setLastName("Si");
063: cus.setAccount(acc);
064: customerDAO.insertCustomer(cus);
065: }
066: }
067:
068: barrier.barrier();
069:
070: if (id == 1) {
071: synchronized (customerDAO) {
072: cus = customerDAO.selectCustomer(0);
073: Account acc = cus.getAccount();
074: Assert.assertEquals("ASI-001", acc.getNumber());
075: }
076: }
077: barrier.barrier();
078: Assert.assertEquals("asi@yahoo.com", cus
079: .getEmailAddress());
080: Assert.assertEquals("Antonio", cus.getFirstName());
081: Assert.assertEquals("Si", cus.getLastName());
082:
083: barrier.barrier();
084: } finally {
085: if (id == 0) {
086: shutdownDatabase();
087: }
088: }
089:
090: } catch (Throwable e) {
091: notifyError(e);
092: }
093:
094: }
095:
096: private void setupDatabase() throws Exception {
097: try {
098: DaoManager daoManager = connectDatabase();
099: dao = (AccountDAO) daoManager.getDao(AccountDAO.class);
100:
101: Connection conn = getConnection(daoManager, dao);
102: PreparedStatement stmt = conn
103: .prepareStatement("create table ACCOUNT (acc_id int not null, acc_number varchar(80) null, constraint pk_acc_id primary key (acc_id))");
104: stmt.execute();
105:
106: stmt = conn
107: .prepareStatement("create table CUSTOMER (cus_id int not null, cus_first_name varchar(80) null, cus_last_name varchar(80) null, cus_email varchar(80) null, cus_account_id varchar(80) null, constraint pk_cus_id primary key (cus_id))");
108: stmt.execute();
109:
110: accountDAO = dao;
111: customerDAO = (CustomerDAO) daoManager
112: .getDao(CustomerDAO.class);
113: } catch (Exception e) {
114: e.printStackTrace(System.err);
115: throw e;
116: }
117: }
118:
119: private DaoManager connectDatabase() throws Exception {
120: dbServer = new HSqlDBServer();
121: dbServer.start();
122: Reader reader = Resources
123: .getResourceAsReader("com/tctest/DAOMap.xml");
124: DaoManager daoManager = DaoManagerBuilder
125: .buildDaoManager(reader);
126: reader.close();
127: return daoManager;
128: }
129:
130: private void shutdownDatabase() throws Exception {
131: dbServer.stop();
132: }
133:
134: private Connection getConnection(DaoManager daoManager, Dao dao) {
135: com.ibatis.dao.client.DaoTransaction trans = daoManager
136: .getTransaction(dao);
137: if (!(trans instanceof ConnectionDaoTransaction))
138: throw new DaoException(
139: "The DAO manager of type "
140: + daoManager.getClass().getName()
141: + " cannot supply a JDBC Connection for this template, and is therefore not"
142: + "supported by JdbcDaoTemplate.");
143: else
144: return ((ConnectionDaoTransaction) trans).getConnection();
145: }
146:
147: public static void visitL1DSOConfig(ConfigVisitor visitor,
148: DSOClientConfigHelper config) {
149: String testClass = IBatisSimpleDaoTestApp.class.getName();
150:
151: config.getOrCreateSpec(testClass).addRoot("barrier", "barrier")
152: .addRoot("cus", "cus").addRoot("list", "list").addRoot(
153: "customerDAO", "customerDAO").addRoot(
154: "accountDAO", "accountDAO");
155:
156: config.addWriteAutolock("* " + testClass + "*.*(..)");
157: config.addIncludePattern("com.tctest.domain.*");
158: new CyclicBarrierSpec().visit(visitor, config);
159:
160: config.addModule(TIMUtil.CGLIB_2_1_3, TIMUtil
161: .getVersion(TIMUtil.CGLIB_2_1_3));
162: config.addModule(TIMUtil.IBATIS_2_2_0, TIMUtil
163: .getVersion(TIMUtil.IBATIS_2_2_0));
164: }
165:
166: }
|