-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBTester.java
More file actions
392 lines (342 loc) · 15 KB
/
DBTester.java
File metadata and controls
392 lines (342 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import DBCommands.*;
import DBEngine.DBCommandHandler;
import DBEngine.DBStorage;
import DBExceptions.DBException;
import java.io.IOException;
import java.util.ArrayList;
public class DBTester
{
public DBTester(){}
public static void main(String args[])
{
testCMDUse();
testCMDCreate();
testCMDDrop();
testCMDAlter();
testCMDInsert();
testCMDSelect();
testCMDUpdate();
testCMDDelete();
testCMDJoin();
try {
testingScript();
}
catch (IOException ioe) {
ioe.toString();
}
System.out.println("Success: All tests passed!");
}
public static void testCMDUse()
{
ArrayList<String> commands = new ArrayList<>();
commands.add("USE databasename;");
commands.add(" USE databasename ; ");
commands.add("use databasename;");
commands.add(" UsE dataBasename ; ");
try{
for (String s : commands)
{
CMDUse query = new CMDUse(s);
query.parseQuery();
}
}
catch (DBException dbe)
{
System.out.println(dbe.toString());
System.exit(1);
}
System.out.println("Completed CMDUse tests.");
}
public static void testCMDCreate()
{
ArrayList<String> commands = new ArrayList<>();
commands.add("Create database keane;");
commands.add(" CREATE table tablename ; ");
commands.add("create database databasename;");
commands.add(" CreaTE DatAbAse dataBasename ; ");
commands.add("CREATE TABLE tablename;");
commands.add(" CREATE TABLE tablename ; ");
commands.add("create table tablename;");
commands.add("creaTE TAbLE tablename;");
commands.add("CREATE TABLE tablename(name, age, dateofbirth);");
commands.add("CREATE TABLE tablename(name);");
commands.add("CREATE TABLE tablename(name1 , age1);");
commands.add("CREATE TABLE tablename(name1 ,age1 , dateofbirth);");
commands.add("CREATE TABLE tablename(name1 ,age1 , dataeofbirth);");
try{
for (String s : commands) {
CMDCreate query = new CMDCreate(s);
query.parseQuery();
}
}
catch (DBException dbe)
{
System.out.println(dbe.toString());
System.exit(1);
}
System.out.println("Completed CMDCreate tests.");
}
public static void testCMDDrop()
{
ArrayList<String> commands = new ArrayList<>();
commands.add("DROP database db1;");
commands.add(" DROP table table1;");
commands.add("DROP TabLE table1;");
commands.add(" DROP table table1;");
try{
for (String s : commands)
{
CMDDrop query = new CMDDrop(s);
query.parseQuery();
}
}
catch (DBException dbe)
{
System.out.println(dbe.toString());
System.exit(1);
}
System.out.println("Completed CMDDrop tests.");
}
public static void testCMDAlter()
{
ArrayList<String> commands = new ArrayList<>();
commands.add("ALTER TABLE tablename ADD attribute;");
commands.add("ALTER TABLE tablename DROP attribute;");
try {
for (String s : commands) {
CMDAlter query = new CMDAlter(s);
query.parseQuery();
}
} catch (DBException dbe) {
System.out.println(dbe.toString());
System.exit(1);
}
System.out.println("Completed CMDAlter tests.");
}
public static void testCMDInsert()
{
ArrayList<String> commands = new ArrayList<>();
commands.add("INSERT INTO marks VALUES ('Steve', 65, true);");
commands.add("INSERT INTO marks VALUES ('Dave', 55, true);");
commands.add("INSERT INTO marks VALUES ('Bob', 35, false);");
commands.add("INSERT INTO marks VALUES ('Clive', 20, false);");
commands.add("INSERT INTO marks VALUES ('Clive');");
commands.add("INSERT INTO marks VALUES ('Clive', false);");
try{
for (String s : commands)
{
CMDInsert query = new CMDInsert(s);
query.parseQuery();
}
}
catch (DBException dbe)
{
System.out.println(dbe.toString());
System.exit(1);
}
System.out.println("Completed CMDInsert tests.");
}
public static void testCMDSelect()
{
ArrayList<String> commands = new ArrayList<>();
commands.add("SELECT * FROM table1;");
commands.add("SELECT name,age FROM table1 ;");
commands.add(" SELECT * FROM marks WHERE pass == 'true' ; ");
commands.add("SELECT * FROM marks WHERE (pass == true) AND (age>5);");
try {
for (String s : commands) {
CMDSelect query = new CMDSelect(s);
query.parseQuery();
}
} catch (DBException dbe) {
System.out.println(dbe.toString());
System.exit(1);
}
System.out.println("Completed CMDSelect tests.");
}
public static void testCMDUpdate()
{
ArrayList<String> commands = new ArrayList<>();
commands.add("UPDATE marks SET mark = 38 , age = 5 WHERE name == 'Clive';");
try {
for (String s : commands) {
CMDUpdate query = new CMDUpdate(s);
query.parseQuery();
}
} catch (DBException dbe) {
System.out.println(dbe.toString());
System.exit(1);
}
System.out.println("Completed CMDUpdate tests.");
}
public static void testCMDDelete()
{
ArrayList<String> commands = new ArrayList<>();
commands.add("DELETE FROM tablename WHERE name == 'Keane';");
try {
for (String s : commands) {
CMDDelete query = new CMDDelete(s);
query.parseQuery();
}
} catch (DBException dbe) {
System.out.println(dbe.toString());
System.exit(1);
}
System.out.println("Completed CMDDelete tests.");
}
public static void testCMDJoin()
{
ArrayList<String> commands = new ArrayList<>();
commands.add("JOIN table1 AND table2 ON attribute1 AND attribute2;");
try {
for (String s : commands) {
CMDJoin query = new CMDJoin(s);
query.parseQuery();
}
} catch (DBException dbe) {
System.out.println(dbe.toString());
System.exit(1);
}
System.out.println("Completed CMDJoin tests.");
}
public static void testingScript() throws IOException {
DBStorage storage = new DBStorage();
storage.rootStorageInit();
ArrayList<String> commands = new ArrayList<>();
/* Basic/core set of test queries */
commands.add("USE markbook;");
commands.add("DROP TABLE marks;");
commands.add("DROP DATABASE markbook;");
commands.add("CREATE DATABASE markbook;");
commands.add("USE markbook;");
commands.add("CREATE TABLE marks (name, mark, pass);");
commands.add("INSERT INTO marks VALUES ('Steve', 65, true);");
commands.add("INSERT INTO marks VALUES ('Dave', 55, true);");
commands.add("INSERT INTO marks VALUES ('Bob', 35, false);");
commands.add("INSERT INTO marks VALUES ('Clive', 20, false);");
commands.add("SELECT * FROM marks;");
commands.add("SELECT * FROM marks where name != 'Dave';");
commands.add("SELECT * FROM marks WHERE (name == 'Dave') OR (pass == true);");
commands.add("SELECT * FROM marks WHERE pass == true;");
commands.add("UPDATE marks SET mark = 38 WHERE name == 'Clive';");
commands.add("SELECT * FROM marks WHERE name == 'Clive';");
commands.add("DELETE FROM marks WHERE name == 'Dave';");
commands.add("SELECT * FROM marks;");
commands.add("DELETE FROM marks WHERE mark < 40;");
commands.add("SELECT * FROM marks;");
/* More substantial sample database */
commands.add("USE imdb;");
commands.add("DROP TABLE actors;");
commands.add("DROP TABLE movies;");
commands.add("DROP TABLE roles;");
commands.add("DROP DATABASE imdb;");
commands.add("CREATE DATABASE imdb;");
commands.add("USE imdb;");
commands.add("CREATE TABLE actors (name, nationality, awards);");
commands.add("INSERT INTO actors VALUES ('Hugh Grant', 'British', 3);");
commands.add("INSERT INTO actors VALUES ('Toni Collette', 'Australian', 12);");
commands.add("INSERT INTO actors VALUES ('James Caan', 'American', 8);");
commands.add("INSERT INTO actors VALUES ('Emma Thompson', 'British', 10);");
commands.add("CREATE TABLE movies (name, genre);");
commands.add("INSERT INTO movies VALUES ('Mickey Blue Eyes', 'Comedy');");
commands.add("INSERT INTO movies VALUES ('About a Boy', 'Comedy');");
commands.add("INSERT INTO movies VALUES ('Sense and Sensibility', 'Period Drama');");
commands.add("SELECT id FROM movies WHERE name == 'Mickey Blue Eyes';");
commands.add("SELECT id FROM movies WHERE name == 'About a Boy';");
commands.add("SELECT id FROM movies WHERE name == 'Sense and Sensibility';");
commands.add("SELECT id FROM actors WHERE name == 'Hugh Grant';");
commands.add("SELECT id FROM actors WHERE name == 'Toni Collette';");
commands.add("SELECT id FROM actors WHERE name == 'James Caan';");
commands.add("SELECT id FROM actors WHERE name == 'Emma Thompson';");
commands.add("CREATE TABLE roles (name, movieID, actorID);");
commands.add("INSERT INTO roles VALUES ('Edward', 3, 1);");
commands.add("INSERT INTO roles VALUES ('Frank', 1, 3);");
commands.add("INSERT INTO roles VALUES ('Fiona', 2, 2);");
commands.add("INSERT INTO roles VALUES ('Elinor', 3, 4);");
/* Advanced test queries */
commands.add("SELECT * FROM actors WHERE awards < 5;");
commands.add("ALTER TABLE actors ADD age;");
commands.add("SELECT * FROM actors;");
commands.add("UPDATE actors SET age = 45 WHERE name == 'Hugh Grant';");
commands.add("SELECT * FROM actors WHERE name == 'Hugh Grant';");
commands.add("SELECT nationality FROM actors WHERE name == 'Hugh Grant';");
commands.add("ALTER TABLE actors DROP age;");
commands.add("SELECT * FROM actors WHERE name == 'Hugh Grant';");
commands.add("SELECT * FROM actors WHERE (awards > 5) AND (nationality == 'British');");
commands.add("SELECT * FROM (awards > 5) AND ((nationality == 'British') OR (nationality == 'Australian'));");
commands.add("SELECT * FROM actors WHERE name LIKE 'an';");
commands.add("SELECT * FROM actors WHERE awards >= 10;");
commands.add("DELETE FROM actors WHERE name == 'Hugh Grant';");
commands.add("DELETE FROM actors WHERE name == 'James Caan';");
commands.add("DELETE FROM actors WHERE name == 'Emma Thompson';");
commands.add("SELECT * FROM actors;");
commands.add("JOIN actors AND roles ON id AND actorID;");
commands.add("JOIN movies AND roles ON id AND movieID;");
commands.add("DROP TABLE actors;");
commands.add("SELECT * FROM actors;");
commands.add("DROP DATABASE imdb;");
commands.add("USE imdb;");
/* Robustness testing queries (imdb database create again) */
commands.add("CREATE DATABASE imdb;");
commands.add("USE imdb;");
commands.add("CREATE TABLE actors (name, nationality, awards);");
commands.add("INSERT INTO actors VALUES ('Hugh Grant', 'British', 3);");
commands.add("INSERT INTO actors VALUES ('Toni Collette', 'Australian', 12);");
commands.add("INSERT INTO actors VALUES ('James Caan', 'American', 8);");
commands.add("INSERT INTO actors VALUES ('Emma Thompson', 'British', 10);");
commands.add("CREATE TABLE movies (name, genre);");
commands.add("INSERT INTO movies VALUES ('Mickey Blue Eyes', 'Comedy');");
commands.add("INSERT INTO movies VALUES ('About a Boy', 'Comedy');");
commands.add("INSERT INTO movies VALUES ('Sense and Sensibility', 'Period Drama');");
commands.add("SELECT * FROM actors");
commands.add("SELECT * FROM crew;");
commands.add("SELECT spouse FROM actors;");
commands.add("SELECT * FROM actors WHERE name == 'Hugh Grant;");
commands.add("SELECT * FROM actors WHERE name > 10;");
commands.add("SELECT name age FROM actors;");
commands.add("SELECT * FROM actors awards > 10;");
commands.add("SELECT * FROM actors WHERE name LIKE 10;");
commands.add(" SELECT * FROM actors WHERE awards > 10;");
commands.add("USE ebay;");
/* My own test commands */
commands.add("DROP DATABASE imdb;");
commands.add("DROP DATABASE markbook;");
commands.add("CREATE DATABASE testdb;");
commands.add("USE testdb;");
commands.add("CREATE TABLE biodata;");
commands.add("ALTER TABLE biodata ADD name;");
commands.add("ALTER TABLE biodata ADD weight;");
commands.add("ALTER TABLE biodata ADD height;");
commands.add("SELECT * FROM biodata;");
commands.add("INSERT INTO biodata VALUES ('Keane', 85.4, 1.77);");
commands.add("INSERT INTO biodata VALUES ('Sohini', 62.5, 1.54);");
commands.add("INSERT INTO biodata VALUES ('Kenzia', 61.7, 1.65);");
commands.add("INSERT INTO biodata VALUES ('Celine', 64.6, 1.55);");
commands.add("INSERT INTO biodata VALUES ('Michael', 90.1, 1.90);");
commands.add("SELECT * FROM biodata;");
commands.add("SELECT name, height FROM biodata WHERE (name == 'Sohini') AND (name == 'Kenzia');");
commands.add("SELECT name, height FROM biodata WHERE (name == 'Sohini') OR (name == 'Kenzia');");
commands.add("SELECT name FROM biodata WHERE (height >= 1.877) AND (weight <= 90.11);");
commands.add("UPDATE biodata SET name='MichaelChanged',weight=95.0 WHERE (height >= 1.877) AND (weight <= 90.11);");
commands.add("SELECT * FROM biodata;");
commands.add("DELETE FROM biodata WHERE weight <=89.999;");
commands.add("SELECT * FROM biodata;");
commands.add("DROP DATABASE testdb;");
for (String s:commands)
{
try {
DBCommandHandler ch = new DBCommandHandler(s);
System.out.println(s);
System.out.println(ch.handleQuery());
}
catch (DBException dbe)
{
System.out.println(dbe.toString());
}
catch (IOException ioe)
{
System.out.println(ioe.toString());
}
}
}
}