diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/xml/UtilNamespaceHandler.java b/spring-beans/src/main/java/org/springframework/beans/factory/xml/UtilNamespaceHandler.java index 4d33f236ce95..67e5a137ddda 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/xml/UtilNamespaceHandler.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/xml/UtilNamespaceHandler.java @@ -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. @@ -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; @@ -38,6 +37,7 @@ * * @author Rob Harrop * @author Juergen Hoeller + * @author Biju Kunjummen * @since 2.0 */ public class UtilNamespaceHandler extends NamespaceHandlerSupport { @@ -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); diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/xml/UtilNamespaceHandlerTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/xml/UtilNamespaceHandlerTests.java index 3bb04d4c5939..e816cdd29e0b 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/xml/UtilNamespaceHandlerTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/xml/UtilNamespaceHandlerTests.java @@ -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; @@ -40,14 +45,16 @@ * @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); @@ -55,16 +62,19 @@ public void setUp() { 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); @@ -77,22 +87,26 @@ 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")); @@ -100,6 +114,7 @@ public void testSimpleMap() throws Exception { assertTrue(map == map2); } + @Test public void testScopedMap() throws Exception { Map map = (Map) this.beanFactory.getBean("scopedMap"); assertEquals("bar", map.get("foo")); @@ -108,6 +123,7 @@ 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)); @@ -115,6 +131,7 @@ public void testSimpleList() throws Exception { assertTrue(list == list2); } + @Test public void testScopedList() throws Exception { List list = (List) this.beanFactory.getBean("scopedList"); assertEquals("Rob Harrop", list.get(0)); @@ -123,6 +140,7 @@ 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")); @@ -130,6 +148,7 @@ public void testSimpleSet() throws Exception { assertTrue(set == set2); } + @Test public void testScopedSet() throws Exception { Set set = (Set) this.beanFactory.getBean("scopedSet"); assertTrue(set.contains("Rob Harrop")); @@ -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"); @@ -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"); @@ -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"); @@ -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"); @@ -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"); @@ -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"); @@ -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"); @@ -295,11 +322,13 @@ 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")); @@ -307,7 +336,16 @@ public void testLoadProperties() throws Exception { 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")); @@ -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")); diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/testUtilNamespace.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/testUtilNamespace.xml index 25f20439fa2e..9333f8108cc4 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/testUtilNamespace.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/testUtilNamespace.xml @@ -155,6 +155,10 @@ + + diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/util2.properties b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/util2.properties new file mode 100644 index 000000000000..40fbf3ad6272 --- /dev/null +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/util2.properties @@ -0,0 +1 @@ +foo2=bar2 \ No newline at end of file