From dc249c1268158125f0582361eea349f58b6d0a4c Mon Sep 17 00:00:00 2001 From: Iwao AVE! Date: Sat, 9 Nov 2024 13:36:01 +0900 Subject: [PATCH] `VendorDatabaseIdProvider#getDatabaseId()` should return product name when properties is empty `XMLConfigBuilder#databaseIdProviderElement()` sets an empty `Properties` rather than `null`. fixes #3296 --- .../mapping/VendorDatabaseIdProvider.java | 10 ++-- .../mapping/VendorDatabaseIdProviderTest.java | 7 +++ .../DatabaseIdProductNameTest.java | 49 +++++++++++++++++++ .../databaseid_productname/Mapper.java | 26 ++++++++++ .../databaseid_productname/mybatis-config.xml | 44 +++++++++++++++++ 5 files changed, 131 insertions(+), 5 deletions(-) create mode 100644 src/test/java/org/apache/ibatis/submitted/databaseid_productname/DatabaseIdProductNameTest.java create mode 100644 src/test/java/org/apache/ibatis/submitted/databaseid_productname/Mapper.java create mode 100644 src/test/resources/org/apache/ibatis/submitted/databaseid_productname/mybatis-config.xml diff --git a/src/main/java/org/apache/ibatis/mapping/VendorDatabaseIdProvider.java b/src/main/java/org/apache/ibatis/mapping/VendorDatabaseIdProvider.java index 41a5dcd8baa..459e16328e5 100644 --- a/src/main/java/org/apache/ibatis/mapping/VendorDatabaseIdProvider.java +++ b/src/main/java/org/apache/ibatis/mapping/VendorDatabaseIdProvider.java @@ -1,5 +1,5 @@ /* - * Copyright 2009-2023 the original author or authors. + * Copyright 2009-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -55,11 +55,11 @@ public void setProperties(Properties p) { private String getDatabaseName(DataSource dataSource) throws SQLException { String productName = getDatabaseProductName(dataSource); - if (this.properties != null) { - return properties.entrySet().stream().filter(entry -> productName.contains((String) entry.getKey())) - .map(entry -> (String) entry.getValue()).findFirst().orElse(null); + if (properties == null || properties.isEmpty()) { + return productName; } - return productName; + return properties.entrySet().stream().filter(entry -> productName.contains((String) entry.getKey())) + .map(entry -> (String) entry.getValue()).findFirst().orElse(null); } private String getDatabaseProductName(DataSource dataSource) throws SQLException { diff --git a/src/test/java/org/apache/ibatis/mapping/VendorDatabaseIdProviderTest.java b/src/test/java/org/apache/ibatis/mapping/VendorDatabaseIdProviderTest.java index 78852b22b67..4852ecb835a 100644 --- a/src/test/java/org/apache/ibatis/mapping/VendorDatabaseIdProviderTest.java +++ b/src/test/java/org/apache/ibatis/mapping/VendorDatabaseIdProviderTest.java @@ -50,6 +50,13 @@ void shouldProductNameBeReturnedIfPropertiesIsNull() throws Exception { assertEquals(PRODUCT_NAME, provider.getDatabaseId(mockDataSource())); } + @Test + void shouldProductNameBeReturnedIfPropertiesIsEmpty() throws Exception { + VendorDatabaseIdProvider provider = new VendorDatabaseIdProvider(); + provider.setProperties(new Properties()); + assertEquals(PRODUCT_NAME, provider.getDatabaseId(mockDataSource())); + } + @Test void shouldProductNameBeTranslated() throws Exception { VendorDatabaseIdProvider provider = new VendorDatabaseIdProvider(); diff --git a/src/test/java/org/apache/ibatis/submitted/databaseid_productname/DatabaseIdProductNameTest.java b/src/test/java/org/apache/ibatis/submitted/databaseid_productname/DatabaseIdProductNameTest.java new file mode 100644 index 00000000000..22c31ce8e00 --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/databaseid_productname/DatabaseIdProductNameTest.java @@ -0,0 +1,49 @@ +/* + * Copyright 2009-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.ibatis.submitted.databaseid_productname; + +import java.io.Reader; + +import org.apache.ibatis.io.Resources; +import org.apache.ibatis.session.SqlSession; +import org.apache.ibatis.session.SqlSessionFactory; +import org.apache.ibatis.session.SqlSessionFactoryBuilder; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +class DatabaseIdProductNameTest { + + private static SqlSessionFactory sqlSessionFactory; + + @BeforeAll + static void setUp() throws Exception { + try (Reader reader = Resources + .getResourceAsReader("org/apache/ibatis/submitted/databaseid_productname/mybatis-config.xml")) { + sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); + } + } + + @Test + void shouldProductNameBeDatabaseIdIfDbVendorHasNoProperties() { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + Mapper mapper = sqlSession.getMapper(Mapper.class); + String result = mapper.select(); + Assertions.assertEquals("hsql", result); + } + } + +} diff --git a/src/test/java/org/apache/ibatis/submitted/databaseid_productname/Mapper.java b/src/test/java/org/apache/ibatis/submitted/databaseid_productname/Mapper.java new file mode 100644 index 00000000000..8b65b2ba3c7 --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/databaseid_productname/Mapper.java @@ -0,0 +1,26 @@ +/* + * Copyright 2009-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.ibatis.submitted.databaseid_productname; + +import org.apache.ibatis.annotations.Select; + +public interface Mapper { + + @Select(value = "select 'mysql'", databaseId = "MySQL") + @Select(value = "select 'hsql' from (values(0))", databaseId = "HSQL Database Engine") + String select(); + +} diff --git a/src/test/resources/org/apache/ibatis/submitted/databaseid_productname/mybatis-config.xml b/src/test/resources/org/apache/ibatis/submitted/databaseid_productname/mybatis-config.xml new file mode 100644 index 00000000000..7468289b43e --- /dev/null +++ b/src/test/resources/org/apache/ibatis/submitted/databaseid_productname/mybatis-config.xml @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + +