Skip to content

Commit

Permalink
Handled failure in getApplicationName() (#2571)
Browse files Browse the repository at this point in the history
* Handled failure in getApplicationName()

* Added test to validate appName using select app_name() query

* Updated connect property

* Updated connect property to add appname

* Fixed checking of APPLICATION_NAME in connect properties

* Added test case

---------

Co-authored-by: machavan <[email protected]>
  • Loading branch information
muskan124947 and machavan authored Jan 2, 2025
1 parent 74a5cb8 commit c7096c7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public void setApplicationName(String applicationName) {
@Override
public String getApplicationName() {
return getStringProperty(connectionProps, SQLServerDriverStringProperty.APPLICATION_NAME.toString(),
SQLServerDriverStringProperty.APPLICATION_NAME.getDefaultValue());
SQLServerDriver.constructedAppName);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1294,7 +1294,9 @@ public java.sql.Connection connect(String url, Properties suppliedProperties) th
Properties connectProperties = parseAndMergeProperties(url, suppliedProperties);
if (connectProperties != null) {
result = DriverJDBCVersion.getSQLServerConnection(toString());
connectProperties.setProperty(SQLServerDriverStringProperty.APPLICATION_NAME.toString(), SQLServerDriver.constructedAppName);
if (connectProperties.getProperty(SQLServerDriverStringProperty.APPLICATION_NAME.toString()) == null) {
connectProperties.setProperty(SQLServerDriverStringProperty.APPLICATION_NAME.toString(), SQLServerDriver.constructedAppName);
}
result.connect(connectProperties, null);
}
loggerExternal.exiting(getClassNameLogging(), "connect", result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,44 @@ public void testApplicationName() throws SQLException {
}
}

/**
* test application name by executing select app_name()
*
* @throws SQLException
*/
@Test
public void testApplicationNameUsingApp_Name() throws SQLException {
try (Connection conn = DriverManager.getConnection(connectionString);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT app_name()")) {
if (rs.next()) {
assertEquals(SQLServerDriver.constructedAppName, rs.getString(1));
}
} catch (SQLException e) {
fail(e.getMessage());
}
}

/**
* test application name by executing select app_name()
*
* @throws SQLException
*/
@Test
public void testAppNameWithSpecifiedApplicationName() throws SQLException {
String url = connectionString + ";applicationName={0123456789012345678901234567890123456789012345678901234567890123456789012345678901234589012345678901234567890123456789012345678}";

try (Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT app_name()")) {
if (rs.next()) {
assertEquals(SQLServerDriver.constructedAppName, rs.getString(1));
}
} catch (SQLException e) {
fail(e.getMessage());
}
}

/**
* test application name when system properties are empty
*
Expand Down

0 comments on commit c7096c7

Please sign in to comment.