Skip to content

Commit 8828b79

Browse files
committed
Fix the JDBC driver version
returned by the methods in the classes CassandraDriver and CassandraDatabaseMetaData to be consistent with the version defined into the POM file. Also externalize some driver properties into a jdbc-driver.properties file. Update JUnit Jupiter dependencies and improve logging in the tests.
1 parent e67060a commit 8828b79

File tree

10 files changed

+182
-44
lines changed

10 files changed

+182
-44
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to
55
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## Unreleased
8+
### Fixed
9+
- Fix the JDBC driver version returned by the methods of the classes `CassandraDriver` and `CassandraDatabaseMetaData`
10+
to be consistent with the version of the JDBC wrapper artifact (see issue
11+
[#19](https://github.com/ing-bank/cassandra-jdbc-wrapper/issues/19)).
12+
713
## 4.8.0 - 2023-01-12
814
### Added
915
- Implement the methods `getMetaData()` and `getParameterMetaData()` into the implementation class

pom.xml

+10-2
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,10 @@
9898
<!-- Versions for test dependencies -->
9999
<achilles-embedded.version>6.1.0</achilles-embedded.version>
100100
<hamcrest.version>2.2</hamcrest.version>
101-
<junit5.version>5.9.1</junit5.version>
102-
<junit-platform.version>1.9.1</junit-platform.version>
101+
<junit5.version>5.9.2</junit5.version>
102+
<junit-platform.version>1.9.2</junit-platform.version>
103103
<mockito.version>3.12.4</mockito.version>
104+
<slf4j.version>1.7.36</slf4j.version>
104105
<!-- Versions for plugins -->
105106
<maven-checkstyle-plugin.version>3.2.0</maven-checkstyle-plugin.version>
106107
<maven-clean-plugin.version>3.2.0</maven-clean-plugin.version>
@@ -191,6 +192,12 @@
191192
<version>${achilles-embedded.version}</version>
192193
<scope>test</scope>
193194
</dependency>
195+
<!-- Logging for tests -->
196+
<dependency>
197+
<groupId>org.slf4j</groupId>
198+
<artifactId>slf4j-simple</artifactId>
199+
<version>${slf4j.version}</version>
200+
</dependency>
194201
</dependencies>
195202

196203
<build>
@@ -215,6 +222,7 @@
215222
<version>${maven-resources-plugin.version}</version>
216223
<configuration>
217224
<encoding>${encoding}</encoding>
225+
<propertiesEncoding>${encoding}</propertiesEncoding>
218226
</configuration>
219227
</plugin>
220228

src/main/java/com/ing/data/cassandra/jdbc/CassandraConnection.java

+5-11
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
import static com.ing.data.cassandra.jdbc.CassandraResultSet.DEFAULT_TYPE;
6666
import static com.ing.data.cassandra.jdbc.Utils.ALWAYS_AUTOCOMMIT;
6767
import static com.ing.data.cassandra.jdbc.Utils.BAD_TIMEOUT;
68-
import static com.ing.data.cassandra.jdbc.Utils.NO_INTERFACE;
6968
import static com.ing.data.cassandra.jdbc.Utils.NO_TRANSACTIONS;
7069
import static com.ing.data.cassandra.jdbc.Utils.PROTOCOL;
7170
import static com.ing.data.cassandra.jdbc.Utils.TAG_ACTIVE_CQL_VERSION;
@@ -77,23 +76,17 @@
7776
import static com.ing.data.cassandra.jdbc.Utils.TAG_USER;
7877
import static com.ing.data.cassandra.jdbc.Utils.WAS_CLOSED_CONN;
7978
import static com.ing.data.cassandra.jdbc.Utils.createSubName;
79+
import static com.ing.data.cassandra.jdbc.Utils.getDriverProperty;
8080

8181
/**
8282
* Cassandra connection: implementation class for {@link Connection} to create a JDBC connection to a Cassandra cluster.
8383
*/
8484
public class CassandraConnection extends AbstractConnection implements Connection {
8585

86-
/**
87-
* The database product name.
88-
*/
89-
public static final String DB_PRODUCT_NAME = "Cassandra";
90-
/**
91-
* The default CQL language version supported by the driver.
92-
*/
93-
public static final String DEFAULT_CQL_VERSION = "3.0.0";
94-
9586
// Minimal Apache Cassandra version supported by the DataStax Java Driver for Apache Cassandra on top which this
9687
// wrapper is built.
88+
// If available, the effective version run by the node on which the connection is established will override these
89+
// values.
9790
/**
9891
* Minimal Apache Cassandra major version supported by the DataStax Java Driver for Apache Cassandra.
9992
*/
@@ -153,7 +146,8 @@ public CassandraConnection(final SessionHolder sessionHolder) throws SQLExceptio
153146
this.optionSet = lookupOptionSet(sessionProperties.getProperty(TAG_COMPLIANCE_MODE));
154147
this.username = sessionProperties.getProperty(TAG_USER,
155148
defaultConfigProfile.getString(DefaultDriverOption.AUTH_PROVIDER_USER_NAME, StringUtils.EMPTY));
156-
final String cqlVersion = sessionProperties.getProperty(TAG_CQL_VERSION, DEFAULT_CQL_VERSION);
149+
final String cqlVersion = sessionProperties.getProperty(TAG_CQL_VERSION,
150+
getDriverProperty("database.defaultCqlVersion"));
157151
this.connectionProperties.setProperty(TAG_ACTIVE_CQL_VERSION, cqlVersion);
158152
this.defaultConsistencyLevel = DefaultConsistencyLevel.valueOf(
159153
sessionProperties.getProperty(TAG_CONSISTENCY_LEVEL,

src/main/java/com/ing/data/cassandra/jdbc/CassandraDatabaseMetaData.java

+9-10
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@
2828
import java.sql.SQLSyntaxErrorException;
2929

3030
import static com.ing.data.cassandra.jdbc.Utils.NOT_SUPPORTED;
31+
import static com.ing.data.cassandra.jdbc.Utils.getDriverProperty;
32+
import static com.ing.data.cassandra.jdbc.Utils.parseVersion;
3133

3234
/**
3335
* Cassandra database metadata: implementation class for {@link DatabaseMetaData}.
3436
*/
3537
public class CassandraDatabaseMetaData implements DatabaseMetaData {
3638

37-
static final int JDBC_MAJOR_VERSION = 4;
38-
static final int JDBC_MINOR_VERSION = 0;
3939
static final int UNKNOWN_MAX_VALUE = 0;
4040
static final int KEYSPACE_NAME_MAX_LENGTH = 48;
4141
static final int TABLE_NAME_MAX_LENGTH = 48;
@@ -223,7 +223,7 @@ public int getDatabaseMinorVersion() {
223223

224224
@Override
225225
public String getDatabaseProductName() {
226-
return CassandraConnection.DB_PRODUCT_NAME;
226+
return getDriverProperty("database.productName");
227227
}
228228

229229
/**
@@ -250,23 +250,22 @@ public int getDefaultTransactionIsolation() {
250250

251251
@Override
252252
public int getDriverMajorVersion() {
253-
return CassandraDriver.DRIVER_MAJOR_VERSION;
253+
return parseVersion(getDriverVersion(), 0);
254254
}
255255

256256
@Override
257257
public int getDriverMinorVersion() {
258-
return CassandraDriver.DRIVER_MINOR_VERSION;
258+
return parseVersion(getDriverVersion(), 1);
259259
}
260260

261261
@Override
262262
public String getDriverName() {
263-
return CassandraDriver.DRIVER_NAME;
263+
return getDriverProperty("driver.name");
264264
}
265265

266266
@Override
267267
public String getDriverVersion() {
268-
return String.format("%d.%d.%d", CassandraDriver.DRIVER_MAJOR_VERSION, CassandraDriver.DRIVER_MINOR_VERSION,
269-
CassandraDriver.DRIVER_PATCH_VERSION);
268+
return getDriverProperty("driver.version");
270269
}
271270

272271
/**
@@ -358,12 +357,12 @@ public ResultSet getIndexInfo(final String catalog, final String schema, final S
358357

359358
@Override
360359
public int getJDBCMajorVersion() {
361-
return JDBC_MAJOR_VERSION;
360+
return parseVersion(getDriverProperty("driver.jdbcVersion"), 0);
362361
}
363362

364363
@Override
365364
public int getJDBCMinorVersion() {
366-
return JDBC_MINOR_VERSION;
365+
return parseVersion(getDriverProperty("driver.jdbcVersion"), 1);
367366
}
368367

369368
@Override

src/main/java/com/ing/data/cassandra/jdbc/CassandraDriver.java

+4-19
Original file line numberDiff line numberDiff line change
@@ -37,30 +37,15 @@
3737
import static com.ing.data.cassandra.jdbc.Utils.PROTOCOL;
3838
import static com.ing.data.cassandra.jdbc.Utils.TAG_PASSWORD;
3939
import static com.ing.data.cassandra.jdbc.Utils.TAG_USER;
40+
import static com.ing.data.cassandra.jdbc.Utils.getDriverProperty;
41+
import static com.ing.data.cassandra.jdbc.Utils.parseVersion;
4042

4143
/**
4244
* The Cassandra driver implementation.
4345
*/
4446
@SuppressWarnings("UnstableApiUsage")
4547
public class CassandraDriver implements Driver {
4648

47-
/**
48-
* The JDBC driver major version.
49-
*/
50-
public static final int DRIVER_MAJOR_VERSION = 4;
51-
/**
52-
* The JDBC driver minor version.
53-
*/
54-
public static final int DRIVER_MINOR_VERSION = 4;
55-
/**
56-
* The JDBC driver patch version.
57-
*/
58-
public static final int DRIVER_PATCH_VERSION = 0;
59-
/**
60-
* The JDBC driver name.
61-
*/
62-
public static final String DRIVER_NAME = "Cassandra JDBC Driver";
63-
6449
static {
6550
// Register the CassandraDriver with DriverManager.
6651
try {
@@ -124,12 +109,12 @@ public Connection connect(final String url, final Properties properties) throws
124109

125110
@Override
126111
public int getMajorVersion() {
127-
return DRIVER_MAJOR_VERSION;
112+
return parseVersion(getDriverProperty("driver.version"), 0);
128113
}
129114

130115
@Override
131116
public int getMinorVersion() {
132-
return DRIVER_MINOR_VERSION;
117+
return parseVersion(getDriverProperty("driver.version"), 1);
133118
}
134119

135120
@Override

src/main/java/com/ing/data/cassandra/jdbc/Utils.java

+49
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import org.slf4j.Logger;
2323
import org.slf4j.LoggerFactory;
2424

25+
import java.io.IOException;
26+
import java.io.InputStream;
2527
import java.io.UnsupportedEncodingException;
2628
import java.net.URI;
2729
import java.net.URISyntaxException;
@@ -54,6 +56,11 @@ public final class Utils {
5456
* Default Cassandra cluster port.
5557
*/
5658
public static final int DEFAULT_PORT = 9042;
59+
/**
60+
* Properties file name containing some properties relative to this JDBC wrapper (such as JDBC driver version,
61+
* name, etc.).
62+
*/
63+
public static final String JDBC_DRIVER_PROPERTIES_FILE = "jdbc-driver.properties";
5764

5865
/**
5966
* JDBC URL parameter key for the database version.
@@ -197,6 +204,48 @@ private Utils() {
197204
// Private constructor to hide the public one.
198205
}
199206

207+
/**
208+
* Gets a property value from the Cassandra JDBC driver properties file.
209+
*
210+
* @param name The name of the property.
211+
* @return The property value or an empty string the value cannot be retrieved.
212+
*/
213+
public static String getDriverProperty(final String name) {
214+
try (final InputStream propertiesFile =
215+
Utils.class.getClassLoader().getResourceAsStream(JDBC_DRIVER_PROPERTIES_FILE)) {
216+
final Properties driverProperties = new Properties();
217+
driverProperties.load(propertiesFile);
218+
return driverProperties.getProperty(name, StringUtils.EMPTY);
219+
} catch (IOException ex) {
220+
LOG.error("Unable to get JDBC driver property: {}.", name, ex);
221+
return StringUtils.EMPTY;
222+
}
223+
}
224+
225+
/**
226+
* Gets a part of a version string.
227+
* <p>
228+
* It uses the dot character as separator to parse the different parts of a version (major, minor, patch).
229+
* </p>
230+
*
231+
* @param version The version string (for example X.Y.Z).
232+
* @param part The part of the version to extract (for the semantic versioning, use 0 for the major version, 1 for
233+
* the minor and 2 for the patch).
234+
* @return The requested part of the version, or 0 if the requested part cannot be parsed correctly.
235+
*/
236+
public static int parseVersion(final String version, final int part) {
237+
if (StringUtils.isBlank(version) || StringUtils.countMatches(version, ".") < part || part < 0 ) {
238+
return 0;
239+
} else {
240+
try {
241+
return Integer.parseInt(version.split("\\.")[part]);
242+
} catch (final NumberFormatException ex) {
243+
LOG.error("Unable to parse version: {}", version);
244+
return 0;
245+
}
246+
}
247+
}
248+
200249
/**
201250
* Parses a URL for the Cassandra JDBC Driver.
202251
* <p>
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
16+
# The Cassandra JDBC Driver version is retrieved from the version defined in the POM file.
17+
driver.version=${project.version}
18+
driver.name=Cassandra JDBC Driver
19+
driver.jdbcVersion=4.0
20+
21+
database.productName=Cassandra
22+
database.defaultCqlVersion=3.0.0

src/test/java/com/ing/data/cassandra/jdbc/ConnectionUnitTest.java

+30-1
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@
4646

4747
import java.net.InetSocketAddress;
4848
import java.net.URL;
49+
import java.sql.DatabaseMetaData;
4950
import java.sql.ResultSet;
5051
import java.sql.SQLException;
5152
import java.sql.SQLNonTransientConnectionException;
52-
import java.sql.Statement;
5353
import java.time.Duration;
5454
import java.util.Collections;
5555
import java.util.Objects;
@@ -65,6 +65,7 @@
6565
import static org.hamcrest.Matchers.instanceOf;
6666
import static org.junit.jupiter.api.Assertions.assertEquals;
6767
import static org.junit.jupiter.api.Assertions.assertFalse;
68+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
6869
import static org.junit.jupiter.api.Assertions.assertNotNull;
6970
import static org.junit.jupiter.api.Assertions.assertNull;
7071
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -367,4 +368,32 @@ void givenSessionToConnect_andLiquibaseCompliance() throws SQLException {
367368
assertNull(jdbcConnection.getCatalog());
368369
}
369370

371+
@Test
372+
void givenConnection_whenGetMetaData_getExpectedResultSet() throws Exception {
373+
initConnection(KEYSPACE);
374+
assertNotNull(sqlConnection);
375+
assertNotNull(sqlConnection.getMetaData());
376+
377+
final DatabaseMetaData dbMetadata = sqlConnection.getMetaData();
378+
log.debug("====================================================");
379+
log.debug("Connection Metadata");
380+
log.debug("====================================================");
381+
log.debug("Driver name: {}", dbMetadata.getDriverName());
382+
log.debug("Driver version: {}", dbMetadata.getDriverVersion());
383+
log.debug("DB name: {}", dbMetadata.getDatabaseProductName());
384+
log.debug("DB version: {}", dbMetadata.getDatabaseProductVersion());
385+
log.debug("JDBC version: {}.{}", dbMetadata.getJDBCMajorVersion(), dbMetadata.getJDBCMinorVersion());
386+
log.debug("====================================================");
387+
388+
assertEquals("Cassandra JDBC Driver", dbMetadata.getDriverName());
389+
assertNotEquals(0, dbMetadata.getDriverMajorVersion());
390+
assertNotEquals(0, dbMetadata.getDriverMinorVersion());
391+
assertEquals(4, dbMetadata.getJDBCMajorVersion());
392+
assertEquals(0, dbMetadata.getJDBCMinorVersion());
393+
assertEquals("Cassandra", dbMetadata.getDatabaseProductName());
394+
assertThat(dbMetadata.getDriverVersion(), Matchers.matchesPattern("\\d.\\d+.\\d+"));
395+
assertThat(dbMetadata.getDatabaseProductVersion(), Matchers.matchesPattern("\\d.\\d+.\\d+"));
396+
sqlConnection.close();
397+
}
398+
370399
}

src/test/java/com/ing/data/cassandra/jdbc/UtilsUnitTest.java

+20-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import com.datastax.oss.driver.api.core.config.DefaultDriverOption;
1818
import com.datastax.oss.driver.api.core.config.DriverOption;
19+
import org.apache.commons.lang3.StringUtils;
1920
import org.junit.jupiter.api.Test;
2021
import org.junit.jupiter.params.ParameterizedTest;
2122
import org.junit.jupiter.params.provider.Arguments;
@@ -36,10 +37,11 @@
3637
import static com.ing.data.cassandra.jdbc.Utils.HOST_IN_URL;
3738
import static com.ing.data.cassandra.jdbc.Utils.HOST_REQUIRED;
3839
import static com.ing.data.cassandra.jdbc.Utils.SECURECONENCTBUNDLE_REQUIRED;
39-
import static com.ing.data.cassandra.jdbc.Utils.TAG_DATABASE_NAME;
4040
import static com.ing.data.cassandra.jdbc.Utils.TAG_PORT_NUMBER;
4141
import static com.ing.data.cassandra.jdbc.Utils.TAG_SERVER_NAME;
4242
import static com.ing.data.cassandra.jdbc.Utils.URI_IS_SIMPLE;
43+
import static com.ing.data.cassandra.jdbc.Utils.getDriverProperty;
44+
import static com.ing.data.cassandra.jdbc.Utils.parseVersion;
4345
import static org.junit.jupiter.api.Assertions.assertEquals;
4446
import static org.junit.jupiter.api.Assertions.assertNotNull;
4547
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -242,4 +244,21 @@ void testMissingSecureConnectBundleOnDbaasConenctionString(final String jdbcUrl)
242244
() -> Utils.parseURL(jdbcUrl));
243245
assertEquals(SECURECONENCTBUNDLE_REQUIRED, exception.getMessage());
244246
}
247+
248+
@Test
249+
void testGetDriverProperty() {
250+
assertEquals(StringUtils.EMPTY, getDriverProperty("invalidProperty"));
251+
assertNotNull(getDriverProperty("driver.name"));
252+
}
253+
254+
@Test
255+
void testParseVersion() {
256+
assertEquals(0, parseVersion(StringUtils.EMPTY, 0));
257+
assertEquals(0, parseVersion("1.0.0", 3));
258+
assertEquals(0, parseVersion("1.0.0", -1));
259+
assertEquals(1, parseVersion("1.2.3", 0));
260+
assertEquals(2, parseVersion("1.2.3", 1));
261+
assertEquals(3, parseVersion("1.2.3", 2));
262+
assertEquals(0, parseVersion("1.a", 1));
263+
}
245264
}

0 commit comments

Comments
 (0)