Convert some JDBC tests from Groovy to Java (#8511)
Co-authored-by: opentelemetrybot <107717825+opentelemetrybot@users.noreply.github.com>
This commit is contained in:
parent
5d8e37ad1b
commit
22bd9f59df
|
@ -1,264 +0,0 @@
|
|||
/*
|
||||
* Copyright The OpenTelemetry Authors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
package io.opentelemetry.instrumentation.jdbc
|
||||
|
||||
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter
|
||||
import io.opentelemetry.instrumentation.jdbc.internal.OpenTelemetryConnection
|
||||
import spock.lang.Specification
|
||||
|
||||
import java.sql.Driver
|
||||
import java.sql.DriverManager
|
||||
import java.sql.SQLFeatureNotSupportedException
|
||||
|
||||
class OpenTelemetryDriverTest extends Specification {
|
||||
|
||||
def cleanup() {
|
||||
if (!OpenTelemetryDriver.registered) {
|
||||
OpenTelemetryDriver.register()
|
||||
}
|
||||
}
|
||||
|
||||
def "verify driver auto registered"() {
|
||||
when:
|
||||
Class driverClass = Class.forName("io.opentelemetry.instrumentation.jdbc.OpenTelemetryDriver")
|
||||
def drivers = DriverManager.drivers
|
||||
|
||||
then:
|
||||
driverClass != null
|
||||
OpenTelemetryDriver.registered
|
||||
drivers.any { driver ->
|
||||
driver instanceof OpenTelemetryDriver && driver == OpenTelemetryDriver.INSTANCE
|
||||
}
|
||||
}
|
||||
|
||||
def "verify standard properties"() {
|
||||
expect:
|
||||
!OpenTelemetryDriver.INSTANCE.jdbcCompliant()
|
||||
|
||||
String[] parts = Instrumenter.getPackage().getImplementationVersion().split("\\.")
|
||||
|
||||
OpenTelemetryDriver.INSTANCE.majorVersion == Integer.parseInt(parts[0])
|
||||
OpenTelemetryDriver.INSTANCE.minorVersion == Integer.parseInt(parts[1])
|
||||
}
|
||||
|
||||
def "verify parent logger thrown an exception"() {
|
||||
when:
|
||||
OpenTelemetryDriver.INSTANCE.parentLogger
|
||||
|
||||
then:
|
||||
def e = thrown(SQLFeatureNotSupportedException)
|
||||
e.message == "Feature not supported"
|
||||
}
|
||||
|
||||
def "verify accepted urls"() {
|
||||
expect:
|
||||
def driver = OpenTelemetryDriver.INSTANCE
|
||||
driver.acceptsURL(url) == expected
|
||||
|
||||
where:
|
||||
url | expected
|
||||
null | false
|
||||
"" | false
|
||||
"jdbc:" | false
|
||||
"jdbc::" | false
|
||||
"bogus:string" | false
|
||||
"jdbc:postgresql://127.0.0.1:5432/dbname" | false
|
||||
"jdbc:otel:postgresql://127.0.0.1:5432/dbname" | true
|
||||
}
|
||||
|
||||
def "verify deregister"() {
|
||||
when:
|
||||
if (OpenTelemetryDriver.registered) {
|
||||
OpenTelemetryDriver.deregister()
|
||||
}
|
||||
|
||||
then:
|
||||
!OpenTelemetryDriver.registered
|
||||
DriverManager.drivers.every { driver ->
|
||||
!(driver instanceof OpenTelemetryDriver)
|
||||
}
|
||||
}
|
||||
|
||||
def "verify register"() {
|
||||
when:
|
||||
if (OpenTelemetryDriver.registered) {
|
||||
OpenTelemetryDriver.deregister()
|
||||
}
|
||||
OpenTelemetryDriver.register()
|
||||
|
||||
then:
|
||||
OpenTelemetryDriver.registered
|
||||
DriverManager.drivers.any { driver ->
|
||||
driver instanceof OpenTelemetryDriver && driver == OpenTelemetryDriver.INSTANCE
|
||||
}
|
||||
}
|
||||
|
||||
def "verify connection with null url"() {
|
||||
when:
|
||||
OpenTelemetryDriver.INSTANCE.connect(null, null)
|
||||
|
||||
then:
|
||||
def e = thrown(IllegalArgumentException)
|
||||
e.message == "url is required"
|
||||
}
|
||||
|
||||
def "verify connection with empty url"() {
|
||||
when:
|
||||
OpenTelemetryDriver.INSTANCE.connect(" ", null)
|
||||
|
||||
then:
|
||||
def e = thrown(IllegalArgumentException)
|
||||
e.message == "url is required"
|
||||
}
|
||||
|
||||
def "verify connection with not accepted url"() {
|
||||
when:
|
||||
def connection = OpenTelemetryDriver.INSTANCE.connect("abc:xyz", null)
|
||||
|
||||
then:
|
||||
connection == null
|
||||
}
|
||||
|
||||
def "verify add driver candidate"() {
|
||||
when:
|
||||
deregisterTestDriver()
|
||||
TestDriver driver = new TestDriver()
|
||||
OpenTelemetryDriver.INSTANCE.addDriverCandidate(driver)
|
||||
def connection = OpenTelemetryDriver.INSTANCE.connect("jdbc:otel:test:", null)
|
||||
OpenTelemetryDriver.INSTANCE.removeDriverCandidate(driver)
|
||||
|
||||
then:
|
||||
connection != null
|
||||
connection instanceof OpenTelemetryConnection
|
||||
}
|
||||
|
||||
def "verify remove driver candidate"() {
|
||||
when:
|
||||
deregisterTestDriver()
|
||||
TestDriver driver = new TestDriver()
|
||||
OpenTelemetryDriver.INSTANCE.addDriverCandidate(driver)
|
||||
OpenTelemetryDriver.INSTANCE.removeDriverCandidate(driver)
|
||||
def connection = OpenTelemetryDriver.INSTANCE.connect("jdbc:otel:test:", null)
|
||||
|
||||
then:
|
||||
connection == null
|
||||
def e = thrown(IllegalStateException)
|
||||
e.message == "Unable to find a driver that accepts url: jdbc:test:"
|
||||
}
|
||||
|
||||
def "verify driver candidate has higher priority"() {
|
||||
when:
|
||||
deregisterTestDriver()
|
||||
TestDriver localDriver = new TestDriver()
|
||||
TestDriver globalDriver = new TestDriver()
|
||||
|
||||
OpenTelemetryDriver.INSTANCE.addDriverCandidate(localDriver)
|
||||
DriverManager.registerDriver(globalDriver)
|
||||
Driver winner = OpenTelemetryDriver.INSTANCE.findDriver("jdbc:test:")
|
||||
OpenTelemetryDriver.INSTANCE.removeDriverCandidate(localDriver)
|
||||
|
||||
then:
|
||||
winner == localDriver
|
||||
winner != globalDriver
|
||||
}
|
||||
|
||||
def "verify two clashing driver candidates"() {
|
||||
when:
|
||||
TestDriver localDriver1 = new TestDriver()
|
||||
TestDriver localDriver2 = new TestDriver()
|
||||
|
||||
OpenTelemetryDriver.INSTANCE.addDriverCandidate(localDriver1)
|
||||
OpenTelemetryDriver.INSTANCE.addDriverCandidate(localDriver2)
|
||||
Driver winner = OpenTelemetryDriver.INSTANCE.findDriver("jdbc:test:")
|
||||
OpenTelemetryDriver.INSTANCE.removeDriverCandidate(localDriver1)
|
||||
OpenTelemetryDriver.INSTANCE.removeDriverCandidate(localDriver2)
|
||||
|
||||
then:
|
||||
winner == localDriver1
|
||||
winner != localDriver2
|
||||
}
|
||||
|
||||
def "verify drivers in DriverManager are used as fallback"() {
|
||||
when:
|
||||
registerTestDriver()
|
||||
TestDriver localDriver = new TestDriver() {
|
||||
boolean acceptsURL(String url) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
OpenTelemetryDriver.INSTANCE.addDriverCandidate(localDriver)
|
||||
|
||||
Driver winner = OpenTelemetryDriver.INSTANCE.findDriver("jdbc:test:")
|
||||
OpenTelemetryDriver.INSTANCE.removeDriverCandidate(localDriver)
|
||||
|
||||
then:
|
||||
winner != null
|
||||
winner != localDriver
|
||||
}
|
||||
|
||||
def "verify connection with accepted url"() {
|
||||
when:
|
||||
registerTestDriver()
|
||||
def connection = OpenTelemetryDriver.INSTANCE.connect("jdbc:otel:test:", null)
|
||||
|
||||
then:
|
||||
connection != null
|
||||
connection instanceof OpenTelemetryConnection
|
||||
}
|
||||
|
||||
def "verify get property info with null url"() {
|
||||
when:
|
||||
OpenTelemetryDriver.INSTANCE.getPropertyInfo(null, null)
|
||||
|
||||
then:
|
||||
def e = thrown(IllegalArgumentException)
|
||||
e.message == "url is required"
|
||||
}
|
||||
|
||||
def "verify get property info with empty url"() {
|
||||
when:
|
||||
OpenTelemetryDriver.INSTANCE.getPropertyInfo(" ", null)
|
||||
|
||||
then:
|
||||
def e = thrown(IllegalArgumentException)
|
||||
e.message == "url is required"
|
||||
}
|
||||
|
||||
def "verify get property info with unknown driver url"() {
|
||||
when:
|
||||
def realUrl = "jdbc:unknown"
|
||||
OpenTelemetryDriver.INSTANCE.getPropertyInfo(realUrl, null)
|
||||
|
||||
then:
|
||||
def e = thrown(IllegalStateException)
|
||||
e.message == "Unable to find a driver that accepts url: ${realUrl}"
|
||||
}
|
||||
|
||||
def "verify get property info with test driver url"() {
|
||||
when:
|
||||
registerTestDriver()
|
||||
def realUrl = "jdbc:otel:test:"
|
||||
def propertyInfos = OpenTelemetryDriver.INSTANCE.getPropertyInfo(realUrl, null)
|
||||
|
||||
then:
|
||||
propertyInfos.size() == 1
|
||||
propertyInfos[0].name == "test"
|
||||
propertyInfos[0].value == "test"
|
||||
}
|
||||
|
||||
private static void registerTestDriver() {
|
||||
if (!(DriverManager.drivers.any { it instanceof TestDriver })) {
|
||||
DriverManager.registerDriver(new TestDriver())
|
||||
}
|
||||
}
|
||||
|
||||
private static void deregisterTestDriver() {
|
||||
if ((DriverManager.drivers.any { it instanceof TestDriver })) {
|
||||
DriverManager.deregisterDriver(new TestDriver())
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,64 +0,0 @@
|
|||
/*
|
||||
* Copyright The OpenTelemetry Authors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
package io.opentelemetry.instrumentation.jdbc.datasource
|
||||
|
||||
import io.opentelemetry.api.OpenTelemetry
|
||||
import io.opentelemetry.context.propagation.ContextPropagators
|
||||
import io.opentelemetry.instrumentation.jdbc.internal.OpenTelemetryConnection
|
||||
import spock.lang.Specification
|
||||
|
||||
class OpenTelemetryDataSourceTest extends Specification {
|
||||
|
||||
def "verify get connection"() {
|
||||
when:
|
||||
def ot = OpenTelemetry.propagating(ContextPropagators.noop())
|
||||
def dataSource = new OpenTelemetryDataSource(new TestDataSource(), ot)
|
||||
def connection = dataSource.getConnection()
|
||||
|
||||
then:
|
||||
connection != null
|
||||
connection instanceof OpenTelemetryConnection
|
||||
connection.statementInstrumenter != null
|
||||
|
||||
when:
|
||||
def dbInfo = ((OpenTelemetryConnection) connection).dbInfo
|
||||
|
||||
then:
|
||||
dbInfo.system == "postgresql"
|
||||
dbInfo.subtype == null
|
||||
dbInfo.shortUrl == "postgresql://127.0.0.1:5432"
|
||||
dbInfo.user == null
|
||||
dbInfo.name == null
|
||||
dbInfo.db == "dbname"
|
||||
dbInfo.host == "127.0.0.1"
|
||||
dbInfo.port == 5432
|
||||
}
|
||||
|
||||
def "verify get connection with username and password"() {
|
||||
when:
|
||||
def ot = OpenTelemetry.propagating(ContextPropagators.noop())
|
||||
def dataSource = new OpenTelemetryDataSource(new TestDataSource(), ot)
|
||||
def connection = dataSource.getConnection(null, null)
|
||||
|
||||
then:
|
||||
connection != null
|
||||
connection instanceof OpenTelemetryConnection
|
||||
connection.statementInstrumenter != null
|
||||
|
||||
when:
|
||||
def dbInfo = ((OpenTelemetryConnection) connection).dbInfo
|
||||
|
||||
then:
|
||||
dbInfo.system == "postgresql"
|
||||
dbInfo.subtype == null
|
||||
dbInfo.shortUrl == "postgresql://127.0.0.1:5432"
|
||||
dbInfo.user == null
|
||||
dbInfo.name == null
|
||||
dbInfo.db == "dbname"
|
||||
dbInfo.host == "127.0.0.1"
|
||||
dbInfo.port == 5432
|
||||
}
|
||||
}
|
|
@ -1,63 +0,0 @@
|
|||
/*
|
||||
* Copyright The OpenTelemetry Authors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
package io.opentelemetry.instrumentation.jdbc.datasource
|
||||
|
||||
import io.opentelemetry.instrumentation.jdbc.TestConnection
|
||||
|
||||
import javax.sql.DataSource
|
||||
import java.sql.Connection
|
||||
import java.sql.SQLException
|
||||
import java.sql.SQLFeatureNotSupportedException
|
||||
import java.util.logging.Logger
|
||||
|
||||
class TestDataSource implements DataSource {
|
||||
|
||||
@Override
|
||||
Connection getConnection() throws SQLException {
|
||||
return new TestConnection()
|
||||
}
|
||||
|
||||
@Override
|
||||
Connection getConnection(String username, String password) throws SQLException {
|
||||
return new TestConnection()
|
||||
}
|
||||
|
||||
@Override
|
||||
PrintWriter getLogWriter() throws SQLException {
|
||||
return null
|
||||
}
|
||||
|
||||
@Override
|
||||
void setLogWriter(PrintWriter out) throws SQLException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
void setLoginTimeout(int seconds) throws SQLException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
int getLoginTimeout() throws SQLException {
|
||||
return 0
|
||||
}
|
||||
|
||||
@Override
|
||||
Logger getParentLogger() throws SQLFeatureNotSupportedException {
|
||||
return null
|
||||
}
|
||||
|
||||
@Override
|
||||
def <T> T unwrap(Class<T> iface) throws SQLException {
|
||||
return null
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean isWrapperFor(Class<?> iface) throws SQLException {
|
||||
return false
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,304 @@
|
|||
/*
|
||||
* Copyright The OpenTelemetry Authors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
package io.opentelemetry.instrumentation.jdbc;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
|
||||
import io.opentelemetry.instrumentation.jdbc.internal.OpenTelemetryConnection;
|
||||
import java.sql.Connection;
|
||||
import java.sql.Driver;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.DriverPropertyInfo;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.SQLFeatureNotSupportedException;
|
||||
import java.util.Enumeration;
|
||||
import java.util.stream.Stream;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
public class OpenTelemetryDriverTest {
|
||||
|
||||
@DisplayName("verify driver auto registered")
|
||||
@Order(1) // verifyRemoveDriverCandidate test method removes the drivers
|
||||
@Test
|
||||
void verifyOpenTelemetryDriverAutoRegistration() {
|
||||
|
||||
Enumeration<Driver> drivers = DriverManager.getDrivers();
|
||||
|
||||
// From JDBC 4.0 (Java SE 6), the driver can be auto-registered from the java.sql.Driver file
|
||||
// contained in the META-INF.services folder
|
||||
assertTrue(OpenTelemetryDriver.isRegistered());
|
||||
|
||||
assertTrue(hasOpenTelemetryDriver(drivers));
|
||||
}
|
||||
|
||||
private static boolean hasOpenTelemetryDriver(Enumeration<Driver> drivers) {
|
||||
while (drivers.hasMoreElements()) {
|
||||
Driver driver = drivers.nextElement();
|
||||
if (driver.equals(OpenTelemetryDriver.INSTANCE)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@DisplayName("verify standard properties")
|
||||
@Test
|
||||
void verifyStandardProperties() {
|
||||
|
||||
assertThat(OpenTelemetryDriver.INSTANCE.jdbcCompliant()).isFalse();
|
||||
|
||||
String[] parts = Instrumenter.class.getPackage().getImplementationVersion().split("\\.");
|
||||
|
||||
assertThat(OpenTelemetryDriver.INSTANCE.getMajorVersion())
|
||||
.isEqualTo(Integer.parseInt(parts[0]));
|
||||
assertThat(OpenTelemetryDriver.INSTANCE.getMinorVersion())
|
||||
.isEqualTo(Integer.parseInt(parts[1]));
|
||||
}
|
||||
|
||||
@DisplayName("verify parent logger thrown an exception")
|
||||
@Test
|
||||
void verifyParentLoggerThrownAnException() {
|
||||
assertThatThrownBy(() -> OpenTelemetryDriver.INSTANCE.getParentLogger())
|
||||
.isInstanceOf(SQLFeatureNotSupportedException.class)
|
||||
.hasMessage("Feature not supported");
|
||||
}
|
||||
|
||||
@DisplayName("verify accepted urls")
|
||||
@ParameterizedTest
|
||||
@MethodSource("provideInputOutputForAcceptUrl")
|
||||
void verifyAcceptedUrls(String input, boolean expected) {
|
||||
OpenTelemetryDriver driver = OpenTelemetryDriver.INSTANCE;
|
||||
assertThat(driver.acceptsURL(input)).isEqualTo(expected);
|
||||
}
|
||||
|
||||
private static Stream<Arguments> provideInputOutputForAcceptUrl() {
|
||||
return Stream.of(
|
||||
Arguments.of(null, false),
|
||||
Arguments.of("", false),
|
||||
Arguments.of("jdbc:", false),
|
||||
Arguments.of("bogus:string", false),
|
||||
Arguments.of("jdbc:postgresql://127.0.0.1:5432/dbname", false),
|
||||
Arguments.of("jdbc:otel:postgresql://127.0.0.1:5432/dbname", true));
|
||||
}
|
||||
|
||||
@DisplayName("verify connection with null url")
|
||||
@Test
|
||||
void verifyConnectionWithNullUrl() {
|
||||
assertThatThrownBy(() -> OpenTelemetryDriver.INSTANCE.connect(null, null))
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessage("url is required");
|
||||
}
|
||||
|
||||
@DisplayName("verify connection with empty url")
|
||||
@Test
|
||||
void verifyConnectionWithEmptyUrl() {
|
||||
assertThatThrownBy(() -> OpenTelemetryDriver.INSTANCE.connect(" ", null))
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessage("url is required");
|
||||
}
|
||||
|
||||
@DisplayName("verify connection with not accepted url")
|
||||
@Test
|
||||
void verifyConnectionWithNotAcceptedUrl() throws SQLException {
|
||||
Connection connection = OpenTelemetryDriver.INSTANCE.connect("abc:xyz", null);
|
||||
assertNull(connection);
|
||||
}
|
||||
|
||||
@DisplayName("verify add driver candidate")
|
||||
@Test
|
||||
void verifyAddDriverCandidate() throws SQLException {
|
||||
TestDriver driver = new TestDriver();
|
||||
OpenTelemetryDriver.addDriverCandidate(driver);
|
||||
Connection connection = OpenTelemetryDriver.INSTANCE.connect("jdbc:otel:test:", null);
|
||||
OpenTelemetryDriver.removeDriverCandidate(driver);
|
||||
|
||||
assertThat(connection).isExactlyInstanceOf(OpenTelemetryConnection.class);
|
||||
}
|
||||
|
||||
@DisplayName("verify remove driver candidate")
|
||||
@Test
|
||||
void verifyRemoveDriverCandidate() throws SQLException {
|
||||
|
||||
unregisterDrivers();
|
||||
|
||||
TestDriver newDriver = new TestDriver();
|
||||
OpenTelemetryDriver.addDriverCandidate(newDriver);
|
||||
OpenTelemetryDriver.removeDriverCandidate(newDriver);
|
||||
|
||||
assertThatThrownBy(() -> OpenTelemetryDriver.INSTANCE.connect("jdbc:otel:test:", null))
|
||||
.isInstanceOf(IllegalStateException.class)
|
||||
.hasMessage("Unable to find a driver that accepts url: jdbc:test:");
|
||||
}
|
||||
|
||||
private static void unregisterDrivers() throws SQLException {
|
||||
Enumeration<Driver> drivers = DriverManager.getDrivers();
|
||||
|
||||
while (drivers.hasMoreElements()) {
|
||||
Driver driver = drivers.nextElement();
|
||||
DriverManager.deregisterDriver(driver);
|
||||
}
|
||||
}
|
||||
|
||||
@DisplayName("Driver candidate has higher priority")
|
||||
@Test
|
||||
void verifyDriverCandidateHasHigherPriority() throws SQLException {
|
||||
|
||||
deregisterTestDriver();
|
||||
TestDriver localDriver = new TestDriver();
|
||||
TestDriver globalDriver = new TestDriver();
|
||||
|
||||
OpenTelemetryDriver.addDriverCandidate(localDriver);
|
||||
DriverManager.registerDriver(globalDriver);
|
||||
Driver winner = OpenTelemetryDriver.findDriver("jdbc:test:");
|
||||
OpenTelemetryDriver.removeDriverCandidate(localDriver);
|
||||
|
||||
assertThat(winner).isEqualTo(localDriver);
|
||||
assertThat(winner).isNotEqualTo(globalDriver);
|
||||
}
|
||||
|
||||
@DisplayName("Two clashing driver candidates")
|
||||
@Test
|
||||
void verifyTwoClashingDriverCandidates() throws SQLException {
|
||||
|
||||
TestDriver localDriver1 = new TestDriver();
|
||||
TestDriver localDriver2 = new TestDriver();
|
||||
|
||||
OpenTelemetryDriver.addDriverCandidate(localDriver1);
|
||||
OpenTelemetryDriver.addDriverCandidate(localDriver2);
|
||||
|
||||
Driver winner2 = OpenTelemetryDriver.findDriver("jdbc:test:");
|
||||
OpenTelemetryDriver.removeDriverCandidate(localDriver1);
|
||||
OpenTelemetryDriver.removeDriverCandidate(localDriver2);
|
||||
|
||||
assertThat(winner2).isEqualTo(localDriver1);
|
||||
assertThat(winner2).isNotEqualTo(localDriver2);
|
||||
}
|
||||
|
||||
@DisplayName("Verify drivers in DriverManager are used as fallback")
|
||||
@Test
|
||||
void verifyDriversInDriverManagerAreUsedAsFallback() {
|
||||
|
||||
registerTestDriver();
|
||||
TestDriver localDriver3 =
|
||||
new TestDriver() {
|
||||
@Override
|
||||
public boolean acceptsURL(String url) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
OpenTelemetryDriver.addDriverCandidate(localDriver3);
|
||||
|
||||
Driver winner3 = OpenTelemetryDriver.findDriver("jdbc:test:");
|
||||
OpenTelemetryDriver.removeDriverCandidate(localDriver3);
|
||||
|
||||
assertThat(winner3).isNotNull();
|
||||
assertThat(winner3).isNotEqualTo(localDriver3);
|
||||
}
|
||||
|
||||
@DisplayName("Verify connection with accepted url")
|
||||
@Test
|
||||
void verifyConnectionWithAcceptedUrl() throws SQLException {
|
||||
|
||||
registerTestDriver();
|
||||
|
||||
Connection connection2 = OpenTelemetryDriver.INSTANCE.connect("jdbc:otel:test:", null);
|
||||
|
||||
assertThat(connection2).isNotNull();
|
||||
assertThat(connection2).isExactlyInstanceOf(OpenTelemetryConnection.class);
|
||||
}
|
||||
|
||||
@DisplayName("Verify get property info with test driver url")
|
||||
@Test
|
||||
void verifyGetPropertyInfoWithTestDriverUrl() throws SQLException {
|
||||
|
||||
registerTestDriver();
|
||||
String testUrl = "jdbc:otel:test:";
|
||||
DriverPropertyInfo[] propertyInfos =
|
||||
OpenTelemetryDriver.INSTANCE.getPropertyInfo(testUrl, null);
|
||||
assertThat(propertyInfos).hasSize(1);
|
||||
assertThat(propertyInfos[0].name).isEqualTo("test");
|
||||
assertThat(propertyInfos[0].value).isEqualTo("test");
|
||||
}
|
||||
|
||||
private static void deregisterTestDriver() {
|
||||
if (hasTestDriver()) {
|
||||
try {
|
||||
DriverManager.deregisterDriver(new TestDriver());
|
||||
} catch (SQLException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void registerTestDriver() {
|
||||
if (!hasTestDriver()) {
|
||||
try {
|
||||
DriverManager.registerDriver(new TestDriver());
|
||||
} catch (SQLException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean hasTestDriver() {
|
||||
Enumeration<Driver> drivers = DriverManager.getDrivers();
|
||||
while (drivers.hasMoreElements()) {
|
||||
Driver driver = drivers.nextElement();
|
||||
if (driver instanceof TestDriver) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@DisplayName("verify get property info with null url")
|
||||
@Test
|
||||
void verifyGetPropertyInfoWithNullUrl() {
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> OpenTelemetryDriver.INSTANCE.getPropertyInfo(null, null),
|
||||
"url is required");
|
||||
}
|
||||
|
||||
@DisplayName("verify get property info with empty url")
|
||||
@Test
|
||||
void verifyGetPropertyInfoWithEmptyUrl() {
|
||||
assertThatThrownBy(() -> OpenTelemetryDriver.INSTANCE.getPropertyInfo(" ", null))
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessage("url is required");
|
||||
}
|
||||
|
||||
@DisplayName("verify get property info with unknown driver url")
|
||||
@Test
|
||||
void verifyGetPropertyInfoWithUnknownDriverUrl() {
|
||||
String unknownUrl = "jdbc:unknown";
|
||||
assertThatThrownBy(() -> OpenTelemetryDriver.INSTANCE.getPropertyInfo(unknownUrl, null))
|
||||
.isInstanceOf(IllegalStateException.class)
|
||||
.hasMessage("Unable to find a driver that accepts url: " + unknownUrl);
|
||||
}
|
||||
|
||||
@DisplayName("verify get property info with test driver url")
|
||||
@Test
|
||||
void verifyGetPropertyInfoWithUnknowDriverUrl() {
|
||||
String unknownUrl = "jdbc:unknown";
|
||||
assertThatThrownBy(() -> OpenTelemetryDriver.INSTANCE.getPropertyInfo(unknownUrl, null))
|
||||
.isInstanceOf(IllegalStateException.class)
|
||||
.hasMessage("Unable to find a driver that accepts url: " + unknownUrl);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
/*
|
||||
* Copyright The OpenTelemetry Authors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
package io.opentelemetry.instrumentation.jdbc.datasource;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
import io.opentelemetry.api.OpenTelemetry;
|
||||
import io.opentelemetry.context.propagation.ContextPropagators;
|
||||
import io.opentelemetry.instrumentation.jdbc.internal.OpenTelemetryConnection;
|
||||
import io.opentelemetry.instrumentation.jdbc.internal.dbinfo.DbInfo;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class OpenTelemetryDataSourceTest {
|
||||
|
||||
@DisplayName("verify get connection")
|
||||
@Test
|
||||
void verifyGetConnection() throws SQLException {
|
||||
|
||||
OpenTelemetry openTelemetry = OpenTelemetry.propagating(ContextPropagators.noop());
|
||||
TestDataSource testDataSource = new TestDataSource();
|
||||
OpenTelemetryDataSource dataSource = new OpenTelemetryDataSource(testDataSource, openTelemetry);
|
||||
Connection connection = dataSource.getConnection();
|
||||
|
||||
assertThat(connection).isExactlyInstanceOf(OpenTelemetryConnection.class);
|
||||
|
||||
DbInfo dbInfo = ((OpenTelemetryConnection) connection).getDbInfo();
|
||||
|
||||
assertThat(dbInfo.getSystem()).isEqualTo("postgresql");
|
||||
assertNull(dbInfo.getSubtype());
|
||||
assertThat(dbInfo.getShortUrl()).isEqualTo("postgresql://127.0.0.1:5432");
|
||||
assertNull(dbInfo.getUser());
|
||||
assertNull(dbInfo.getName());
|
||||
assertThat(dbInfo.getDb()).isEqualTo("dbname");
|
||||
assertThat(dbInfo.getHost()).isEqualTo("127.0.0.1");
|
||||
assertThat(dbInfo.getPort()).isEqualTo(5432);
|
||||
}
|
||||
|
||||
@DisplayName("verify get connection with username and password")
|
||||
@Test
|
||||
void verifyGetConnectionWithUserNameAndPassword() throws SQLException {
|
||||
|
||||
OpenTelemetry openTelemetry = OpenTelemetry.propagating(ContextPropagators.noop());
|
||||
OpenTelemetryDataSource dataSource =
|
||||
new OpenTelemetryDataSource(new TestDataSource(), openTelemetry);
|
||||
Connection connection = dataSource.getConnection(null, null);
|
||||
|
||||
assertThat(connection).isExactlyInstanceOf(OpenTelemetryConnection.class);
|
||||
|
||||
DbInfo dbInfo = ((OpenTelemetryConnection) connection).getDbInfo();
|
||||
|
||||
assertThat(dbInfo.getSystem()).isEqualTo("postgresql");
|
||||
assertNull(dbInfo.getSubtype());
|
||||
assertThat(dbInfo.getShortUrl()).isEqualTo("postgresql://127.0.0.1:5432");
|
||||
assertNull(dbInfo.getUser());
|
||||
assertNull(dbInfo.getName());
|
||||
assertThat(dbInfo.getDb()).isEqualTo("dbname");
|
||||
assertThat(dbInfo.getHost()).isEqualTo("127.0.0.1");
|
||||
assertThat(dbInfo.getPort()).isEqualTo(5432);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
/*
|
||||
* Copyright The OpenTelemetry Authors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
package io.opentelemetry.instrumentation.jdbc.datasource;
|
||||
|
||||
import io.opentelemetry.instrumentation.jdbc.TestConnection;
|
||||
import java.io.PrintWriter;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.SQLFeatureNotSupportedException;
|
||||
import java.util.logging.Logger;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
class TestDataSource implements DataSource {
|
||||
|
||||
@Override
|
||||
public Connection getConnection() {
|
||||
return new TestConnection();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection getConnection(String username, String password) {
|
||||
return new TestConnection();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PrintWriter getLogWriter() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLogWriter(PrintWriter out) {}
|
||||
|
||||
@Override
|
||||
public void setLoginTimeout(int seconds) {}
|
||||
|
||||
@Override
|
||||
public int getLoginTimeout() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T unwrap(Class<T> iface) throws SQLException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isWrapperFor(Class<?> iface) throws SQLException {
|
||||
return false;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue