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.sql.Connection; 019import java.sql.SQLException; 020import java.sql.Statement; 021import java.util.Properties; 022 023import org.h2.Driver; 024 025import ch.qos.logback.core.testUtil.RandomUtil; 026 027public class DBAppenderH2TestFixture { 028 029 public enum H2Mode { 030 MEM, FILE, NET; 031 } 032 033 public static final String H2_DRIVER_CLASS = "org.h2.Driver"; 034 String url = null; 035 String user = "sa"; 036 String password = ""; 037 038 // boolean isNetwork = true; 039 H2Mode mode = H2Mode.MEM; 040 041 int diff = RandomUtil.getPositiveInt(); 042 043 Connection connection; 044 045 public void setUp() throws SQLException { 046 047 switch (mode) { 048 case NET: 049 url = "jdbc:h2:tcp://localhost:4808/test"; 050 break; 051 case MEM: 052 url = "jdbc:h2:mem:test" + diff; 053 break; 054 case FILE: 055 url = "jdbc:hsqldb:file:test;sql.enforce_strict_size=true"; 056 break; 057 058 } 059 connection = newConnection(); 060 createTables(); 061 } 062 063 public void tearDown() throws SQLException { 064 dropTables(); 065 connection.close(); 066 } 067 068 Connection newConnection() throws SQLException { 069 System.out.println("url=" + url); 070 org.h2.Driver driver = Driver.load(); 071 Properties props = new Properties(); 072 props.setProperty("user", user); 073 props.setProperty("password", password); 074 return driver.connect(url, props); 075 } 076 077 private void createTables() throws SQLException { 078 assertNotNull(connection); 079 StringBuilder buf = new StringBuilder(); 080 buf.append("CREATE TABLE LOGGING_EVENT ("); 081 buf.append("TIMESTMP BIGINT NOT NULL,"); 082 buf.append("FORMATTED_MESSAGE LONGVARCHAR NOT NULL,"); 083 buf.append("LOGGER_NAME VARCHAR(256) NOT NULL,"); 084 buf.append("LEVEL_STRING VARCHAR(256) NOT NULL,"); 085 buf.append("THREAD_NAME VARCHAR(256),"); 086 buf.append("REFERENCE_FLAG SMALLINT,"); 087 buf.append("ARG0 VARCHAR(256),"); 088 buf.append("ARG1 VARCHAR(256),"); 089 buf.append("ARG2 VARCHAR(256),"); 090 buf.append("ARG3 VARCHAR(256),"); 091 buf.append("CALLER_FILENAME VARCHAR(256), "); 092 buf.append("CALLER_CLASS VARCHAR(256), "); 093 buf.append("CALLER_METHOD VARCHAR(256), "); 094 buf.append("CALLER_LINE CHAR(4), "); 095 buf.append("EVENT_ID IDENTITY NOT NULL);"); 096 executeQuery(connection, buf.toString()); 097 098 buf = new StringBuilder(); 099 buf.append("CREATE TABLE LOGGING_EVENT_PROPERTY ("); 100 buf.append("EVENT_ID BIGINT NOT NULL,"); 101 buf.append("MAPPED_KEY VARCHAR(254) NOT NULL,"); 102 buf.append("MAPPED_VALUE LONGVARCHAR,"); 103 buf.append("PRIMARY KEY(EVENT_ID, MAPPED_KEY),"); 104 buf.append("FOREIGN KEY (EVENT_ID) REFERENCES LOGGING_EVENT(EVENT_ID));"); 105 executeQuery(connection, buf.toString()); 106 107 buf = new StringBuilder(); 108 buf.append("CREATE TABLE LOGGING_EVENT_EXCEPTION ("); 109 buf.append("EVENT_ID BIGINT NOT NULL,"); 110 buf.append("I SMALLINT NOT NULL,"); 111 buf.append("TRACE_LINE VARCHAR(256) NOT NULL,"); 112 buf.append("PRIMARY KEY(EVENT_ID, I),"); 113 buf.append("FOREIGN KEY (EVENT_ID) REFERENCES LOGGING_EVENT(EVENT_ID));"); 114 executeQuery(connection, buf.toString()); 115 } 116 117 private void dropTables() throws SQLException { 118 StringBuilder buf = new StringBuilder(); 119 buf.append("DROP TABLE LOGGING_EVENT_EXCEPTION IF EXISTS;"); 120 executeQuery(connection, buf.toString()); 121 122 buf = new StringBuilder(); 123 buf.append("DROP TABLE LOGGING_EVENT_PROPERTY IF EXISTS;"); 124 executeQuery(connection, buf.toString()); 125 126 buf = new StringBuilder(); 127 buf.append("DROP TABLE LOGGING_EVENT IF EXISTS;"); 128 executeQuery(connection, buf.toString()); 129 } 130 131 private void executeQuery(Connection conn, String expression) throws SQLException { 132 Statement st = null; 133 st = conn.createStatement(); 134 int i = st.executeUpdate(expression); 135 if (i == -1) { 136 throw new IllegalStateException("db error : " + expression); 137 } 138 st.close(); 139 } 140 141}