KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > test > TestSelf


1 /* Copyright (c) 1995-2000, The Hypersonic SQL Group.
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the Hypersonic SQL Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE HYPERSONIC SQL GROUP,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * This software consists of voluntary contributions made by many individuals
31  * on behalf of the Hypersonic SQL Group.
32  *
33  *
34  * For work added by the HSQL Development Group:
35  *
36  * Copyright (c) 2001-2005, The HSQL Development Group
37  * All rights reserved.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions are met:
41  *
42  * Redistributions of source code must retain the above copyright notice, this
43  * list of conditions and the following disclaimer.
44  *
45  * Redistributions in binary form must reproduce the above copyright notice,
46  * this list of conditions and the following disclaimer in the documentation
47  * and/or other materials provided with the distribution.
48  *
49  * Neither the name of the HSQL Development Group nor the names of its
50  * contributors may be used to endorse or promote products derived from this
51  * software without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
54  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
57  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
58  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
59  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
60  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
61  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
62  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
63  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64  */

65
66
67 package org.hsqldb.test;
68
69 import java.io.File JavaDoc;
70 import java.sql.Connection JavaDoc;
71 import java.sql.DatabaseMetaData JavaDoc;
72 import java.sql.Date JavaDoc;
73 import java.sql.DriverManager JavaDoc;
74 import java.sql.PreparedStatement JavaDoc;
75 import java.sql.ResultSet JavaDoc;
76 import java.sql.Statement JavaDoc;
77 import java.sql.Types JavaDoc;
78
79 import org.hsqldb.lib.Sort;
80
81 /**
82  * Main test class, containing several JDBC and script based tests to
83  * verify correct operation of the engine.<p>
84  *
85  * The tests consist of the following:
86  * <ul>
87  * <li>
88  * Built-in tests for operations, especially those relating to JDBC.
89  *</li>
90  * <li>
91  * Speed tests using insert / delete / update on a simple table.<p>
92  *</li>
93  * <li>
94  * Script based SQL tests consisting of:<p>
95  * <code>TestSelf.txt</code> : the main test script.<p>
96  * <code>TestSelfXXXX.txt</code> : specialised test scripts that
97  * will be run in alphabetical filename order.<p>
98  *</li>
99  * </ul>
100  *
101  * Tests can be added by writing new scripts in the standard format described
102  * in <code>TestSelf.txt</code> and naming the script in the correct format,
103  * <code>TestSelfXXXX.txt</code>, where XXXX is the description of the new
104  * test.<p>
105  * The database can be shutdown at the end of each script (using the
106  * SHUTDOWN command). This allows a test to be divided into two or more
107  * scripts in order to test the persistence mechanism for both objects
108  * created via DDL or data stored in the database. An example of this
109  * is the set of supplied scripts, <code>TestSelfCreate.txt</code>,
110  * <code>TestSelfModify.txt</code> and <code>TestSelfVerify.txt</code>.
111  * (fredt@users)
112  *
113  * @author Thomas Mueller (Hypersonic SQL Group)
114   * @version 1.7.2
115   * @since Hypersonic SQL
116  */

