Skip to content

Commit

Permalink
SimpleAliasRegistry.hasAlias properly resolves multiple chained aliases
Browse files Browse the repository at this point in the history
Issue: SPR-17191

(cherry picked from commit 2ac23ba)
  • Loading branch information
jhoeller committed Aug 17, 2018
1 parent 5bd4f88 commit d38eb9d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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"));
}

}

0 comments on commit d38eb9d

Please sign in to comment.