tobrien 2004/03/08 09:05:06
Added: beanutils/src/java/org/apache/commons/beanutils
BeanPredicate.java
beanutils/src/test/org/apache/commons/beanutils
BeanPredicateTestCase.java
Log:
Added BeanPredicate with a TestCase
Revision Changes Path
1.1
jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/BeanPredicate.java
Index: BeanPredicate.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 org.apache.commons.collections.Predicate;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.lang.reflect.InvocationTargetException;
public class BeanPredicate implements Predicate {
private final Log log = LogFactory.getLog(this.getClass());
private String propertyName;
private Predicate predicate;
public BeanPredicate(String propertyName, Predicate predicate) {
this.propertyName = propertyName;
this.predicate = predicate;
}
public boolean evaluate(Object object) {
boolean evaluation = false;
try {
Object propValue = PropertyUtils.getProperty( object, propertyName );
evaluation = predicate.evaluate(propValue);
} catch (IllegalArgumentException e) {
final String errorMsg = "Problem during evaluation.";
log.error("ERROR: " + errorMsg, e);
throw e;
} catch (IllegalAccessException e) {
final String errorMsg = "Unable to access the property provided.";
log.error(errorMsg, e);
throw new IllegalArgumentException(errorMsg);
} catch (InvocationTargetException e) {
final String errorMsg = "Exception occurred in property's getter";
log.error(errorMsg, e);
throw new IllegalArgumentException(errorMsg);
} catch (NoSuchMethodException e) {
final String errorMsg = "Property not found.";
log.error(errorMsg, e);
throw new IllegalArgumentException(errorMsg);
}
return evaluation;
}
public String getPropertyName() {
return propertyName;
}
public void setPropertyName(String propertyName) {
this.propertyName = propertyName;
}
public Predicate getPredicate() {
return predicate;
}
public void setPredicate(Predicate predicate) {
this.predicate = predicate;
}
}
1.1
jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/BeanPredicateTestCase.java
Index: BeanPredicateTestCase.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;
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")));
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]