Skip to content

Commit e3f0395

Browse files
author
diego Dupin
committed
[misc] test coverage addition
1 parent ab72818 commit e3f0395

File tree

13 files changed

+252
-86
lines changed

13 files changed

+252
-86
lines changed

src/main/java/org/mariadb/jdbc/ClientPreparedStatement.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.mariadb.jdbc.message.client.*;
1818
import org.mariadb.jdbc.message.server.ColumnDefinitionPacket;
1919
import org.mariadb.jdbc.message.server.OkPacket;
20+
import org.mariadb.jdbc.message.server.PrepareResultPacket;
2021
import org.mariadb.jdbc.util.ClientParser;
2122
import org.mariadb.jdbc.util.ParameterList;
2223
import org.mariadb.jdbc.util.constants.Capabilities;
@@ -120,7 +121,12 @@ private void executeBatchBulk() throws SQLException {
120121
ResultSet.CONCUR_READ_ONLY,
121122
ResultSet.TYPE_FORWARD_ONLY,
122123
closeOnCompletion);
123-
results = res.subList(1, res.size());
124+
// in case of failover, prepare is done in failover, skipping prepare result
125+
if (res.get(0) instanceof PrepareResultPacket) {
126+
results = res.subList(1, res.size());
127+
} else {
128+
results = res;
129+
}
124130
} else {
125131
results =
126132
con.getClient()

src/main/java/org/mariadb/jdbc/ServerPreparedStatement.java

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -161,34 +161,45 @@ private void executeInternalPreparedBatch() throws SQLException {
161161
* @throws SQLException if IOException / Command error
162162
*/
163163
private void executeBatchBulk(String cmd) throws SQLException {
164-
ClientMessage[] packets;
164+
List<Completion> res;
165165
if (prepareResult == null) prepareResult = con.getContext().getPrepareCache().get(cmd, this);
166-
if (prepareResult == null) {
167-
packets =
168-
new ClientMessage[] {
169-
new PreparePacket(cmd), new BulkExecutePacket(null, batchParameters, cmd, this)
170-
};
171-
} else {
172-
packets =
173-
new ClientMessage[] {new BulkExecutePacket(prepareResult, batchParameters, cmd, this)};
174-
}
175166
try {
176-
List<Completion> res =
177-
con.getClient()
178-
.executePipeline(
179-
packets,
180-
this,
181-
0,
182-
maxRows,
183-
ResultSet.CONCUR_READ_ONLY,
184-
ResultSet.TYPE_FORWARD_ONLY,
185-
closeOnCompletion);
186-
// in case of failover, prepare is done in failover, skipping prepare result
187-
if (res.get(0) instanceof PrepareResultPacket) {
188-
results = res.subList(1, res.size());
167+
if (prepareResult == null) {
168+
ClientMessage[] packets;
169+
packets =
170+
new ClientMessage[] {
171+
new PreparePacket(cmd), new BulkExecutePacket(null, batchParameters, cmd, this)
172+
};
173+
res =
174+
con.getClient()
175+
.executePipeline(
176+
packets,
177+
this,
178+
0,
179+
maxRows,
180+
ResultSet.CONCUR_READ_ONLY,
181+
ResultSet.TYPE_FORWARD_ONLY,
182+
closeOnCompletion);
183+
184+
// in case of failover, prepare is done in failover, skipping prepare result
185+
if (res.get(0) instanceof PrepareResultPacket) {
186+
results = res.subList(1, res.size());
187+
} else {
188+
results = res;
189+
}
189190
} else {
190-
results = res;
191+
results =
192+
con.getClient()
193+
.execute(
194+
new BulkExecutePacket(prepareResult, batchParameters, cmd, this),
195+
this,
196+
0,
197+
maxRows,
198+
ResultSet.CONCUR_READ_ONLY,
199+
ResultSet.TYPE_FORWARD_ONLY,
200+
closeOnCompletion);
191201
}
202+
192203
} catch (SQLException bue) {
193204
results = null;
194205
throw exceptionFactory()

src/main/java/org/mariadb/jdbc/client/impl/ConnectionHelper.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ public static Socket connectSocket(final Configuration conf, final HostAddress h
108108
throws SQLException {
109109
Socket socket;
110110
try {
111+
if (conf.pipe() == null && conf.localSocket() == null && hostAddress == null)
112+
throw new SQLException(
113+
"hostname must be set to connect socket if not using local socket or pipe");
111114
socket = createSocket(conf, hostAddress);
112115
SocketHelper.setSocketOption(conf, socket);
113116
if (!socket.isConnected()) {
@@ -192,16 +195,12 @@ public static long initializeClientCapabilities(
192195
public static byte decideLanguage(InitialHandshakePacket handshake) {
193196
short serverLanguage = handshake.getDefaultCollation();
194197
// return current server utf8mb4 collation
195-
if (serverLanguage == 45 // utf8mb4_general_ci
196-
|| serverLanguage == 46 // utf8mb4_bin
197-
|| (serverLanguage >= 224 && serverLanguage <= 247)) {
198-
return (byte) serverLanguage;
199-
}
200-
if (!handshake.getVersion().versionGreaterOrEqual(5, 5, 0)) {
201-
// 5.1 version doesn't know 4 bytes utf8
202-
return (byte) 33; // utf8_general_ci
203-
}
204-
return (byte) 224; // UTF8MB4_UNICODE_CI;
198+
return (byte)
199+
((serverLanguage == 45 // utf8mb4_general_ci
200+
|| serverLanguage == 46 // utf8mb4_bin
201+
|| (serverLanguage >= 224 && serverLanguage <= 247))
202+
? serverLanguage
203+
: 224); // UTF8MB4_UNICODE_CI;
205204
}
206205

207206
public static void authenticationHandler(

src/main/java/org/mariadb/jdbc/client/impl/MultiPrimaryClient.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public MultiPrimaryClient(Configuration conf, ReentrantLock lock) throws SQLExce
6060
* searching in temporary denied host if not succeed, until reaching `retriesAllDown` attempts.
6161
*
6262
* @param readOnly must connect a replica / primary
63-
* @param failFast must try only not denyed server
63+
* @param failFast must try only not denied server
6464
* @return a valid connection client
6565
* @throws SQLException if not succeed to create a connection.
6666
*/
@@ -86,7 +86,7 @@ protected Client connectHost(boolean readOnly, boolean failFast) throws SQLExcep
8686
if (failFast) {
8787
throw (lastSqle != null)
8888
? lastSqle
89-
: new SQLNonTransientConnectionException("No host not blacklisted");
89+
: new SQLNonTransientConnectionException("all hosts are blacklisted");
9090
}
9191

9292
// All server corresponding to type are in deny list
@@ -224,8 +224,6 @@ public void syncNewState(Client oldCli) throws SQLException {
224224
case java.sql.Connection.TRANSACTION_SERIALIZABLE:
225225
query += " SERIALIZABLE";
226226
break;
227-
default:
228-
throw new SQLException("Unsupported transaction isolation level");
229227
}
230228
currentClient
231229
.getContext()
@@ -395,9 +393,6 @@ public void abort(Executor executor) throws SQLException {
395393

396394
@Override
397395
public void close() throws SQLException {
398-
if (closed) {
399-
throw new SQLNonTransientConnectionException("Connection is closed", "08000", 1220);
400-
}
401396
closed = true;
402397
currentClient.close();
403398
}

src/main/java/org/mariadb/jdbc/client/impl/MultiPrimaryReplicaClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public void abort(Executor executor) throws SQLException {
239239
}
240240

241241
@Override
242-
public void close() {
242+
public void close() throws SQLException {
243243
if (!closed) {
244244
closed = true;
245245
try {

src/main/java/org/mariadb/jdbc/client/socket/impl/SocketUtility.java

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,21 @@ public class SocketUtility {
1818
*/
1919
@SuppressWarnings("unchecked")
2020
public static SocketHandlerFunction getSocketHandler() {
21-
try {
22-
// forcing use of JNA to ensure AOT compilation
23-
Platform.getOSType();
21+
// forcing use of JNA to ensure AOT compilation
22+
Platform.getOSType();
2423

25-
return (conf, hostAddress) -> {
26-
if (conf.pipe() != null) {
27-
return new NamedPipeSocket(hostAddress != null ? hostAddress.host : null, conf.pipe());
28-
} else if (conf.localSocket() != null) {
29-
try {
30-
return new UnixDomainSocket(conf.localSocket());
31-
} catch (RuntimeException re) {
32-
throw new IOException(re.getMessage(), re.getCause());
33-
}
34-
} else {
35-
return ConnectionHelper.standardSocket(conf, hostAddress);
24+
return (conf, hostAddress) -> {
25+
if (conf.pipe() != null) {
26+
return new NamedPipeSocket(hostAddress != null ? hostAddress.host : null, conf.pipe());
27+
} else if (conf.localSocket() != null) {
28+
try {
29+
return new UnixDomainSocket(conf.localSocket());
30+
} catch (RuntimeException re) {
31+
throw new IOException(re.getMessage(), re.getCause());
3632
}
37-
};
38-
} catch (Throwable cle) {
39-
// jna jar's are not in classpath
40-
}
41-
return ConnectionHelper::standardSocket;
33+
} else {
34+
return ConnectionHelper.standardSocket(conf, hostAddress);
35+
}
36+
};
4237
}
4338
}

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ public void differentParameterType(Connection con, boolean expectSuccessUnknown)
134134
assertEquals(2, rs.getInt(1));
135135
assertEquals("2", rs.getString(2));
136136
assertFalse(rs.next());
137-
138137
stmt.execute("TRUNCATE BatchTest");
138+
139139
try (PreparedStatement prep =
140140
con.prepareStatement("INSERT INTO BatchTest(t1, t2) VALUES (?,?)")) {
141141
prep.setInt(1, 1);
@@ -195,6 +195,24 @@ public void differentParameterType(Connection con, boolean expectSuccessUnknown)
195195
assertEquals(2, res.length);
196196
assertEquals(1, res[0]);
197197
assertEquals(1, res[1]);
198+
199+
stmt.execute("TRUNCATE BatchTest");
200+
if (isMariaDBServer()) {
201+
stmt.setFetchSize(1);
202+
rs = stmt.executeQuery("SELECT * FROM seq_1_to_10");
203+
rs.next();
204+
}
205+
prep.setInt(1, 1);
206+
prep.setString(2, "1");
207+
prep.addBatch();
208+
209+
prep.setInt(1, 2);
210+
prep.setInt(2, 2);
211+
prep.addBatch();
212+
res = prep.executeBatch();
213+
assertEquals(2, res.length);
214+
assertEquals(1, res[0]);
215+
assertEquals(1, res[1]);
198216
}
199217
rs = stmt.executeQuery("SELECT * FROM BatchTest");
200218
assertTrue(rs.next());

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

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.concurrent.Executor;
1717
import org.junit.jupiter.api.*;
1818
import org.mariadb.jdbc.*;
19+
import org.mariadb.jdbc.integration.util.SocketFactoryBasicTest;
1920
import org.mariadb.jdbc.integration.util.SocketFactoryTest;
2021

2122
@DisplayName("Connection Test")
@@ -39,6 +40,18 @@ void isValidWrongValue() {
3940
}
4041
}
4142

43+
@Test
44+
void missingHost() {
45+
assertThrowsContains(
46+
SQLException.class,
47+
() -> DriverManager.getConnection("jdbc:mariadb:///db"),
48+
"hostname must be set to connect socket if not using local socket or pipe");
49+
assertThrowsContains(
50+
SQLException.class,
51+
() -> DriverManager.getConnection("jdbc:mariadb:///db?socketFactory=test"),
52+
"hostname must be set to connect socket");
53+
}
54+
4255
@Test
4356
void socketTimeout() throws SQLException {
4457
Assumptions.assumeTrue(
@@ -882,30 +895,29 @@ public void windowsNamedPipe() throws SQLException {
882895
if (rs != null) {
883896
assertTrue(rs.next());
884897
System.out.println("named_pipe:" + rs.getString(1));
885-
if (rs.getBoolean(1)) {
886-
String namedPipeName = rs.getString(2);
887-
System.out.println("namedPipeName:" + namedPipeName);
888-
889-
// skip test if no namedPipeName was obtained because then we do not use a socket connection
890-
Assumptions.assumeTrue(namedPipeName != null);
891-
try (Connection connection = createCon("pipe=" + namedPipeName)) {
892-
java.sql.Statement stmt = connection.createStatement();
893-
try (ResultSet rs2 = stmt.executeQuery("SELECT 1")) {
894-
assertTrue(rs2.next());
895-
}
898+
Assumptions.assumeTrue(rs.getBoolean(1));
899+
String namedPipeName = rs.getString(2);
900+
System.out.println("namedPipeName:" + namedPipeName);
901+
902+
// skip test if no namedPipeName was obtained because then we do not use a socket connection
903+
Assumptions.assumeTrue(namedPipeName != null);
904+
try (Connection connection = createCon("pipe=" + namedPipeName)) {
905+
java.sql.Statement stmt = connection.createStatement();
906+
try (ResultSet rs2 = stmt.executeQuery("SELECT 1")) {
907+
assertTrue(rs2.next());
896908
}
897-
// connection without host name
898-
try (java.sql.Connection connection =
899-
DriverManager.getConnection(
900-
"jdbc:mariadb:///"
901-
+ sharedConn.getCatalog()
902-
+ mDefUrl.substring(mDefUrl.indexOf("?user="))
903-
+ "&pipe="
904-
+ namedPipeName)) {
905-
java.sql.Statement stmt = connection.createStatement();
906-
try (ResultSet rs2 = stmt.executeQuery("SELECT 1")) {
907-
assertTrue(rs2.next());
908-
}
909+
}
910+
// connection without host name
911+
try (java.sql.Connection connection =
912+
DriverManager.getConnection(
913+
"jdbc:mariadb:///"
914+
+ sharedConn.getCatalog()
915+
+ mDefUrl.substring(mDefUrl.indexOf("?user="))
916+
+ "&pipe="
917+
+ namedPipeName)) {
918+
java.sql.Statement stmt = connection.createStatement();
919+
try (ResultSet rs2 = stmt.executeQuery("SELECT 1")) {
920+
assertTrue(rs2.next());
909921
}
910922
}
911923
}
@@ -969,9 +981,14 @@ public void localSocket() throws Exception {
969981

970982
@Test
971983
public void socketFactoryTest() throws SQLException {
984+
try (Connection conn = createCon("socketFactory=" + SocketFactoryBasicTest.class.getName())) {
985+
conn.isValid(1);
986+
}
987+
972988
try (Connection conn = createCon("socketFactory=" + SocketFactoryTest.class.getName())) {
973989
conn.isValid(1);
974990
}
991+
975992
Common.assertThrowsContains(
976993
SQLNonTransientConnectionException.class,
977994
() -> createCon("socketFactory=wrongClass"),

0 commit comments

Comments
 (0)