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.sql.Connection;
19  import java.sql.SQLException;
20  import java.sql.Statement;
21  import java.util.Properties;
22  
23  import org.h2.Driver;
24  
25  import ch.qos.logback.core.testUtil.RandomUtil;
26  
27  public class DBAppenderH2TestFixture {
28  
29      public enum H2Mode {
30          MEM, FILE, NET;
31      }
32  
33      public static final String H2_DRIVER_CLASS = "org.h2.Driver";
34      String url = null;
35      String user = "sa";
36      String password = "";
37  
38      // boolean isNetwork = true;
39      H2Mode mode = H2Mode.MEM;
40  
41      int diff = RandomUtil.getPositiveInt();
42  
43      Connection connection;
44  
45      public void setUp() throws SQLException {
46  
47          switch (mode) {
48          case NET:
49              url = "jdbc:h2:tcp://localhost:4808/test";
50              break;
51          case MEM:
52              url = "jdbc:h2:mem:test" + diff;
53              break;
54          case FILE:
55              url = "jdbc:hsqldb:file:test;sql.enforce_strict_size=true";
56              break;
57  
58          }
59          connection = newConnection();
60          createTables();
61      }
62  
63      public void tearDown() throws SQLException {
64          dropTables();
65          connection.close();
66      }
67  
68      Connection newConnection() throws SQLException {
69          System.out.println("url=" + url);
70          org.h2.Driver driver = Driver.load();
71          Properties props = new Properties();
72          props.setProperty("user", user);
73          props.setProperty("password", password);
74          return driver.connect(url, props);
75      }
76  
77      private void createTables() throws SQLException {
78          assertNotNull(connection);
79          StringBuilder buf = new StringBuilder();
80          buf.append("CREATE TABLE LOGGING_EVENT (");
81          buf.append("TIMESTMP BIGINT NOT NULL,");
82          buf.append("FORMATTED_MESSAGE LONGVARCHAR NOT NULL,");
83          buf.append("LOGGER_NAME VARCHAR(256) NOT NULL,");
84          buf.append("LEVEL_STRING VARCHAR(256) NOT NULL,");
85          buf.append("THREAD_NAME VARCHAR(256),");
86          buf.append("REFERENCE_FLAG SMALLINT,");
87          buf.append("ARG0 VARCHAR(256),");
88          buf.append("ARG1 VARCHAR(256),");
89          buf.append("ARG2 VARCHAR(256),");
90          buf.append("ARG3 VARCHAR(256),");
91          buf.append("CALLER_FILENAME VARCHAR(256), ");
92          buf.append("CALLER_CLASS VARCHAR(256), ");
93          buf.append("CALLER_METHOD VARCHAR(256), ");
94          buf.append("CALLER_LINE CHAR(4), ");
95          buf.append("EVENT_ID IDENTITY NOT NULL);");
96          executeQuery(connection, buf.toString());
97  
98          buf = new StringBuilder();
99          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 }