From d38eb9d6a0c952cd38861d56c86d8a5eac821dfc Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 17 Aug 2018 09:54:14 +0200 Subject: [PATCH] SimpleAliasRegistry.hasAlias properly resolves multiple chained aliases Issue: SPR-17191 (cherry picked from commit 2ac23badee02697c5eb87c46f955387b32a0d581) --- .../core/SimpleAliasRegistry.java | 4 +- .../core/SimpleAliasRegistryTests.java | 63 +++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 spring-core/src/test/java/org/springframework/core/SimpleAliasRegistryTests.java diff --git a/spring-core/src/main/java/org/springframework/core/SimpleAliasRegistry.java b/spring-core/src/main/java/org/springframework/core/SimpleAliasRegistry.java index fcfbd0f6379b..0556a9d6353d 100644 --- a/spring-core/src/main/java/org/springframework/core/SimpleAliasRegistry.java +++ b/spring-core/src/main/java/org/springframework/core/SimpleAliasRegistry.java @@ -86,7 +86,9 @@ public boolean hasAlias(String name, String alias) { String registeredName = entry.getValue(); if (registeredName.equals(name)) { String registeredAlias = entry.getKey(); - return (registeredAlias.equals(alias) || hasAlias(registeredAlias, alias)); + if (registeredAlias.equals(alias) || hasAlias(registeredAlias, alias)) { + return true; + } } } return false; diff --git a/spring-core/src/test/java/org/springframework/core/SimpleAliasRegistryTests.java b/spring-core/src/test/java/org/springframework/core/SimpleAliasRegistryTests.java new file mode 100644 index 000000000000..6aa5658bd607 --- /dev/null +++ b/spring-core/src/test/java/org/springframework/core/SimpleAliasRegistryTests.java @@ -0,0 +1,63 @@ +/* + * Copyright 2002-2018 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 + * + * http://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.springframework.core; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * @author Juergen Hoeller + */ +public class SimpleAliasRegistryTests { + + @Test + public void testAliasChaining() { + SimpleAliasRegistry registry = new SimpleAliasRegistry(); + registry.registerAlias("test", "testAlias"); + registry.registerAlias("testAlias", "testAlias2"); + registry.registerAlias("testAlias2", "testAlias3"); + + assertTrue(registry.hasAlias("test", "testAlias")); + assertTrue(registry.hasAlias("test", "testAlias2")); + assertTrue(registry.hasAlias("test", "testAlias3")); + assertSame("test", registry.canonicalName("testAlias")); + assertSame("test", registry.canonicalName("testAlias2")); + assertSame("test", registry.canonicalName("testAlias3")); + } + + @Test // SPR-17191 + public void testAliasChainingWithMultipleAliases() { + SimpleAliasRegistry registry = new SimpleAliasRegistry(); + registry.registerAlias("name", "alias_a"); + registry.registerAlias("name", "alias_b"); + assertTrue(registry.hasAlias("name", "alias_a")); + assertTrue(registry.hasAlias("name", "alias_b")); + + registry.registerAlias("real_name", "name"); + assertTrue(registry.hasAlias("real_name", "name")); + assertTrue(registry.hasAlias("real_name", "alias_a")); + assertTrue(registry.hasAlias("real_name", "alias_b")); + + registry.registerAlias("name", "alias_c"); + assertTrue(registry.hasAlias("real_name", "name")); + assertTrue(registry.hasAlias("real_name", "alias_a")); + assertTrue(registry.hasAlias("real_name", "alias_b")); + assertTrue(registry.hasAlias("real_name", "alias_c")); + } + +}