117 class TestSelf extends TestUtil {
118
119     /**
120      * This test in invoked from the command line using:
121      * <pre>
122      * TestSelf [records [-m]]
123      *
124      * </pre>
125      *
126      * -m means run the tests in-memory only
127      *
128      * @param argv
129      */

130     public static void main(String JavaDoc[] argv) {
131
132         print("Usage: TestSelf [records [-m]] (-m means in-memory only)");
133
134         int max = 500;
135
136         if (argv.length >= 1) {
137             max = Integer.parseInt(argv[0]);
138         }
139
140         boolean persistent = true;
141         boolean update = false;
142
143         if (argv.length >= 2) {
144             String JavaDoc a1 = argv[1];
145
146             if (a1.equals("-m")) {
147                 persistent = false;
148             }
149         }
150
151         test(max, persistent);
152     }
153
154     /**
155      * Method declaration
156      *
157      * @param max
158      * @param persistent
159      */

160     static void test(int max, boolean persistent) {
161
162         // DriverManager.setLogStream(System.out);
163
try {
164             DriverManager.registerDriver(new org.hsqldb.jdbcDriver());
165
166             if (persistent) {
167                 testPersistence();
168                 deleteDatabase("test2");
169                 test("jdbc:hsqldb:test2", "sa", "", true);
170                 testPerformance("jdbc:hsqldb:test2", "sa", "", max, true);
171             }
172
173             test("jdbc:hsqldb:.", "sa", "", false);
174             testPerformance("jdbc:hsqldb:.", "sa", "", max, false);
175         } catch (Exception JavaDoc e) {
176             print("TestSelf error: " + e.getMessage());
177             e.printStackTrace();
178         }
179     }
180
181     static void delete(String JavaDoc file) {
182
183         try {
184             new File JavaDoc(file).delete();
185         } catch (Exception JavaDoc e) {}
186     }
187
188     static void deleteDatabase(String JavaDoc path) {
189
190         delete(path + ".backup");
191         delete(path + ".properties");
192         delete(path + ".script");
193         delete(path + ".data");
194         delete(path + ".log");
195     }
196
197     static void test(String JavaDoc url, String JavaDoc user, String JavaDoc password,
198                      boolean persistent) throws Exception JavaDoc {
199
200         String JavaDoc name = persistent ? "Persistent"
201                                  : "Memory";
202
203         print(name);
204
205         Connection JavaDoc cConnection = null;
206
207         try {
208             cConnection = DriverManager.getConnection(url, user, password);
209         } catch (Exception JavaDoc e) {
210             e.printStackTrace();
211             print("TestSelf init error: " + e.getMessage());
212         }
213
214         testMainScript(cConnection, persistent);
215         testTabProfile(cConnection, persistent);
216         testMarotest(cConnection, persistent);
217         cConnection.createStatement().execute("SHUTDOWN");
218         cConnection.close();
219     }
220
221     static void testPersistence() {
222
223         deleteDatabase("test1");
224
225         try {
226             String JavaDoc url = "jdbc:hsqldb:test1;sql.enforce_strict_size=true";
227             String JavaDoc user = "sa";
228             String JavaDoc password = "";
229             Connection JavaDoc cConnection = null;
230             String JavaDoc[] filelist;
231             String JavaDoc absolute = new File JavaDoc("TestSelf.txt").getAbsolutePath();
232
233             filelist = new File JavaDoc(new File JavaDoc(absolute).getParent()).list();
234
235             Sort.sort((Object JavaDoc[]) filelist, new Sort.StringComparator(), 0,
236                       filelist.length - 1);
237
238             for (int i = 0; i < filelist.length; i++) {
239                 String JavaDoc fname = filelist[i];
240
241                 if (fname.startsWith("TestSelf") && fname.endsWith(".txt")
242                         &&!fname.equals("TestSelf.txt")) {
243                     print("Openning DB");
244
245                     cConnection = DriverManager.getConnection(url, user,
246                             password);
247
248                     testScript(cConnection, fname);
249                     cConnection.close();
250                 }
251             }
252         } catch (Exception JavaDoc e) {
253             e.printStackTrace();
254             print("TestSelf init error: " + e.getMessage());
255         }
256     }
257
258     static void testMainScript(Connection JavaDoc cConnection, boolean persistent) {
259
260         String JavaDoc name = persistent ? "Persistent"
261                                  : "Memory";
262
263         print(name + " TestScript");
264
265         // location of TestSelf.txt relative to the development environment
266
String JavaDoc path = "TestSelf.txt";
267
268         testScript(cConnection, path);
269     }
270
271     static byte[] b1 = {
272         0, 1, -128, 44, 12
273     };
274     static byte[] b2 = {
275         10, 127
276     };
277
278     static void testTabProfile(Connection JavaDoc cConnection, boolean persistent) {
279
280         Statement JavaDoc sStatement = null;
281         ResultSet JavaDoc r;
282         String JavaDoc s = "";
283         long start;
284         boolean bDropError = false;
285         String JavaDoc name = persistent ? "Persistent"
286                                           : "Memory";
287
288         print(name + " TabProfile");
289
290         try {
291             sStatement = cConnection.createStatement();
292         } catch (Exception JavaDoc e) {
293             e.printStackTrace();
294             print("TabProfile init error: " + e.getMessage());
295
296             return;
297         }
298
299         try {
300
301             // prepared statements
302
s = "create table TabProfile(id int primary key,"
303                 + "car char,won bit,licence varbinary,"
304                 + "name char,sex char,chance double,birthday date,temp char)";
305
306             sStatement.execute(s);
307
308             s = "insert into TabProfile values ( ?, ?, ?, ?,"
309                 + "'\"John\" the bird''s best friend', 'M',?,?,'')";
310
311             PreparedStatement JavaDoc p = cConnection.prepareStatement(s);
312
313             p.clearParameters();
314             p.setInt(1, 10);
315             p.setString(2, "Matchcartoon");
316             p.setBoolean(3, true);
317             p.setBytes(4, b1);
318             p.setDouble(5, 50.5);
319             p.setNull(6, Types.DATE);
320             p.executeUpdate();
321             p.clearParameters();
322             p.setInt(1, -2);
323             p.setString(2, "\"Birdie\"'s car ?");
324             p.setBoolean(3, false);
325
326             byte[] b2 = {
327                 10, 127
328             };
329
330             p.setBytes(4, b2);
331             p.setDouble(5, -3.1415e-20);
332
333             java.util.Calendar JavaDoc cal = java.util.Calendar.getInstance();
334
335             cal.set(2000, 2, 29);
336
337             // fredt@users - who designed the java.util.Calendar API?
338
p.setDate(6, new Date JavaDoc(cal.getTime().getTime()));
339             p.executeUpdate();
340             readTabProfileTest(sStatement);
341
342             byte[] b2n;
343             byte[] b1n;
344             boolean mismatch;
345
346             s = "select \"org.hsqldb.lib.ArrayUtil.containsAt\"(licence,0, ?) "
347                 + "from TabProfile";
348             p = cConnection.prepareStatement(s);
349
350             p.setBytes(1, b2);
351
352             r = p.executeQuery();
353
354             r.next();
355
356             boolean boo1 = r.getBoolean(1);
357
358             r.next();
359
360             boolean boo2 = r.getBoolean(1);
361
362             // test boo1 != boo2
363

364 /** @todo fredt - nested procedure call doesn't parse */
365 /*
366             s = "select \"org.hsqldb.lib.StringConverter.hexToByte\""
367                 + "(\"org.hsqldb.lib.StringConverter.byteToHex\"(car)) "
368                 + "from TabProfile";
369             r = sStatement.executeQuery(s);
370
371             r.next();
372
373             b1n = r.getBytes(1);
374
375             r.next();
376
377             b1n = r.getBytes(1);
378 */

379
380 /** @todo fredt - alias does not resolve */
381 /*
382             s = "select \"org.hsqldb.lib.StringConverter.byteToHex\"(car) temp, " +
383                 "\"org.hsqldb.lib.StringConverter.hexToByte\"(temp) "
384                 + "from TabProfile";
385             r = sStatement.executeQuery(s);
386
387             r.next();
388
389             b1n = r.getBytes(2);
390
391             r.next();
392
393             b1n = r.getBytes(2);
394 */

395             s = "update tabprofile set temp = \"org.hsqldb.lib.StringConverter.byteToHex\"(licence)";
396
397             sStatement.executeUpdate(s);
398
399             s = "select \"org.hsqldb.lib.StringConverter.hexToByte\"(temp) "
400                 + "from TabProfile order by id desc";
401             r = sStatement.executeQuery(s);
402
403             r.next();
404
405             b1n = r.getBytes(1);
406
407             for (int i = 0; i < b1n.length; i++) {
408                 if (b1[i] != b1n[i]) {
409                     mismatch = true;
410                 }
411             }
412
413             r.next();
414
415             b2n = r.getBytes(1);
416
417             for (int i = 0; i < b2n.length; i++) {
418                 if (b2[i] != b2n[i]) {
419                     mismatch = true;
420                 }
421             }
422
423 // s = "drop table TabProfile";
424
// sStatement.execute(s);
425
s = "create table obj(id int,o object)";
426
427             sStatement.execute(s);
428
429             s = "insert into obj values(?,?)";
430             p = cConnection.prepareStatement(s);
431
432             p.setInt(1, 1);
433
434             int[] ia1 = {
435                 1, 2, 3
436             };
437
438             p.setObject(2, ia1);
439             p.executeUpdate();
440             p.clearParameters();
441             p.setInt(1, 2);
442
443             java.awt.Rectangle JavaDoc r1 = new java.awt.Rectangle JavaDoc(10, 11, 12, 13);
444
445             p.setObject(2, r1);
446             p.executeUpdate();
447
448             r = sStatement.executeQuery("SELECT o FROM obj ORDER BY id DESC");
449
450             r.next();
451
452             java.awt.Rectangle JavaDoc r2 = (java.awt.Rectangle JavaDoc) r.getObject(1);
453
454             if (r2.x != 10 || r2.y != 11 || r2.width != 12
455                     || r2.height != 13) {
456                 throw new Exception JavaDoc("Object data error: Rectangle");
457             }
458
459             r.next();
460
461             int[] ia2 = (int[]) (r.getObject(1));
462
463             if (ia2[0] != 1 || ia2[1] != 2 || ia2[2] != 3
464                     || ia2.length != 3) {
465                 throw new Exception JavaDoc("Object data error: int[]");
466             }
467
468 // s = "drop table obj";
469
// sStatement.execute(s);
470
sStatement.close();
471         } catch (Exception JavaDoc e) {
472             print("");
473             print("TabProfile error: " + e);
474             print("with SQL command: " + s);
475             e.printStackTrace();
476         }
477     }
478
479     static void readTabProfileTest(Statement JavaDoc sStatement) throws Exception JavaDoc {
480
481         String JavaDoc s = "select * from TabProfile where id=-2";
482         ResultSet JavaDoc r = sStatement.executeQuery(s);
483
484         r.next();
485
486         if (!r.getString(2).equals("\"Birdie\"'s car ?")) {
487             throw new Exception JavaDoc("Unicode error.");
488         }
489
490         boolean mismatch = false;
491         byte[] b2n = r.getBytes(4);
492
493         for (int i = 0; i < b2n.length; i++) {
494             if (b2[i] != b2n[i]) {
495                 mismatch = true;
496             }
497         }
498
499         r.close();
500
501         s = "select * from TabProfile where id=10";
502         r = sStatement.executeQuery(s);
503
504         r.next();
505
506         byte[] b1n = r.getBytes(4);
507
508         for (int i = 0; i < b1n.length; i++) {
509             if (b1[i] != b1n[i]) {
510                 mismatch = true;
511             }
512         }
513
514         r.close();
515     }
516
517     static void testMarotest(Connection JavaDoc cConnection, boolean persistent) {
518
519         Statement JavaDoc sStatement = null;
520         ResultSet JavaDoc r;
521         String JavaDoc s = "";
522         long start;
523         boolean bDropError = false;
524         String JavaDoc name = persistent ? "Persistent"
525                                           : "Memory";
526
527         print(name + " Marotest");
528
529         try {
530             sStatement = cConnection.createStatement();
531         } catch (Exception JavaDoc e) {
532             e.printStackTrace();
533             print("Marotest init error: " + e.getMessage());
534         }
535
536         try {
537
538             // test duplicate keys & small transaction rollback
539
s = "CREATE TABLE marotest (id int PRIMARY KEY, dat int);"
540                 + "INSERT INTO marotest VALUES (1,0);"
541                 + "INSERT INTO marotest VALUES (2,0);"
542                 + "INSERT INTO marotest VALUES (2,0);";
543
544             try {
545                 sStatement.execute(s);
546
547                 s = "";
548             } catch (Exception JavaDoc e) {}
549
550             if (s.equals("")) {
551                 throw new Exception JavaDoc("Duplicate key gave no error on insert");
552             }
553
554             try {
555                 s = "UPDATE marotest SET id=1, dat=-1 WHERE dat=0";
556
557                 sStatement.execute(s);
558
559                 s = "";
560             } catch (Exception JavaDoc e) {}
561
562             if (s.equals("")) {
563                 throw new Exception JavaDoc("Duplicate key gave no error on update");
564             }
565
566             int count = 0;
567
568             s = "SELECT *, id as marotest_id FROM marotest";
569             r = sStatement.executeQuery(s);
570
571             while (r.next()) {
572                 r.getFloat(1);
573                 r.getString("ID");
574                 r.getInt("DAT");
575                 r.getInt("MAROTEST_ID");
576
577                 if (r.getShort("DAT") != 0) {
578                     throw new Exception JavaDoc("Bad update worked");
579                 }
580
581                 r.getLong("DAT");
582                 r.getString(2);
583                 r.getObject("ID");
584                 r.clearWarnings();
585
586                 try {
587
588                     // this must throw an error
589
r.getTimestamp("Timestamp?");
590
591                     count = 99;
592                 } catch (Exception JavaDoc e) {}
593
594                 count++;
595             }
596
597             r.close();
598
599             if (count != 2) {
600                 throw new Exception JavaDoc("Should have 2 but has " + count
601                                     + " rows");
602             }
603
604             // test database meta data
605
DatabaseMetaData JavaDoc dbMeta = cConnection.getMetaData();
606
607             r = dbMeta.getColumns(null, "DBO", "MAROTEST", "%");
608
609             while (r.next()) {
610                 s = r.getString(4).trim(); // COLUMN_NAME
611

612                 int i = r.getInt(5); // DATA_TYPE
613

614                 s += i + r.getString("TYPE_NAME");
615                 i = r.getInt(7); // COLUMN_SIZE
616
i = r.getInt(9); // "Decimal_Digits"
617
i = r.getInt(11); // NULLABLE
618
s = s.toUpperCase();
619
620                 if (!s.equals("ID4INTEGER") &&!s.equals("DAT4INTEGER")) {
621                     throw new Exception JavaDoc("Wrong database meta data");
622                 }
623             }
624
625             s = "DROP TABLE marotest";
626
627             sStatement.execute(s);
628             sStatement.close();
629         } catch (Exception JavaDoc e) {
630             print("");
631             print("Marotest error: " + e);
632             print("with SQL command: " + s);
633             e.printStackTrace();
634         }
635     }
636
637     static void testPerformance(String JavaDoc url, String JavaDoc user, String JavaDoc password,
638                                 int max,
639                                 boolean persistent) throws Exception JavaDoc {
640
641         if (persistent) {
642             deleteDatabase("test2");
643         }
644
645         Statement JavaDoc sStatement = null;
646         Connection JavaDoc cConnection = null;
647         ResultSet JavaDoc r;
648         String JavaDoc s = "";
649         long start;
650         boolean bDropError = false;
651         String JavaDoc name = persistent ? "Persistent"
652                                            : "Memory";
653
654         print(name + " Performance");
655
656         try {
657             cConnection = DriverManager.getConnection(url, user, password);
658             sStatement = cConnection.createStatement();
659         } catch (Exception JavaDoc e) {
660             e.printStackTrace();
661             print("TestSelf init error: " + e.getMessage());
662         }
663
664         try {
665
666             // cache, index and performance tests
667
s = "CREATE CACHED TABLE Addr(ID INT PRIMARY KEY,First CHAR,"
668                 + "Name CHAR,ZIP INT)";
669
670             sStatement.execute(s);
671
672             s = "CREATE INDEX iName ON Addr(Name)";
673
674             sStatement.execute(s);
675
676             s = "SET WRITE_DELAY TRUE";
677
678             sStatement.execute(s);
679
680             start = System.currentTimeMillis();
681
682             for (int i = 0; i < max; i++) {
683                 s = "INSERT INTO Addr VALUES(" + i + ",'Marcel" + i + "',"
684                     + "'Renggli" + (max - i - (i % 31)) + "',"
685                     + (3000 + i % 100) + ")";
686
687                 if (sStatement.executeUpdate(s) != 1) {
688                     throw new Exception JavaDoc("Insert failed");
689                 }
690
691                 if (i % 100 == 0) {
692                     printStatus("insert ", i, max, start);
693                 }
694             }
695
696             printStatus("insert ", max, max, start);
697             print("");
698
699             s = "SELECT COUNT(*) FROM Addr";
700             r = sStatement.executeQuery(s);
701
702             r.next();
703
704             int c = r.getInt(1);
705
706             if (c != max) {
707                 throw new Exception JavaDoc("Count should be " + (max) + " but is "
708                                     + c);
709             }
710
711             if (persistent) {
712
713                 // close & reopen to test backup
714
cConnection.close();
715
716                 cConnection = DriverManager.getConnection(url, user,
717                         password);
718                 sStatement = cConnection.createStatement();
719             }
720
721             start = System.currentTimeMillis();
722
723             for (int i = 0; i < max; i++) {
724                 s = "UPDATE Addr SET Name='Robert" + (i + (i % 31))
725                     + "' WHERE ID=" + i;
726
727                 if (sStatement.executeUpdate(s) != 1) {
728                     throw new Exception JavaDoc("Update failed");
729                 }
730
731                 if (i % 100 == 0) {
732                     printStatus("updated ", i, max, start);
733
734                     // s="SELECT COUNT(*) FROM Addr";
735
// r=sStatement.executeQuery(s);
736
// r.next();
737
// int c=r.getInt(1);
738
// if(c!=max) {
739
// throw new Exception("Count should be "+max+" but is "+c);
740
// }
741
}
742             }
743
744             printStatus("update ", max, max, start);
745             print("");
746
747             if (persistent) {
748                 s = "SHUTDOWN IMMEDIATELY";
749
750                 sStatement.execute(s);
751
752                 // open the database; it must be restored after shutdown
753
cConnection.close();
754
755                 cConnection = DriverManager.getConnection(url, user,
756                         password);
757                 sStatement = cConnection.createStatement();
758             }
759
760             start = System.currentTimeMillis();
761
762             for (int i = 0; i < max; i++) {
763                 s = "DELETE FROM Addr WHERE ID=" + (max - 1 - i);
764
765                 if (sStatement.executeUpdate(s) != 1) {
766                     throw new Exception JavaDoc("Delete failed");
767                 }
768
769                 if (i % 100 == 0) {
770                     printStatus("deleting ", i, max, start);
771
772                     // s="SELECT COUNT(*) FROM Addr";
773
// r=sStatement.executeQuery(s);
774
// r.next();
775
// int c=r.getInt(1);
776
// if(c!=max-i-1) {
777
// throw new Exception("Count should be "+(max-i-1)+" but is "+c);
778
// }
779
}
780             }
781
782             printStatus("delete ", max, max, start);
783             print("");
784             sStatement.execute("DROP TABLE Addr");
785         } catch (Exception JavaDoc e) {
786             print("");
787             print("TestSelf error: " + e);
788             print("with SQL command: " + s);
789             e.printStackTrace();
790         }
791
792         cConnection.close();
793         print("Test finished");
794     }
795
796     /**
797      * Method declaration
798      *
799      * @param s
800      * @param i
801      * @param max
802      * @param start
803      */

804     static void printStatus(String JavaDoc s, int i, int max, long start) {
805
806         System.out.print(s + ": " + i + "/" + max + " " + (100 * i / max)
807                          + "% ");
808
809         long now = System.currentTimeMillis();
810
811         if (now > start) {
812             System.out.print((i * 1000 / (now - start)));
813         }
814
815         System.out.print(" rows/s \r");
816     }
817 }
818
Popular Tags