1 5 package org.h2.test.synth; 6 7 import java.sql.Connection ; 8 import java.sql.SQLException ; 9 import java.sql.Statement ; 10 import java.util.ArrayList ; 11 12 import org.h2.bnf.Bnf; 13 import org.h2.bnf.RuleHead; 14 import org.h2.engine.Constants; 15 import org.h2.test.TestAll; 16 import org.h2.test.TestBase; 17 import org.h2.util.RandomUtils; 18 19 public class TestRandomSQL extends TestBase { 20 21 private int dbId; 22 private boolean showSQL; 23 private ArrayList statements; 24 private int seed; 25 private boolean exitOnError = true; 26 private Bnf bnf; 27 28 private void processException(String sql, SQLException e) { 29 if(e.getSQLState().equals("HY000")) { 30 TestBase.logError("new TestRandomSQL().init(test).testCase("+seed+"); // FAIL: " + e.toString(), e); 31 if(exitOnError) { 32 System.exit(0); 33 } 34 } 35 } 36 37 private String getDatabaseName() { 38 return "dataSynth/randomsql" + dbId; 40 } 41 42 private Connection connect() throws Exception { 43 while(true) { 44 try { 45 return getConnection(getDatabaseName()); 46 } catch(SQLException e) { 47 dbId--; 48 try { 49 deleteDb(getDatabaseName()); 50 } catch(Exception e2) { 51 } 53 dbId++; 54 try { 55 deleteDb(getDatabaseName()); 56 } catch(Exception e2) { 57 } 59 dbId++; 60 try { 61 deleteDb(getDatabaseName()); 62 } catch(SQLException e2) { 63 dbId++; 64 deleteDb(getDatabaseName()); 65 } 66 } 67 } 68 69 } 70 71 public TestBase init(TestAll conf) throws Exception { 72 super.init(conf); 73 bnf = Bnf.getInstance(null); 74 bnf.linkStatements(); 75 statements = bnf.getStatements(); 76 77 for(int i=statements.size() - 1; i>=0; i--) { 79 RuleHead r = (RuleHead) statements.get(i); 80 String topic = r.getTopic(); 81 int weight = 0; 82 if(topic.equals("select")) { 83 weight = 50; 84 } else if(topic.equals("createtable")) { 85 weight = 20; 86 } else if(topic.equals("insert")) { 87 weight = 20; 88 } else if(topic.startsWith("update")) { 89 weight = 10; 90 } else if(topic.startsWith("delete")) { 91 weight = 5; 92 } else if(topic.startsWith("drop")) { 93 weight = 5; 94 } 95 if(showSQL) { 96 System.out.println(r.getTopic()); 97 } 98 for(int j=0; j<weight; j++) { 99 statements.add(r); 100 } 101 } 102 return this; 103 } 104 105 private void testWithSeed(Bnf config) throws Exception { 106 config.getRandom().setSeed(seed); 107 Connection conn = null; 108 try { 109 conn = connect(); 110 } catch(SQLException e) { 111 processException("connect", e); 112 conn = connect(); 113 } 114 Statement stat = conn.createStatement(); 115 116 for(int i=0; i<statements.size(); i++) { 117 int sid = config.getRandom().nextInt(statements.size()); 118 RuleHead r = (RuleHead) statements.get(sid); 119 String rand = r.getRule().random(config, 0); 120 if(rand.length() > 0) { 121 try { 122 if(showSQL) { 123 System.out.println(i+" "+rand); 124 } 125 Thread.yield(); 126 if(rand.indexOf("TRACE_LEVEL_SYSTEM_OUT") < 0) { 127 stat.execute(rand); 128 } 129 } catch(SQLException e) { 130 processException(rand, e); 131 } 132 } 133 } 134 try { 135 conn.close(); 136 } catch(SQLException e) { 137 processException("conn.close", e); 138 } 139 } 140 141 public void testCase(int i) throws Exception { 142 String old = Constants.SCRIPT_DIRECTORY; 143 Constants.SCRIPT_DIRECTORY = "dataScript/"; 144 seed = i; 145 printTime("TestRandomSQL " + seed); 146 try { 147 deleteDb(getDatabaseName()); 148 } catch(SQLException e) { 149 processException("deleteDb", e); 150 } 151 testWithSeed(bnf); 152 Constants.SCRIPT_DIRECTORY = old; 153 } 154 155 public void test() throws Exception { 156 exitOnError = false; 157 showSQL = false; 158 for(int a=0; ; a++) { 159 int seed = RandomUtils.nextInt(Integer.MAX_VALUE); 160 testCase(seed); 161 } 162 } 163 164 } 165 | Popular Tags |