1 package ch.qos.logback.classic.db;
2
3 import static org.junit.Assert.assertNotNull;
4
5 import java.io.PrintWriter;
6 import java.sql.Connection;
7 import java.sql.SQLException;
8 import java.sql.Statement;
9 import java.util.Properties;
10
11 import org.hsqldb.Server;
12 import org.hsqldb.ServerConstants;
13 import org.hsqldb.jdbcDriver;
14
15 public class DBAppenderTestFixture {
16
17 public static final String HSQLDB_DRIVER_CLASS = "org.hsqldb.jdbcDriver";
18
19 String url = null;
20 String user = "sa";
21 String password = "";
22 Server server;
23
24
25 HsqlMode mode = HsqlMode.MEM;
26
27 public DBAppenderTestFixture() {
28 }
29
30 public void setUp() throws SQLException {
31
32 switch (mode) {
33 case NET:
34 url = "jdbc:hsqldb:hsql://localhost:4808/test";
35 break;
36 case MEM:
37 url = "jdbc:hsqldb:mem:test;sql.enforce_strict_size=true";
38 server = new Server();
39 server.setDatabaseName(0, "test");
40 server.setDatabasePath(0, url);
41 server.setLogWriter(new PrintWriter(System.out));
42 server.setErrWriter(new PrintWriter(System.out));
43 server.setTrace(false);
44 server.setSilent(false);
45 server.start();
46
47 break;
48 case FILE:
49 url = "jdbc:hsqldb:file:test;sql.enforce_strict_size=true";
50 break;
51
52 }
53
54
55
56
57
58
59
60
61 System.out.println(server.getState());
62
63 int waitCount = 0;
64 while (server.getState() != ServerConstants.SERVER_STATE_ONLINE
65 && waitCount < 5) {
66 try {
67 waitCount++;
68 Thread.sleep(1);
69 } catch (InterruptedException e) {
70 }
71 }
72 createTables();
73 }
74
75 public void tearDown() throws SQLException {
76 dropTables();
77
78 if (mode == HsqlMode.MEM) {
79 server.stop();
80 server = null;
81 }
82 }
83
84 Connection newConnection() throws SQLException {
85 jdbcDriver driver = new jdbcDriver();
86 Properties props = new Properties();
87 props.setProperty("user", user);
88 props.setProperty("password", password);
89 return driver.connect(url, props);
90
91
92 }
93
94 private void createTables() throws SQLException {
95 Connection conn = newConnection();
96 assertNotNull(conn);
97 StringBuffer buf = new StringBuffer();
98 buf.append("CREATE TABLE logging_event (");
99 buf.append("timestmp BIGINT NOT NULL,");
100 buf.append("formatted_message LONGVARCHAR NOT NULL,");
101 buf.append("logger_name VARCHAR(256) NOT NULL,");
102 buf.append("level_string VARCHAR(256) NOT NULL,");
103 buf.append("thread_name VARCHAR(256),");
104 buf.append("reference_flag SMALLINT,");
105 buf.append("caller_filename VARCHAR(256), ");
106 buf.append("caller_class VARCHAR(256), ");
107 buf.append("caller_method VARCHAR(256), ");
108 buf.append("caller_line CHAR(4), ");
109 buf.append("event_id INT NOT NULL IDENTITY);");
110 query(conn, buf.toString());
111
112 buf = new StringBuffer();
113 buf.append("CREATE TABLE logging_event_property (");
114 buf.append("event_id INT NOT NULL,");
115 buf.append("mapped_key VARCHAR(254) NOT NULL,");
116 buf.append("mapped_value LONGVARCHAR,");
117 buf.append("PRIMARY KEY(event_id, mapped_key),");
118 buf.append("FOREIGN KEY (event_id) REFERENCES logging_event(event_id));");
119 query(conn, buf.toString());
120
121 buf = new StringBuffer();
122 buf.append("CREATE TABLE logging_event_exception (");
123 buf.append("event_id INT NOT NULL,");
124 buf.append("i SMALLINT NOT NULL,");
125 buf.append("trace_line VARCHAR(256) NOT NULL,");
126 buf.append("PRIMARY KEY(event_id, i),");
127 buf.append("FOREIGN KEY (event_id) REFERENCES logging_event(event_id));");
128 query(conn, buf.toString());
129 }
130
131 private void dropTables() throws SQLException {
132 Connection conn = newConnection();
133 StringBuffer buf = new StringBuffer();
134 buf.append("DROP TABLE logging_event_exception IF EXISTS;");
135 query(conn, buf.toString());
136
137 buf = new StringBuffer();
138 buf.append("DROP TABLE logging_event_property IF EXISTS;");
139 query(conn, buf.toString());
140
141 buf = new StringBuffer();
142 buf.append("DROP TABLE logging_event IF EXISTS;");
143 query(conn, buf.toString());
144 }
145
146 private void query(Connection conn, String expression) throws SQLException {
147
148 Statement st = null;
149 st = conn.createStatement();
150 int i = st.executeUpdate(expression);
151 if (i == -1) {
152 System.out.println("db error : " + expression);
153 }
154 st.close();
155 }
156
157 public enum HsqlMode {
158 MEM, FILE, NET;
159 }
160 }