You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by rd...@apache.org on 2004/05/10 22:02:54 UTC
cvs commit: jakarta-commons/beanutils/optional/bean-collections/src/test/org/apache/commons/beanutils BeanPredicateTestCase.java BeanPropertyValueChangeClosureTest.java BeanPropertyValueEqualsPredicateTest.java
rdonkin 2004/05/10 13:02:54
Added: beanutils/optional/bean-collections/src/test/org/apache/commons/beanutils
BeanPredicateTestCase.java
BeanPropertyValueChangeClosureTest.java
BeanPropertyValueEqualsPredicateTest.java
Log:
Moved tests for collection related classes
Revision Changes Path
1.1 jakarta-commons/beanutils/optional/bean-collections/src/test/org/apache/commons/beanutils/BeanPredicateTestCase.java
Index: BeanPredicateTestCase.java
===================================================================
/*
* Copyright 2004 The Apache Software Foundation.
*
* 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.
*/
package org.apache.commons.beanutils;
import junit.framework.TestCase;
import org.apache.commons.collections.functors.EqualPredicate;
import org.apache.commons.collections.functors.InstanceofPredicate;
import org.apache.commons.collections.functors.NotPredicate;
import org.apache.commons.collections.functors.NullPredicate;
public class BeanPredicateTestCase extends TestCase {
public BeanPredicateTestCase(String name) {
super(name);
}
public void testEqual() {
BeanPredicate predicate =
new BeanPredicate("stringProperty",new EqualPredicate("foo"));
assertTrue(predicate.evaluate(new TestBean("foo")));
assertTrue(!predicate.evaluate(new TestBean("bar")));
}
public void testNotEqual() {
BeanPredicate predicate =
new BeanPredicate("stringProperty",new NotPredicate( new EqualPredicate("foo")));
assertTrue(!predicate.evaluate(new TestBean("foo")));
assertTrue(predicate.evaluate(new TestBean("bar")));
}
public void testInstanceOf() {
BeanPredicate predicate =
new BeanPredicate("stringProperty",new InstanceofPredicate( String.class ));
assertTrue(predicate.evaluate(new TestBean("foo")));
assertTrue(predicate.evaluate(new TestBean("bar")));
}
public void testNull() {
BeanPredicate predicate =
new BeanPredicate("stringProperty", NullPredicate.INSTANCE);
String nullString = null;
assertTrue(predicate.evaluate(new TestBean(nullString)));
assertTrue(!predicate.evaluate(new TestBean("bar")));
}
}
1.1 jakarta-commons/beanutils/optional/bean-collections/src/test/org/apache/commons/beanutils/BeanPropertyValueChangeClosureTest.java
Index: BeanPropertyValueChangeClosureTest.java
===================================================================
/*
* Copyright 2001-2004 The Apache Software Foundation.
*
* 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.
*/
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 */
}
}
}
1.1 jakarta-commons/beanutils/optional/bean-collections/src/test/org/apache/commons/beanutils/BeanPropertyValueEqualsPredicateTest.java
Index: BeanPropertyValueEqualsPredicateTest.java
===================================================================
/*
* Copyright 2001-2004 The Apache Software Foundation.
*
* 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.
*/
package org.apache.commons.beanutils;
import junit.framework.TestCase;
/**
* Test cases for <code>BeanPropertyValueEqualsPredicateTest</code>.
*
* @author Norm Deane
*/
public class BeanPropertyValueEqualsPredicateTest 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 BeanPropertyValueEqualsPredicateTest.
*
* @param name Name of this test case.
*/
public BeanPropertyValueEqualsPredicateTest(String name) {
super(name);
}
/**
* Test evaluate with simple String property.
*/
public void testEvaluateWithSimpleStringProperty() {
BeanPropertyValueEqualsPredicate predicate =
new BeanPropertyValueEqualsPredicate("stringProperty","foo");
assertTrue(predicate.evaluate(new TestBean("foo")));
assertTrue(!predicate.evaluate(new TestBean("bar")));
}
/**
* Test evaluate with simple String property and null values.
*/
public void testEvaluateWithSimpleStringPropertyWithNullValues() {
BeanPropertyValueEqualsPredicate predicate =
new BeanPropertyValueEqualsPredicate("stringProperty",null);
assertTrue(predicate.evaluate(new TestBean((String) null)));
assertTrue(!predicate.evaluate(new TestBean("bar")));
}
/**
* Test evaluate with nested property.
*/
public void testEvaluateWithNestedProperty() {
BeanPropertyValueEqualsPredicate predicate =
new BeanPropertyValueEqualsPredicate("anotherNested.stringProperty","match");
TestBean testBean = new TestBean();
TestBean nestedBean = new TestBean("match");
testBean.setAnotherNested(nestedBean);
assertTrue(predicate.evaluate(testBean));
testBean.setAnotherNested(new TestBean("no-match"));
assertTrue(!predicate.evaluate(testBean));
}
/**
* Test evaluate with null in property path and ignore=false.
*/
public void testEvaluateWithNullInPath() {
BeanPropertyValueEqualsPredicate predicate =
new BeanPropertyValueEqualsPredicate("anotherNested.stringProperty","foo");
try {
// try to evaluate the predicate
predicate.evaluate(new TestBean());
fail("Should have throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
/* ignore this is what should happen */
}
}
/**
* Test evaluate with null in property path and ignore=true.
*/
public void testEvaluateWithNullInPathAndIgnoreTrue() {
BeanPropertyValueEqualsPredicate predicate =
new BeanPropertyValueEqualsPredicate("anotherNested.stringProperty","foo", true);
try {
assertTrue(!predicate.evaluate(new TestBean()));
} catch (IllegalArgumentException e) {
fail("Should not have throw IllegalArgumentException");
}
}
/**
* Test evaluate with int property.
*/
public void testEvaluateWithIntProperty() {
BeanPropertyValueEqualsPredicate predicate =
new BeanPropertyValueEqualsPredicate("intProperty",expectedIntegerValue);
assertTrue(predicate.evaluate(new TestBean(expectedIntegerValue.intValue())));
assertTrue(!predicate.evaluate(new TestBean(expectedIntegerValue.intValue() - 1)));
}
/**
* Test evaluate with float property.
*/
public void testEvaluateWithFloatProperty() {
BeanPropertyValueEqualsPredicate predicate =
new BeanPropertyValueEqualsPredicate("floatProperty",expectedFloatValue);
assertTrue(predicate.evaluate(new TestBean(expectedFloatValue.floatValue())));
assertTrue(!predicate.evaluate(new TestBean(expectedFloatValue.floatValue() - 1)));
}
/**
* Test evaluate with double property.
*/
public void testEvaluateWithDoubleProperty() {
BeanPropertyValueEqualsPredicate predicate =
new BeanPropertyValueEqualsPredicate("doubleProperty",expectedDoubleValue);
assertTrue(predicate.evaluate(new TestBean(expectedDoubleValue.doubleValue())));
assertTrue(!predicate.evaluate(new TestBean(expectedDoubleValue.doubleValue() - 1)));
}
/**
* Test evaluate with boolean property.
*/
public void testEvaluateWithBooleanProperty() {
BeanPropertyValueEqualsPredicate predicate =
new BeanPropertyValueEqualsPredicate("booleanProperty",expectedBooleanValue);
assertTrue(predicate.evaluate(new TestBean(expectedBooleanValue.booleanValue())));
assertTrue(!predicate.evaluate(new TestBean(!expectedBooleanValue.booleanValue())));
}
/**
* Test evaluate with byte property.
*/
public void testEvaluateWithByteProperty() {
BeanPropertyValueEqualsPredicate predicate =
new BeanPropertyValueEqualsPredicate("byteProperty",expectedByteValue);
TestBean testBean = new TestBean();
testBean.setByteProperty(expectedByteValue.byteValue());
assertTrue(predicate.evaluate(testBean));
testBean.setByteProperty((byte) (expectedByteValue.byteValue() - 1));
assertTrue(!predicate.evaluate(testBean));
}
/**
* Test evaluate with mapped property.
*/
public void testEvaluateWithMappedProperty() {
// try a key that is in the map
BeanPropertyValueEqualsPredicate predicate =
new BeanPropertyValueEqualsPredicate("mappedProperty(test-key)","match");
TestBean testBean = new TestBean();
testBean.setMappedProperty("test-key", "match");
assertTrue(predicate.evaluate(testBean));
testBean.setMappedProperty("test-key", "no-match");
assertTrue(!predicate.evaluate(testBean));
// try a key that isn't in the map
predicate = new BeanPropertyValueEqualsPredicate("mappedProperty(invalid-key)", "match");
assertTrue(!predicate.evaluate(testBean));
}
/**
* Test evaluate with indexed property.
*/
public void testEvaluateWithIndexedProperty() {
// try a valid index
BeanPropertyValueEqualsPredicate predicate =
new BeanPropertyValueEqualsPredicate("intIndexed[0]",expectedIntegerValue);
TestBean testBean = new TestBean();
testBean.setIntIndexed(0, expectedIntegerValue.intValue());
assertTrue(predicate.evaluate(testBean));
testBean.setIntIndexed(0, expectedIntegerValue.intValue() - 1);
assertTrue(!predicate.evaluate(testBean));
// try an invalid index
predicate = new BeanPropertyValueEqualsPredicate("intIndexed[999]", "exception-ahead");
try {
assertTrue(!predicate.evaluate(testBean));
} catch (ArrayIndexOutOfBoundsException e) {
/* this is what should happen */
}
}
/**
* Test evaluate with primitive property and null value.
*/
public void testEvaluateWithPrimitiveAndNull() {
BeanPropertyValueEqualsPredicate predicate =
new BeanPropertyValueEqualsPredicate("intProperty",null);
assertTrue(!predicate.evaluate(new TestBean(0)));
predicate = new BeanPropertyValueEqualsPredicate("booleanProperty", null);
assertTrue(!predicate.evaluate(new TestBean(true)));
predicate = new BeanPropertyValueEqualsPredicate("floatProperty", null);
assertTrue(!predicate.evaluate(new TestBean(expectedFloatValue.floatValue())));
}
/**
* Test evaluate with nested mapped property.
*/
public void testEvaluateWithNestedMappedProperty() {
BeanPropertyValueEqualsPredicate predicate =
new BeanPropertyValueEqualsPredicate("anotherNested.mappedProperty(test-key)","match");
TestBean testBean = new TestBean();
TestBean nestedBean = new TestBean();
nestedBean.setMappedProperty("test-key", "match");
testBean.setAnotherNested(nestedBean);
assertTrue(predicate.evaluate(testBean));
nestedBean.setMappedProperty("test-key", "no-match");
assertTrue(!predicate.evaluate(testBean));
}
/**
* Test evaluate with write only property.
*/
public void testEvaluateWithWriteOnlyProperty() {
try {
new BeanPropertyValueEqualsPredicate("writeOnlyProperty", null).evaluate(new TestBean());
} catch (IllegalArgumentException e) {
/* This is what should happen */
}
}
/**
* Test evaluate with read only property.
*/
public void testEvaluateWithReadOnlyProperty() {
TestBean testBean = new TestBean();
BeanPropertyValueEqualsPredicate predicate =
new BeanPropertyValueEqualsPredicate("readOnlyProperty",testBean.getReadOnlyProperty());
assertTrue(predicate.evaluate(new TestBean()));
}
/**
* Test evaluate with an invalid property name.
*/
public void testEvaluateWithInvalidPropertyName() {
try {
new BeanPropertyValueEqualsPredicate("bogusProperty", null).evaluate(new TestBean());
} catch (IllegalArgumentException e) {
/* This is what should happen */
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org