Skip to content

Commit 5894652

Browse files
committed
use default properties for mssql when connection properties does not exist
Signed-off-by: Billy Yuan <billy112487983@gmail.com>
1 parent ca6c8b8 commit 5894652

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

vertx-mssql-client/src/main/java/io/vertx/mssqlclient/MSSQLConnectOptions.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,14 @@ public class MSSQLConnectOptions extends SqlConnectOptions {
4141
public static final String DEFAULT_USER = "sa";
4242
public static final String DEFAULT_PASSWORD = "";
4343
public static final String DEFAULT_SCHEMA = "";
44+
public static final String DEFAULT_APP_NAME = "vertx-mssql-client";
45+
public static final String DEFAULT_CLIENT_INTERFACE_NAME = "Vert.x";
4446
public static final Map<String, String> DEFAULT_PROPERTIES;
4547

4648
static {
4749
Map<String, String> defaultProperties = new HashMap<>();
48-
defaultProperties.put("appName", "vertx-mssql-client");
49-
defaultProperties.put("clientInterfaceName", "Vert.x");
50+
defaultProperties.put("appName", DEFAULT_APP_NAME);
51+
defaultProperties.put("clientInterfaceName", DEFAULT_CLIENT_INTERFACE_NAME);
5052
DEFAULT_PROPERTIES = defaultProperties;
5153
}
5254

@@ -58,7 +60,7 @@ public MSSQLConnectOptions(JsonObject json) {
5860
super(json);
5961
MSSQLConnectOptionsConverter.fromJson(json, this);
6062
}
61-
63+
6264
public MSSQLConnectOptions(SqlConnectOptions other) {
6365
super(other);
6466
}

vertx-mssql-client/src/main/java/io/vertx/mssqlclient/impl/codec/InitCommandCodec.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import io.netty.buffer.ByteBuf;
1515
import io.netty.channel.ChannelHandlerContext;
16+
import io.vertx.mssqlclient.MSSQLConnectOptions;
1617
import io.vertx.mssqlclient.impl.protocol.MessageStatus;
1718
import io.vertx.mssqlclient.impl.protocol.MessageType;
1819
import io.vertx.mssqlclient.impl.protocol.TdsMessage;
@@ -116,6 +117,9 @@ private void sendLoginMessage() {
116117

117118
// AppName
118119
CharSequence appName = properties.get("appName");
120+
if (appName == null || appName.length() == 0) {
121+
appName = MSSQLConnectOptions.DEFAULT_APP_NAME;
122+
}
119123
int appNameOffsetLengthIdx = packet.writerIndex();
120124
packet.writeShortLE(0x00); // offset
121125
packet.writeShortLE(appName.length());
@@ -133,6 +137,9 @@ private void sendLoginMessage() {
133137

134138
// CltIntName
135139
CharSequence interfaceLibraryName = properties.get("clientInterfaceName");
140+
if (interfaceLibraryName == null || interfaceLibraryName.length() == 0) {
141+
interfaceLibraryName = MSSQLConnectOptions.DEFAULT_CLIENT_INTERFACE_NAME;
142+
}
136143
int cltIntNameOffsetLengthIdx = packet.writerIndex();
137144
packet.writeShortLE(0x00); // offset
138145
packet.writeShortLE(interfaceLibraryName.length());
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2011-2020 Contributors to the Eclipse Foundation
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7+
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
8+
*
9+
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
10+
*/
11+
12+
package io.vertx.mssqlclient;
13+
14+
import io.vertx.core.Vertx;
15+
import io.vertx.ext.unit.TestContext;
16+
import io.vertx.ext.unit.junit.VertxUnitRunner;
17+
import io.vertx.sqlclient.SqlConnection;
18+
import org.junit.After;
19+
import org.junit.Before;
20+
import org.junit.Test;
21+
import org.junit.runner.RunWith;
22+
23+
import java.util.HashMap;
24+
25+
@RunWith(VertxUnitRunner.class)
26+
public class MSSQLConnectionTest extends MSSQLTestBase {
27+
Vertx vertx;
28+
MSSQLConnectOptions options;
29+
30+
@Before
31+
public void setup() {
32+
vertx = Vertx.vertx();
33+
options = new MSSQLConnectOptions(MSSQLTestBase.options);
34+
}
35+
36+
@After
37+
public void tearDown(TestContext ctx) {
38+
vertx.close(ctx.asyncAssertSuccess());
39+
}
40+
41+
@Test
42+
public void testConnectWithEmptyProperties(TestContext ctx) {
43+
options.setProperties(new HashMap<>());
44+
MSSQLConnection.connect(vertx, options, ctx.asyncAssertSuccess(SqlConnection::close));
45+
}
46+
}

0 commit comments

Comments
 (0)