Browse Source

SimpleAliasRegistry.hasAlias properly resolves multiple chained aliases

Issue: SPR-17191

(cherry picked from commit 2ac23badee)
pull/1946/head
Juergen Hoeller 6 years ago
parent
commit
68cf18f4a3
  1. 4
      spring-core/src/main/java/org/springframework/core/SimpleAliasRegistry.java
  2. 63
      spring-core/src/test/java/org/springframework/core/SimpleAliasRegistryTests.java

4
spring-core/src/main/java/org/springframework/core/SimpleAliasRegistry.java

@ -102,7 +102,9 @@ public class SimpleAliasRegistry implements AliasRegistry { @@ -102,7 +102,9 @@ public class SimpleAliasRegistry implements AliasRegistry {
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;

63
spring-core/src/test/java/org/springframework/core/SimpleAliasRegistryTests.java

@ -0,0 +1,63 @@ @@ -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"));
}
}
Loading…
Cancel
Save