[jboss-svn-commits] JBL Code SVN: r17216 - labs/jbossrules/branches/temporal_rete/drools-core/src/test/java/org/drools/base.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Thu Dec 13 06:38:00 EST 2007
Author: tolbrino
Date: 2007-12-13 06:38:00 -0500 (Thu, 13 Dec 2007)
New Revision: 17216
Added:
labs/jbossrules/branches/temporal_rete/drools-core/src/test/java/org/drools/base/TemporalEvaluatorFactoryTest.java
Log:
JBRULES-1374
added test coverage for 'after' and 'before' operators
Added: labs/jbossrules/branches/temporal_rete/drools-core/src/test/java/org/drools/base/TemporalEvaluatorFactoryTest.java
===================================================================
--- labs/jbossrules/branches/temporal_rete/drools-core/src/test/java/org/drools/base/TemporalEvaluatorFactoryTest.java (rev 0)
+++ labs/jbossrules/branches/temporal_rete/drools-core/src/test/java/org/drools/base/TemporalEvaluatorFactoryTest.java 2007-12-13 11:38:00 UTC (rev 17216)
@@ -0,0 +1,230 @@
+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.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+
+import org.drools.base.evaluators.EvaluatorDefinition;
+import org.drools.base.evaluators.EvaluatorRegistry;
+import org.drools.common.EventFactHandle;
+import org.drools.common.InternalWorkingMemory;
+import org.drools.spi.Evaluator;
+import org.drools.spi.Extractor;
+import org.drools.spi.FieldExtractor;
+import org.drools.spi.FieldValue;
+
+import junit.framework.TestCase;
+
+/**
+ * Test coverage for the temporal evaluators.
+ *
+ * @author Tino Breddin
+ */
+public class TemporalEvaluatorFactoryTest extends TestCase {
+
+ private EvaluatorRegistry registry = new EvaluatorRegistry();
+
+ public void testAfter() {
+ registry
+ .addEvaluatorDefinition("org.drools.base.evaluators.AfterEvaluatorDefinition");
+
+ EventFactHandle foo = new EventFactHandle(1, "foo", 1, 1, 2);
+ EventFactHandle bar = new EventFactHandle(2, "bar", 1, 2, 2);
+ EventFactHandle drool = new EventFactHandle(1, "drool", 1, 4, 2);
+
+ final Object[][] data = { { drool, "after", foo, Boolean.TRUE },
+ { foo, "after", drool, Boolean.FALSE },
+ { bar, "after", foo, Boolean.FALSE },
+ { drool, "after[1]", foo, Boolean.TRUE },
+ { foo, "after[1]", drool, Boolean.FALSE },
+ { bar, "after[2]", foo, Boolean.FALSE },
+ { drool, "after[1,3]", foo, Boolean.TRUE },
+ { bar, "after[0,1]", foo, Boolean.FALSE },
+ { drool, "after[0,2]", bar, Boolean.TRUE }};
+
+ runEvaluatorTest(data, ValueType.OBJECT_TYPE);
+ }
+
+ public void testBefore() {
+ registry
+ .addEvaluatorDefinition("org.drools.base.evaluators.BeforeEvaluatorDefinition");
+
+ EventFactHandle foo = new EventFactHandle(1, "foo", 1, 1, 2);
+ EventFactHandle bar = new EventFactHandle(2, "bar", 1, 2, 2);
+ EventFactHandle drool = new EventFactHandle(1, "drool", 1, 4, 2);
+
+ final Object[][] data = { { foo, "before", drool, Boolean.TRUE },
+ { drool, "before", foo, Boolean.FALSE },
+ { foo, "before", bar, Boolean.FALSE },
+ { bar, "before", drool, Boolean.FALSE },
+ { foo, "before[1]", drool, Boolean.TRUE },
+ { bar, "before[1]", drool, Boolean.FALSE },
+ { bar, "before[1]", foo, Boolean.FALSE },
+ { foo, "before[1,3]", drool, Boolean.TRUE },
+ { foo, "before[0,1]", bar, Boolean.FALSE },
+ { bar, "before[0,2]", drool, Boolean.TRUE }};
+
+ runEvaluatorTest(data, ValueType.OBJECT_TYPE);
+ }
+
+ private void runEvaluatorTest(final Object[][] data,
+ final ValueType valueType) {
+ final Extractor extractor = new MockExtractor();
+ for (int i = 0; i < data.length; i++) {
+ final Object[] row = data[i];
+ boolean isNegated = ((String) row[1]).startsWith("not ");
+ System.out.println((String) row[1]);
+ String evaluatorStr = isNegated ? ((String) row[1]).substring(4)
+ : (String) row[1];
+ boolean isConstrained = evaluatorStr.endsWith("]");
+ String parameters = null;
+ if (isConstrained) {
+ parameters = evaluatorStr.split("\\[")[1];
+ evaluatorStr = evaluatorStr.split("\\[")[0];
+ parameters = parameters.split("\\]")[0];
+ }
+ EvaluatorDefinition evalDef = registry
+ .getEvaluatorDefinition(evaluatorStr);
+ assertNotNull(evalDef);
+ @SuppressWarnings("unused")
+ final Evaluator evaluator = evalDef.getEvaluator(valueType,
+ evaluatorStr, isNegated, parameters);
+ System.out.println(evaluator);
+
+ checkEvaluatorMethodWith2Extractors(valueType, extractor, row,
+ evaluator);
+ /*
+ * checkEvaluatorMethodWithFieldValue(valueType, extractor, row,
+ * evaluator); checkEvaluatorMethodCachedRight(valueType, extractor,
+ * row, evaluator); checkEvaluatorMethodCachedLeft(valueType,
+ * extractor, row, evaluator);
+ *
+ */
+ assertEquals(valueType, evaluator.getValueType());
+
+ }
+ }
+
+ private void checkEvaluatorMethodWith2Extractors(final ValueType valueType,
+ final Extractor extractor, final Object[] row,
+ final Evaluator evaluator) {
+ final boolean result = evaluator.evaluate(null, extractor, row[0],
+ extractor, row[2]);
+ final String message = "The evaluator type: [" + valueType
+ + "] with 2 extractors incorrectly returned " + result
+ + " for [" + row[0] + " " + row[1] + " " + row[2]
+ + "]. It was asserted to return " + row[3];
+
+ if (row[3] == Boolean.TRUE) {
+ assertTrue(message, result);
+ } else {
+ assertFalse(message, result);
+ }
+ }
+
+ private static class MockExtractor implements FieldExtractor {
+
+ private static final long serialVersionUID = 400L;
+
+ public boolean getBooleanValue(InternalWorkingMemory workingMemory,
+ final Object object) {
+ return object != null ? ((Boolean) object).booleanValue() : false;
+ }
+
+ public byte getByteValue(InternalWorkingMemory workingMemory,
+ final Object object) {
+ return object != null ? ((Number) object).byteValue() : (byte) 0;
+ }
+
+ public char getCharValue(InternalWorkingMemory workingMemory,
+ final Object object) {
+ return object != null ? ((Character) object).charValue() : '\0';
+ }
+
+ public double getDoubleValue(InternalWorkingMemory workingMemory,
+ final Object object) {
+ return object != null ? ((Number) object).doubleValue() : 0.0;
+ }
+
+ public Class getExtractToClass() {
+ return null;
+ }
+
+ public String getExtractToClassName() {
+ return null;
+ }
+
+ public float getFloatValue(InternalWorkingMemory workingMemory,
+ final Object object) {
+ return object != null ? ((Number) object).floatValue()
+ : (float) 0.0;
+ }
+
+ public int getHashCode(InternalWorkingMemory workingMemory,
+ final Object object) {
+ return 0;
+ }
+
+ public int getIntValue(InternalWorkingMemory workingMemory,
+ final Object object) {
+ return object != null ? ((Number) object).intValue() : 0;
+ }
+
+ public long getLongValue(InternalWorkingMemory workingMemory,
+ final Object object) {
+ return object != null ? ((Number) object).longValue() : 0;
+ }
+
+ public Method getNativeReadMethod() {
+ return null;
+ }
+
+ public short getShortValue(InternalWorkingMemory workingMemory,
+ final Object object) {
+ return object != null ? ((Number) object).shortValue() : (short) 0;
+ }
+
+ public Object getValue(InternalWorkingMemory workingMemory,
+ final Object object) {
+ return object;
+ }
+
+ public boolean isNullValue(InternalWorkingMemory workingMemory,
+ final Object object) {
+ return object == null;
+ }
+
+ public ValueType getValueType() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public int getIndex() {
+ return 0;
+ }
+
+ public boolean isGlobal() {
+ return false;
+ }
+
+ }
+
+}
Property changes on: labs/jbossrules/branches/temporal_rete/drools-core/src/test/java/org/drools/base/TemporalEvaluatorFactoryTest.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
More information about the jboss-svn-commits
mailing list