|
| 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 | +} |
0 commit comments