Skip to content

Commit 71b6a79

Browse files
committed
DATAJDBC-637 - Fix tests for all databases.
Adds SQL scripts for all databases. Separates tests for all databases vs. those that actually support nanosecond precision. Original pull request: #255.
1 parent 9f0f930 commit 71b6a79

9 files changed

+519
-2
lines changed

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/JdbcAggregateTemplateIntegrationTests.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -853,11 +853,26 @@ public void resavingAnUnversionedEntity() {
853853
}
854854

855855
@Test // DATAJDBC-637
856+
@EnabledOnFeature(SUPPORTS_NANOSECOND_PRECISION)
856857
public void saveAndLoadDateTimeWithFullPrecision() {
857858

858859
WithLocalDateTime entity = new WithLocalDateTime();
859860
entity.id = 23L;
860-
entity.testTime = LocalDateTime.of(5, 5, 5, 5, 5, 5, 123456789);
861+
entity.testTime = LocalDateTime.of(2005, 5, 5, 5, 5, 5, 123456789);
862+
863+
template.insert(entity);
864+
865+
WithLocalDateTime loaded = template.findById(23L, WithLocalDateTime.class);
866+
867+
assertThat(loaded.testTime).isEqualTo(entity.testTime);
868+
}
869+
870+
@Test // DATAJDBC-637
871+
public void saveAndLoadDateTimeWithMicrosecondPrecision() {
872+
873+
WithLocalDateTime entity = new WithLocalDateTime();
874+
entity.id = 23L;
875+
entity.testTime = LocalDateTime.of(2005, 5, 5, 5, 5, 5, 123456000);
861876

862877
template.insert(entity);
863878

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* Copyright 2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jdbc.testing;
17+
18+
import static org.assertj.core.api.Assumptions.*;
19+
20+
import java.util.Arrays;
21+
import java.util.Locale;
22+
import java.util.function.Consumer;
23+
24+
import org.springframework.jdbc.core.ConnectionCallback;
25+
import org.springframework.jdbc.core.JdbcOperations;
26+
27+
/**
28+
* This class provides information about which features a database integration supports in order to react on the
29+
* presence or absence of features in tests.
30+
*
31+
* @author Jens Schauder
32+
*/
33+
public class TestDatabaseFeatures {
34+
35+
private final Database database;
36+
37+
public TestDatabaseFeatures(JdbcOperations jdbcTemplate) {
38+
39+
String productName = jdbcTemplate.execute(
40+
(ConnectionCallback<String>) c -> c.getMetaData().getDatabaseProductName().toLowerCase(Locale.ENGLISH));
41+
42+
database = Arrays.stream(Database.values()).filter(db -> db.matches(productName)).findFirst().get();
43+
}
44+
45+
/**
46+
* Not all databases support really huge numbers as represented by {@link java.math.BigDecimal} and similar.
47+
*/
48+
private void supportsHugeNumbers() {
49+
assumeThat(database).isNotIn(Database.Oracle, Database.SqlServer);
50+
}
51+
52+
/**
53+
* Oracles JDBC driver seems to have a bug that makes it impossible to acquire generated keys when the column is
54+
* quoted. See
55+
* https://stackoverflow.com/questions/62263576/how-to-get-the-generated-key-for-a-column-with-lowercase-characters-from-oracle
56+
*/
57+
private void supportsQuotedIds() {
58+
assumeThat(database).isNotEqualTo(Database.Oracle);
59+
}
60+
61+
/**
62+
* Microsoft SqlServer does not allow explicitly setting ids in columns where the value gets generated by the
63+
* database. Such columns therefore must not be used in referenced entities, since we do a delete and insert, which
64+
* must not recreate an id. See https://jira.spring.io/browse/DATAJDBC-210
65+
*/
66+
private void supportsGeneratedIdsInReferencedEntities() {
67+
assumeThat(database).isNotEqualTo(Database.SqlServer);
68+
}
69+
70+
private void supportsArrays() {
71+
72+
assumeThat(database).isNotIn(Database.MySql, Database.MariaDb, Database.SqlServer, Database.Db2, Database.Oracle);
73+
}
74+
75+
private void supportsNanosecondPrecision() {
76+
77+
assumeThat(database).isNotIn(Database.MySql, Database.PostgreSql, Database.MariaDb, Database.SqlServer);
78+
}
79+
80+
private void supportsMultiDimensionalArrays() {
81+
82+
supportsArrays();
83+
assumeThat(database).isNotIn(Database.H2, Database.Hsql);
84+
}
85+
86+
public void databaseIs(Database database) {
87+
assumeThat(this.database).isEqualTo(database);
88+
}
89+
90+
public enum Database {
91+
Hsql, H2, MySql, MariaDb, PostgreSql, SqlServer("microsoft"), Db2, Oracle;
92+
93+
private final String identification;
94+
95+
Database(String identification) {
96+
this.identification = identification;
97+
}
98+
99+
Database() {
100+
this.identification = null;
101+
}
102+
103+
boolean matches(String productName) {
104+
105+
String identification = this.identification == null ? name().toLowerCase() : this.identification;
106+
return productName.contains(identification);
107+
}
108+
}
109+
110+
public enum Feature {
111+
112+
SUPPORTS_MULTIDIMENSIONAL_ARRAYS(TestDatabaseFeatures::supportsMultiDimensionalArrays), //
113+
SUPPORTS_QUOTED_IDS(TestDatabaseFeatures::supportsQuotedIds), //
114+
SUPPORTS_HUGE_NUMBERS(TestDatabaseFeatures::supportsHugeNumbers), //
115+
SUPPORTS_ARRAYS(TestDatabaseFeatures::supportsArrays), //
116+
SUPPORTS_GENERATED_IDS_IN_REFERENCED_ENTITIES(TestDatabaseFeatures::supportsGeneratedIdsInReferencedEntities), //
117+
SUPPORTS_NANOSECOND_PRECISION(TestDatabaseFeatures::supportsNanosecondPrecision), //
118+
IS_HSQL(f -> f.databaseIs(Database.Hsql));
119+
120+
private final Consumer<TestDatabaseFeatures> featureMethod;
121+
122+
Feature(Consumer<TestDatabaseFeatures> featureMethod) {
123+
this.featureMethod = featureMethod;
124+
}
125+
126+
void test(TestDatabaseFeatures features) {
127+
featureMethod.accept(features);
128+
}
129+
}
130+
}

spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-db2.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ DROP TABLE NO_ID_LIST_CHAIN4;
3535

3636
DROP TABLE WITH_READ_ONLY;
3737
DROP TABLE VERSIONED_AGGREGATE;
38+
DROP TABLE WITH_LOCAL_DATE_TIME;
3839

3940
CREATE TABLE LEGO_SET
4041
(
@@ -343,3 +344,10 @@ CREATE TABLE VERSIONED_AGGREGATE
343344
ID BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY,
344345
VERSION BIGINT
345346
);
347+
348+
349+
CREATE TABLE WITH_LOCAL_DATE_TIME
350+
(
351+
ID BIGINT NOT NULL PRIMARY KEY,
352+
TEST_TIME TIMESTAMP(9)
353+
);

spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-h2.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,6 @@ CREATE TABLE WITH_READ_ONLY
307307

308308
CREATE TABLE WITH_LOCAL_DATE_TIME
309309
(
310-
ID SERIAL PRIMARY KEY,
310+
ID BIGINT PRIMARY KEY,
311311
TEST_TIME TIMESTAMP(9) WITHOUT TIME ZONE
312312
);

spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-mariadb.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,10 @@ CREATE TABLE VERSIONED_AGGREGATE
290290
ID BIGINT AUTO_INCREMENT PRIMARY KEY,
291291
VERSION BIGINT
292292
);
293+
294+
295+
CREATE TABLE WITH_LOCAL_DATE_TIME
296+
(
297+
ID BIGINT PRIMARY KEY,
298+
TEST_TIME TIMESTAMP(6)
299+
);

spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-mssql.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,12 @@ CREATE TABLE VERSIONED_AGGREGATE
298298
ID BIGINT IDENTITY PRIMARY KEY,
299299
VERSION BIGINT
300300
);
301+
302+
303+
DROP TABLE IF EXISTS WITH_LOCAL_DATE_TIME;
304+
305+
CREATE TABLE WITH_LOCAL_DATE_TIME
306+
(
307+
ID BIGINT PRIMARY KEY,
308+
TEST_TIME datetime2(7)
309+
);

spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-mysql.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,3 +295,10 @@ CREATE TABLE WITH_READ_ONLY
295295
NAME VARCHAR(200),
296296
READ_ONLY VARCHAR(200) DEFAULT 'from-db'
297297
);
298+
299+
300+
CREATE TABLE WITH_LOCAL_DATE_TIME
301+
(
302+
ID BIGINT PRIMARY KEY,
303+
TEST_TIME TIMESTAMP(6)
304+
);

0 commit comments

Comments
 (0)