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.access.db; 015 016import java.sql.Connection; 017import java.sql.DriverManager; 018import java.sql.SQLException; 019import java.sql.Statement; 020 021import org.hsqldb.Server; 022 023public class DBAppenderHSQLTestFixture { 024 025 public static final String DRIVER_CLASS = "org.hsqldb.jdbcDriver"; 026 String serverProps; 027 String url; 028 String user = "sa"; 029 String password = ""; 030 Server server; 031 boolean isNetwork = true; 032 033 void setUp() throws SQLException { 034 if (isNetwork) { 035 if (url == null) { 036 url = "jdbc:hsqldb:hsql://localhost/test"; 037 } 038 039 server = new Server(); 040 041 server.setDatabaseName(0, "test"); 042 server.setDatabasePath(0, "mem:test;sql.enforce_strict_size=true"); 043 server.setLogWriter(null); 044 server.setErrWriter(null); 045 server.setTrace(false); 046 server.setSilent(true); 047 server.start(); 048 } else { 049 if (url == null) { 050 url = "jdbc:hsqldb:file:test;sql.enforce_strict_size=true"; 051 } 052 } 053 054 try { 055 Class.forName(DRIVER_CLASS); 056 } catch (Exception e) { 057 e.printStackTrace(); 058 System.out.println(this + ".setUp() error: " + e.getMessage()); 059 } 060 Thread.yield(); 061 062 createTables(); 063 } 064 065 void tearDown() throws SQLException { 066 dropTables(); 067 if (isNetwork) { 068 server.stop(); 069 server = null; 070 } 071 } 072 073 Connection newConnection() throws SQLException { 074 return DriverManager.getConnection(url, user, password); 075 } 076 077 private void createTables() throws SQLException { 078 Connection conn = newConnection(); 079 StringBuilder buf = new StringBuilder(); 080 buf.append("CREATE TABLE access_event ("); 081 buf.append("timestmp BIGINT NOT NULL,"); 082 buf.append("requestURI VARCHAR(254),"); 083 buf.append("requestURL VARCHAR(254),"); 084 buf.append("remoteHost VARCHAR(254),"); 085 buf.append("remoteUser VARCHAR(254),"); 086 buf.append("remoteAddr VARCHAR(254),"); 087 buf.append("protocol VARCHAR(254),"); 088 buf.append("method VARCHAR(254),"); 089 buf.append("serverName VARCHAR(254),"); 090 buf.append("postContent VARCHAR(254),"); 091 buf.append("event_id INT NOT NULL IDENTITY);"); 092 query(conn, buf.toString()); 093 094 buf = new StringBuilder(); 095 buf.append("CREATE TABLE access_event_header ("); 096 buf.append("event_id INT NOT NULL,"); 097 buf.append("header_key VARCHAR(254) NOT NULL,"); 098 buf.append("header_value LONGVARCHAR,"); 099 buf.append("PRIMARY KEY(event_id, header_key),"); 100 buf.append("FOREIGN KEY (event_id) REFERENCES access_event(event_id));"); 101 query(conn, buf.toString()); 102 } 103 104 private void dropTables() throws SQLException { 105 Connection conn = newConnection(); 106 107 StringBuilder buf = new StringBuilder(); 108 buf.append("DROP TABLE access_event_header IF EXISTS;"); 109 query(conn, buf.toString()); 110 111 buf = new StringBuilder(); 112 buf.append("DROP TABLE access_event IF EXISTS;"); 113 query(conn, buf.toString()); 114 } 115 116 private void query(Connection conn, String expression) throws SQLException { 117 Statement st = null; 118 st = conn.createStatement(); 119 120 int i = st.executeUpdate(expression); 121 if (i == -1) { 122 System.out.println("db error : " + expression); 123 } 124 125 st.close(); 126 } 127}