📄 evaluatorfactorytest.java
字号:
package org.drools.base;
/*
* Copyright 2005 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import junit.framework.TestCase;
import org.drools.spi.Evaluator;
/**
* Some test coverage goodness for the evaluators.
* Evaluator concrete instances are inside the factory at this time.
* @author Michael Neale
*/
public class EvaluatorFactoryTest extends TestCase {
public void testObject() {
final List list = new ArrayList();
list.add( "foo" );
final Object[][] data = {
{"foo", "==", "bar", Boolean.FALSE},
{"foo", "==", "foo", Boolean.TRUE},
{"foo", "!=", "bar", Boolean.TRUE},
{list, "contains", "foo", Boolean.TRUE},
{list, "contains", "bar", Boolean.FALSE},
{list, "==", null, Boolean.FALSE},
{list, "!=", null, Boolean.TRUE},
{null, "==", null, Boolean.TRUE},
{null, "==", list, Boolean.FALSE},
{null, "!=", list, Boolean.TRUE},
{new BigDecimal( "42.42" ), "<", new BigDecimal( "43" ), Boolean.TRUE},
{new BigDecimal( "42.42" ), ">", new BigDecimal( "43" ), Boolean.FALSE},
{new BigDecimal( "42.42" ), "<=", new BigDecimal( "42.42" ), Boolean.TRUE},
{new BigInteger( "42" ), ">=", new BigInteger( "43" ), Boolean.FALSE},
{new BigInteger( "42" ), ">=", new BigInteger( "43" ), Boolean.FALSE},
{list, "excludes", "baz", Boolean.TRUE},
{list, "excludes", "foo", Boolean.FALSE}
};
runEvaluatorTest( data,
Evaluator.OBJECT_TYPE );
}
public void testArrayType() {
final Object[] field = new Object[]{"foo", "bar"};
final Object[][] data = {
{field, "==", new Object[]{"foo"}, Boolean.FALSE},
{field, "==", field, Boolean.TRUE},
{field, "!=", new Object[]{"foo"}, Boolean.TRUE},
{field, "contains", "foo", Boolean.TRUE},
{field, "!=", null, Boolean.TRUE},
{field, "==", null, Boolean.FALSE},
{null, "==", field, Boolean.FALSE},
{null, "==", null, Boolean.TRUE},
{null, "!=", field, Boolean.TRUE},
{null, "!=", null, Boolean.FALSE}};
runEvaluatorTest( data,
Evaluator.ARRAY_TYPE );
}
public void testString() {
final Object[][] data = {
{"foo", "==", "bar", Boolean.FALSE},
{"foo", "==", "foo", Boolean.TRUE},
{"foo", "!=", "bar", Boolean.TRUE},
{"something foo", "matches", ".*foo", Boolean.TRUE},
{"foo", "matches", ".*foo", Boolean.TRUE},
{"foo", "matches", "bar", Boolean.FALSE},
{null, "matches", ".*foo", Boolean.FALSE},
{"foo", "==", null, Boolean.FALSE},
{"foo", "!=", null, Boolean.TRUE},
{null, "==", null, Boolean.TRUE},
{"foo", "!=", null, Boolean.TRUE},
{null, "!=", "foo", Boolean.TRUE},
{null, "!=", null, Boolean.FALSE}
};
runEvaluatorTest( data,
Evaluator.STRING_TYPE );
}
public void testInteger() {
final Object[][] data = {
{new Integer( 42 ), "==", new Integer( 42 ), Boolean.TRUE},
{new Integer( 42 ), "<", new Integer( 43 ), Boolean.TRUE},
{new Integer( 42 ), ">=", new Integer( 41 ), Boolean.TRUE},
{new Integer( 42 ), "!=", new Integer( 41 ), Boolean.TRUE},
{new Integer( 42 ), ">", new Integer( 41 ), Boolean.TRUE},
{new Integer( 42 ), "<=", new Integer( 42 ), Boolean.TRUE},
{new Integer( 42 ), ">", new Integer( 100 ), Boolean.FALSE},
{new Integer( 42 ), "==", null, Boolean.FALSE},
{new Integer( 42 ), "!=", null, Boolean.TRUE},
{null, "==", null, Boolean.TRUE},
{null, "==", new Integer( 42 ), Boolean.FALSE},
{null, "!=", null, Boolean.FALSE},
{null, "!=", new Integer( 42 ), Boolean.TRUE}};
runEvaluatorTest( data,
Evaluator.INTEGER_TYPE );
}
public void testShort() {
//Test data: Obj1, Operand, Obj2
final Object[][] data = {
{new Short( (short) 42 ), "==", new Short( (short) 42 ), Boolean.TRUE},
{new Short( (short) 42 ), "<", new Short( (short) 43 ), Boolean.TRUE},
{new Short( (short) 42 ), ">=", new Short( (short) 41 ), Boolean.TRUE},
{new Short( (short) 42 ), "!=", new Short( (short) 41 ), Boolean.TRUE},
{new Short( (short) 42 ), "!=", null, Boolean.TRUE},
{null, "==", null, Boolean.TRUE},
{null, "==", new Short( (short) 42 ), Boolean.FALSE},
{null, "!=", null, Boolean.FALSE},
{null, "!=", new Short( (short) 42 ), Boolean.TRUE}};
runEvaluatorTest( data,
Evaluator.SHORT_TYPE );
}
public void testBoolean() {
//Test data: Obj1, Operand, Obj2
final Object[][] data = {
{new Boolean( true ), "==", new Boolean( true ), Boolean.TRUE},
{new Boolean( false ), "!=", new Boolean( true ), Boolean.TRUE},
{new Boolean( true ), "==", new Boolean( false ), Boolean.FALSE},
{new Boolean( true ), "!=", new Boolean( false ), Boolean.TRUE},
{new Boolean( true ), "==", null, Boolean.FALSE},
{new Boolean( true ), "!=", null, Boolean.TRUE},
{null, "==", new Boolean( true ), Boolean.FALSE},
{null, "!=", new Boolean( true ), Boolean.TRUE},
{null, "==", null, Boolean.TRUE}
};
runEvaluatorTest( data,
Evaluator.BOOLEAN_TYPE );
}
public void testDouble() {
final Object[][] data = {
{new Double( 42 ), "==", new Double( 42 ), Boolean.TRUE},
{new Double( 42 ), "<", new Double( 43 ), Boolean.TRUE},
{new Double( 42 ), ">=", new Double( 41 ), Boolean.TRUE},
{new Double( 42 ), "!=", new Double( 41 ), Boolean.TRUE},
{new Double( 42 ), ">", new Double( 41 ), Boolean.TRUE},
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -