View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.db;
15  
16  import static org.junit.Assert.assertNotNull;
17  
18  import java.io.PrintWriter;
19  import java.sql.Connection;
20  import java.sql.SQLException;
21  import java.sql.Statement;
22  import java.util.Properties;
23  
24  import org.hsqldb.Server;
25  import org.hsqldb.jdbcDriver;
26  import org.hsqldb.server.ServerConstants;
27  
28  public class DBAppenderHSQLTestFixture {
29  
30      public static final String HSQLDB_DRIVER_CLASS = "org.hsqldb.jdbcDriver";
31      // String serverProps;
32      String url = null;
33      String user = "sa";
34      String password = "";
35      Server server;
36  
37      // boolean isNetwork = true;
38      HsqlMode mode = HsqlMode.MEM;
39  
40      public void setUp() throws SQLException {
41  
42          switch (mode) {
43          case NET:
44              url = "jdbc:hsqldb:hsql://localhost:4808/test";
45              break;
46          case MEM:
47              url = "jdbc:hsqldb:mem:test;sql.enforce_strict_size=true";
48              server = new Server();
49              server.setDatabaseName(0, "test");
50              server.setDatabasePath(0, url);
51              server.setLogWriter(new PrintWriter(System.out));
52              server.setErrWriter(new PrintWriter(System.out));
53              server.setTrace(false);
54              server.setSilent(false);
55              server.start();
56  
57              break;
58          case FILE:
59              url = "jdbc:hsqldb:file:test;sql.enforce_strict_size=true";
60              break;
61  
62          }
63  
64          // try {
65          // Class.forName(DRIVER_CLASS);
66          // } catch (Exception e) {
67          // e.printStackTrace();
68          // System.out.println(this + ".setUp() error: " + e.getMessage());
69          // }
70          // Thread.yield();
71          System.out.println(server.getState());
72  
73          int waitCount = 0;
74          while (server.getState() != ServerConstants.SERVER_STATE_ONLINE && waitCount < 5) {
75              try {
76                  waitCount++;
77                  Thread.sleep(1);
78              } catch (InterruptedException e) {
79              }
80          }
81          createTables();
82      }
83  
84      public void tearDown() throws SQLException {
85          dropTables();
86  
87          if (mode == HsqlMode.MEM) {
88              server.stop();
89              server = null;
90          }
91      }
92  
93      Connection newConnection() throws SQLException {
94          jdbcDriver driver = new jdbcDriver();
95          Properties props = new Properties();
96          props.setProperty("user", user);
97          props.setProperty("password", password);
98          return driver.connect(url, props);
99  
100         // return DriverManager.getConnection(url, user, password);
101     }
102 
103     private void createTables() throws SQLException {
104         Connection conn = newConnection();
105         assertNotNull(conn);
106         StringBuilder buf = new StringBuilder();
107         buf.append("CREATE TABLE LOGGING_EVENT (");
108         buf.append("TIMESTMP BIGINT NOT NULL,");
109         buf.append("FORMATTED_MESSAGE LONGVARCHAR NOT NULL,");
110         buf.append("LOGGER_NAME VARCHAR(256) NOT NULL,");
111         buf.append("LEVEL_STRING VARCHAR(256) NOT NULL,");
112         buf.append("THREAD_NAME VARCHAR(256),");
113         buf.append("REFERENCE_FLAG SMALLINT,");
114 
115         buf.append("ARG0 VARCHAR(256),");
116         buf.append("ARG1 VARCHAR(256),");
117         buf.append("ARG2 VARCHAR(256),");
118         buf.append("ARG3 VARCHAR(256),");
119 
120         buf.append("CALLER_FILENAME VARCHAR(256), ");
121         buf.append("CALLER_CLASS VARCHAR(256), ");
122         buf.append("CALLER_METHOD VARCHAR(256), ");
123         buf.append("CALLER_LINE CHAR(4), ");
124         buf.append("EVENT_ID BIGINT NOT NULL IDENTITY);");
125         query(conn, buf.toString());
126 
127         buf = new StringBuilder();
128         buf.append("CREATE TABLE LOGGING_EVENT_PROPERTY (");
129         buf.append("EVENT_ID BIGINT NOT NULL,");
130         buf.append("MAPPED_KEY  VARCHAR(254) NOT NULL,");
131         buf.append("MAPPED_VALUE LONGVARCHAR,");
132         buf.append("PRIMARY KEY(EVENT_ID, MAPPED_KEY),");
133         buf.append("FOREIGN KEY (EVENT_ID) REFERENCES LOGGING_EVENT(EVENT_ID));");
134         query(conn, buf.toString());
135 
136         buf = new StringBuilder();
137         buf.append("CREATE TABLE LOGGING_EVENT_EXCEPTION (");
138         buf.append("EVENT_ID BIGINT NOT NULL,");
139         buf.append("I SMALLINT NOT NULL,");
140         buf.append("TRACE_LINE VARCHAR(256) NOT NULL,");
141         buf.append("PRIMARY KEY(EVENT_ID, I),");
142         buf.append("FOREIGN KEY (EVENT_ID) REFERENCES LOGGING_EVENT(EVENT_ID));");
143         query(conn, buf.toString());
144     }
145 
146     private void dropTables() throws SQLException {
147         Connection conn = newConnection();
148         StringBuilder buf = new StringBuilder();
149         buf.append("DROP TABLE LOGGING_EVENT_EXCEPTION IF EXISTS;");
150         query(conn, buf.toString());
151 
152         buf = new StringBuilder();
153         buf.append("DROP TABLE LOGGING_EVENT_PROPERTY IF EXISTS;");
154         query(conn, buf.toString());
155 
156         buf = new StringBuilder();
157         buf.append("DROP TABLE LOGGING_EVENT IF EXISTS;");
158         query(conn, buf.toString());
159     }
160 
161     private void query(Connection conn, String expression) throws SQLException {
162 
163         Statement st = null;
164         st = conn.createStatement();
165         int i = st.executeUpdate(expression);
166         if (i == -1) {
167             System.out.println("db error : " + expression);
168         }
169         st.close();
170     }
171 
172     public enum HsqlMode {
173         MEM, FILE, NET;
174     }
175 }