KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > test > synth > TestKill


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.test.synth;
6
7 import java.sql.Connection JavaDoc;
8 import java.sql.PreparedStatement JavaDoc;
9 import java.sql.ResultSet JavaDoc;
10 import java.sql.SQLException JavaDoc;
11 import java.sql.Statement JavaDoc;
12 import java.util.Random JavaDoc;
13
14 import org.h2.test.TestBase;
15
16 public class TestKill extends TestBase {
17
18     Connection JavaDoc conn;
19     int accounts = 10;
20     Random JavaDoc random = new Random JavaDoc(1);
21     private String JavaDoc DIR = "synth";
22     
23     public void test() throws Exception JavaDoc {
24         String JavaDoc connect = "";
25     
26         connect = ";MAX_LOG_SIZE=10;THROTTLE=80";
27         
28         String JavaDoc url = getURL(DIR+"/kill" + connect, true);
29         String JavaDoc user = getUser();
30         String JavaDoc password = getPassword();
31         
32         String JavaDoc[] procDef = new String JavaDoc[]{
33                 "java.exe", "-cp", "bin",
34                 "org.h2.test.synth.TestKillProcess",
35                 url, user, password,
36                 BASE_DIR,
37                 ""+accounts
38         };
39
40         for(int i=0;; i++) {
41             printTime("TestKill " + i);
42             if(i % 10 == 0) {
43                 trace("deleting db...");
44                 deleteDb(BASE_DIR, "kill");
45             }
46             conn = getConnection(url);
47             createTables();
48             checkData();
49             initData();
50             conn.close();
51             Process JavaDoc proc = Runtime.getRuntime().exec(procDef);
52 // while(true) {
53
// int ch = proc.getErrorStream().read();
54
// if(ch < 0) {
55
// break;
56
// }
57
// System.out.print((char)ch);
58
// }
59
int runtime = random.nextInt(10000);
60             trace("running...");
61             Thread.sleep(runtime);
62             trace("stopping...");
63             proc.destroy();
64             proc.waitFor();
65             trace("stopped");
66         }
67     }
68     
69     private void createTables() throws SQLException JavaDoc {
70         trace("createTables...");
71         Statement JavaDoc stat = conn.createStatement();
72         stat.execute("CREATE TABLE IF NOT EXISTS ACCOUNT(ID INT PRIMARY KEY, SUM INT)");
73         stat.execute("CREATE TABLE IF NOT EXISTS LOG(ID IDENTITY, ACCOUNTID INT, AMOUNT INT, FOREIGN KEY(ACCOUNTID) REFERENCES ACCOUNT(ID))");
74         stat.execute("CREATE TABLE IF NOT EXISTS TEST_A(ID INT PRIMARY KEY, DATA VARCHAR)");
75         stat.execute("CREATE TABLE IF NOT EXISTS TEST_B(ID INT PRIMARY KEY, DATA VARCHAR)");
76     }
77
78     private void initData() throws SQLException JavaDoc {
79         trace("initData...");
80         conn.createStatement().execute("DROP TABLE LOG");
81         conn.createStatement().execute("DROP TABLE ACCOUNT");
82         conn.createStatement().execute("DROP TABLE TEST_A");
83         conn.createStatement().execute("DROP TABLE TEST_B");
84         createTables();
85         PreparedStatement JavaDoc prep = conn.prepareStatement("INSERT INTO ACCOUNT VALUES(?, 0)");
86         for(int i=0; i<accounts; i++) {
87             prep.setInt(1, i);
88             prep.execute();
89         }
90         PreparedStatement JavaDoc p1 = conn.prepareStatement("INSERT INTO TEST_A VALUES(?, '')");
91         PreparedStatement JavaDoc p2 = conn.prepareStatement("INSERT INTO TEST_B VALUES(?, '')");
92         for(int i=0; i<accounts; i++) {
93             p1.setInt(1, i);
94             p2.setInt(1, i);
95             p1.execute();
96             p2.execute();
97         }
98     }
99
100     private void checkData() throws Exception JavaDoc {
101         trace("checkData...");
102         ResultSet JavaDoc rs = conn.createStatement().executeQuery("SELECT * FROM ACCOUNT ORDER BY ID");
103         PreparedStatement JavaDoc prep = conn.prepareStatement("SELECT SUM(AMOUNT) FROM LOG WHERE ACCOUNTID=?");
104         while(rs.next()) {
105             int account = rs.getInt(1);
106             int sum = rs.getInt(2);
107             prep.setInt(1, account);
108             ResultSet JavaDoc rs2 = prep.executeQuery();
109             rs2.next();
110             int sumLog = rs2.getInt(1);
111             check(sumLog, sum);
112             trace("account="+account+" sum="+sum);
113         }
114         PreparedStatement JavaDoc p1 = conn.prepareStatement("SELECT * FROM TEST_A WHERE ID=?");
115         PreparedStatement JavaDoc p2 = conn.prepareStatement("SELECT * FROM TEST_B WHERE ID=?");
116         for(int i=0; i<accounts; i++) {
117             p1.setInt(1, i);
118             p2.setInt(1, i);
119             ResultSet JavaDoc r1 = p1.executeQuery();
120             ResultSet JavaDoc r2 = p2.executeQuery();
121             boolean hasData = r1.next();
122             check(r2.next(), hasData);
123             if(hasData) {
124                 String JavaDoc d1 = r1.getString("DATA");
125                 String JavaDoc d2 = r2.getString("DATA");
126                 check(d1, d2);
127                 checkFalse(r1.next());
128                 checkFalse(r2.next());
129                 trace("test: data="+d1);
130             } else {
131                 trace("test: empty");
132             }
133         }
134     }
135
136 }
137
Popular Tags