KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
8
9 import org.h2.test.TestAll;
10 import org.h2.test.TestBase;
11 import org.h2.util.RandomUtils;
12
13 // TODO hsqldb: call 1||null should return 1 but returns null
14
// TODO hsqldb: call mod(1) should return invalid parameter count but returns null
15
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 JavaDoc databases;
21     private ArrayList JavaDoc commands;
22     private RandomGen random = new RandomGen(this);
23     private boolean showError, showLog;
24     private boolean stopImmediately;
25     private int mode;
26     private String JavaDoc 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 JavaDoc 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 JavaDoc {
47         command.run(db);
48         commands.add(command);
49     }
50     
51     private void addRandomCommands() throws Exception JavaDoc {
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 JavaDoc {
96         random.setSeed(seed);
97         commands = new ArrayList JavaDoc();
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 // for (int i = 0; i < 20; i++) {
110
// Table table = randomTable();
111
// add(Command.getRandomInsert(this, table));
112
// }
113
// for (int i = 0; i < 100; i++) {
114
// Table table = randomTable();
115
// add(Command.getRandomSelect(this, table));
116
// }
117
// for (int i = 0; i < 10; i++) {
118
// Table table = randomTable();
119
// add(Command.getRandomUpdate(this, table));
120
// }
121
// for (int i = 0; i < 30; i++) {
122
// Table table = randomTable();
123
// add(Command.getRandomSelect(this, table));
124
// }
125
// for (int i = 0; i < 50; i++) {
126
// Table table = randomTable();
127
// add(Command.getRandomDelete(this, table));
128
// }
129
// for (int i = 0; i < 10; i++) {
130
// Table table = randomTable();
131
// add(Command.getRandomSelect(this, table));
132
// }
133
// while(true) {
134
// Table table = randomTable();
135
// if(table == null) {
136
// break;
137
// }
138
// add(Command.getDropTable(this, table));
139
// }
140
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 JavaDoc {
153         try {
154             
155             ArrayList JavaDoc results = new ArrayList JavaDoc();
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 // result.log();
162
}
163             }
164             compareResults(results);
165         
166         } catch(Error JavaDoc 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 JavaDoc 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 JavaDoc("Results don't match: original (0): \r\n" + original + "\r\nother:\r\n" + copy);
186                 } else {
187                     throw new Error JavaDoc("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 JavaDoc 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 JavaDoc className, String JavaDoc url, String JavaDoc user, String JavaDoc password, boolean useSentinel) {
208         DbConnection db = new DbConnection(this, className, url, user, password, databases.size(), useSentinel);
209         databases.add(db);
210     }
211     
212     // java -cp .;..\..\java\mysql.jar;..\..\java\ldbc.jar;..\..\java\postgresql-8.0-311.jdbc3.jar org.h2.test.TestAll
213

214     public TestBase init(TestAll conf) throws Exception JavaDoc {
215         super.init(conf);
216         BASE_DIR = "dataSynth";
217         deleteDb("synth");
218         databases = new ArrayList JavaDoc();
219
220 // mode = HSQLDB;
221
// addDatabase("org.hsqldb.jdbcDriver", "jdbc:hsqldb:test", "sa", "" );
222
// addDatabase("org.h2.Driver", "jdbc:h2:synth;mode=hsqldb", "sa", "");
223

224 // mode = POSTGRESQL;
225
// addDatabase("org.postgresql.Driver", "jdbc:postgresql:test", "sa", "sa");
226
// addDatabase("org.h2.Driver", "jdbc:h2:synth;mode=postgresql", "sa", "");
227

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 // addDatabase("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/test", "sa", "");
234
// addDatabase("org.h2.Driver", "jdbc:h2:synth;mode=mysql", "sa", "");
235

236 // addDatabase("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/test", "sa", "");
237
// addDatabase("org.ldbc.jdbc.jdbcDriver", "jdbc:ldbc:mysql://localhost/test", "sa", "");
238
// addDatabase("org.h2.Driver", "jdbc:h2:inmemory:synth", "sa", "");
239

240         // MySQL: NOT is bound to column: NOT ID = 1 means (NOT ID) = 1 instead of NOT (ID=1)
241
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 // stopImmediately = true;
249
// showLog = true;
250
// testRun(110600); // id=27 java.lang.Error: Results don't match: original (0):
251
// System.exit(0);
252

253         
254         BASE_DIR = "data";
255         return this;
256     }
257     
258     public void testCase(int i) throws Exception JavaDoc {
259         BASE_DIR = "dataCrash";
260         deleteDb(BASE_DIR, DIR+"/synth");
261         try {
262             printTime("TestSynth " + i);
263             testRun(i);
264         } catch (Error JavaDoc e) {
265             TestBase.logError("error", e);
266             System.exit(0);
267         }
268         BASE_DIR = "data";
269     }
270     
271     public void test() throws Exception JavaDoc {
272         while(true) {
273             int seed = RandomUtils.nextInt(Integer.MAX_VALUE);
274             testCase(seed);
275         }
276     }
277
278 }
279
Popular Tags