KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > test > db > TestListener


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.db;
6
7 import java.sql.Connection JavaDoc;
8 import java.sql.PreparedStatement JavaDoc;
9 import java.sql.SQLException JavaDoc;
10 import java.sql.Statement JavaDoc;
11
12 import org.h2.api.DatabaseEventListener;
13 import org.h2.test.TestBase;
14
15 public class TestListener extends TestBase implements DatabaseEventListener {
16     
17     private long last, start;
18     
19     public TestListener() {
20         start = last = System.currentTimeMillis();
21     }
22
23     public void test() throws Exception JavaDoc {
24         if(config.networked) {
25             return;
26         }
27         deleteDb("listener");
28         Connection JavaDoc conn;
29         conn = getConnection("listener");
30         Statement JavaDoc stat = conn.createStatement();
31         stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR)");
32         PreparedStatement JavaDoc prep = conn.prepareStatement("INSERT INTO TEST VALUES(?, 'Test' || SPACE(100))");
33         int len = getSize(100, 100000);
34         for(int i=0; i<len; i++) {
35             prep.setInt(1, i);
36             prep.execute();
37         }
38         crash(conn);
39         
40         conn = getConnection("listener;database_event_listener='" + getClass().getName() + "'");
41         conn.close();
42         
43     }
44
45     public void diskSpaceIsLow(long stillAvailable) throws SQLException JavaDoc {
46         System.out.println("diskSpaceIsLow stillAvailable="+stillAvailable);
47     }
48
49     public void exceptionThrown(SQLException JavaDoc e) {
50         TestBase.logError("exceptionThrown", e);
51     }
52
53     public void setProgress(int state, String JavaDoc name, int current, int max) {
54         long time = System.currentTimeMillis();
55         if(time < last+1000) {
56             return;
57         }
58         last = time;
59         String JavaDoc stateName;
60         switch(state) {
61         case STATE_SCAN_FILE:
62             stateName = "Scan " + name;
63             break;
64         case STATE_CREATE_INDEX:
65             stateName = "Create Index " + name;
66             break;
67         case STATE_RECOVER:
68             stateName = "Recover";
69             break;
70         default:
71             TestBase.logError("unknownn state: " + state, null);
72             stateName = "? " + name;
73         }
74         try {
75             Thread.sleep(1);
76         } catch (InterruptedException JavaDoc e) {
77         }
78         System.out.println("state: " + stateName + " " + (100*current/max) + " " + (time-start));
79     }
80
81     public void closingDatabase() {
82     }
83
84     public void init(String JavaDoc url) {
85     }
86
87 }
88
Popular Tags