KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > IBatisSimpleTestApp


1 package com.tctest;
2
3 import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
4
5 import com.ibatis.common.resources.Resources;
6 import com.ibatis.sqlmap.client.SqlMapClient;
7 import com.ibatis.sqlmap.client.SqlMapClientBuilder;
8 import com.tc.object.config.ConfigVisitor;
9 import com.tc.object.config.DSOClientConfigHelper;
10 import com.tc.object.config.spec.CyclicBarrierSpec;
11 import com.tc.simulator.app.ApplicationConfig;
12 import com.tc.simulator.listener.ListenerProvider;
13 import com.tc.util.Assert;
14 import com.tctest.domain.Account;
15 import com.tctest.domain.Customer;
16 import com.tctest.runner.AbstractTransparentApp;
17 import com.tctest.server.HSqlDBServer;
18
19 import java.io.Reader JavaDoc;
20 import java.sql.Connection JavaDoc;
21 import java.sql.PreparedStatement JavaDoc;
22 import java.sql.SQLException JavaDoc;
23
24 public class IBatisSimpleTestApp extends AbstractTransparentApp {
25   private CyclicBarrier barrier;
26
27   private SqlMapClient sqlMapper;
28
29   private Customer cus;
30   private HSqlDBServer dbServer = null;
31
32   public IBatisSimpleTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
33     super(appId, cfg, listenerProvider);
34     barrier = new CyclicBarrier(getParticipantCount());
35   }
36
37   public void run() {
38     try {
39       int id = barrier.barrier();
40
41       try {
42
43         if (id == 0) {
44
45           setupDatabaseResource();
46
47           Account acc = new Account();
48           acc.setNumber("ASI-001");
49           insertAccount(acc);
50           Customer cus = new Customer();
51           cus.setEmailAddress("asi@yahoo.com");
52           cus.setFirstName("Antonio");
53           cus.setLastName("Si");
54           cus.setAccount(acc);
55           insertCustomer(cus);
56
57         }
58
59         barrier.barrier();
60
61         if (id == 0) {
62           Customer cus1 = selectCustomerById(0);
63           cus = cus1;
64         }
65         barrier.barrier();
66
67         if (id == 1) {
68           Account acc = cus.getAccount();
69           Assert.assertEquals("ASI-001", acc.getNumber());
70         }
71
72         barrier.barrier();
73
74       } finally {
75         if (id == 0) {
76           shutdownDatabase();
77         }
78       }
79
80     } catch (Throwable JavaDoc e) {
81       notifyError(e);
82     }
83
84   }
85
86   private void setupDatabaseResource() throws Exception JavaDoc {
87     sqlMapper = connectDatabase();
88
89     Connection JavaDoc conn = sqlMapper.getDataSource().getConnection();
90     PreparedStatement JavaDoc stmt = conn
91         .prepareStatement("create table ACCOUNT (acc_id int not null, acc_number varchar(80) null, constraint pk_acc_id primary key (acc_id))");
92     stmt.execute();
93
94     stmt = conn
95         .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))");
96     stmt.execute();
97   }
98
99   private SqlMapClient connectDatabase() throws Exception JavaDoc {
100     dbServer = new HSqlDBServer();
101     dbServer.start();
102
103     Reader JavaDoc reader = Resources.getResourceAsReader("com/tctest/SqlMapConfig.xml");
104     SqlMapClient sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
105     reader.close();
106
107     return sqlMapper;
108   }
109
110   private void shutdownDatabase() throws Exception JavaDoc {
111     dbServer.stop();
112   }
113
114   public Customer selectCustomerById(int id) throws SQLException JavaDoc {
115     return (Customer) sqlMapper.queryForObject("selectCustomerById", new Integer JavaDoc(id));
116   }
117
118   public void insertAccount(Account acc) throws SQLException JavaDoc {
119     sqlMapper.insert("insertAccount", acc);
120   }
121
122   public void insertCustomer(Customer customer) throws SQLException JavaDoc {
123     sqlMapper.insert("insertCustomer", customer);
124   }
125
126   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
127     String JavaDoc testClass = IBatisSimpleTestApp.class.getName();
128
129     config.getOrCreateSpec(testClass).addRoot("barrier", "barrier").addRoot("cus", "cus");
130
131     config.addWriteAutolock("* " + testClass + "*.*(..)");
132     config.addIncludePattern("com.tctest.domain.Account");
133     config.addIncludePattern("com.tctest.domain.Customer");
134     new CyclicBarrierSpec().visit(visitor, config);
135
136     config.addNewModule("clustered-cglib-2.1.3", "1.0.0");
137     config.addNewModule("clustered-iBatis-2.2.0", "1.0.0");
138   }
139
140 }
141
Popular Tags