Skip to content

Commit f737786

Browse files
committed
[misc] logging test addition
1 parent f88cae6 commit f737786

File tree

4 files changed

+143
-6
lines changed

4 files changed

+143
-6
lines changed

src/main/java/org/mariadb/jdbc/client/socket/PacketReader.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@
3434

3535
public class PacketReader {
3636

37-
private static final int REUSABLE_buf_LENGTH = 1024;
37+
private static final int REUSABLE_BUFFER_LENGTH = 1024;
3838
private static final int MAX_PACKET_SIZE = 0xffffff;
3939
private static final Logger logger = Loggers.getLogger(PacketReader.class);
4040

4141
private final byte[] header = new byte[4];
42-
private final byte[] reusableArray = new byte[REUSABLE_buf_LENGTH];
42+
private final byte[] reusableArray = new byte[REUSABLE_BUFFER_LENGTH];
4343
private final InputStream inputStream;
4444
private final int maxQuerySizeToLog;
4545

@@ -104,7 +104,7 @@ public ReadableByteBuf readReadablePacket(boolean reUsable, boolean traceEnable)
104104

105105
// prepare array
106106
byte[] rawBytes;
107-
if (reUsable && lastPacketLength < REUSABLE_buf_LENGTH) {
107+
if (reUsable && lastPacketLength < REUSABLE_BUFFER_LENGTH) {
108108
rawBytes = reusableArray;
109109
} else {
110110
rawBytes = new byte[lastPacketLength];
@@ -220,7 +220,7 @@ public byte[] readPacket(boolean reUsable, boolean traceEnable) throws IOExcepti
220220

221221
// prepare array
222222
byte[] rawBytes;
223-
if (reUsable && lastPacketLength < REUSABLE_buf_LENGTH) {
223+
if (reUsable && lastPacketLength < REUSABLE_BUFFER_LENGTH) {
224224
rawBytes = reusableArray;
225225
} else {
226226
rawBytes = new byte[lastPacketLength];

src/test/java/org/mariadb/jdbc/integration/ConnectionTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,8 @@ public void windowsNamedPipe() throws SQLException {
876876

877877
@Test
878878
public void localSocket() throws Exception {
879-
Assumptions.assumeTrue(System.getenv("local") != null
879+
Assumptions.assumeTrue(
880+
System.getenv("local") != null
880881
&& "1".equals(System.getenv("local"))
881882
&& !System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("win"));
882883
Statement stmt = sharedConn.createStatement();
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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+
}

src/test/java/org/mariadb/jdbc/integration/Sha256AuthenticationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ public void cachingSha256PluginTestSsl() throws Exception {
124124
sharedConn.createStatement().execute("FLUSH PRIVILEGES"); // reset cache
125125

126126
Assumptions.assumeTrue(haveSsl());
127-
try (Connection con = createCon("user=cachingSha256User&password=MySup8%rPassw@ord&sslMode=trust")) {
127+
try (Connection con =
128+
createCon("user=cachingSha256User&password=MySup8%rPassw@ord&sslMode=trust")) {
128129
con.isValid(1);
129130
}
130131
}

0 commit comments

Comments
 (0)