rdonkin 2003/08/28 14:10:36
Modified: beanutils build.xml
beanutils/src/java/org/apache/commons/beanutils
ConstructorUtils.java package.html
beanutils/src/test/org/apache/commons/beanutils
TestBean.java
Added: beanutils/src/test/org/apache/commons/beanutils
BeanPropertyValueChangeClosureTest.java
Log:
Bean property functor implementation tests. Also documentation improvements.
Submitted by Norm Deane
Revision Changes Path
1.57 +5 -5 jakarta-commons/beanutils/build.xml
Index: build.xml
===================================================================
RCS file: /home/cvs/jakarta-commons/beanutils/build.xml,v
retrieving revision 1.56
retrieving revision 1.57
diff -u -r1.56 -r1.57
--- build.xml 2 Aug 2003 20:37:16 -0000 1.56
+++ build.xml 28 Aug 2003 21:10:36 -0000 1.57
@@ -264,7 +264,7 @@
test.method,
test.dyna.result,
test.dyna.row,
- test.bean.comparator,
+ test.bean.collections,
test.locale.convert,
test.converters,
test.beanification
@@ -423,8 +423,8 @@
</java>
</target>
- <target name="test.bean.comparator" depends="compile.tests">
- <echo message="Running BeanComparator tests ..."/>
+ <target name="test.bean.collections" depends="compile.tests">
+ <echo message="Running bean collection utilities tests ..."/>
<java classname="${test.runner}" fork="yes"
failonerror="${test.failonerror}">
<sysproperty key="org.apache.commons.logging.LogFactory"
@@ -433,7 +433,7 @@
value="${test.log}"/>
<sysproperty key="org.apache.commons.logging.simplelog.defaultlog"
value="${test.level}"/>
- <arg value="org.apache.commons.beanutils.BeanComparatorTestCase"/>
+ <arg value="org.apache.commons.beanutils.BeanCollectionsTestSuite"/>
<classpath refid="test.classpath"/>
</java>
</target>
1.4 +1 -2
jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/ConstructorUtils.java
Index: ConstructorUtils.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/ConstructorUtils.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- ConstructorUtils.java 3 Feb 2003 20:50:49 -0000 1.3
+++ ConstructorUtils.java 28 Aug 2003 21:10:36 -0000 1.4
@@ -62,8 +62,6 @@
import java.lang.reflect.Modifier;
/**
- * @version $Revision$ $Date$
- *
* <p> Utility reflection methods focussed on constructors, modelled after [EMAIL
PROTECTED] MethodUtils}. </p>
*
* <h3>Known Limitations</h3>
@@ -86,6 +84,7 @@
* @author Jan Sorensen
* @author Robert Burrell Donkin
* @author Rodney Waldhoff
+ * @version $Revision$ $Date$
*/
public class ConstructorUtils {
1.16 +95 -0
jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/package.html
Index: package.html
===================================================================
RCS file:
/home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/package.html,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -r1.15 -r1.16
--- package.html 15 Mar 2003 11:38:11 -0000 1.15
+++ package.html 28 Aug 2003 21:10:36 -0000 1.16
@@ -43,6 +43,13 @@
<ul>
<li><a href="#instances.background">Background</a></li>
</ul></li>
+<li><a href="#collections">Collections</a>
+ <ul>
+ <li><a href="#bean-comparator">Comparing Beans</a></li>
+ <li><a href="#bean-property-closure">Operating On Collections Of Beans</a></li>
+ <li><a href="#bean-property-predicate">Querying Or Filtering Collections Of
Beans</a></li>
+ <li><a href="#bean-property-transformer">Transforming Collections Of
Beans</a></li>
+ </ul></li>
<li><a href="#FAQ">Frequently Asked Questions</a>
<ul>
<li><a href="#FAQ.property">Why Can't BeanUtils Find My Method?</a></li>
@@ -693,6 +700,94 @@
<p>
Creating an instances allow gives guarenteed control of the caching and
registration
to the code that creates it.
+</p>
+
+<a name="collections"></a>
+<h1>Collections</h1>
+<a name="bean-comparator"></a>
+<h3>Comparing Beans</h3>
+<p>
+<code>org.apache.commons.beanutils.BeanComparator</code> is a
<code>Comparator</code> implementation
+that compares beans based on a shared property value.
+</p>
+<a name="bean-property-closure"></a>
+<h3>Operating On Collections Of Beans</h3>
+<p>
+The <code>Closure</code> interface in <code>commons-collections</code> encapsulates
a block of code that
+executes on an arbitrary input Object. <code>Commons-collections</code> contains
code that allows
+<code>Closures</code> to be applied to the contents of a Collection. For more
details, see the
+<a href='http://jakarta.apache.org/commons/collections.html'>commons-collections</a>
+documentation.
+</p>
+<p>
+<code>BeanPropertyValueChangeClosure</code> is a <code>Closure</code> that sets a
specified property
+to a particular value. A typical usage is to combine this with
<code>commons-collections</code>
+so that all the beans in a collection can have a particular property set to a
particular value.
+</p>
+<p>
+For example, set the activeEmployee property to TRUE for an entire collection:
+ <code><pre>
+ // create the closure
+ BeanPropertyValueChangeClosure closure =
+ new BeanPropertyValueChangeClosure( "activeEmployee", Boolean.TRUE );
+
+ // update the Collection
+ CollectionUtils.forAllDo( peopleCollection, closure );
+ </pre></code>
+</p>
+
+<a name="bean-property-predicate"></a>
+<h3>Querying Or Filtering Collections Of Beans</h3>
+<p>
+The <code>Predicate</code> interface in <code>commons-collections</code>
encapsulates an evaluation
+of an input Object that returns either true or false.
<code>Commons-collections</code> contains code
+that allows
+<code>Predicates</code> to be applied to be used to filter collections. For more
details, see the
+<a href='http://jakarta.apache.org/commons/collections.html'>commons-collections</a>
+documentation.
+</p>
+<p>
+<code>BeanPropertyValueEqualsPredicate</code> is a <code>Predicate</code> that
evaluates a
+set property value against a given value. A typical usage is
+(in combination with <code>commons-collections</code>)
+to filter collections on the basis of a property value.
+</p>
+<p>
+For example, to filter a collection to find all beans where active employee is
false use:
+<code><pre>
+ BeanPropertyValueEqualsPredicate predicate =
+ new BeanPropertyValueEqualsPredicate( "activeEmployee", Boolean.FALSE );
+
+ // filter the Collection
+ CollectionUtils.filter( peopleCollection, predicate );
+</pre</code>
+</p>
+
+<a href="bean-property-transformer"></a>
+<h3>Transforming Collections Of Beans</h3>
+<p>
+The <code>Transformer</code> interface in <code>commons-collections</code>
encapsulates the transformation
+of an input Object into an output object. <code>Commons-collections</code> contains
code
+that allows
+<code>Transformers</code> to be applied produce a collection of outputs from a
collection of inputs.
+For more details, see the
+<a href='http://jakarta.apache.org/commons/collections.html'>commons-collections</a>
+documentation.
+</p>
+<p>
+<code>BeanToPropertyTransformer</code> is a <code>Transformer</code> implementation
+that transforms a bean into it's property value.
+</p>
+<p>
+For example, to find all cities that are contained in the address of each person
property of each bean in
+a collection:
+ <code><pre>
+ // create the transformer
+ BeanToPropertyValueTransformer transformer = new
BeanToPropertyValueTransformer( "person.address.city" );
+
+ // transform the Collection
+ Collection peoplesCities = CollectionUtils.collect( peopleCollection,
transformer );
+ </pre></code>
</p>
<a name="FAQ"></a>
1.17 +21 -5
jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/TestBean.java
Index: TestBean.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/TestBean.java,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -r1.16 -r1.17
--- TestBean.java 3 Jul 2003 19:10:27 -0000 1.16
+++ TestBean.java 28 Aug 2003 21:10:36 -0000 1.17
@@ -119,6 +119,10 @@
setIntProperty(intProperty.intValue());
}
+ public TestBean(double doubleProperty) {
+ setDoubleProperty(doubleProperty);
+ }
+
TestBean(int intProperty) {
setIntProperty(intProperty);
}
@@ -410,7 +414,19 @@
return (nested);
}
-
+ /**
+ * Another nested reference to another test bean,
+ */
+ private TestBean anotherNested = null;
+
+ public TestBean getAnotherNested() {
+ return anotherNested;
+ }
+
+ public void setAnotherNested( TestBean anotherNested ) {
+ this.anotherNested = anotherNested;
+ }
+
/*
* Another nested reference to a bean containing mapp properties
*/
1.1
jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/BeanPropertyValueChangeClosureTest.java
Index: BeanPropertyValueChangeClosureTest.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/BeanPropertyValueChangeClosureTest.java,v
1.1 2003/08/28 21:10:36 rdonkin Exp $
* $Revision: 1.1 $
* $Date: 2003/08/28 21:10:36 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.beanutils;
import junit.framework.TestCase;
/**
* Test cases for <code>BeanPropertyValueChangeClosure</code>.
*
* @author Norm Deane
*/
public class BeanPropertyValueChangeClosureTest 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 BeanPropertyValueChangeClosureTest(String name) {
super(name);
}
/**
* Test execute with simple float property and Float value.
*/
public void testExecuteWithSimpleFloatPropertyAndFloatValue() {
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 (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 (IllegalArgumentException e) {
/* this is what we expect */
}
}
/**
* Test execute with simple float property and Integer value.
*/
public void testExecuteWithSimpleFloatPropertyAndIntegerValue() {
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() {
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 (IllegalArgumentException e) {
/* this is what we expect */
}
}
/**
* Test execute with simple double property and Float value.
*/
public void testExecuteWithSimpleDoublePropertyAndFloatValue() {
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() {
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 (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 (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 (IllegalArgumentException e) {
/* this is what we expect */
}
}
/**
* Test execute with simple int property and Integer value.
*/
public void testExecuteWithSimpleIntPropertyAndIntegerValue() {
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() {
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 (IllegalArgumentException e) {
/* this is what we expect */
}
}
/**
* Test execute with simple byte property and Byte value.
*/
public void testExecuteWithSimpleBytePropertyAndByteValue() {
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 (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 (NullPointerException 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 (IllegalArgumentException e) {
/* this is what we expect */
}
}
/**
* Test execute with write only property.
*/
public void testExecuteWithWriteOnlyProperty() {
TestBean testBean = new TestBean();
new BeanPropertyValueChangeClosure("writeOnlyProperty",
"foo").execute(testBean);
assertEquals("foo", testBean.getWriteOnlyPropertyValue());
}
/**
* Test execute with a nested property.
*/
public void testExecuteWithNestedProperty() {
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 (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() {
TestBean testBean = new TestBean();
// create a closure that will attempt to set a property on the null bean in
the path
BeanPropertyValueChangeClosure closure = new
BeanPropertyValueChangeClosure("anotherNested.stringProperty",
"Should ignore exception", true);
try {
closure.execute(testBean);
} catch (IllegalArgumentException e) {
fail("Should have ignored the exception.");
}
}
/**
* Test execute with indexed property.
*/
public void testExecuteWithIndexedProperty() {
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() {
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() {
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 (IllegalArgumentException e) {
/* this is what we expect */
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]