forked from eclipse-vertx/vertx-sql-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySQLUnixDomainSocketTest.java
More file actions
129 lines (115 loc) · 4.5 KB
/
MySQLUnixDomainSocketTest.java
File metadata and controls
129 lines (115 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Copyright (c) 2011-2026 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package io.vertx.tests.mysqlclient;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.junit.VertxUnitRunner;
import io.vertx.mysqlclient.MySQLBuilder;
import io.vertx.mysqlclient.MySQLConnectOptions;
import io.vertx.mysqlclient.SslMode;
import io.vertx.sqlclient.Pool;
import io.vertx.sqlclient.SqlClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assume.assumeTrue;
@RunWith(VertxUnitRunner.class)
public class MySQLUnixDomainSocketTest extends MySQLTestBase {
private static final String unixSocketFile = System.getProperty("unix.socket.file");
private Pool client;
private MySQLConnectOptions options;
private Vertx vertx;
@Before
public void setUp() {
String osName = System.getProperty("os.name");
assumeTrue(osName != null && (osName.startsWith("Linux") || osName.startsWith("LINUX")));
options = new MySQLConnectOptions(MySQLTestBase.options);
if (unixSocketFile != null && !unixSocketFile.isEmpty()) {
options.setHost(unixSocketFile);
} else {
options.setHost(rule.domainSocketPath());
}
assumeTrue(options.isUsingDomainSocket());
// Only use native transport for JDK < 16 (Unix domain socket support added in JDK 16)
boolean useNativeTransport = Runtime.version().feature() < 16;
vertx = Vertx.vertx(new VertxOptions().setPreferNativeTransport(useNativeTransport));
assertEquals(useNativeTransport, vertx.isNativeTransportEnabled());
}
@After
public void after() {
if (client != null) {
client.close().await();
}
if (vertx != null) {
vertx.close().await();
}
}
@Test
public void uriSocketHostTest(TestContext context) throws UnsupportedEncodingException {
String path = URLEncoder.encode(options.getHost(), "UTF-8");
uriTest(context, "mysql://" + options.getUser() + ":" + options.getPassword() + "@" + path);
}
@Test
public void uriSocketAttributeTest(TestContext context) throws UnsupportedEncodingException {
String path = URLEncoder.encode(options.getHost(), "UTF-8");
uriTest(context, "mysql://" + options.getUser() + ":" + options.getPassword() + "@192.168.0.67?socket=" + path);
}
private void uriTest(TestContext context, String uri) throws UnsupportedEncodingException {
client = MySQLBuilder.pool(builder -> builder.connectingTo(uri).using(vertx));
client
.getConnection()
.onComplete(context.asyncAssertSuccess(SqlClient::close));
}
@Test
public void simpleConnect(TestContext context) {
client = MySQLBuilder.pool(builder -> builder.connectingTo(new MySQLConnectOptions(options)).using(vertx));
client
.getConnection()
.onComplete(context.asyncAssertSuccess(SqlClient::close));
}
@Test
public void connectWithVertxInstance(TestContext context) {
// Only use native transport for JDK < 16 (Unix domain socket support added in JDK 16)
boolean useNativeTransport = Runtime.version().feature() < 16;
Vertx vertx = Vertx.vertx(new VertxOptions().setPreferNativeTransport(useNativeTransport));
assertEquals(useNativeTransport, vertx.isNativeTransportEnabled());
try {
client = MySQLBuilder.pool(builder -> builder.connectingTo(new MySQLConnectOptions(options)).using(vertx));
Async async = context.async();
client
.getConnection()
.onComplete(context.asyncAssertSuccess(conn -> {
async.complete();
conn.close();
}));
async.await();
} finally {
vertx.close();
}
}
@Test
public void testIgnoreSslMode(TestContext context) {
client = MySQLBuilder.pool(builder -> builder.connectingTo(new MySQLConnectOptions(options).setSslMode(SslMode.REQUIRED)).using(vertx));
client
.getConnection()
.onComplete(context.asyncAssertSuccess(conn -> {
assertFalse(conn.isSSL());
conn.close();
}));
}
}