diff --git a/README b/README
index e69de29..dcd3a1b 100644
--- a/README
+++ b/README
@@ -0,0 +1,21 @@
+
+Copyright (C) 2010 Shopzilla, Inc
+
+
+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.
+
+
+
+http://tech.shopzilla.com
+
+
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..154ba7b
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,232 @@
+
+
+
The property name will be the camel-case equivalent of the lower case hyphen separated attribute (e.g. the "foo-bar" attribute would + * match the "fooBar" property). + * + * @param beanDefinitionBuilder - the bean definition to be configured + * @param element - the XML element where the attribute should be defined + * @param attributeName - the name of the attribute whose value will be set on the property + * + * @see org.springframework.core.Conventions#attributeNameToPropertyName(String) + */ + public void setValueIfAttributeDefined(BeanDefinitionBuilder beanDefinitionBuilder, + Element element, String attributeName) { + setValueIfAttributeDefined(beanDefinitionBuilder, element, attributeName, + Conventions.attributeNameToPropertyName(attributeName)); + } + + /** + * Populates the specified bean definition property with the reference to a bean. The bean reference is identified by the value from the attribute whose name is provided if that attribute is defined in the given element. + * + * @param builder the bean definition to be configured + * @param element the XML element where the attribute should be defined + * @param attributeName the name of the attribute whose value will be used as a bean reference to populate the property + * @param propertyName the name of the property to be populated + */ + public void setReferenceIfAttributeDefined(BeanDefinitionBuilder builder, Element element, + String attributeName, String propertyName) { + String attributeValue = element.getAttribute(attributeName); + + if (StringUtils.hasText(attributeValue)) { + builder.addPropertyReference(propertyName, attributeValue); + } + } + + /** + * Populates the bean definition property corresponding to the specified attributeName with the reference to a bean identified by the value of that attribute if the attribute is defined in the given element.
The property name will be the camel-case equivalent of the lower case hyphen + * separated attribute (e.g. the "foo-bar" attribute would match the "fooBar" property). + * + * @param builder - the bean definition to be configured + * @param element - the XML element where the attribute should be defined + * @param attributeName - the name of the attribute whose value will be used as a bean reference to populate the property + * + * @see Conventions#attributeNameToPropertyName(String) + * @see Conventions#attributeNameToPropertyName(String) + */ + public void setReferenceIfAttributeDefined(BeanDefinitionBuilder builder, Element element, + String attributeName) { + setReferenceIfAttributeDefined(builder, element, attributeName, + Conventions.attributeNameToPropertyName(attributeName)); + } + + /** + * Provides a user friendly description of an element based on its node name and, if available, its "id" attribute value. This is useful for creating error messages from within bean definition parsers. + */ + public String createElementDescription(Element element) { + String elementId = "'" + element.getNodeName() + "'"; + String id = element.getAttribute("id"); + + if (StringUtils.hasText(id)) { + elementId += (" with id='" + id + "'"); + } + + return elementId; + } +} diff --git a/src/test/java/com/shopzilla/spring/util/config/ShopzillaNamespaceUtilsTest.java b/src/test/java/com/shopzilla/spring/util/config/ShopzillaNamespaceUtilsTest.java new file mode 100644 index 0000000..2fc539a --- /dev/null +++ b/src/test/java/com/shopzilla/spring/util/config/ShopzillaNamespaceUtilsTest.java @@ -0,0 +1,181 @@ +/* + * + * Copyright (C) 2010 Shopzilla, Inc + * + * + * 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. + * + * + * + * http://tech.shopzilla.com + * + * + */ + +package com.shopzilla.spring.util.config; + +import org.jmock.Expectations; +import org.jmock.Mockery; +import org.jmock.lib.legacy.ClassImposteriser; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.w3c.dom.Element; + + +/** + * @author Josh Long + */ +public class ShopzillaNamespaceUtilsTest { + private Mockery context = new Mockery() { + { + setImposteriser(ClassImposteriser.INSTANCE); + } + }; + + private BeanDefinitionBuilder beanDefinitionBuilder; + private Element element; + private ShopzillaNamespaceUtils shopzillaNamespaceUtils; + + @Before + public void before() throws Throwable { + this.shopzillaNamespaceUtils = new ShopzillaNamespaceUtils(); + this.beanDefinitionBuilder = this.context.mock(BeanDefinitionBuilder.class); + this.element = this.context.mock(Element.class); + } + + @After + public void after() throws Throwable { + this.context.assertIsSatisfied(); + } + + @Test + public void testSetValueIfAttributeDefined4ParamWithEmptyAttribute() + throws Throwable { + final String attr = "attr"; + final String prop = "prop"; + this.context.checking(new Expectations() { + { + one(element).getAttribute(attr); + will(returnValue(null)); + } + }); + + this.shopzillaNamespaceUtils.setValueIfAttributeDefined(beanDefinitionBuilder, element, attr, prop); + } + + @Test + public void testSetValueIfAttributeDefined4Param() + throws Throwable { + final String attr = "attr"; + final String prop = "prop"; + final String val = "value"; + this.context.checking(new Expectations() { + { + one(element).getAttribute(attr); + will(returnValue(val)); + one(beanDefinitionBuilder).addPropertyValue(prop, val); + } + }); + this.shopzillaNamespaceUtils.setValueIfAttributeDefined(beanDefinitionBuilder, element, attr, prop); + } + + @Test + public void testSetValueIfAttributeDefined3Param() + throws Throwable { + final String attr = "attr"; + final String prop = "attr"; + final String val = "value"; + this.context.checking(new Expectations() { + { + one(element).getAttribute(attr); + will(returnValue(val)); + one(beanDefinitionBuilder).addPropertyValue(prop, val); + } + }); + this.shopzillaNamespaceUtils.setValueIfAttributeDefined(beanDefinitionBuilder, element, attr); + } + + @Test + public void testSetReferenceIfAttributeDefinedWithNullAttribute() + throws Throwable { + final String attr = "attr"; + final String prop = "attr"; + this.context.checking(new Expectations() { + { + one(element).getAttribute(attr); + will(returnValue(null)); + } + }); + this.shopzillaNamespaceUtils.setReferenceIfAttributeDefined(beanDefinitionBuilder, element, attr, prop); + } + + @Test + public void testSetReferenceIfAttributeDefined() throws Throwable { + final String attr = "attr"; + final String prop = "attr"; + final String val = "value"; + this.context.checking(new Expectations() { + { + one(element).getAttribute(attr); + will(returnValue(val)); + one(beanDefinitionBuilder).addPropertyReference(prop, val); + } + }); + this.shopzillaNamespaceUtils.setReferenceIfAttributeDefined(beanDefinitionBuilder, element, attr, prop); + } + + @Test + public void testSetReferenceIfAttributeDefined3Param() + throws Throwable { + final String attr = "attr"; + final String prop = "attr"; + final String val = "value"; + this.context.checking(new Expectations() { + { + one(element).getAttribute(attr); + will(returnValue(val)); + one(beanDefinitionBuilder).addPropertyReference(prop, val); + } + }); + this.shopzillaNamespaceUtils.setReferenceIfAttributeDefined(beanDefinitionBuilder, element, attr); + } + + @Test + public void testCreateElementDescriptionWithNullID() + throws Throwable { + this.context.checking(new Expectations() { + { + one(element).getNodeName(); + will(returnValue("nodeName")); + one(element).getAttribute("id"); + will(returnValue(null)); + } + }); + this.shopzillaNamespaceUtils.createElementDescription(element); + } + + @Test + public void testCreateElementDescription() throws Throwable { + this.context.checking(new Expectations() { + { + one(element).getNodeName(); + will(returnValue("nodeName")); + one(element).getAttribute("id"); + will(returnValue("id")); + } + }); + this.shopzillaNamespaceUtils.createElementDescription(element); + } +}