1 5 package org.h2.test.synth; 6 7 import java.sql.PreparedStatement ; 8 import java.sql.ResultSet ; 9 import java.sql.SQLException ; 10 import java.sql.Statement ; 11 12 public class TestHaltApp extends TestHalt { 13 14 private int rowCount; 15 16 public static void main(String [] args) throws Exception { 17 new TestHaltApp().start(args); 18 } 19 20 protected void testInit() throws SQLException { 21 Statement stat = conn.createStatement(); 22 try { 23 stat.execute("DROP TABLE TEST"); 24 } catch(SQLException e) { 25 } 27 for(int i=0; i< 20; i++) { 29 stat.execute("DROP TABLE IF EXISTS TEST" + i); 30 stat.execute("CREATE TABLE TEST"+i +"(ID INT PRIMARY KEY, NAME VARCHAR(255))"); 31 } 32 for(int i=0; i< 20; i+=2) { 33 stat.execute("DROP TABLE TEST" + i); 34 } 35 stat.execute("CREATE TABLE TEST(ID BIGINT GENERATED BY DEFAULT AS IDENTITY, NAME VARCHAR(255), DATA CLOB)"); 36 } 37 38 protected void testWaitAfterAppStart() throws Exception { 39 int sleep = 10 + random.nextInt(300); 40 Thread.sleep(sleep); 41 } 42 43 protected void testCheckAfterCrash() throws Exception { 44 Statement stat = conn.createStatement(); 45 ResultSet rs = stat.executeQuery("SELECT COUNT(*) FROM TEST"); 46 rs.next(); 47 int count = rs.getInt(1); 48 System.out.println("count: " + count); 49 if(count % 2 == 1) { 50 throw new Exception ("Unexpected odd row count"); 51 } 52 } 53 54 protected void appStart() throws SQLException { 55 Statement stat = conn.createStatement(); 56 if((flags & FLAG_NODELAY) != 0) { 57 stat.execute("SET WRITE_DELAY 0"); 58 stat.execute("SET MAX_LOG_SIZE 1"); 59 } 60 ResultSet rs = stat.executeQuery("SELECT COUNT(*) FROM TEST"); 61 rs.next(); 62 rowCount = rs.getInt(1); 63 log("rows: " + rowCount, null); 64 } 65 66 protected void appRun() throws SQLException { 67 conn.setAutoCommit(false); 68 int rows = 10000 + value; 69 PreparedStatement prepInsert = conn.prepareStatement("INSERT INTO TEST(NAME, DATA) VALUES('Hello World', ?)"); 70 PreparedStatement prepUpdate = conn.prepareStatement("UPDATE TEST SET NAME = 'Hallo Welt', DATA = ? WHERE ID = ?"); 71 for(int i=0; i<rows; i++) { 72 Statement stat = conn.createStatement(); 73 if((operations & OP_INSERT) != 0) { 74 if((flags & FLAG_LOBS) != 0) { 75 prepInsert.setString(1, getRandomString(random.nextInt(200))); 76 prepInsert.execute(); 77 } else { 78 stat.execute("INSERT INTO TEST(NAME) VALUES('Hello World')"); 79 } 80 rowCount++; 81 } 82 if((operations & OP_UPDATE) != 0) { 83 if((flags & FLAG_LOBS) != 0) { 84 prepUpdate.setString(1, getRandomString(random.nextInt(200))); 85 prepUpdate.setInt(2, random.nextInt(rowCount+1)); 86 prepUpdate.execute(); 87 } else { 88 stat.execute("UPDATE TEST SET VALUE = 'Hallo Welt' WHERE ID = " + random.nextInt(rowCount+1)); 89 } 90 } 91 if((operations & OP_DELETE) != 0) { 92 int uc = stat.executeUpdate("DELETE FROM TEST WHERE ID = " + random.nextInt(rowCount+1)); 93 rowCount-=uc; 94 } 95 log("rows now: " + rowCount, null); 96 if(rowCount % 2 == 0) { 97 conn.commit(); 98 log("committed: " + rowCount, null); 99 } 100 if((flags & FLAG_NODELAY) != 0) { 101 if(random.nextInt(100) == 0) { 102 stat.execute("CHECKPOINT"); 103 } 104 } 105 } 106 conn.rollback(); 107 } 108 109 } 110 | Popular Tags |