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 ch.qos.logback.classic.LoggerContext;
17  import ch.qos.logback.classic.joran.JoranConfigurator;
18  import ch.qos.logback.core.db.DriverManagerConnectionSource;
19  import ch.qos.logback.core.joran.spi.JoranException;
20  import ch.qos.logback.core.testUtil.RandomUtil;
21  import ch.qos.logback.core.testUtil.StatusChecker;
22  import ch.qos.logback.core.util.EnvUtil;
23  import ch.qos.logback.core.util.StatusPrinter;
24  import org.junit.*;
25  import org.slf4j.Logger;
26  import org.slf4j.MDC;
27  
28  import java.net.InetAddress;
29  import java.sql.Connection;
30  import java.sql.ResultSet;
31  import java.sql.SQLException;
32  import java.sql.Statement;
33  import java.util.HashMap;
34  import java.util.Map;
35  
36  import static org.junit.Assert.*;
37  
38  public class DBAppenderIntegrationTest {
39  
40      static String LOCAL_HOST_NAME;
41      static String[] CONFORMING_HOST_LIST = new String[] { "Orion" };
42      static String[] POSTGRES_CONFORMING_HOST_LIST = new String[] { "haro" };
43      static String[] MYSQL_CONFORMING_HOST_LIST = new String[] { "xharo" };
44      static String[] ORACLE_CONFORMING_HOST_LIST = new String[] { "xharo" };
45  
46      int diff = RandomUtil.getPositiveInt();
47      LoggerContext lc = new LoggerContext();
48  
49      @BeforeClass
50      public static void setUpBeforeClass() throws Exception {
51          InetAddress localhostIA = InetAddress.getLocalHost();
52          LOCAL_HOST_NAME = localhostIA.getHostName();
53      }
54  
55      @AfterClass
56      public static void tearDownAfterClass() throws Exception {
57      }
58  
59      @Before
60      public void setUp() throws Exception {
61          lc.setName("lc" + diff);
62      }
63  
64      @After
65      public void tearDown() throws Exception {
66          // lc will never be used again
67          lc.stop();
68      }
69  
70      DriverManagerConnectionSource getConnectionSource() {
71          ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) lc.getLogger(Logger.ROOT_LOGGER_NAME);
72  
73          DBAppender dbAppender = (DBAppender) root.getAppender("DB");
74          assertNotNull(dbAppender);
75          return (DriverManagerConnectionSource) dbAppender.getConnectionSource();
76  
77      }
78  
79      public void doTest(String configFile) throws JoranException, SQLException {
80          JoranConfigurator configurator = new JoranConfigurator();
81          configurator.setContext(lc);
82          configurator.doConfigure(configFile);
83  
84          Logger logger = lc.getLogger(DBAppenderIntegrationTest.class);
85  
86          // the key userid is used in SiftingAppender test
87          // suffix with diff to avoid collision
88          MDC.put("userid" + diff, "user" + diff);
89          int runLength = 5;
90          for (int i = 1; i <= runLength; i++) {
91              logger.debug("This is a debug message. Message number: " + (diff + i));
92          }
93  
94          Exception e = new Exception("Just testing", getCause());
95          logger.error("At last an error.", e);
96  
97          StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
98  
99          long lastEventId = getLastEventId();
100         verify(lastEventId);
101 
102         // check that there were no errors
103         StatusChecker checker = new StatusChecker(lc);
104         checker.assertIsErrorFree();
105     }
106 
107     long getLastEventId() throws SQLException {
108         DriverManagerConnectionSource cs = getConnectionSource();
109 
110         Connection con = cs.getConnection();
111         Statement statement = con.createStatement();
112         statement.setMaxRows(1);
113         ResultSet rs = statement.executeQuery("select event_id from logging_event order by event_id desc");
114         rs.next();
115         long eventId = rs.getLong(1);
116         rs.close();
117         statement.close();
118         return eventId;
119     }
120 
121     void verify(long lastEventId) throws SQLException {
122         verifyDebugMsg(lastEventId);
123         verifyException(lastEventId);
124         verifyProperty(lastEventId);
125 
126     }
127 
128     void verifyDebugMsg(long lastEventId) throws SQLException {
129         DriverManagerConnectionSource cs = getConnectionSource();
130         Connection con = cs.getConnection();
131         Statement statement = con.createStatement();
132         ResultSet rs = statement.executeQuery("select formatted_message from logging_event where event_id='" + (lastEventId - 1) + "'");
133         rs.next();
134         String msg = rs.getString(1);
135         assertEquals("This is a debug message. Message number: " + (diff + 5), msg);
136     }
137 
138     void verifyProperty(long lastEventId) throws SQLException {
139         DriverManagerConnectionSource cs = getConnectionSource();
140         Connection con = cs.getConnection();
141         Statement statement = con.createStatement();
142         ResultSet rs = statement.executeQuery("select mapped_key, mapped_value from logging_event_property where event_id='" + (lastEventId - 1) + "'");
143 
144         Map<String, String> witness = lc.getCopyOfPropertyMap();
145         witness.putAll(MDC.getCopyOfContextMap());
146 
147         Map<String, String> map = new HashMap<String, String>();
148         while (rs.next()) {
149             String key = rs.getString(1);
150             String val = rs.getString(2);
151             map.put(key, val);
152         }
153 
154         assertEquals(witness, map);
155     }
156 
157     void verifyException(long lastEventId) throws SQLException {
158         DriverManagerConnectionSource cs = getConnectionSource();
159         Connection con = cs.getConnection();
160         Statement statement = con.createStatement();
161         ResultSet rs = statement.executeQuery("select trace_line from logging_event_exception where event_id='" + (lastEventId) + "' AND I='0'");
162         rs.next();
163         String traceLine = rs.getString(1);
164         assertEquals("java.lang.Exception: Just testing", traceLine);
165     }
166 
167     Throwable getCause() {
168         return new IllegalStateException("test cause");
169     }
170 
171     static boolean isConformingHostAndJDK16OrHigher(String[] conformingHostList) {
172         if (!EnvUtil.isJDK6OrHigher()) {
173             return false;
174         }
175         for (String conformingHost : conformingHostList) {
176             if (conformingHost.equalsIgnoreCase(LOCAL_HOST_NAME)) {
177                 return true;
178             }
179         }
180         return false;
181     }
182 
183     static boolean isConformingHostAndJDK16OrHigher() {
184         return isConformingHostAndJDK16OrHigher(CONFORMING_HOST_LIST);
185     }
186 
187     @Test
188     public void sqlserver() throws Exception {
189         // perform test only on conforming hosts
190         if (!isConformingHostAndJDK16OrHigher()) {
191             return;
192         }
193         doTest("src/test/input/integration/db/sqlserver-with-driver.xml");
194     }
195 
196     @Test
197     public void oracle10g() throws Exception {
198         // perform test only on conforming hosts
199         if (!isConformingHostAndJDK16OrHigher(ORACLE_CONFORMING_HOST_LIST)) {
200             return;
201         }
202         doTest("src/test/input/integration/db/oracle10g-with-driver.xml");
203     }
204 
205     @Test
206     @Ignore
207     public void oracle11g() throws Exception {
208         // perform test only on conforming hosts
209         if (!isConformingHostAndJDK16OrHigher()) {
210             return;
211         }
212         doTest("src/test/input/integration/db/oracle11g-with-driver.xml");
213     }
214 
215     @Test
216     public void mysql() throws Exception {
217         // perform test only on conforming hosts
218         if (!isConformingHostAndJDK16OrHigher(MYSQL_CONFORMING_HOST_LIST)) {
219             return;
220         }
221         doTest("src/test/input/integration/db/mysql-with-driver.xml");
222     }
223 
224     @Test
225     public void postgres() throws Exception {
226         // perform test only on conforming hosts
227         if (!isConformingHostAndJDK16OrHigher(POSTGRES_CONFORMING_HOST_LIST)) {
228             return;
229         }
230         System.out.println("running postgres() test");
231         doTest("src/test/input/integration/db/postgresql-with-driver.xml");
232     }
233 
234 }