KickJava   Java API By Example, From Geeks To Geeks.

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


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.SQLException JavaDoc;
9 import java.sql.Statement JavaDoc;
10 import java.util.ArrayList JavaDoc;
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 JavaDoc statements;
24     private int seed;
25     private boolean exitOnError = true;
26     private Bnf bnf;
27
28     private void processException(String JavaDoc sql, SQLException JavaDoc 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 JavaDoc getDatabaseName() {
38 // return "dataSynth/randomsql" + dbId+";TRACE_LEVEL_FILE=3";
39
return "dataSynth/randomsql" + dbId;
40     }
41     
42     private Connection JavaDoc connect() throws Exception JavaDoc {
43         while(true) {
44             try {
45                 return getConnection(getDatabaseName());
46             } catch(SQLException JavaDoc e) {
47                 dbId--;
48                 try {
49                     deleteDb(getDatabaseName());
50                 } catch(Exception JavaDoc e2) {
51                     // ignore
52
}
53                 dbId++;
54                 try {
55                     deleteDb(getDatabaseName());
56                 } catch(Exception JavaDoc e2) {
57                     // ignore
58
}
59                 dbId++;
60                 try {
61                     deleteDb(getDatabaseName());
62                 } catch(SQLException JavaDoc e2) {
63                     dbId++;
64                     deleteDb(getDatabaseName());
65                 }
66             }
67         }
68         
69     }
70     
71     public TestBase init(TestAll conf) throws Exception JavaDoc {
72         super.init(conf);
73         bnf = Bnf.getInstance(null);
74         bnf.linkStatements();
75         statements = bnf.getStatements();
76         
77         // go backwards so we can append at the end
78
for(int i=statements.size() - 1; i>=0; i--) {
79             RuleHead r = (RuleHead) statements.get(i);
80             String JavaDoc 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 JavaDoc {
106         config.getRandom().setSeed(seed);
107         Connection JavaDoc conn = null;
108         try {
109             conn = connect();
110         } catch(SQLException JavaDoc e) {
111             processException("connect", e);
112             conn = connect();
113         }
114         Statement JavaDoc 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 JavaDoc 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 JavaDoc e) {
130                     processException(rand, e);
131                 }
132             }
133         }
134         try {
135             conn.close();
136         } catch(SQLException JavaDoc e) {
137             processException("conn.close", e);
138         }
139     }
140     
141     public void testCase(int i) throws Exception JavaDoc {
142         String JavaDoc old = Constants.SCRIPT_DIRECTORY;
143         Constants.SCRIPT_DIRECTORY = "dataScript/";
144         seed = i;
145         printTime("TestRandomSQL " + seed);
146         try {
147             deleteDb(getDatabaseName());
148         } catch(SQLException JavaDoc e) {
149             processException("deleteDb", e);
150         }
151         testWithSeed(bnf);
152         Constants.SCRIPT_DIRECTORY = old;
153     }
154
155     public void test() throws Exception JavaDoc {
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