001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.classic.db;
015
016import static org.junit.Assert.assertNotNull;
017
018import java.io.PrintWriter;
019import java.sql.Connection;
020import java.sql.SQLException;
021import java.sql.Statement;
022import java.util.Properties;
023
024import org.hsqldb.Server;
025import org.hsqldb.jdbcDriver;
026import org.hsqldb.server.ServerConstants;
027
028public class DBAppenderHSQLTestFixture {
029
030    public static final String HSQLDB_DRIVER_CLASS = "org.hsqldb.jdbcDriver";
031    // String serverProps;
032    String url = null;
033    String user = "sa";
034    String password = "";
035    Server server;
036
037    // boolean isNetwork = true;
038    HsqlMode mode = HsqlMode.MEM;
039
040    public void setUp() throws SQLException {
041
042        switch (mode) {
043        case NET:
044            url = "jdbc:hsqldb:hsql://localhost:4808/test";
045            break;
046        case MEM:
047            url = "jdbc:hsqldb:mem:test;sql.enforce_strict_size=true";
048            server = new Server();
049            server.setDatabaseName(0, "test");
050            server.setDatabasePath(0, url);
051            server.setLogWriter(new PrintWriter(System.out));
052            server.setErrWriter(new PrintWriter(System.out));
053            server.setTrace(false);
054            server.setSilent(false);
055            server.start();
056
057            break;
058        case FILE:
059            url = "jdbc:hsqldb:file:test;sql.enforce_strict_size=true";
060            break;
061
062        }
063
064        // try {
065        // Class.forName(DRIVER_CLASS);
066        // } catch (Exception e) {
067        // e.printStackTrace();
068        // System.out.println(this + ".setUp() error: " + e.getMessage());
069        // }
070        // Thread.yield();
071        System.out.println(server.getState());
072
073        int waitCount = 0;
074        while (server.getState() != ServerConstants.SERVER_STATE_ONLINE && waitCount < 5) {
075            try {
076                waitCount++;
077                Thread.sleep(1);
078            } catch (InterruptedException e) {
079            }
080        }
081        createTables();
082    }
083
084    public void tearDown() throws SQLException {
085        dropTables();
086
087        if (mode == HsqlMode.MEM) {
088            server.stop();
089            server = null;
090        }
091    }
092
093    Connection newConnection() throws SQLException {
094        jdbcDriver driver = new jdbcDriver();
095        Properties props = new Properties();
096        props.setProperty("user", user);
097        props.setProperty("password", password);
098        return driver.connect(url, props);
099
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}