001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (license2)
004: * Initial Developer: H2 Group
005: */
006: package org.h2.test.synth;
007:
008: import java.sql.PreparedStatement;
009: import java.sql.ResultSet;
010: import java.sql.SQLException;
011: import java.sql.Statement;
012:
013: import org.h2.test.unit.SelfDestructor;
014:
015: /**
016: * The application code for the {@link TestHalt} application.
017: */
018: public class TestHaltApp extends TestHalt {
019:
020: private int rowCount;
021:
022: public static void main(String[] args) throws Exception {
023: SelfDestructor.startCountdown(60);
024: baseDir = TestHalt.DIR;
025: new TestHaltApp().start(args);
026: }
027:
028: private void execute(Statement stat, String sql)
029: throws SQLException {
030: traceOperation("execute: " + sql);
031: stat.execute(sql);
032: }
033:
034: protected void testInit() throws SQLException {
035: Statement stat = conn.createStatement();
036: // stat.execute("CREATE TABLE TEST(ID IDENTITY, NAME VARCHAR(255))");
037: for (int i = 0; i < 20; i++) {
038: execute(stat, "DROP TABLE IF EXISTS TEST" + i);
039: execute(stat, "CREATE TABLE TEST" + i
040: + "(ID INT PRIMARY KEY, NAME VARCHAR(255))");
041: }
042: for (int i = 0; i < 20; i += 2) {
043: execute(stat, "DROP TABLE TEST" + i);
044: }
045: execute(stat, "DROP TABLE IF EXISTS TEST");
046: execute(
047: stat,
048: "CREATE TABLE TEST(ID BIGINT GENERATED BY DEFAULT AS IDENTITY, NAME VARCHAR(255), DATA CLOB)");
049: }
050:
051: protected void testWaitAfterAppStart() throws Exception {
052: int sleep = 10 + random.nextInt(300);
053: if ((flags & FLAG_NO_DELAY) == 0) {
054: sleep += 1000;
055: }
056: Thread.sleep(sleep);
057: }
058:
059: protected void testCheckAfterCrash() throws Exception {
060: Statement stat = conn.createStatement();
061: ResultSet rs = stat.executeQuery("SELECT COUNT(*) FROM TEST");
062: rs.next();
063: int count = rs.getInt(1);
064: System.out.println("count: " + count);
065: if (count % 2 == 1) {
066: traceOperation("row count: " + count);
067: throw new Exception("Unexpected odd row count");
068: }
069: }
070:
071: protected void appStart() throws SQLException {
072: Statement stat = conn.createStatement();
073: if ((flags & FLAG_NO_DELAY) != 0) {
074: execute(stat, "SET WRITE_DELAY 0");
075: execute(stat, "SET MAX_LOG_SIZE 1");
076: }
077: ResultSet rs = stat.executeQuery("SELECT COUNT(*) FROM TEST");
078: rs.next();
079: rowCount = rs.getInt(1);
080: trace("rows: " + rowCount, null);
081: }
082:
083: protected void appRun() throws Exception {
084: conn.setAutoCommit(false);
085: traceOperation("setAutoCommit false");
086: int rows = 10000 + value;
087: PreparedStatement prepInsert = conn
088: .prepareStatement("INSERT INTO TEST(NAME, DATA) VALUES('Hello World', ?)");
089: PreparedStatement prepUpdate = conn
090: .prepareStatement("UPDATE TEST SET NAME = 'Hallo Welt', DATA = ? WHERE ID = ?");
091: for (int i = 0; i < rows; i++) {
092: Statement stat = conn.createStatement();
093: if ((operations & OP_INSERT) != 0) {
094: if ((flags & FLAG_LOBS) != 0) {
095: String s = getRandomString(random.nextInt(200));
096: prepInsert.setString(1, s);
097: traceOperation("insert " + s);
098: prepInsert.execute();
099: } else {
100: execute(stat,
101: "INSERT INTO TEST(NAME) VALUES('Hello World')");
102: }
103: ResultSet rs = stat.getGeneratedKeys();
104: rs.next();
105: int key = rs.getInt(1);
106: traceOperation("inserted key: " + key);
107: rowCount++;
108: }
109: if ((operations & OP_UPDATE) != 0) {
110: if ((flags & FLAG_LOBS) != 0) {
111: String s = getRandomString(random.nextInt(200));
112: prepUpdate.setString(1, s);
113: int x = random.nextInt(rowCount + 1);
114: prepUpdate.setInt(2, x);
115: traceOperation("update " + s + " " + x);
116: prepUpdate.execute();
117: } else {
118: int x = random.nextInt(rowCount + 1);
119: execute(stat,
120: "UPDATE TEST SET VALUE = 'Hallo Welt' WHERE ID = "
121: + x);
122: }
123: }
124: if ((operations & OP_DELETE) != 0) {
125: int x = random.nextInt(rowCount + 1);
126: traceOperation("deleting " + x);
127: int uc = stat
128: .executeUpdate("DELETE FROM TEST WHERE ID = "
129: + x);
130: traceOperation("updated: " + uc);
131: rowCount -= uc;
132: }
133: traceOperation("rowCount " + rowCount);
134: trace("rows now: " + rowCount, null);
135: if (rowCount % 2 == 0) {
136: traceOperation("commit " + rowCount);
137: conn.commit();
138: trace("committed: " + rowCount, null);
139: }
140: if ((flags & FLAG_NO_DELAY) != 0) {
141: if (random.nextInt(10) == 0 && (rowCount % 2 == 0)) {
142: execute(stat, "CHECKPOINT");
143: }
144: }
145: }
146: traceOperation("rollback");
147: conn.rollback();
148: }
149:
150: }
|