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.sqlmap.client.SqlMapClient;
007: import com.ibatis.sqlmap.client.SqlMapClientBuilder;
008: import com.tc.object.config.ConfigVisitor;
009: import com.tc.object.config.DSOClientConfigHelper;
010: import com.tc.object.config.spec.CyclicBarrierSpec;
011: import com.tc.simulator.app.ApplicationConfig;
012: import com.tc.simulator.listener.ListenerProvider;
013: import com.tc.test.HSqlDBServer;
014: import com.tc.util.Assert;
015: import com.tc.util.TIMUtil;
016: import com.tctest.domain.Account;
017: import com.tctest.domain.Customer;
018: import com.tctest.runner.AbstractTransparentApp;
019:
020: import java.io.Reader;
021: import java.sql.Connection;
022: import java.sql.PreparedStatement;
023: import java.sql.SQLException;
024:
025: public class IBatisSimpleTestApp extends AbstractTransparentApp {
026: private CyclicBarrier barrier;
027:
028: private SqlMapClient sqlMapper;
029:
030: private Customer cus;
031: private HSqlDBServer dbServer = null;
032:
033: public IBatisSimpleTestApp(String appId, ApplicationConfig cfg,
034: ListenerProvider listenerProvider) {
035: super (appId, cfg, listenerProvider);
036: barrier = new CyclicBarrier(getParticipantCount());
037: }
038:
039: public void run() {
040: try {
041: int id = barrier.barrier();
042:
043: try {
044:
045: if (id == 0) {
046:
047: setupDatabaseResource();
048:
049: Account acc = new Account();
050: acc.setNumber("ASI-001");
051: insertAccount(acc);
052: Customer cus = new Customer();
053: cus.setEmailAddress("asi@yahoo.com");
054: cus.setFirstName("Antonio");
055: cus.setLastName("Si");
056: cus.setAccount(acc);
057: insertCustomer(cus);
058:
059: }
060:
061: barrier.barrier();
062:
063: if (id == 0) {
064: Customer cus1 = selectCustomerById(0);
065: cus = cus1;
066: }
067: barrier.barrier();
068:
069: if (id == 1) {
070: Account acc = cus.getAccount();
071: Assert.assertEquals("ASI-001", acc.getNumber());
072: }
073:
074: barrier.barrier();
075:
076: } finally {
077: if (id == 0) {
078: shutdownDatabase();
079: }
080: }
081:
082: } catch (Throwable e) {
083: notifyError(e);
084: }
085:
086: }
087:
088: private void setupDatabaseResource() throws Exception {
089: sqlMapper = connectDatabase();
090:
091: Connection conn = sqlMapper.getDataSource().getConnection();
092: PreparedStatement stmt = conn
093: .prepareStatement("create table ACCOUNT (acc_id int not null, acc_number varchar(80) null, constraint pk_acc_id primary key (acc_id))");
094: stmt.execute();
095:
096: stmt = conn
097: .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))");
098: stmt.execute();
099: }
100:
101: private SqlMapClient connectDatabase() throws Exception {
102: dbServer = new HSqlDBServer();
103: dbServer.start();
104:
105: Reader reader = Resources
106: .getResourceAsReader("com/tctest/SqlMapConfig.xml");
107: SqlMapClient sqlMapper = SqlMapClientBuilder
108: .buildSqlMapClient(reader);
109: reader.close();
110:
111: return sqlMapper;
112: }
113:
114: private void shutdownDatabase() throws Exception {
115: dbServer.stop();
116: }
117:
118: public Customer selectCustomerById(int id) throws SQLException {
119: return (Customer) sqlMapper.queryForObject(
120: "selectCustomerById", new Integer(id));
121: }
122:
123: public void insertAccount(Account acc) throws SQLException {
124: sqlMapper.insert("insertAccount", acc);
125: }
126:
127: public void insertCustomer(Customer customer) throws SQLException {
128: sqlMapper.insert("insertCustomer", customer);
129: }
130:
131: public static void visitL1DSOConfig(ConfigVisitor visitor,
132: DSOClientConfigHelper config) {
133: String testClass = IBatisSimpleTestApp.class.getName();
134:
135: config.getOrCreateSpec(testClass).addRoot("barrier", "barrier")
136: .addRoot("cus", "cus");
137:
138: config.addWriteAutolock("* " + testClass + "*.*(..)");
139: config.addIncludePattern("com.tctest.domain.Account");
140: config.addIncludePattern("com.tctest.domain.Customer");
141: new CyclicBarrierSpec().visit(visitor, config);
142:
143: config.addModule(TIMUtil.IBATIS_2_2_0, TIMUtil
144: .getVersion(TIMUtil.IBATIS_2_2_0));
145:
146: // transient stuff
147: config.addModule(TIMUtil.CGLIB_2_1_3, TIMUtil
148: .getVersion(TIMUtil.CGLIB_2_1_3));
149: }
150:
151: }
|