|
| 1 | +/* |
| 2 | + * MariaDB Client for Java |
| 3 | + * |
| 4 | + * Copyright (c) 2012-2014 Monty Program Ab. |
| 5 | + * Copyright (c) 2015-2020 MariaDB Corporation Ab. |
| 6 | + * |
| 7 | + * This library is free software; you can redistribute it and/or modify it under |
| 8 | + * the terms of the GNU Lesser General Public License as published by the Free |
| 9 | + * Software Foundation; either version 2.1 of the License, or (at your option) |
| 10 | + * any later version. |
| 11 | + * |
| 12 | + * This library is distributed in the hope that it will be useful, but |
| 13 | + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 15 | + * for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Lesser General Public License along |
| 18 | + * with this library; if not, write to Monty Program Ab info@montyprogram.com. |
| 19 | + * |
| 20 | + */ |
| 21 | + |
| 22 | +package org.mariadb.jdbc.integration; |
| 23 | + |
| 24 | +import ch.qos.logback.classic.Level; |
| 25 | +import ch.qos.logback.classic.Logger; |
| 26 | +import ch.qos.logback.classic.LoggerContext; |
| 27 | +import ch.qos.logback.classic.encoder.PatternLayoutEncoder; |
| 28 | +import ch.qos.logback.classic.spi.ILoggingEvent; |
| 29 | +import ch.qos.logback.core.FileAppender; |
| 30 | +import java.io.File; |
| 31 | +import java.io.IOException; |
| 32 | +import java.nio.file.Files; |
| 33 | +import java.nio.file.Paths; |
| 34 | +import org.junit.jupiter.api.Assertions; |
| 35 | +import org.junit.jupiter.api.Test; |
| 36 | +import org.mariadb.jdbc.Common; |
| 37 | +import org.mariadb.jdbc.Connection; |
| 38 | +import org.mariadb.jdbc.Statement; |
| 39 | +import org.slf4j.LoggerFactory; |
| 40 | + |
| 41 | +public class LoggingTest extends Common { |
| 42 | + |
| 43 | + @Test |
| 44 | + void basicLogging() throws Exception { |
| 45 | + File tempFile = File.createTempFile("log", ".tmp"); |
| 46 | + |
| 47 | + Logger logger = (Logger) LoggerFactory.getLogger("org.mariadb.jdbc"); |
| 48 | + Level initialLevel = logger.getLevel(); |
| 49 | + logger.setLevel(Level.TRACE); |
| 50 | + logger.setAdditive(false); |
| 51 | + logger.detachAndStopAllAppenders(); |
| 52 | + |
| 53 | + LoggerContext context = new LoggerContext(); |
| 54 | + FileAppender<ILoggingEvent> fa = new FileAppender<ILoggingEvent>(); |
| 55 | + fa.setName("FILE"); |
| 56 | + fa.setImmediateFlush(true); |
| 57 | + PatternLayoutEncoder pa = new PatternLayoutEncoder(); |
| 58 | + pa.setPattern("%r %5p %c [%t] - %m%n"); |
| 59 | + pa.setContext(context); |
| 60 | + pa.start(); |
| 61 | + fa.setEncoder(pa); |
| 62 | + |
| 63 | + fa.setFile(tempFile.getPath()); |
| 64 | + fa.setAppend(true); |
| 65 | + fa.setContext(context); |
| 66 | + fa.start(); |
| 67 | + |
| 68 | + logger.addAppender(fa); |
| 69 | + |
| 70 | + try (Connection conn = createCon()) { |
| 71 | + Statement stmt = conn.createStatement(); |
| 72 | + stmt.execute("SELECT 1"); |
| 73 | + } |
| 74 | + try (Connection conn = createCon("useCompression=true")) { |
| 75 | + Statement stmt = conn.createStatement(); |
| 76 | + stmt.execute("SELECT 1"); |
| 77 | + } |
| 78 | + try { |
| 79 | + String contents = new String(Files.readAllBytes(Paths.get(tempFile.getPath()))); |
| 80 | + String defaultRequest = |
| 81 | + "+--------------------------------------------------+\n" |
| 82 | + + "| 0 1 2 3 4 5 6 7 8 9 a b c d e f |\n" |
| 83 | + + "+--------------------------------------------------+------------------+\n" |
| 84 | + + "| 5F 00 00 00 03 73 65 74 20 61 75 74 6F 63 6F 6D | _....set autocom |\n" |
| 85 | + + "| 6D 69 74 3D 31 2C 20 73 71 6C 5F 6D 6F 64 65 20 | mit=1, sql_mode |\n" |
| 86 | + + "| 3D 20 63 6F 6E 63 61 74 28 40 40 73 71 6C 5F 6D | = concat(@@sql_m |\n" |
| 87 | + + "| 6F 64 65 2C 27 2C 53 54 52 49 43 54 5F 54 52 41 | ode,',STRICT_TRA |\n" |
| 88 | + + "| 4E 53 5F 54 41 42 4C 45 53 27 29 2C 20 73 65 73 | NS_TABLES'), ses |\n" |
| 89 | + + "| 73 69 6F 6E 5F 74 72 61 63 6B 5F 73 63 68 65 6D | sion_track_schem |\n" |
| 90 | + + "| 61 3D 31 | a=1 |\n" |
| 91 | + + "+--------------------------------------------------+------------------+\n"; |
| 92 | + |
| 93 | + Assertions.assertTrue( |
| 94 | + contents.contains(defaultRequest) |
| 95 | + || contents.contains(defaultRequest.replace("\r\n", "\n"))); |
| 96 | + |
| 97 | + String selectOne = |
| 98 | + "+--------------------------------------------------+\n" |
| 99 | + + "| 0 1 2 3 4 5 6 7 8 9 a b c d e f |\n" |
| 100 | + + "+--------------------------------------------------+------------------+\n" |
| 101 | + + "| 09 00 00 00 03 53 45 4C 45 43 54 20 31 | .....SELECT 1 |\n" |
| 102 | + + "+--------------------------------------------------+------------------+\n"; |
| 103 | + Assertions.assertTrue( |
| 104 | + contents.contains(selectOne) || contents.contains(selectOne.replace("\r\n", "\n"))); |
| 105 | + String rowResult = |
| 106 | + "+--------------------------------------------------+\n" |
| 107 | + + "| 0 1 2 3 4 5 6 7 8 9 a b c d e f |\n" |
| 108 | + + "+--------------------------------------------------+------------------+\n" |
| 109 | + + "| 02 00 00 03 01 31 | .....1 |\n" |
| 110 | + + "+--------------------------------------------------+------------------+\n"; |
| 111 | + Assertions.assertTrue( |
| 112 | + contents.contains(rowResult) || contents.contains(rowResult.replace("\r\n", "\n"))); |
| 113 | + logger.setLevel(initialLevel); |
| 114 | + logger.detachAppender(fa); |
| 115 | + } catch (IOException e) { |
| 116 | + e.printStackTrace(); |
| 117 | + Assertions.fail(); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + public String encodeHexString(byte[] byteArray) { |
| 122 | + StringBuffer hexStringBuffer = new StringBuffer(); |
| 123 | + for (int i = 0; i < byteArray.length; i++) { |
| 124 | + hexStringBuffer.append(byteToHex(byteArray[i])); |
| 125 | + } |
| 126 | + return hexStringBuffer.toString(); |
| 127 | + } |
| 128 | + |
| 129 | + public String byteToHex(byte num) { |
| 130 | + char[] hexDigits = new char[2]; |
| 131 | + hexDigits[0] = Character.forDigit((num >> 4) & 0xF, 16); |
| 132 | + hexDigits[1] = Character.forDigit((num & 0xF), 16); |
| 133 | + return new String(hexDigits); |
| 134 | + } |
| 135 | +} |
0 commit comments