Skip to content

Commit d38eb9d

Browse files
committed
SimpleAliasRegistry.hasAlias properly resolves multiple chained aliases
Issue: SPR-17191 (cherry picked from commit 2ac23ba)
1 parent 5bd4f88 commit d38eb9d

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

Diff for: spring-core/src/main/java/org/springframework/core/SimpleAliasRegistry.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ public boolean hasAlias(String name, String alias) {
8686
String registeredName = entry.getValue();
8787
if (registeredName.equals(name)) {
8888
String registeredAlias = entry.getKey();
89-
return (registeredAlias.equals(alias) || hasAlias(registeredAlias, alias));
89+
if (registeredAlias.equals(alias) || hasAlias(registeredAlias, alias)) {
90+
return true;
91+
}
9092
}
9193
}
9294
return false;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2002-2018 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+
* http://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+
17+
package org.springframework.core;
18+
19+
import org.junit.Test;
20+
21+
import static org.junit.Assert.*;
22+
23+
/**
24+
* @author Juergen Hoeller
25+
*/
26+
public class SimpleAliasRegistryTests {
27+
28+
@Test
29+
public void testAliasChaining() {
30+
SimpleAliasRegistry registry = new SimpleAliasRegistry();
31+
registry.registerAlias("test", "testAlias");
32+
registry.registerAlias("testAlias", "testAlias2");
33+
registry.registerAlias("testAlias2", "testAlias3");
34+
35+
assertTrue(registry.hasAlias("test", "testAlias"));
36+
assertTrue(registry.hasAlias("test", "testAlias2"));
37+
assertTrue(registry.hasAlias("test", "testAlias3"));
38+
assertSame("test", registry.canonicalName("testAlias"));
39+
assertSame("test", registry.canonicalName("testAlias2"));
40+
assertSame("test", registry.canonicalName("testAlias3"));
41+
}
42+
43+
@Test // SPR-17191
44+
public void testAliasChainingWithMultipleAliases() {
45+
SimpleAliasRegistry registry = new SimpleAliasRegistry();
46+
registry.registerAlias("name", "alias_a");
47+
registry.registerAlias("name", "alias_b");
48+
assertTrue(registry.hasAlias("name", "alias_a"));
49+
assertTrue(registry.hasAlias("name", "alias_b"));
50+
51+
registry.registerAlias("real_name", "name");
52+
assertTrue(registry.hasAlias("real_name", "name"));
53+
assertTrue(registry.hasAlias("real_name", "alias_a"));
54+
assertTrue(registry.hasAlias("real_name", "alias_b"));
55+
56+
registry.registerAlias("name", "alias_c");
57+
assertTrue(registry.hasAlias("real_name", "name"));
58+
assertTrue(registry.hasAlias("real_name", "alias_a"));
59+
assertTrue(registry.hasAlias("real_name", "alias_b"));
60+
assertTrue(registry.hasAlias("real_name", "alias_c"));
61+
}
62+
63+
}

0 commit comments

Comments
 (0)