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 @@ + + + + 4.0.0 + com.shopzilla.utilities + spring + 1.0-SNAPSHOT + core + + 3.0.2.RELEASE + 4.7 + + + + + Tim Morrow + tmorrow@Shopzilla.com + http://timmorrow.org/ + + + Rod Barlow + rbarlow@Shopzilla.com + http://rodneybarlow.org + + + Robert Roland + rroland@shopzilla.com + http://robertroland.org + + + Josh Long + jlong@shopzilla.com + http://joshlong.com + + + Mark Lui + mlui@Shopzilla.com + + + + + + + org.springframework + spring-context + ${spring.version} + compile + + + org.springframework + spring-context-support + ${spring.version} + compile + + + org.springframework + spring-core + ${spring.version} + compile + + + junit + junit + ${junit.version} + test + + + + + + org.springframework + spring-context + + + org.springframework + spring-context-support + + + org.springframework + spring-core + + + junit + junit + + + org.jmock + jmock + 2.5.1 + + + org.jmock + jmock-legacy + 2.5.1 + + + + install + + + org.apache.maven.plugins + maven-help-plugin + 2.1 + + + org.apache.maven.plugins + maven-compiler-plugin + 2.1 + + 1.6 + 1.6 + + + + org.codehaus.mojo + cobertura-maven-plugin + + + html + xml + + + + 90 + 90 + + + + org.apache.commons.logging.* + + + + + + + clean + check + + + + + + + + + + org.apache.maven.plugins + maven-project-info-reports-plugin + true + + + + index + + + + sunlink + + javadoc + + true + + + http://java.sun.com/javase/6/docs/api/ + + + + + + + maven-checkstyle-plugin + + + maven-docck-plugin + + + maven-javadoc-plugin + + + + http://java.sun.com/javase/6/docs/api/ + + true + true + private + ${basedir}/src/main/java;${basedir}/src/test/java + + + + maven-jxr-plugin + + + maven-surefire-report-plugin + + + org.codehaus.mojo + cobertura-maven-plugin + + + org.codehaus.mojo + findbugs-maven-plugin + + Normal + + + + + + + diff --git a/src/main/java/com/shopzilla/spring/util/config/ShopzillaNamespaceUtils.java b/src/main/java/com/shopzilla/spring/util/config/ShopzillaNamespaceUtils.java new file mode 100644 index 0000000..13a6cec --- /dev/null +++ b/src/main/java/com/shopzilla/spring/util/config/ShopzillaNamespaceUtils.java @@ -0,0 +1,117 @@ +/* + * + * 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.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.core.Conventions; +import org.springframework.util.StringUtils; +import org.w3c.dom.Element; + +/** + * This class was stolen largely from {@link org.springframework.integration.config.xml.IntegrationNamespaceUtils} from the Spring Integration project: it's useful, but we can't risk having a forced dependency on it ATM. + */ +public class ShopzillaNamespaceUtils { + + /** + * Populates the specified bean definition property with the value of 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 to populate the property + * @param propertyName the name of the property to be populated + */ + public void setValueIfAttributeDefined(BeanDefinitionBuilder builder, Element element, + String attributeName, String propertyName) { + String attributeValue = element.getAttribute(attributeName); + + if (StringUtils.hasText(attributeValue)) { + builder.addPropertyValue(propertyName, attributeValue); + } + } + + /** + * Populates the bean definition property corresponding to the specified attributeName with the value of that attribute if it 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 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); + } +}