SPR-5906: test and fix for using expressions in property list keys and values

This commit is contained in:
Andy Clement 2009-07-07 20:13:18 +00:00
parent a2bce8c2a8
commit dcb52dbbc1
2 changed files with 118 additions and 0 deletions

View File

@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="properties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<props>
<prop key="user.name">Dave</prop>
<prop key="username">Andy</prop>
</props>
</property>
</bean>
<!-- spr5906 -->
<bean id="derived"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<props>
<prop key="user.name">#{properties['user.name']}</prop>
<prop key="username">#{properties['username']}</prop>
<prop key="username.no.quotes">#{properties[username]}</prop>
<prop key="username.no.brackets">#{properties.username}</prop>
<prop key="#{properties['user.name']}">exists</prop>
<prop key="#{properties.username}">exists also</prop>
</props>
</property>
</bean>
<!-- spr5847 -->
<bean id="andy"
class="org.springframework.test.context.expression.ExpressionUsageTests$Foo">
<property name="name" value="#{properties.username}" />
</bean>
<bean id="andy2"
class="org.springframework.test.context.expression.ExpressionUsageTests$Foo">
<property name="name" value="#{properties.username }" /><!-- space in expression -->
</bean>
</beans>

View File

@ -0,0 +1,74 @@
/*
* Copyright 2002-2006 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.test.context.expression;
import static junit.framework.Assert.*;
import java.util.Properties;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
/**
* @author Andy Clement
* @author Dave Syer
*/
@ContextConfiguration
public class ExpressionUsageTests extends AbstractJUnit4SpringContextTests {
@Test
public void testSpr5906() throws Exception {
Properties props = (Properties)applicationContext.getBean("derived");
// verify the property values have been evaluated as expressions
assertEquals("Dave",props.getProperty("user.name"));
assertEquals("Andy",props.getProperty("username"));
// verify the property keys have been evaluated as expressions
assertEquals("exists",props.getProperty("Dave"));
assertEquals("exists also",props.getProperty("Andy"));
}
public static class Foo {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Autowired
@Qualifier("andy2")
private Foo andy2;
@Autowired
@Qualifier("andy")
private Foo andy;
@Test
public void testSpr5847() throws Exception {
assertEquals("Andy", andy2.getName());
assertEquals("Andy", andy.getName());
}
}