1 5 package org.h2.test.synth; 6 7 import java.util.ArrayList ; 8 9 import org.h2.test.TestAll; 10 import org.h2.test.TestBase; 11 import org.h2.util.RandomUtils; 12 13 public class TestSynth extends TestBase { 16 17 static final int H2 = 0, H2_MEM = 1, HSQLDB = 2, MYSQL = 3, POSTGRESQL = 4; 18 19 private DbState db = new DbState(this); 20 private ArrayList databases; 21 private ArrayList commands; 22 private RandomGen random = new RandomGen(this); 23 private boolean showError, showLog; 24 private boolean stopImmediately; 25 private int mode; 26 private String DIR = "synth"; 27 28 public boolean is(int isType) { 29 return mode == isType; 30 } 31 32 public TestSynth() { 33 } 34 35 public RandomGen random() { 36 return random; 37 } 38 39 public String randomIdentifier() { 40 int len = random.getLog(8)+2; 41 while(true) { 42 return random.randomString(len); 43 } 44 } 45 46 private void add(Command command) throws Exception { 47 command.run(db); 48 commands.add(command); 49 } 50 51 private void addRandomCommands() throws Exception { 52 switch(random.getInt(20)) { 53 case 0: { 54 add(Command.getDisconnect(this)); 55 add(Command.getConnect(this)); 56 break; 57 } 58 case 1: { 59 Table table = Table.newRandomTable(this); 60 add(Command.getCreateTable(this, table)); 61 break; 62 } 63 case 2: { 64 Table table = randomTable(); 65 add(Command.getCreateIndex(this, table.newRandomIndex())); 66 break; 67 } 68 case 3: 69 case 4: 70 case 5: { 71 Table table = randomTable(); 72 add(Command.getRandomInsert(this, table)); 73 break; 74 } 75 case 6: 76 case 7: 77 case 8: { 78 Table table = randomTable(); 79 add(Command.getRandomUpdate(this, table)); 80 break; 81 } 82 case 9: 83 case 10: { 84 Table table = randomTable(); 85 add(Command.getRandomDelete(this, table)); 86 break; 87 } 88 default: { 89 Table table = randomTable(); 90 add(Command.getRandomSelect(this, table)); 91 } 92 } 93 } 94 95 private void testRun(int seed) throws Exception { 96 random.setSeed(seed); 97 commands = new ArrayList (); 98 add(Command.getConnect(this)); 99 add(Command.getReset(this)); 100 101 for(int i=0; i<1; i++) { 102 Table table = Table.newRandomTable(this); 103 add(Command.getCreateTable(this, table)); 104 add(Command.getCreateIndex(this, table.newRandomIndex())); 105 } 106 for(int i=0; i<2000; i++) { 107 addRandomCommands(); 108 } 109 add(Command.getDisconnect(this)); 141 add(Command.getEnd(this)); 142 143 for(int i=0; i<commands.size(); i++) { 144 Command command = (Command) commands.get(i); 145 boolean stop = process(seed, i, command); 146 if(stop) { 147 break; 148 } 149 } 150 } 151 152 private boolean process(int seed, int id, Command command) throws Exception { 153 try { 154 155 ArrayList results = new ArrayList (); 156 for(int i=0; i<databases.size(); i++) { 157 DbInterface db = (DbInterface)databases.get(i); 158 Result result = command.run(db); 159 results.add(result); 160 if(showError && i==0) { 161 } 163 } 164 compareResults(results); 165 166 } catch(Error e) { 167 if(showError) { 168 TestBase.logError("synth", e); 169 } 170 System.out.println("new TestSynth().init(test).testCase(" + seed+"); // id="+id +" " + e.toString()); 171 if(stopImmediately) { 172 System.exit(0); 173 } 174 return true; 175 } 176 return false; 177 } 178 179 private void compareResults(ArrayList results) { 180 Result original = (Result) results.get(0); 181 for (int i = 1; i < results.size(); i++) { 182 Result copy = (Result) results.get(i); 183 if (original.compareTo(copy) != 0) { 184 if (showError) { 185 throw new Error ("Results don't match: original (0): \r\n" + original + "\r\nother:\r\n" + copy); 186 } else { 187 throw new Error ("Results don't match"); 188 } 189 } 190 } 191 } 192 193 public Table randomTable() { 194 return db.randomTable(); 195 } 196 197 public void log(int id, String s) { 198 if(showLog && id==0) { 199 System.out.println(s); 200 } 201 } 202 203 public int getMode() { 204 return mode; 205 } 206 207 private void addDatabase(String className, String url, String user, String password, boolean useSentinel) { 208 DbConnection db = new DbConnection(this, className, url, user, password, databases.size(), useSentinel); 209 databases.add(db); 210 } 211 212 214 public TestBase init(TestAll conf) throws Exception { 215 super.init(conf); 216 BASE_DIR = "dataSynth"; 217 deleteDb("synth"); 218 databases = new ArrayList (); 219 220 224 228 mode = H2_MEM; 229 Class.forName("org.h2.Driver"); 230 addDatabase("org.h2.Driver", "jdbc:h2:mem:synth", "sa", "", true); 231 addDatabase("org.h2.Driver", "jdbc:h2:"+BASE_DIR+"/"+DIR+"/synth", "sa", "", false); 232 233 236 240 for (int i = 0; i < databases.size(); i++) { 242 DbConnection conn = (DbConnection) databases.get(i); 243 System.out.println(i + " = " + conn.toString()); 244 } 245 showError = true; 246 showLog = false; 247 248 253 254 BASE_DIR = "data"; 255 return this; 256 } 257 258 public void testCase(int i) throws Exception { 259 BASE_DIR = "dataCrash"; 260 deleteDb(BASE_DIR, DIR+"/synth"); 261 try { 262 printTime("TestSynth " + i); 263 testRun(i); 264 } catch (Error e) { 265 TestBase.logError("error", e); 266 System.exit(0); 267 } 268 BASE_DIR = "data"; 269 } 270 271 public void test() throws Exception { 272 while(true) { 273 int seed = RandomUtils.nextInt(Integer.MAX_VALUE); 274 testCase(seed); 275 } 276 } 277 278 } 279 | Popular Tags |