Skip to content

Commit

Permalink
SQL-2271: Fix appending of clientInfo to appName if it exists (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
bucaojit authored Aug 14, 2024
1 parent 51bebc1 commit 1690365
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 23 deletions.
41 changes: 18 additions & 23 deletions src/main/java/com/mongodb/jdbc/MongoConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public class MongoConnection implements Connection {
private String logDirPath;
private boolean extJsonMode;
private UuidRepresentation uuidRepresentation;
private String appName;

public MongoConnection(
MongoClient mongoClient, MongoConnectionProperties connectionProperties) {
Expand All @@ -104,7 +105,13 @@ public MongoConnection(
this.mongoClientSettings = createMongoClientSettings(connectionProperties);

if (mongoClient == null) {
this.mongoClient = createMongoClient(connectionProperties);
this.mongoClient =
MongoClients.create(
this.mongoClientSettings,
MongoDriverInformation.builder()
.driverName(MongoDriver.NAME)
.driverVersion(MongoDriver.getVersion())
.build());
} else {
this.mongoClient = mongoClient;
}
Expand All @@ -121,44 +128,32 @@ private void initializeConnection(MongoConnectionProperties connectionProperties
this.extJsonMode = connectionProperties.getExtJsonMode();
this.uuidRepresentation =
connectionProperties.getConnectionString().getUuidRepresentation();
this.appName = buildAppName(connectionProperties);

this.isClosed = false;
}

private MongoClient createMongoClient(MongoConnectionProperties connectionProperties) {
StringBuilder appName =
private String buildAppName(MongoConnectionProperties connectionProperties) {
StringBuilder appNameBuilder =
new StringBuilder(MongoDriver.NAME).append("+").append(MongoDriver.getVersion());

MongoDriverInformation.Builder mdiBuilder;
String clientInfo = connectionProperties.getClientInfo();
String[] clientInfoSplit = (clientInfo == null) ? null : clientInfo.split("\\+");
if (clientInfoSplit != null && clientInfoSplit.length == 2) {
appName.append('|').append(clientInfo);
MongoDriverInformation driverInfoWithClientInfo =
MongoDriverInformation.builder()
.driverName(clientInfoSplit[0])
.driverVersion(clientInfoSplit[1])
.build();
mdiBuilder = MongoDriverInformation.builder(driverInfoWithClientInfo);
} else {
mdiBuilder = MongoDriverInformation.builder();
if (clientInfo != null) {
String[] clientInfoSplit = clientInfo.split("\\+");
if (clientInfoSplit.length == 2) {
appNameBuilder.append('|').append(clientInfo);
}
}
MongoDriverInformation mongoDriverInformation =
mdiBuilder
.driverName(MongoDriver.NAME)
.driverVersion(MongoDriver.getVersion())
.build();

return MongoClients.create(this.mongoClientSettings, mongoDriverInformation);
return appNameBuilder.toString();
}

private MongoClientSettings createMongoClientSettings(
MongoConnectionProperties connectionProperties) {
String appName = MongoDriver.NAME + "+" + MongoDriver.getVersion();

MongoClientSettings.Builder settingsBuilder =
MongoClientSettings.builder()
.applicationName(appName)
.applicationName(this.appName)
.applyConnectionString(connectionProperties.getConnectionString());

MongoCredential credential = connectionProperties.getConnectionString().getCredential();
Expand Down
53 changes: 53 additions & 0 deletions src/test/java/com/mongodb/jdbc/MongoConnectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.internal.MongoClientImpl;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Savepoint;
Expand All @@ -28,6 +31,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
Expand All @@ -37,6 +41,9 @@
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@MockitoSettings(strictness = Strictness.WARN)
class MongoConnectionTest extends MongoMock {
static final String localhost = "mongodb://localhost";
@Mock private MongoConnectionProperties mockConnectionProperties;

@BeforeAll
protected void initMocks() {
MockitoAnnotations.initMocks(this);
Expand All @@ -49,6 +56,52 @@ void setupTest() throws NoSuchFieldException {
resetMockObjs();
}

@BeforeEach
void setUp() {
when(mockConnectionProperties.getConnectionString())
.thenReturn(new ConnectionString(localhost));
when(mockConnectionProperties.getDatabase()).thenReturn("test");
}

private String getApplicationName(MongoConnection connection) {
MongoClientImpl mongoClientImpl = (MongoClientImpl) connection.getMongoClient();
MongoClientSettings mcs = mongoClientImpl.getSettings();
return mcs.getApplicationName();
}

@Test
void testBuildAppNameWithoutClientInfo() {
when(mockConnectionProperties.getClientInfo()).thenReturn(null);

mongoConnection = new MongoConnection(null, mockConnectionProperties);

String expectedAppName = MongoDriver.NAME + "+" + MongoDriver.getVersion();
assertEquals(expectedAppName, getApplicationName(mongoConnection));
}

@Test
void testAppNameWithValidClientInfo() {
String clientInfo = "test-client+1.0.0";
when(mockConnectionProperties.getClientInfo()).thenReturn(clientInfo);

mongoConnection = new MongoConnection(null, mockConnectionProperties);

String expectedAppName =
MongoDriver.NAME + "+" + MongoDriver.getVersion() + "|" + clientInfo;
assertEquals(expectedAppName, getApplicationName(mongoConnection));
}

@Test
void testAppNameWithInvalidClientInfo() {
// Client information has to be in the format 'name+version'
when(mockConnectionProperties.getClientInfo()).thenReturn("invalid-client-info");

mongoConnection = new MongoConnection(null, mockConnectionProperties);

String expectedAppName = MongoDriver.NAME + "+" + MongoDriver.getVersion();
assertEquals(expectedAppName, getApplicationName(mongoConnection));
}

// to replace lambda as input in the testExceptionAfterConnectionClosed
interface TestInterface {
void test() throws SQLException;
Expand Down

0 comments on commit 1690365

Please sign in to comment.