Skip to content

Fix for SPR-9732 - Support for multiple resource entry for location attribute of util:properties #382

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 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.
Expand All @@ -22,7 +22,6 @@
import java.util.Set;

import org.w3c.dom.Element;

import org.springframework.beans.factory.config.FieldRetrievingFactoryBean;
import org.springframework.beans.factory.config.ListFactoryBean;
import org.springframework.beans.factory.config.MapFactoryBean;
Expand All @@ -38,6 +37,7 @@
*
* @author Rob Harrop
* @author Juergen Hoeller
* @author Biju Kunjummen
* @since 2.0
*/
public class UtilNamespaceHandler extends NamespaceHandlerSupport {
Expand Down Expand Up @@ -180,21 +180,22 @@ protected void doParse(Element element, ParserContext parserContext, BeanDefinit
}


private static class PropertiesBeanDefinitionParser extends AbstractSimpleBeanDefinitionParser {
private static class PropertiesBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {

@Override
protected Class getBeanClass(Element element) {
return PropertiesFactoryBean.class;
}

@Override
protected boolean isEligibleAttribute(String attributeName) {
return super.isEligibleAttribute(attributeName) && !SCOPE_ATTRIBUTE.equals(attributeName);
}

@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
super.doParse(element, parserContext, builder);
String location = element.getAttribute("location");
if (StringUtils.hasLength(location)) {
String[] locations = StringUtils.commaDelimitedListToStringArray(location);
builder.addPropertyValue("locations", locations);
}
builder.addPropertyValue("localOverride",
Boolean.valueOf(element.getAttribute("local-override")));
Properties parsedProps = parserContext.getDelegate().parsePropsElement(element);
builder.addPropertyValue("properties", parsedProps);
String scope = element.getAttribute(SCOPE_ATTRIBUTE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@
import java.util.TreeMap;
import java.util.Arrays;

import junit.framework.TestCase;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;

import org.springframework.beans.factory.config.FieldRetrievingFactoryBean;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
Expand All @@ -40,31 +45,36 @@
* @author Rob Harrop
* @author Juergen Hoeller
* @author Mark Fisher
* @author Biju Kunjummen
*/
public class UtilNamespaceHandlerTests extends TestCase {

public class UtilNamespaceHandlerTests {

private DefaultListableBeanFactory beanFactory;

private CollectingReaderEventListener listener = new CollectingReaderEventListener();

@Override
@Before
public void setUp() {
this.beanFactory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this.beanFactory);
reader.setEventListener(this.listener);
reader.loadBeanDefinitions(new ClassPathResource("testUtilNamespace.xml", getClass()));
}

@Test
public void testConstant() throws Exception {
Integer min = (Integer) this.beanFactory.getBean("min");
assertEquals(Integer.MIN_VALUE, min.intValue());
}

@Test
public void testConstantWithDefaultName() throws Exception {
Integer max = (Integer) this.beanFactory.getBean("java.lang.Integer.MAX_VALUE");
assertEquals(Integer.MAX_VALUE, max.intValue());
}

@Test
public void testEvents() throws Exception {
ComponentDefinition propertiesComponent = this.listener.getComponentDefinition("myProperties");
assertNotNull("Event for 'myProperties' not sent", propertiesComponent);
Expand All @@ -77,29 +87,34 @@ public void testEvents() throws Exception {
assertEquals("Incorrect BeanDefinition", FieldRetrievingFactoryBean.class, constantBean.getBeanClass());
}

@Test
public void testNestedProperties() throws Exception {
TestBean bean = (TestBean) this.beanFactory.getBean("testBean");
Properties props = bean.getSomeProperties();
assertEquals("Incorrect property value", "bar", props.get("foo"));
}

@Test
public void testPropertyPath() throws Exception {
String name = (String) this.beanFactory.getBean("name");
assertEquals("Rob Harrop", name);
}

@Test
public void testNestedPropertyPath() throws Exception {
TestBean bean = (TestBean) this.beanFactory.getBean("testBean");
assertEquals("Rob Harrop", bean.getName());
}

@Test
public void testSimpleMap() throws Exception {
Map map = (Map) this.beanFactory.getBean("simpleMap");
assertEquals("bar", map.get("foo"));
Map map2 = (Map) this.beanFactory.getBean("simpleMap");
assertTrue(map == map2);
}

@Test
public void testScopedMap() throws Exception {
Map map = (Map) this.beanFactory.getBean("scopedMap");
assertEquals("bar", map.get("foo"));
Expand All @@ -108,13 +123,15 @@ public void testScopedMap() throws Exception {
assertTrue(map != map2);
}

@Test
public void testSimpleList() throws Exception {
List list = (List) this.beanFactory.getBean("simpleList");
assertEquals("Rob Harrop", list.get(0));
List list2 = (List) this.beanFactory.getBean("simpleList");
assertTrue(list == list2);
}

@Test
public void testScopedList() throws Exception {
List list = (List) this.beanFactory.getBean("scopedList");
assertEquals("Rob Harrop", list.get(0));
Expand All @@ -123,13 +140,15 @@ public void testScopedList() throws Exception {
assertTrue(list != list2);
}

@Test
public void testSimpleSet() throws Exception {
Set set = (Set) this.beanFactory.getBean("simpleSet");
assertTrue(set.contains("Rob Harrop"));
Set set2 = (Set) this.beanFactory.getBean("simpleSet");
assertTrue(set == set2);
}

@Test
public void testScopedSet() throws Exception {
Set set = (Set) this.beanFactory.getBean("scopedSet");
assertTrue(set.contains("Rob Harrop"));
Expand All @@ -138,12 +157,14 @@ public void testScopedSet() throws Exception {
assertTrue(set != set2);
}

@Test
public void testMapWithRef() throws Exception {
Map map = (Map) this.beanFactory.getBean("mapWithRef");
assertTrue(map instanceof TreeMap);
assertEquals(this.beanFactory.getBean("testBean"), map.get("bean"));
}

@Test
public void testNestedCollections() throws Exception {
TestBean bean = (TestBean) this.beanFactory.getBean("nestedCollectionsBean");

Expand Down Expand Up @@ -171,6 +192,7 @@ public void testNestedCollections() throws Exception {
assertFalse(map == bean2.getSomeMap());
}

@Test
public void testNestedShortcutCollections() throws Exception {
TestBean bean = (TestBean) this.beanFactory.getBean("nestedShortcutCollections");

Expand All @@ -194,6 +216,7 @@ public void testNestedShortcutCollections() throws Exception {
assertFalse(set == bean2.getSomeSet());
}

@Test
public void testNestedInCollections() throws Exception {
TestBean bean = (TestBean) this.beanFactory.getBean("nestedCustomTagBean");

Expand All @@ -219,6 +242,7 @@ public void testNestedInCollections() throws Exception {
assertFalse(map == bean2.getSomeMap());
}

@Test
public void testCircularCollections() throws Exception {
TestBean bean = (TestBean) this.beanFactory.getBean("circularCollectionsBean");

Expand All @@ -235,6 +259,7 @@ public void testCircularCollections() throws Exception {
assertEquals(bean, map.get("foo"));
}

@Test
public void testCircularCollectionBeansStartingWithList() throws Exception {
this.beanFactory.getBean("circularList");
TestBean bean = (TestBean) this.beanFactory.getBean("circularCollectionBeansBean");
Expand All @@ -255,6 +280,7 @@ public void testCircularCollectionBeansStartingWithList() throws Exception {
assertEquals(bean, map.get("foo"));
}

@Test
public void testCircularCollectionBeansStartingWithSet() throws Exception {
this.beanFactory.getBean("circularSet");
TestBean bean = (TestBean) this.beanFactory.getBean("circularCollectionBeansBean");
Expand All @@ -275,6 +301,7 @@ public void testCircularCollectionBeansStartingWithSet() throws Exception {
assertEquals(bean, map.get("foo"));
}

@Test
public void testCircularCollectionBeansStartingWithMap() throws Exception {
this.beanFactory.getBean("circularMap");
TestBean bean = (TestBean) this.beanFactory.getBean("circularCollectionBeansBean");
Expand All @@ -295,19 +322,30 @@ public void testCircularCollectionBeansStartingWithMap() throws Exception {
assertEquals(bean, map.get("foo"));
}

@Test
public void testNestedInConstructor() throws Exception {
TestBean bean = (TestBean) this.beanFactory.getBean("constructedTestBean");
assertEquals("Rob Harrop", bean.getName());
}

@Test
public void testLoadProperties() throws Exception {
Properties props = (Properties) this.beanFactory.getBean("myProperties");
assertEquals("Incorrect property value", "bar", props.get("foo"));
assertEquals("Incorrect property value", null, props.get("foo2"));
Properties props2 = (Properties) this.beanFactory.getBean("myProperties");
assertTrue(props == props2);
}

@Test
public void testLoadPropertiesWithMultipleResources() throws Exception {
Properties props = (Properties) this.beanFactory.getBean("multiLocationProperties");
assertEquals("Incorrect property value", "bar", props.get("foo"));
assertEquals("Incorrect property value", "bar2", props.get("foo2"));
assertEquals("Incorrect property value", null, props.get("foo3"));
}

@Test
public void testScopedProperties() throws Exception {
Properties props = (Properties) this.beanFactory.getBean("myScopedProperties");
assertEquals("Incorrect property value", "bar", props.get("foo"));
Expand All @@ -318,30 +356,35 @@ public void testScopedProperties() throws Exception {
assertTrue(props != props2);
}

@Test
public void testLocalProperties() throws Exception {
Properties props = (Properties) this.beanFactory.getBean("myLocalProperties");
assertEquals("Incorrect property value", null, props.get("foo"));
assertEquals("Incorrect property value", "bar2", props.get("foo2"));
}

@Test
public void testMergedProperties() throws Exception {
Properties props = (Properties) this.beanFactory.getBean("myMergedProperties");
assertEquals("Incorrect property value", "bar", props.get("foo"));
assertEquals("Incorrect property value", "bar2", props.get("foo2"));
}

@Test
public void testLocalOverrideDefault() {
Properties props = (Properties) this.beanFactory.getBean("defaultLocalOverrideProperties");
assertEquals("Incorrect property value", "bar", props.get("foo"));
assertEquals("Incorrect property value", "local2", props.get("foo2"));
}

@Test
public void testLocalOverrideFalse() {
Properties props = (Properties) this.beanFactory.getBean("falseLocalOverrideProperties");
assertEquals("Incorrect property value", "bar", props.get("foo"));
assertEquals("Incorrect property value", "local2", props.get("foo2"));
}

@Test
public void testLocalOverrideTrue() {
Properties props = (Properties) this.beanFactory.getBean("trueLocalOverrideProperties");
assertEquals("Incorrect property value", "local", props.get("foo"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@

<util:properties id="myProperties"
location="classpath:/org/springframework/beans/factory/xml/util.properties"/>

<util:properties id="multiLocationProperties"
location="classpath:/org/springframework/beans/factory/xml/util.properties,
classpath:/org/springframework/beans/factory/xml/util2.properties"/>

<util:properties id="myScopedProperties"
location="classpath:/org/springframework/beans/factory/xml/util.properties" scope="prototype"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo2=bar2