From 4a9d42ca47236187fa01cbe072a2121c8df0d360 Mon Sep 17 00:00:00 2001 From: nieqiurong Date: Thu, 18 Apr 2024 17:00:41 +0800 Subject: [PATCH 1/2] Shared ambiguity instance --- .../apache/ibatis/session/Configuration.java | 19 +++---------- .../ConfigurationTest.java | 28 +++++++++++++++++++ 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/apache/ibatis/session/Configuration.java b/src/main/java/org/apache/ibatis/session/Configuration.java index 72b50d3808f..11f6d4cb101 100644 --- a/src/main/java/org/apache/ibatis/session/Configuration.java +++ b/src/main/java/org/apache/ibatis/session/Configuration.java @@ -1106,6 +1106,7 @@ protected static class StrictMap extends ConcurrentHashMap { private static final long serialVersionUID = -4950446264854982944L; private final String name; private BiFunction conflictMessageProducer; + private static final Object AMBIGUITY_INSTANCE = new Object(); public StrictMap(String name, int initialCapacity, float loadFactor) { super(initialCapacity, loadFactor); @@ -1155,7 +1156,7 @@ public V put(String key, V value) { if (super.get(shortKey) == null) { super.put(shortKey, value); } else { - super.put(shortKey, (V) new Ambiguity(shortKey)); + super.put(shortKey, (V) AMBIGUITY_INSTANCE); } } return super.put(key, value); @@ -1176,25 +1177,13 @@ public V get(Object key) { if (value == null) { throw new IllegalArgumentException(name + " does not contain value for " + key); } - if (value instanceof Ambiguity) { - throw new IllegalArgumentException(((Ambiguity) value).getSubject() + " is ambiguous in " + name + if (AMBIGUITY_INSTANCE == value) { + throw new IllegalArgumentException(key + " is ambiguous in " + name + " (try using the full name including the namespace, or rename one of the entries)"); } return value; } - protected static class Ambiguity { - private final String subject; - - public Ambiguity(String subject) { - this.subject = subject; - } - - public String getSubject() { - return subject; - } - } - private String getShortName(String key) { final String[] keyParts = key.split("\\."); return keyParts[keyParts.length - 1]; diff --git a/src/test/java/org/apache/ibatis/submitted/global_variables_defaults/ConfigurationTest.java b/src/test/java/org/apache/ibatis/submitted/global_variables_defaults/ConfigurationTest.java index 6f940466f5d..c8a69edbed2 100644 --- a/src/test/java/org/apache/ibatis/submitted/global_variables_defaults/ConfigurationTest.java +++ b/src/test/java/org/apache/ibatis/submitted/global_variables_defaults/ConfigurationTest.java @@ -17,10 +17,15 @@ import java.io.IOException; import java.io.Reader; +import java.util.ArrayList; import java.util.Properties; +import org.apache.ibatis.builder.StaticSqlSource; import org.apache.ibatis.datasource.unpooled.UnpooledDataSource; import org.apache.ibatis.io.Resources; +import org.apache.ibatis.mapping.MappedStatement; +import org.apache.ibatis.mapping.ResultMap; +import org.apache.ibatis.mapping.SqlCommandType; import org.apache.ibatis.parsing.PropertyParser; import org.apache.ibatis.session.Configuration; import org.apache.ibatis.session.SqlSessionFactory; @@ -79,4 +84,27 @@ void applyPropertyValueOnXmlConfiguration() throws IOException { } + @Test + void testAmbiguityCache() { + Configuration configuration = new Configuration(); + configuration.addMappedStatement( + new MappedStatement.Builder(configuration, + "org.apache.ibatis.submitted.DemoMapper1.selectById", + new StaticSqlSource(configuration, "select * from test where id = 1"), SqlCommandType.SELECT).build() + ); + configuration.addMappedStatement( + new MappedStatement.Builder(configuration, + "org.apache.ibatis.submitted.DemoMapper1.test", + new StaticSqlSource(configuration, "select * from test"), SqlCommandType.SELECT).build() + ); + configuration.addMappedStatement( + new MappedStatement.Builder(configuration, + "org.apache.ibatis.submitted.DemoMapper2.test", + new StaticSqlSource(configuration, "select * from test"), SqlCommandType.SELECT).build() + ); + Assertions.assertThat(configuration.getMappedStatement("selectById")).isNotNull(); + Assertions.assertThatThrownBy(() -> configuration.getMappedStatement("test")) + .isInstanceOf(IllegalArgumentException.class).hasMessage("test is ambiguous in Mapped Statements collection (try using the full name including the namespace, or rename one of the entries)"); + } + } From ad8c4d0669d1182c512e357426e21d97fe1fb53a Mon Sep 17 00:00:00 2001 From: Willie Scholtz Date: Fri, 3 Jan 2025 11:57:34 +0100 Subject: [PATCH 2/2] #3146 Add additional test to ensure the fully qualified statements are found --- .../apache/ibatis/session/Configuration.java | 30 ++++++------- .../ConfigurationTest.java | 42 +++++++++---------- 2 files changed, 35 insertions(+), 37 deletions(-) diff --git a/src/main/java/org/apache/ibatis/session/Configuration.java b/src/main/java/org/apache/ibatis/session/Configuration.java index acfbf17e14f..8234250a6ed 100644 --- a/src/main/java/org/apache/ibatis/session/Configuration.java +++ b/src/main/java/org/apache/ibatis/session/Configuration.java @@ -1,5 +1,5 @@ /* - * Copyright 2009-2024 the original author or authors. + * Copyright 2009-2025 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. @@ -15,20 +15,6 @@ */ package org.apache.ibatis.session; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Properties; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.locks.ReentrantLock; -import java.util.function.BiFunction; - import org.apache.ibatis.binding.MapperRegistry; import org.apache.ibatis.builder.CacheRefResolver; import org.apache.ibatis.builder.IncompleteElementException; @@ -97,6 +83,20 @@ import org.apache.ibatis.type.TypeHandler; import org.apache.ibatis.type.TypeHandlerRegistry; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.locks.ReentrantLock; +import java.util.function.BiFunction; + /** * @author Clinton Begin */ diff --git a/src/test/java/org/apache/ibatis/submitted/global_variables_defaults/ConfigurationTest.java b/src/test/java/org/apache/ibatis/submitted/global_variables_defaults/ConfigurationTest.java index c8a69edbed2..1550979147c 100644 --- a/src/test/java/org/apache/ibatis/submitted/global_variables_defaults/ConfigurationTest.java +++ b/src/test/java/org/apache/ibatis/submitted/global_variables_defaults/ConfigurationTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2009-2023 the original author or authors. + * Copyright 2009-2025 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. @@ -15,16 +15,10 @@ */ package org.apache.ibatis.submitted.global_variables_defaults; -import java.io.IOException; -import java.io.Reader; -import java.util.ArrayList; -import java.util.Properties; - import org.apache.ibatis.builder.StaticSqlSource; import org.apache.ibatis.datasource.unpooled.UnpooledDataSource; import org.apache.ibatis.io.Resources; import org.apache.ibatis.mapping.MappedStatement; -import org.apache.ibatis.mapping.ResultMap; import org.apache.ibatis.mapping.SqlCommandType; import org.apache.ibatis.parsing.PropertyParser; import org.apache.ibatis.session.Configuration; @@ -34,6 +28,10 @@ import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; +import java.io.IOException; +import java.io.Reader; +import java.util.Properties; + class ConfigurationTest { @Test @@ -87,24 +85,24 @@ void applyPropertyValueOnXmlConfiguration() throws IOException { @Test void testAmbiguityCache() { Configuration configuration = new Configuration(); + configuration.addMappedStatement( - new MappedStatement.Builder(configuration, - "org.apache.ibatis.submitted.DemoMapper1.selectById", - new StaticSqlSource(configuration, "select * from test where id = 1"), SqlCommandType.SELECT).build() - ); - configuration.addMappedStatement( - new MappedStatement.Builder(configuration, - "org.apache.ibatis.submitted.DemoMapper1.test", - new StaticSqlSource(configuration, "select * from test"), SqlCommandType.SELECT).build() - ); - configuration.addMappedStatement( - new MappedStatement.Builder(configuration, - "org.apache.ibatis.submitted.DemoMapper2.test", - new StaticSqlSource(configuration, "select * from test"), SqlCommandType.SELECT).build() - ); + new MappedStatement.Builder(configuration, "org.apache.ibatis.submitted.DemoMapper1.selectById", + new StaticSqlSource(configuration, "select * from test where id = 1"), SqlCommandType.SELECT).build()); + configuration + .addMappedStatement(new MappedStatement.Builder(configuration, "org.apache.ibatis.submitted.DemoMapper1.test", + new StaticSqlSource(configuration, "select * from test"), SqlCommandType.SELECT).build()); + configuration + .addMappedStatement(new MappedStatement.Builder(configuration, "org.apache.ibatis.submitted.DemoMapper2.test", + new StaticSqlSource(configuration, "select * from test"), SqlCommandType.SELECT).build()); + Assertions.assertThat(configuration.getMappedStatement("selectById")).isNotNull(); + Assertions.assertThat(configuration.getMappedStatement("org.apache.ibatis.submitted.DemoMapper1.test")).isNotNull(); + Assertions.assertThat(configuration.getMappedStatement("org.apache.ibatis.submitted.DemoMapper2.test")).isNotNull(); + Assertions.assertThatThrownBy(() -> configuration.getMappedStatement("test")) - .isInstanceOf(IllegalArgumentException.class).hasMessage("test is ambiguous in Mapped Statements collection (try using the full name including the namespace, or rename one of the entries)"); + .isInstanceOf(IllegalArgumentException.class).hasMessage( + "test is ambiguous in Mapped Statements collection (try using the full name including the namespace, or rename one of the entries)"); } }