This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-beanutils.git
commit a92a9817b7415f953d78da8413f60fbccc40ed7d Author: Gary Gregory <[email protected]> AuthorDate: Sun Oct 20 18:18:27 2019 -0400 [BEANUTILS-527] Convert from Collections4 to java.util.function #8. Implement java.util.function.Consumer<T> instead of org.apache.commons.collections4.Closure: org.apache.commons.beanutils2.BeanPropertyValueChangeClosure renamed to BeanPropertyValueChangeConsumer. --- ...e.java => BeanPropertyValueChangeConsumer.java} | 477 +++++++-------- .../BeanPropertyValueEqualsPredicate.java | 3 +- ...> BeanPropertyValueChangeConsumerTestCase.java} | 642 ++++++++++----------- 3 files changed, 563 insertions(+), 559 deletions(-) diff --git a/src/main/java/org/apache/commons/beanutils2/BeanPropertyValueChangeClosure.java b/src/main/java/org/apache/commons/beanutils2/BeanPropertyValueChangeConsumer.java similarity index 91% rename from src/main/java/org/apache/commons/beanutils2/BeanPropertyValueChangeClosure.java rename to src/main/java/org/apache/commons/beanutils2/BeanPropertyValueChangeConsumer.java index 2801af4..fc44b53 100644 --- a/src/main/java/org/apache/commons/beanutils2/BeanPropertyValueChangeClosure.java +++ b/src/main/java/org/apache/commons/beanutils2/BeanPropertyValueChangeConsumer.java @@ -1,237 +1,240 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.commons.beanutils2; - -import java.lang.reflect.InvocationTargetException; - -import org.apache.commons.collections4.Closure; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - - -/** - * <p><code>Closure</code> that sets a property.</p> - * <p> - * An implementation of <code>org.apache.commons.collections4.Closure</code> that updates - * a specified property on the object provided with a specified value. - * The <code>BeanPropertyValueChangeClosure</code> constructor takes two parameters which determine - * what property will be updated and with what value. - * <dl> - * <dt> - * <code>public BeanPropertyValueChangeClosure( String propertyName, Object propertyValue )</code> - * </dt> - * <dd> - * Will create a <code>Closure</code> that will update an object by setting the property - * specified by <code>propertyName</code> to the value specified by <code>propertyValue</code>. - * </dd> - * </dl> - * - * <p> - * <strong>Note:</strong> Property names can be a simple, nested, indexed, or mapped property as defined by - * <code>org.apache.commons.beanutils2.PropertyUtils</code>. If any object in the property path - * specified by <code>propertyName</code> is <code>null</code> then the outcome is based on the - * value of the <code>ignoreNull</code> attribute. - * </p> - * <p> - * A typical usage might look like: - * </p> - * <pre><code> - * // create the closure - * BeanPropertyValueChangeClosure closure = - * new BeanPropertyValueChangeClosure( "activeEmployee", Boolean.TRUE ); - * - * // update the Collection - * CollectionUtils.forAllDo( peopleCollection, closure ); - * </code></pre> - * - * This would take a <code>Collection</code> of person objects and update the - * <code>activeEmployee</code> property of each object in the <code>Collection</code> to - * <code>true</code>. Assuming... - * <ul> - * <li> - * The top level object in the <code>peopleCollection</code> is an object which represents a - * person. - * </li> - * <li> - * The person object has a <code>setActiveEmployee( boolean )</code> method which updates - * the value for the object's <code>activeEmployee</code> property. - * </li> - * </ul> - * - * @see org.apache.commons.beanutils2.PropertyUtils - * @see org.apache.commons.collections4.Closure - */ -public class BeanPropertyValueChangeClosure implements Closure { - - /** For logging. */ - private final Log log = LogFactory.getLog(this.getClass()); - - /** - * The name of the property which will be updated when this <code>Closure</code> executes. - */ - private String propertyName; - - /** - * The value that the property specified by <code>propertyName</code> - * will be updated to when this <code>Closure</code> executes. - */ - private Object propertyValue; - - /** - * Determines whether <code>null</code> objects in the property path will genenerate an - * <code>IllegalArgumentException</code> or not. If set to <code>true</code> then if any objects - * in the property path leading up to the target property evaluate to <code>null</code> then the - * <code>IllegalArgumentException</code> throw by <code>PropertyUtils</code> will be logged but - * not rethrown. If set to <code>false</code> then if any objects in the property path leading - * up to the target property evaluate to <code>null</code> then the - * <code>IllegalArgumentException</code> throw by <code>PropertyUtils</code> will be logged and - * rethrown. - */ - private boolean ignoreNull; - - /** - * Constructor which takes the name of the property to be changed, the new value to set - * the property to, and assumes <code>ignoreNull</code> to be <code>false</code>. - * - * @param propertyName The name of the property that will be updated with the value specified by - * <code>propertyValue</code>. - * @param propertyValue The value that <code>propertyName</code> will be set to on the target - * object. - * @throws IllegalArgumentException If the propertyName provided is null or empty. - */ - public BeanPropertyValueChangeClosure(final String propertyName, final Object propertyValue) { - this(propertyName, propertyValue, false); - } - - /** - * Constructor which takes the name of the property to be changed, the new value to set - * the property to and a boolean which determines whether <code>null</code> objects in the - * property path will genenerate an <code>IllegalArgumentException</code> or not. - * - * @param propertyName The name of the property that will be updated with the value specified by - * <code>propertyValue</code>. - * @param propertyValue The value that <code>propertyName</code> will be set to on the target - * object. - * @param ignoreNull Determines whether <code>null</code> objects in the property path will - * genenerate an <code>IllegalArgumentException</code> or not. - * @throws IllegalArgumentException If the propertyName provided is null or empty. - */ - public BeanPropertyValueChangeClosure(final String propertyName, final Object propertyValue, final boolean ignoreNull) { - super(); - - if (propertyName != null && propertyName.length() > 0) { - this.propertyName = propertyName; - this.propertyValue = propertyValue; - this.ignoreNull = ignoreNull; - } else { - throw new IllegalArgumentException("propertyName cannot be null or empty"); - } - } - - /** - * Updates the target object provided using the property update criteria provided when this - * <code>BeanPropertyValueChangeClosure</code> was constructed. If any object in the property - * path leading up to the target property is <code>null</code> then the outcome will be based on - * the value of the <code>ignoreNull</code> attribute. By default, <code>ignoreNull</code> is - * <code>false</code> and would result in an <code>IllegalArgumentException</code> if an object - * in the property path leading up to the target property is <code>null</code>. - * - * @param object The object to be updated. - * @throws IllegalArgumentException If an IllegalAccessException, InvocationTargetException, or - * NoSuchMethodException is thrown when trying to access the property specified on the object - * provided. Or if an object in the property path provided is <code>null</code> and - * <code>ignoreNull</code> is set to <code>false</code>. - */ - @Override - public void execute(final Object object) { - - try { - PropertyUtils.setProperty(object, propertyName, propertyValue); - } catch (final IllegalArgumentException e) { - final String errorMsg = "Unable to execute Closure. Null value encountered in property path..."; - - if (ignoreNull) { - log.warn("WARNING: " + errorMsg + e); - } else { - final IllegalArgumentException iae = new IllegalArgumentException(errorMsg); - if (!BeanUtils.initCause(iae, e)) { - log.error(errorMsg, e); - } - throw iae; - } - } catch (final IllegalAccessException e) { - final String errorMsg = "Unable to access the property provided."; - final IllegalArgumentException iae = new IllegalArgumentException(errorMsg); - if (!BeanUtils.initCause(iae, e)) { - log.error(errorMsg, e); - } - throw iae; - } catch (final InvocationTargetException e) { - final String errorMsg = "Exception occurred in property's getter"; - final IllegalArgumentException iae = new IllegalArgumentException(errorMsg); - if (!BeanUtils.initCause(iae, e)) { - log.error(errorMsg, e); - } - throw iae; - } catch (final NoSuchMethodException e) { - final String errorMsg = "Property not found"; - final IllegalArgumentException iae = new IllegalArgumentException(errorMsg); - if (!BeanUtils.initCause(iae, e)) { - log.error(errorMsg, e); - } - throw iae; - } - } - - /** - * Returns the name of the property which will be updated when this <code>Closure</code> executes. - * - * @return The name of the property which will be updated when this <code>Closure</code> executes. - */ - public String getPropertyName() { - return propertyName; - } - - /** - * Returns the value that the property specified by <code>propertyName</code> - * will be updated to when this <code>Closure</code> executes. - * - * @return The value that the property specified by <code>propertyName</code> - * will be updated to when this <code>Closure</code> executes. - */ - public Object getPropertyValue() { - return propertyValue; - } - - /** - * Returns the flag that determines whether <code>null</code> objects in the property path will - * genenerate an <code>IllegalArgumentException</code> or not. If set to <code>true</code> then - * if any objects in the property path leading up to the target property evaluate to - * <code>null</code> then the <code>IllegalArgumentException</code> throw by - * <code>PropertyUtils</code> will be logged but not rethrown. If set to <code>false</code> then - * if any objects in the property path leading up to the target property evaluate to - * <code>null</code> then the <code>IllegalArgumentException</code> throw by - * <code>PropertyUtils</code> will be logged and rethrown. - * - * @return The flag that determines whether <code>null</code> objects in the property path will - * genenerate an <code>IllegalArgumentException</code> or not. - */ - public boolean isIgnoreNull() { - return ignoreNull; - } -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.commons.beanutils2; + +import java.lang.reflect.InvocationTargetException; +import java.util.function.Consumer; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + + +/** + * <p><code>Closure</code> that sets a property.</p> + * <p> + * An implementation of <code>java.util.function.Consumer</code> that updates + * a specified property on the object provided with a specified value. + * The <code>BeanPropertyValueChangeClosure</code> constructor takes two parameters which determine + * what property will be updated and with what value. + * <dl> + * <dt> + * <code>public BeanPropertyValueChangeClosure( String propertyName, Object propertyValue )</code> + * </dt> + * <dd> + * Will create a <code>Closure</code> that will update an object by setting the property + * specified by <code>propertyName</code> to the value specified by <code>propertyValue</code>. + * </dd> + * </dl> + * + * <p> + * <strong>Note:</strong> Property names can be a simple, nested, indexed, or mapped property as defined by + * <code>org.apache.commons.beanutils2.PropertyUtils</code>. If any object in the property path + * specified by <code>propertyName</code> is <code>null</code> then the outcome is based on the + * value of the <code>ignoreNull</code> attribute. + * </p> + * <p> + * A typical usage might look like: + * </p> + * <pre><code> + * // create the closure + * BeanPropertyValueChangeClosure closure = + * new BeanPropertyValueChangeClosure( "activeEmployee", Boolean.TRUE ); + * + * // update the Collection + * CollectionUtils.forAllDo( peopleCollection, closure ); + * </code></pre> + * + * This would take a <code>Collection</code> of person objects and update the + * <code>activeEmployee</code> property of each object in the <code>Collection</code> to + * <code>true</code>. Assuming... + * <ul> + * <li> + * The top level object in the <code>peopleCollection</code> is an object which represents a + * person. + * </li> + * <li> + * The person object has a <code>setActiveEmployee( boolean )</code> method which updates + * the value for the object's <code>activeEmployee</code> property. + * </li> + * </ul> + * + * @param <T> The type of the input to the operation + * @param <V> The property value type. + * + * @see org.apache.commons.beanutils2.PropertyUtils + * @see java.util.function.Consumer + */ +public class BeanPropertyValueChangeConsumer<T, V> implements Consumer<T> { + + /** For logging. */ + private final Log log = LogFactory.getLog(this.getClass()); + + /** + * The name of the property which will be updated when this <code>Closure</code> executes. + */ + private String propertyName; + + /** + * The value that the property specified by <code>propertyName</code> + * will be updated to when this <code>Closure</code> executes. + */ + private V propertyValue; + + /** + * Determines whether <code>null</code> objects in the property path will genenerate an + * <code>IllegalArgumentException</code> or not. If set to <code>true</code> then if any objects + * in the property path leading up to the target property evaluate to <code>null</code> then the + * <code>IllegalArgumentException</code> throw by <code>PropertyUtils</code> will be logged but + * not rethrown. If set to <code>false</code> then if any objects in the property path leading + * up to the target property evaluate to <code>null</code> then the + * <code>IllegalArgumentException</code> throw by <code>PropertyUtils</code> will be logged and + * rethrown. + */ + private boolean ignoreNull; + + /** + * Constructor which takes the name of the property to be changed, the new value to set + * the property to, and assumes <code>ignoreNull</code> to be <code>false</code>. + * + * @param propertyName The name of the property that will be updated with the value specified by + * <code>propertyValue</code>. + * @param propertyValue The value that <code>propertyName</code> will be set to on the target + * object. + * @throws IllegalArgumentException If the propertyName provided is null or empty. + */ + public BeanPropertyValueChangeConsumer(final String propertyName, final V propertyValue) { + this(propertyName, propertyValue, false); + } + + /** + * Constructor which takes the name of the property to be changed, the new value to set + * the property to and a boolean which determines whether <code>null</code> objects in the + * property path will genenerate an <code>IllegalArgumentException</code> or not. + * + * @param propertyName The name of the property that will be updated with the value specified by + * <code>propertyValue</code>. + * @param propertyValue The value that <code>propertyName</code> will be set to on the target + * object. + * @param ignoreNull Determines whether <code>null</code> objects in the property path will + * genenerate an <code>IllegalArgumentException</code> or not. + * @throws IllegalArgumentException If the propertyName provided is null or empty. + */ + public BeanPropertyValueChangeConsumer(final String propertyName, final V propertyValue, final boolean ignoreNull) { + super(); + + if (propertyName != null && propertyName.length() > 0) { + this.propertyName = propertyName; + this.propertyValue = propertyValue; + this.ignoreNull = ignoreNull; + } else { + throw new IllegalArgumentException("propertyName cannot be null or empty"); + } + } + + /** + * Updates the target object provided using the property update criteria provided when this + * <code>BeanPropertyValueChangeClosure</code> was constructed. If any object in the property + * path leading up to the target property is <code>null</code> then the outcome will be based on + * the value of the <code>ignoreNull</code> attribute. By default, <code>ignoreNull</code> is + * <code>false</code> and would result in an <code>IllegalArgumentException</code> if an object + * in the property path leading up to the target property is <code>null</code>. + * + * @param object The object to be updated. + * @throws IllegalArgumentException If an IllegalAccessException, InvocationTargetException, or + * NoSuchMethodException is thrown when trying to access the property specified on the object + * provided. Or if an object in the property path provided is <code>null</code> and + * <code>ignoreNull</code> is set to <code>false</code>. + */ + @Override + public void accept(final Object object) { + + try { + PropertyUtils.setProperty(object, propertyName, propertyValue); + } catch (final IllegalArgumentException e) { + final String errorMsg = "Unable to execute Closure. Null value encountered in property path..."; + + if (ignoreNull) { + log.warn("WARNING: " + errorMsg + e); + } else { + final IllegalArgumentException iae = new IllegalArgumentException(errorMsg); + if (!BeanUtils.initCause(iae, e)) { + log.error(errorMsg, e); + } + throw iae; + } + } catch (final IllegalAccessException e) { + final String errorMsg = "Unable to access the property provided."; + final IllegalArgumentException iae = new IllegalArgumentException(errorMsg); + if (!BeanUtils.initCause(iae, e)) { + log.error(errorMsg, e); + } + throw iae; + } catch (final InvocationTargetException e) { + final String errorMsg = "Exception occurred in property's getter"; + final IllegalArgumentException iae = new IllegalArgumentException(errorMsg); + if (!BeanUtils.initCause(iae, e)) { + log.error(errorMsg, e); + } + throw iae; + } catch (final NoSuchMethodException e) { + final String errorMsg = "Property not found"; + final IllegalArgumentException iae = new IllegalArgumentException(errorMsg); + if (!BeanUtils.initCause(iae, e)) { + log.error(errorMsg, e); + } + throw iae; + } + } + + /** + * Returns the name of the property which will be updated when this <code>Closure</code> executes. + * + * @return The name of the property which will be updated when this <code>Closure</code> executes. + */ + public String getPropertyName() { + return propertyName; + } + + /** + * Returns the value that the property specified by <code>propertyName</code> + * will be updated to when this <code>Closure</code> executes. + * + * @return The value that the property specified by <code>propertyName</code> + * will be updated to when this <code>Closure</code> executes. + */ + public V getPropertyValue() { + return propertyValue; + } + + /** + * Returns the flag that determines whether <code>null</code> objects in the property path will + * genenerate an <code>IllegalArgumentException</code> or not. If set to <code>true</code> then + * if any objects in the property path leading up to the target property evaluate to + * <code>null</code> then the <code>IllegalArgumentException</code> throw by + * <code>PropertyUtils</code> will be logged but not rethrown. If set to <code>false</code> then + * if any objects in the property path leading up to the target property evaluate to + * <code>null</code> then the <code>IllegalArgumentException</code> throw by + * <code>PropertyUtils</code> will be logged and rethrown. + * + * @return The flag that determines whether <code>null</code> objects in the property path will + * genenerate an <code>IllegalArgumentException</code> or not. + */ + public boolean isIgnoreNull() { + return ignoreNull; + } +} diff --git a/src/main/java/org/apache/commons/beanutils2/BeanPropertyValueEqualsPredicate.java b/src/main/java/org/apache/commons/beanutils2/BeanPropertyValueEqualsPredicate.java index bd859d2..31b84e7 100644 --- a/src/main/java/org/apache/commons/beanutils2/BeanPropertyValueEqualsPredicate.java +++ b/src/main/java/org/apache/commons/beanutils2/BeanPropertyValueEqualsPredicate.java @@ -102,7 +102,8 @@ import org.apache.commons.logging.LogFactory; * the value for the object's <code>personId</code> property. * </li> * </ul> - * @param <T> the type of the input to the predicate. + * + * @param <T> The type of the input to the predicate. * @param <V> The property value type. * * @see org.apache.commons.beanutils2.PropertyUtils diff --git a/src/test/java/org/apache/commons/beanutils2/BeanPropertyValueChangeClosureTestCase.java b/src/test/java/org/apache/commons/beanutils2/BeanPropertyValueChangeConsumerTestCase.java similarity index 73% rename from src/test/java/org/apache/commons/beanutils2/BeanPropertyValueChangeClosureTestCase.java rename to src/test/java/org/apache/commons/beanutils2/BeanPropertyValueChangeConsumerTestCase.java index cf77d7e..befef64 100644 --- a/src/test/java/org/apache/commons/beanutils2/BeanPropertyValueChangeClosureTestCase.java +++ b/src/test/java/org/apache/commons/beanutils2/BeanPropertyValueChangeConsumerTestCase.java @@ -1,321 +1,321 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.commons.beanutils2; - -import junit.framework.TestCase; - - -/** - * Test cases for <code>BeanPropertyValueChangeClosure</code>. - * - */ -public class BeanPropertyValueChangeClosureTestCase extends TestCase { - - private static final Integer expectedIntegerValue = new Integer(123); - private static final Float expectedFloatValue = new Float(123.123f); - private static final Double expectedDoubleValue = new Double(567879.12344d); - private static final Boolean expectedBooleanValue = Boolean.TRUE; - private static final Byte expectedByteValue = new Byte("12"); - - /** - * Constructor for BeanPropertyValueChangeClosureTest. - * - * @param name Name of this test case. - */ - public BeanPropertyValueChangeClosureTestCase(final String name) { - super(name); - } - - /** - * Test execute with simple float property and Float value. - */ - public void testExecuteWithSimpleFloatPropertyAndFloatValue() { - final TestBean testBean = new TestBean(); - new BeanPropertyValueChangeClosure("floatProperty", expectedFloatValue).execute(testBean); - assertTrue(expectedFloatValue.floatValue() == testBean.getFloatProperty()); - } - - /** - * Test execute with simple float property and String value. - */ - public void testExecuteWithSimpleFloatPropertyAndStringValue() { - try { - new BeanPropertyValueChangeClosure("floatProperty", "123").execute(new TestBean()); - fail("Should have thrown an IllegalArgumentException"); - } catch (final IllegalArgumentException e) { - /* this is what we expect */ - } - } - - /** - * Test execute with simple float property and Double value. - */ - public void testExecuteWithSimpleFloatPropertyAndDoubleValue() { - try { - new BeanPropertyValueChangeClosure("floatProperty", expectedDoubleValue).execute(new TestBean()); - fail("Should have thrown an IllegalArgumentException"); - } catch (final IllegalArgumentException e) { - /* this is what we expect */ - } - } - - /** - * Test execute with simple float property and Integer value. - */ - public void testExecuteWithSimpleFloatPropertyAndIntegerValue() { - final TestBean testBean = new TestBean(); - new BeanPropertyValueChangeClosure("floatProperty", expectedIntegerValue).execute(testBean); - assertTrue(expectedIntegerValue.floatValue() == testBean.getFloatProperty()); - } - - /** - * Test execute with simple double property and Double value. - */ - public void testExecuteWithSimpleDoublePropertyAndDoubleValue() { - final TestBean testBean = new TestBean(); - new BeanPropertyValueChangeClosure("doubleProperty", expectedDoubleValue).execute(testBean); - assertTrue(expectedDoubleValue.doubleValue() == testBean.getDoubleProperty()); - } - - /** - * Test execute with simple double property and String value. - */ - public void testExecuteWithSimpleDoublePropertyAndStringValue() { - try { - new BeanPropertyValueChangeClosure("doubleProperty", "123").execute(new TestBean()); - fail("Should have thrown an IllegalArgumentException"); - } catch (final IllegalArgumentException e) { - /* this is what we expect */ - } - } - - /** - * Test execute with simple double property and Float value. - */ - public void testExecuteWithSimpleDoublePropertyAndFloatValue() { - final TestBean testBean = new TestBean(); - new BeanPropertyValueChangeClosure("doubleProperty", expectedFloatValue).execute(testBean); - assertTrue(expectedFloatValue.doubleValue() == testBean.getDoubleProperty()); - } - - /** - * Test execute with simple double property and Integer value. - */ - public void testExecuteWithSimpleDoublePropertyAndIntegerValue() { - final TestBean testBean = new TestBean(); - new BeanPropertyValueChangeClosure("doubleProperty", expectedIntegerValue).execute(testBean); - assertTrue(expectedIntegerValue.doubleValue() == testBean.getDoubleProperty()); - } - - /** - * Test execute with simple int property and Double value. - */ - public void testExecuteWithSimpleIntPropertyAndDoubleValue() { - try { - new BeanPropertyValueChangeClosure("intProperty", expectedDoubleValue).execute(new TestBean()); - fail("Should have thrown an IllegalArgumentException"); - } catch (final IllegalArgumentException e) { - /* this is what we expect */ - } - } - - /** - * Test execute with simple int property and String value. - */ - public void testExecuteWithSimpleIntPropertyAndStringValue() { - try { - new BeanPropertyValueChangeClosure("intProperty", "123").execute(new TestBean()); - fail("Should have thrown an IllegalArgumentException"); - } catch (final IllegalArgumentException e) { - /* this is what we expect */ - } - } - - /** - * Test execute with simple int property and Float value. - */ - public void testExecuteWithSimpleIntPropertyAndFloatValue() { - try { - new BeanPropertyValueChangeClosure("intProperty", expectedFloatValue).execute(new TestBean()); - fail("Should have thrown an IllegalArgumentException"); - } catch (final IllegalArgumentException e) { - /* this is what we expect */ - } - } - - /** - * Test execute with simple int property and Integer value. - */ - public void testExecuteWithSimpleIntPropertyAndIntegerValue() { - final TestBean testBean = new TestBean(); - new BeanPropertyValueChangeClosure("intProperty", expectedIntegerValue).execute(testBean); - assertTrue(expectedIntegerValue.intValue() == testBean.getIntProperty()); - } - - /** - * Test execute with simple boolean property and Boolean value. - */ - public void testExecuteWithSimpleBooleanPropertyAndBooleanValue() { - final TestBean testBean = new TestBean(); - new BeanPropertyValueChangeClosure("booleanProperty", expectedBooleanValue).execute(testBean); - assertTrue(expectedBooleanValue.booleanValue() == testBean.getBooleanProperty()); - } - - /** - * Test execute with simple boolean property and String value. - */ - public void testExecuteWithSimpleBooleanPropertyAndStringValue() { - try { - new BeanPropertyValueChangeClosure("booleanProperty", "true").execute(new TestBean()); - fail("Should have thrown an IllegalArgumentException"); - } catch (final IllegalArgumentException e) { - /* this is what we expect */ - } - } - - /** - * Test execute with simple byte property and Byte value. - */ - public void testExecuteWithSimpleBytePropertyAndByteValue() { - final TestBean testBean = new TestBean(); - new BeanPropertyValueChangeClosure("byteProperty", expectedByteValue).execute(testBean); - assertTrue(expectedByteValue.byteValue() == testBean.getByteProperty()); - } - - /** - * Test execute with simple boolean property and String value. - */ - public void testExecuteWithSimpleBytePropertyAndStringValue() { - try { - new BeanPropertyValueChangeClosure("byteProperty", "foo").execute(new TestBean()); - fail("Should have thrown an IllegalArgumentException"); - } catch (final IllegalArgumentException e) { - /* this is what we expect */ - } - } - - /** - * Test execute with simple primitive property and null value. - */ - public void testExecuteWithSimplePrimitivePropertyAndNullValue() { - try { - new BeanPropertyValueChangeClosure("intProperty", null).execute(new TestBean()); - fail("Should have thrown an IllegalArgumentException"); - } catch (final IllegalArgumentException e) { - /* this is what we expect */ - } - } - - /** - * Test execute with read only property. - */ - public void testExecuteWithReadOnlyProperty() { - try { - new BeanPropertyValueChangeClosure("readOnlyProperty", "foo").execute(new TestBean()); - fail("Should have thrown an IllegalArgumentException"); - } catch (final IllegalArgumentException e) { - /* this is what we expect */ - } - } - - /** - * Test execute with write only property. - */ - public void testExecuteWithWriteOnlyProperty() { - final TestBean testBean = new TestBean(); - new BeanPropertyValueChangeClosure("writeOnlyProperty", "foo").execute(testBean); - assertEquals("foo", testBean.getWriteOnlyPropertyValue()); - } - - /** - * Test execute with a nested property. - */ - public void testExecuteWithNestedProperty() { - final TestBean testBean = new TestBean(); - new BeanPropertyValueChangeClosure("nested.stringProperty", "bar").execute(testBean); - assertEquals("bar", testBean.getNested().getStringProperty()); - } - - /** - * Test execute with a nested property and null in the property path. - */ - public void testExecuteWithNullInPropertyPath() { - try { - new BeanPropertyValueChangeClosure("anotherNested.stringProperty", "foo").execute(new TestBean()); - fail("Should have thrown an IllegalArgumentException"); - } catch (final IllegalArgumentException e) { - /* this is what we expect */ - } - } - - /** - * Test execute with a nested property and null in the property path and ignoreNull = true. - */ - public void testExecuteWithNullInPropertyPathAngIgnoreTrue() { - final TestBean testBean = new TestBean(); - - // create a closure that will attempt to set a property on the null bean in the path - final BeanPropertyValueChangeClosure closure = new BeanPropertyValueChangeClosure("anotherNested.stringProperty", - "Should ignore exception", true); - - try { - closure.execute(testBean); - } catch (final IllegalArgumentException e) { - fail("Should have ignored the exception."); - } - } - - /** - * Test execute with indexed property. - */ - public void testExecuteWithIndexedProperty() { - final TestBean testBean = new TestBean(); - new BeanPropertyValueChangeClosure("intIndexed[0]", expectedIntegerValue).execute(testBean); - assertTrue(expectedIntegerValue.intValue() == testBean.getIntIndexed(0)); - } - - /** - * Test execute with mapped property. - */ - public void testExecuteWithMappedProperty() { - final TestBean testBean = new TestBean(); - new BeanPropertyValueChangeClosure("mappedProperty(fred)", "barney").execute(testBean); - assertEquals("barney", testBean.getMappedProperty("fred")); - } - - /** - * Test execute with a simple String property. - */ - public void testExecuteWithSimpleStringProperty() { - final TestBean testBean = new TestBean(); - new BeanPropertyValueChangeClosure("stringProperty", "barney").execute(testBean); - assertEquals("barney", testBean.getStringProperty()); - } - - /** - * Test execute with an invalid property name. - */ - public void testExecuteWithInvalidPropertyName() { - try { - new BeanPropertyValueChangeClosure("bogusProperty", "foo").execute(new TestBean()); - fail("Should have thrown an IllegalArgumentException"); - } catch (final IllegalArgumentException e) { - /* this is what we expect */ - } - } -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.commons.beanutils2; + +import junit.framework.TestCase; + + +/** + * Test cases for <code>BeanPropertyValueChangeClosure</code>. + * + */ +public class BeanPropertyValueChangeConsumerTestCase extends TestCase { + + private static final Integer expectedIntegerValue = new Integer(123); + private static final Float expectedFloatValue = new Float(123.123f); + private static final Double expectedDoubleValue = new Double(567879.12344d); + private static final Boolean expectedBooleanValue = Boolean.TRUE; + private static final Byte expectedByteValue = new Byte("12"); + + /** + * Constructor for BeanPropertyValueChangeClosureTest. + * + * @param name Name of this test case. + */ + public BeanPropertyValueChangeConsumerTestCase(final String name) { + super(name); + } + + /** + * Test execute with simple float property and Float value. + */ + public void testExecuteWithSimpleFloatPropertyAndFloatValue() { + final TestBean testBean = new TestBean(); + new BeanPropertyValueChangeConsumer<>("floatProperty", expectedFloatValue).accept(testBean); + assertTrue(expectedFloatValue.floatValue() == testBean.getFloatProperty()); + } + + /** + * Test execute with simple float property and String value. + */ + public void testExecuteWithSimpleFloatPropertyAndStringValue() { + try { + new BeanPropertyValueChangeConsumer<>("floatProperty", "123").accept(new TestBean()); + fail("Should have thrown an IllegalArgumentException"); + } catch (final IllegalArgumentException e) { + /* this is what we expect */ + } + } + + /** + * Test execute with simple float property and Double value. + */ + public void testExecuteWithSimpleFloatPropertyAndDoubleValue() { + try { + new BeanPropertyValueChangeConsumer<>("floatProperty", expectedDoubleValue).accept(new TestBean()); + fail("Should have thrown an IllegalArgumentException"); + } catch (final IllegalArgumentException e) { + /* this is what we expect */ + } + } + + /** + * Test execute with simple float property and Integer value. + */ + public void testExecuteWithSimpleFloatPropertyAndIntegerValue() { + final TestBean testBean = new TestBean(); + new BeanPropertyValueChangeConsumer<>("floatProperty", expectedIntegerValue).accept(testBean); + assertTrue(expectedIntegerValue.floatValue() == testBean.getFloatProperty()); + } + + /** + * Test execute with simple double property and Double value. + */ + public void testExecuteWithSimpleDoublePropertyAndDoubleValue() { + final TestBean testBean = new TestBean(); + new BeanPropertyValueChangeConsumer<>("doubleProperty", expectedDoubleValue).accept(testBean); + assertTrue(expectedDoubleValue.doubleValue() == testBean.getDoubleProperty()); + } + + /** + * Test execute with simple double property and String value. + */ + public void testExecuteWithSimpleDoublePropertyAndStringValue() { + try { + new BeanPropertyValueChangeConsumer<>("doubleProperty", "123").accept(new TestBean()); + fail("Should have thrown an IllegalArgumentException"); + } catch (final IllegalArgumentException e) { + /* this is what we expect */ + } + } + + /** + * Test execute with simple double property and Float value. + */ + public void testExecuteWithSimpleDoublePropertyAndFloatValue() { + final TestBean testBean = new TestBean(); + new BeanPropertyValueChangeConsumer<>("doubleProperty", expectedFloatValue).accept(testBean); + assertTrue(expectedFloatValue.doubleValue() == testBean.getDoubleProperty()); + } + + /** + * Test execute with simple double property and Integer value. + */ + public void testExecuteWithSimpleDoublePropertyAndIntegerValue() { + final TestBean testBean = new TestBean(); + new BeanPropertyValueChangeConsumer<>("doubleProperty", expectedIntegerValue).accept(testBean); + assertTrue(expectedIntegerValue.doubleValue() == testBean.getDoubleProperty()); + } + + /** + * Test execute with simple int property and Double value. + */ + public void testExecuteWithSimpleIntPropertyAndDoubleValue() { + try { + new BeanPropertyValueChangeConsumer<>("intProperty", expectedDoubleValue).accept(new TestBean()); + fail("Should have thrown an IllegalArgumentException"); + } catch (final IllegalArgumentException e) { + /* this is what we expect */ + } + } + + /** + * Test execute with simple int property and String value. + */ + public void testExecuteWithSimpleIntPropertyAndStringValue() { + try { + new BeanPropertyValueChangeConsumer<>("intProperty", "123").accept(new TestBean()); + fail("Should have thrown an IllegalArgumentException"); + } catch (final IllegalArgumentException e) { + /* this is what we expect */ + } + } + + /** + * Test execute with simple int property and Float value. + */ + public void testExecuteWithSimpleIntPropertyAndFloatValue() { + try { + new BeanPropertyValueChangeConsumer<>("intProperty", expectedFloatValue).accept(new TestBean()); + fail("Should have thrown an IllegalArgumentException"); + } catch (final IllegalArgumentException e) { + /* this is what we expect */ + } + } + + /** + * Test execute with simple int property and Integer value. + */ + public void testExecuteWithSimpleIntPropertyAndIntegerValue() { + final TestBean testBean = new TestBean(); + new BeanPropertyValueChangeConsumer<>("intProperty", expectedIntegerValue).accept(testBean); + assertTrue(expectedIntegerValue.intValue() == testBean.getIntProperty()); + } + + /** + * Test execute with simple boolean property and Boolean value. + */ + public void testExecuteWithSimpleBooleanPropertyAndBooleanValue() { + final TestBean testBean = new TestBean(); + new BeanPropertyValueChangeConsumer<>("booleanProperty", expectedBooleanValue).accept(testBean); + assertTrue(expectedBooleanValue.booleanValue() == testBean.getBooleanProperty()); + } + + /** + * Test execute with simple boolean property and String value. + */ + public void testExecuteWithSimpleBooleanPropertyAndStringValue() { + try { + new BeanPropertyValueChangeConsumer<>("booleanProperty", "true").accept(new TestBean()); + fail("Should have thrown an IllegalArgumentException"); + } catch (final IllegalArgumentException e) { + /* this is what we expect */ + } + } + + /** + * Test execute with simple byte property and Byte value. + */ + public void testExecuteWithSimpleBytePropertyAndByteValue() { + final TestBean testBean = new TestBean(); + new BeanPropertyValueChangeConsumer<>("byteProperty", expectedByteValue).accept(testBean); + assertTrue(expectedByteValue.byteValue() == testBean.getByteProperty()); + } + + /** + * Test execute with simple boolean property and String value. + */ + public void testExecuteWithSimpleBytePropertyAndStringValue() { + try { + new BeanPropertyValueChangeConsumer<>("byteProperty", "foo").accept(new TestBean()); + fail("Should have thrown an IllegalArgumentException"); + } catch (final IllegalArgumentException e) { + /* this is what we expect */ + } + } + + /** + * Test execute with simple primitive property and null value. + */ + public void testExecuteWithSimplePrimitivePropertyAndNullValue() { + try { + new BeanPropertyValueChangeConsumer<>("intProperty", null).accept(new TestBean()); + fail("Should have thrown an IllegalArgumentException"); + } catch (final IllegalArgumentException e) { + /* this is what we expect */ + } + } + + /** + * Test execute with read only property. + */ + public void testExecuteWithReadOnlyProperty() { + try { + new BeanPropertyValueChangeConsumer<>("readOnlyProperty", "foo").accept(new TestBean()); + fail("Should have thrown an IllegalArgumentException"); + } catch (final IllegalArgumentException e) { + /* this is what we expect */ + } + } + + /** + * Test execute with write only property. + */ + public void testExecuteWithWriteOnlyProperty() { + final TestBean testBean = new TestBean(); + new BeanPropertyValueChangeConsumer<>("writeOnlyProperty", "foo").accept(testBean); + assertEquals("foo", testBean.getWriteOnlyPropertyValue()); + } + + /** + * Test execute with a nested property. + */ + public void testExecuteWithNestedProperty() { + final TestBean testBean = new TestBean(); + new BeanPropertyValueChangeConsumer<>("nested.stringProperty", "bar").accept(testBean); + assertEquals("bar", testBean.getNested().getStringProperty()); + } + + /** + * Test execute with a nested property and null in the property path. + */ + public void testExecuteWithNullInPropertyPath() { + try { + new BeanPropertyValueChangeConsumer<>("anotherNested.stringProperty", "foo").accept(new TestBean()); + fail("Should have thrown an IllegalArgumentException"); + } catch (final IllegalArgumentException e) { + /* this is what we expect */ + } + } + + /** + * Test execute with a nested property and null in the property path and ignoreNull = true. + */ + public void testExecuteWithNullInPropertyPathAngIgnoreTrue() { + final TestBean testBean = new TestBean(); + + // create a consumer that will attempt to set a property on the null bean in the path + final BeanPropertyValueChangeConsumer<TestBean, Object> consumer = new BeanPropertyValueChangeConsumer<>( + "anotherNested.stringProperty", "Should ignore exception", true); + + try { + consumer.accept(testBean); + } catch (final IllegalArgumentException e) { + fail("Should have ignored the exception."); + } + } + + /** + * Test execute with indexed property. + */ + public void testExecuteWithIndexedProperty() { + final TestBean testBean = new TestBean(); + new BeanPropertyValueChangeConsumer<>("intIndexed[0]", expectedIntegerValue).accept(testBean); + assertTrue(expectedIntegerValue.intValue() == testBean.getIntIndexed(0)); + } + + /** + * Test execute with mapped property. + */ + public void testExecuteWithMappedProperty() { + final TestBean testBean = new TestBean(); + new BeanPropertyValueChangeConsumer<>("mappedProperty(fred)", "barney").accept(testBean); + assertEquals("barney", testBean.getMappedProperty("fred")); + } + + /** + * Test execute with a simple String property. + */ + public void testExecuteWithSimpleStringProperty() { + final TestBean testBean = new TestBean(); + new BeanPropertyValueChangeConsumer<>("stringProperty", "barney").accept(testBean); + assertEquals("barney", testBean.getStringProperty()); + } + + /** + * Test execute with an invalid property name. + */ + public void testExecuteWithInvalidPropertyName() { + try { + new BeanPropertyValueChangeConsumer<>("bogusProperty", "foo").accept(new TestBean()); + fail("Should have thrown an IllegalArgumentException"); + } catch (final IllegalArgumentException e) { + /* this is what we expect */ + } + } +}
