[jboss-svn-commits] JBL Code SVN: r32933 - labs/jbossrules/trunk/drools-ide-common/src/main/java/org/drools/testframework.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue May 18 08:29:07 EDT 2010


Author: Rikkola
Date: 2010-05-18 08:29:06 -0400 (Tue, 18 May 2010)
New Revision: 32933

Added:
   labs/jbossrules/trunk/drools-ide-common/src/main/java/org/drools/testframework/FactFieldValueVerifier.java
Modified:
   labs/jbossrules/trunk/drools-ide-common/src/main/java/org/drools/testframework/ScenarioRunner.java
Log:
Cleaned the ScenarioRunner.java a bit

Added: labs/jbossrules/trunk/drools-ide-common/src/main/java/org/drools/testframework/FactFieldValueVerifier.java
===================================================================
--- labs/jbossrules/trunk/drools-ide-common/src/main/java/org/drools/testframework/FactFieldValueVerifier.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-ide-common/src/main/java/org/drools/testframework/FactFieldValueVerifier.java	2010-05-18 12:29:06 UTC (rev 32933)
@@ -0,0 +1,127 @@
+package org.drools.testframework;
+
+import static org.mvel2.MVEL.eval;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import org.drools.ide.common.client.modeldriven.testing.VerifyField;
+import org.mvel2.MVEL;
+import org.mvel2.ParserContext;
+import org.mvel2.compiler.CompiledExpression;
+import org.mvel2.compiler.ExpressionCompiler;
+
+/**
+ * 
+ * @author rikkola
+ *
+ */
+public class FactFieldValueVerifier {
+
+    private final Map<String, Object> populatedData;
+    private final String              factName;
+    private final Object              factObject;
+
+    private VerifyField               currentField;
+
+    public FactFieldValueVerifier(Map<String, Object> populatedData,
+                             String factName,
+                             Object factObject) {
+        this.populatedData = populatedData;
+        this.factName = factName;
+        this.factObject = factObject;
+    }
+
+    public void checkFields(List<VerifyField> fieldValues) {
+        Iterator<VerifyField> fields = fieldValues.iterator();
+        while ( fields.hasNext() ) {
+            this.currentField = fields.next();
+
+            if ( currentField.expected != null ) {
+                ResultVerifier resultVerifier = new ResultVerifier( factObject );
+
+                resultVerifier.setExpected( getExpectedResult() );
+
+                currentField.successResult = resultVerifier.isSuccess( currentField );
+
+                if ( !currentField.successResult ) {
+                    currentField.actualResult = resultVerifier.getActual( currentField );
+
+                    currentField.explanation = getFailingExplanation();
+                } else {
+                    currentField.explanation = getSuccesfulExplanation();
+                }
+            }
+
+        }
+
+    }
+
+    private Object getExpectedResult() {
+        Object expectedResult = currentField.expected.trim();
+        if ( currentField.expected.startsWith( "=" ) ) {
+            expectedResult = eval( currentField.expected.substring( 1 ),
+                                   this.populatedData );
+        }
+        return expectedResult;
+    }
+
+    private String getSuccesfulExplanation() {
+        if ( currentField.operator.equals( "==" ) ) {
+            return "[" + factName + "] field [" + currentField.fieldName + "] was [" + currentField.expected + "].";
+        } else if ( currentField.operator.equals( "!=" ) ) {
+            return "[" + factName + "] field [" + currentField.fieldName + "] was not [" + currentField.expected + "].";
+        }
+
+        return "";
+    }
+
+    private String getFailingExplanation() {
+        if ( currentField.operator.equals( "==" ) ) {
+            return "[" + factName + "] field [" + currentField.fieldName + "] was [" + currentField.actualResult + "] expected [" + currentField.expected + "].";
+        } else {
+            return "[" + factName + "] field [" + currentField.fieldName + "] was not expected to be [" + currentField.actualResult + "].";
+        }
+    }
+}
+class ResultVerifier {
+
+    private final Map<String, Object> variables     = new HashMap<String, Object>();
+    private ParserContext             parserContext = new ParserContext();
+
+    protected ResultVerifier(Object factObject) {
+        addVariable( "__fact__",
+                     factObject );
+    }
+
+    protected void setExpected(Object expected) {
+        addVariable( "__expected__",
+                     expected );
+    }
+
+    private void addVariable(String name,
+                             Object object) {
+        variables.put( name,
+                       object );
+
+        parserContext.addInput( name,
+                                object.getClass() );
+    }
+
+    protected Boolean isSuccess(VerifyField currentField) {
+        CompiledExpression expression = new ExpressionCompiler( "__fact__." + currentField.fieldName + " " + currentField.operator + " __expected__" ).compile( parserContext );
+
+        return (Boolean) MVEL.executeExpression( expression,
+                                                 variables );
+    }
+
+    protected String getActual(VerifyField currentField) {
+        Object actualValue = MVEL.executeExpression( new ExpressionCompiler( "__fact__." + currentField.fieldName ).compile( parserContext ),
+                                                     variables );
+
+        return (actualValue != null) ? actualValue.toString() : "";
+
+    }
+}

Modified: labs/jbossrules/trunk/drools-ide-common/src/main/java/org/drools/testframework/ScenarioRunner.java
===================================================================
--- labs/jbossrules/trunk/drools-ide-common/src/main/java/org/drools/testframework/ScenarioRunner.java	2010-05-18 09:35:41 UTC (rev 32932)
+++ labs/jbossrules/trunk/drools-ide-common/src/main/java/org/drools/testframework/ScenarioRunner.java	2010-05-18 12:29:06 UTC (rev 32933)
@@ -2,7 +2,14 @@
 
 import static org.mvel2.MVEL.eval;
 
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
 
 import org.drools.FactHandle;
 import org.drools.RuleBase;
@@ -25,11 +32,7 @@
 import org.drools.rule.Package;
 import org.drools.rule.TimeMachine;
 import org.mvel2.MVEL;
-import org.mvel2.ParserContext;
-import org.mvel2.compiler.CompiledExpression;
-import org.mvel2.compiler.ExpressionCompiler;
 
-
 /**
  * This actually runs the test scenarios.
  *
@@ -38,346 +41,316 @@
  */
 public class ScenarioRunner {
 
-	final Scenario scenario;
-	final Map<String, Object> populatedData = new HashMap<String, Object>();
-	final Map<String, Object> globalData = new HashMap<String, Object>();
-	final Map<String, FactHandle> factHandles = new HashMap<String, FactHandle>();
+    final Scenario                scenario;
+    final Map<String, Object>     populatedData = new HashMap<String, Object>();
+    final Map<String, Object>     globalData    = new HashMap<String, Object>();
+    final Map<String, FactHandle> factHandles   = new HashMap<String, FactHandle>();
 
-	final InternalWorkingMemory workingMemory;
+    final InternalWorkingMemory   workingMemory;
 
+    /**
+     * This constructor is normally used by Guvnor for running tests on a users request.
+     * @param scenario
+     *            The scenario to run.
+     * @param resolver
+     *            A populated type resolved to be used to resolve the types in
+     *            the scenario.
+     *
+     * For info on how to invoke this, see
+     * ContentPackageAssemblerTest.testPackageWithRuleflow in drools-guvnor This
+     * requires that the classloader for the thread context be set
+     * appropriately. The PackageBuilder can provide a suitable TypeResolver for
+     * a given package header, and the Package config can provide a classloader.
+     *
+     */
+    public ScenarioRunner(final Scenario scenario,
+                          final TypeResolver resolver,
+                          final InternalWorkingMemory wm) throws ClassNotFoundException {
+        this.scenario = scenario;
+        this.workingMemory = wm;
+        runScenario( scenario,
+                     resolver,
+                     wm );
+    }
 
-	/**
-	 * This constructor is normally used by Guvnor for running tests on a users request.
-	 * @param scenario
-	 *            The scenario to run.
-	 * @param resolver
-	 *            A populated type resolved to be used to resolve the types in
-	 *            the scenario.
-	 *
-	 * For info on how to invoke this, see
-	 * ContentPackageAssemblerTest.testPackageWithRuleflow in drools-guvnor This
-	 * requires that the classloader for the thread context be set
-	 * appropriately. The PackageBuilder can provide a suitable TypeResolver for
-	 * a given package header, and the Package config can provide a classloader.
-	 *
-	 */
-	public ScenarioRunner(final Scenario scenario, final TypeResolver resolver,
-			final InternalWorkingMemory wm) throws ClassNotFoundException {
-		this.scenario = scenario;
-		this.workingMemory = wm;
-		runScenario(scenario, resolver, wm);
-	}
-
-	/**
-	 * Use this constructor if you have a scenario in a file, for instance.
-	 * @throws ClassNotFoundException
-	 */
-	public ScenarioRunner(String xml, RuleBase rb) throws ClassNotFoundException {
-		this.scenario = ScenarioXMLPersistence.getInstance().unmarshal(xml);
-		this.workingMemory = (InternalWorkingMemory) rb.newStatefulSession();
-		Package pk = rb.getPackages()[0];
+    /**
+     * Use this constructor if you have a scenario in a file, for instance.
+     * @throws ClassNotFoundException
+     */
+    public ScenarioRunner(String xml,
+                          RuleBase rb) throws ClassNotFoundException {
+        this.scenario = ScenarioXMLPersistence.getInstance().unmarshal( xml );
+        this.workingMemory = (InternalWorkingMemory) rb.newStatefulSession();
+        Package pk = rb.getPackages()[0];
         ClassLoader cl = ((InternalRuleBase) rb).getRootClassLoader();
         HashSet<String> imports = new HashSet<String>();
-        imports.add(pk.getName() + ".*");
-        imports.addAll(pk.getImports().keySet());
-        TypeResolver resolver = new ClassTypeResolver( imports, cl );
-        runScenario(scenario, resolver, this.workingMemory);
-	}
+        imports.add( pk.getName() + ".*" );
+        imports.addAll( pk.getImports().keySet() );
+        TypeResolver resolver = new ClassTypeResolver( imports,
+                                                       cl );
+        runScenario( scenario,
+                     resolver,
+                     this.workingMemory );
+    }
 
     interface Populate {
         public void go();
     }
 
-	private void runScenario(final Scenario scenario,
-			final TypeResolver resolver, final InternalWorkingMemory wm)
-			throws ClassNotFoundException {
-		MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL = true;
-		scenario.lastRunResult = new Date();
-		//stub out any rules we don't want to have the consequences firing of.
-		HashSet<String> ruleList = new HashSet<String>();
-		ruleList.addAll(scenario.rules);
-		//TestingEventListener.stubOutRules(ruleList, wm.getRuleBase(), scenario.inclusive);
+    private void runScenario(final Scenario scenario,
+                             final TypeResolver resolver,
+                             final InternalWorkingMemory wm) throws ClassNotFoundException {
+        MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL = true;
+        scenario.lastRunResult = new Date();
+        //stub out any rules we don't want to have the consequences firing of.
+        HashSet<String> ruleList = new HashSet<String>();
+        ruleList.addAll( scenario.rules );
+        //TestingEventListener.stubOutRules(ruleList, wm.getRuleBase(), scenario.inclusive);
 
-		TestingEventListener listener = null;
+        TestingEventListener listener = null;
 
         List<Populate> toPopulate = new ArrayList<Populate>();
 
-		for (Iterator iterator = scenario.globals.iterator(); iterator.hasNext();) {
-			final FactData fact = (FactData) iterator.next();
-			final Object f = eval("new " + getTypeName(resolver, fact) + "()");
-            toPopulate.add(new Populate() {
+        for ( Iterator iterator = scenario.globals.iterator(); iterator.hasNext(); ) {
+            final FactData fact = (FactData) iterator.next();
+            final Object f = eval( "new " + getTypeName( resolver,
+                                                         fact ) + "()" );
+            toPopulate.add( new Populate() {
                 public void go() {
-                    populateFields(fact, globalData, f);
+                    populateFields( fact,
+                                    globalData,
+                                    f );
                 }
-            });
-			globalData.put(fact.name, f);
-			wm.setGlobal(fact.name, f);
-		}
+            } );
+            globalData.put( fact.name,
+                            f );
+            wm.setGlobal( fact.name,
+                          f );
+        }
 
-        doPopulate(toPopulate);
+        doPopulate( toPopulate );
 
-		for (Iterator<Fixture> iterator = scenario.fixtures.iterator(); iterator.hasNext();) {
-			Fixture fx = iterator.next();
+        for ( Iterator<Fixture> iterator = scenario.fixtures.iterator(); iterator.hasNext(); ) {
+            Fixture fx = iterator.next();
 
-			if (fx instanceof FactData) {
-				//deal with facts and globals
-				final FactData fact = (FactData)fx;
-				final Object f = (fact.isModify)? this.populatedData.get(fact.name) : eval("new " + getTypeName(resolver, fact) + "()");
-				if (fact.isModify) {
-					if (!this.factHandles.containsKey(fact.name)) {
-						throw new IllegalArgumentException("Was not a previously inserted fact. [" + fact.name  + "]");
-					}
-                    toPopulate.add(new Populate() {
+            if ( fx instanceof FactData ) {
+                //deal with facts and globals
+                final FactData fact = (FactData) fx;
+                final Object f = (fact.isModify) ? this.populatedData.get( fact.name ) : eval( "new " + getTypeName( resolver,
+                                                                                                                     fact ) + "()" );
+                if ( fact.isModify ) {
+                    if ( !this.factHandles.containsKey( fact.name ) ) {
+                        throw new IllegalArgumentException( "Was not a previously inserted fact. [" + fact.name + "]" );
+                    }
+                    toPopulate.add( new Populate() {
                         public void go() {
-                            populateFields(fact, populatedData, f);
-                            workingMemory.update(factHandles.get(fact.name), f);
+                            populateFields( fact,
+                                            populatedData,
+                                            f );
+                            workingMemory.update( factHandles.get( fact.name ),
+                                                  f );
                         }
-                    });
-				} else /* a new one */ {
-                    populatedData.put(fact.name, f);
-                    toPopulate.add(new Populate() {
+                    } );
+                } else /* a new one */{
+                    populatedData.put( fact.name,
+                                       f );
+                    toPopulate.add( new Populate() {
                         public void go() {
-                            populateFields(fact, populatedData, f);
-                            factHandles.put(fact.name, wm.insert(f));
+                            populateFields( fact,
+                                            populatedData,
+                                            f );
+                            factHandles.put( fact.name,
+                                             wm.insert( f ) );
                         }
-                    });
-				}
-			} else if (fx instanceof RetractFact) {
-				RetractFact f = (RetractFact)fx;
-				this.workingMemory.retract(this.factHandles.get(f.name));
-				this.populatedData.remove(f.name);
-			} else if ( fx instanceof ActivateRuleFlowGroup ) {
+                    } );
+                }
+            } else if ( fx instanceof RetractFact ) {
+                RetractFact f = (RetractFact) fx;
+                this.workingMemory.retract( this.factHandles.get( f.name ) );
+                this.populatedData.remove( f.name );
+            } else if ( fx instanceof ActivateRuleFlowGroup ) {
                 workingMemory.getAgenda().activateRuleFlowGroup( ((ActivateRuleFlowGroup) fx).name );
             } else if ( fx instanceof ExecutionTrace ) {
-                doPopulate(toPopulate);
-				ExecutionTrace executionTrace = (ExecutionTrace)fx;
-				//create the listener to trace rules
+                doPopulate( toPopulate );
+                ExecutionTrace executionTrace = (ExecutionTrace) fx;
+                //create the listener to trace rules
 
-				if (listener != null) wm.removeEventListener(listener); //remove the old
-				listener = new TestingEventListener();
+                if ( listener != null ) wm.removeEventListener( listener ); //remove the old
+                listener = new TestingEventListener();
 
-				wm.addEventListener(listener);
+                wm.addEventListener( listener );
 
-				//set up the time machine
-				applyTimeMachine(wm, executionTrace);
+                //set up the time machine
+                applyTimeMachine( wm,
+                                  executionTrace );
 
-				//love you
-				long time = System.currentTimeMillis();
-				wm.fireAllRules(listener.getAgendaFilter(ruleList, scenario.inclusive),scenario.maxRuleFirings);
-				executionTrace.executionTimeResult = System.currentTimeMillis() - time;
-				executionTrace.numberOfRulesFired = listener.totalFires;
-				executionTrace.rulesFired = listener.getRulesFiredSummary();
+                //love you
+                long time = System.currentTimeMillis();
+                wm.fireAllRules( listener.getAgendaFilter( ruleList,
+                                                           scenario.inclusive ),
+                                 scenario.maxRuleFirings );
+                executionTrace.executionTimeResult = System.currentTimeMillis() - time;
+                executionTrace.numberOfRulesFired = listener.totalFires;
+                executionTrace.rulesFired = listener.getRulesFiredSummary();
 
+            } else if ( fx instanceof Expectation ) {
+                doPopulate( toPopulate );
+                Expectation assertion = (Expectation) fx;
+                if ( assertion instanceof VerifyFact ) {
+                    verify( (VerifyFact) assertion );
+                } else if ( assertion instanceof VerifyRuleFired ) {
+                    verify( (VerifyRuleFired) assertion,
+                            (listener.firingCounts != null) ? listener.firingCounts : new HashMap<String, Integer>() );
+                }
+            } else {
+                throw new IllegalArgumentException( "Not sure what to do with " + fx );
+            }
 
-			} else if (fx instanceof Expectation) {
-                doPopulate(toPopulate);
-					Expectation assertion = (Expectation) fx;
-					if (assertion instanceof VerifyFact) {
-						verify((VerifyFact) assertion);
-					} else if (assertion instanceof VerifyRuleFired) {
-						verify((VerifyRuleFired) assertion,
-								(listener.firingCounts != null) ? listener.firingCounts : new HashMap<String, Integer>());
-					}
-			} else {
-				throw new IllegalArgumentException("Not sure what to do with " + fx);
-			}
+        }
 
+        doPopulate( toPopulate );
+    }
 
-
-		}
-        
-        doPopulate(toPopulate);
-	}
-
     private void doPopulate(List<Populate> toPopulate) {
-        for (Populate p : toPopulate) {p.go();}
+        for ( Populate p : toPopulate ) {
+            p.go();
+        }
         toPopulate.clear();
     }
 
-    private String getTypeName(TypeResolver resolver, FactData fact) throws ClassNotFoundException {
+    private String getTypeName(TypeResolver resolver,
+                               FactData fact) throws ClassNotFoundException {
 
-        String fullName = resolver.getFullTypeName(fact.type);
-        if (fullName.equals("java.util.List") || fullName.equals("java.util.Collection")) {
-                return "java.util.ArrayList";
+        String fullName = resolver.getFullTypeName( fact.type );
+        if ( fullName.equals( "java.util.List" ) || fullName.equals( "java.util.Collection" ) ) {
+            return "java.util.ArrayList";
         } else {
-                return fullName;
+            return fullName;
         }
     }
 
     private void applyTimeMachine(final InternalWorkingMemory wm,
-			ExecutionTrace executionTrace) {
-		if (executionTrace.scenarioSimulatedDate != null) {
-			final Calendar now = Calendar.getInstance();
-			now.setTimeInMillis(executionTrace.scenarioSimulatedDate.getTime());
-			wm.setTimeMachine(new TimeMachine() {
-				@Override
-				public Calendar getNow() {
-					return now;
-				}
-			});
-		} else {
-			//normal time.
-			wm.setTimeMachine(new TimeMachine());
-		}
-	}
+                                  ExecutionTrace executionTrace) {
+        if ( executionTrace.scenarioSimulatedDate != null ) {
+            final Calendar now = Calendar.getInstance();
+            now.setTimeInMillis( executionTrace.scenarioSimulatedDate.getTime() );
+            wm.setTimeMachine( new TimeMachine() {
+                @Override
+                public Calendar getNow() {
+                    return now;
+                }
+            } );
+        } else {
+            //normal time.
+            wm.setTimeMachine( new TimeMachine() );
+        }
+    }
 
-	void verify(VerifyRuleFired assertion, Map<String, Integer> firingCounts) {
+    void verify(VerifyRuleFired assertion,
+                Map<String, Integer> firingCounts) {
 
-		assertion.actualResult = firingCounts.containsKey(assertion.ruleName) ? firingCounts
-				.get(assertion.ruleName)
-				: 0;
-		if (assertion.expectedFire != null) {
-			if (assertion.expectedFire) {
-				if (assertion.actualResult > 0) {
-					assertion.successResult = true;
-					assertion.explanation = "Rule [" + assertion.ruleName + "] was actived " + assertion.actualResult + " times.";
-				} else {
-					assertion.successResult = false;
-					assertion.explanation = "Rule [" + assertion.ruleName + "] was not activated. Expected it to be activated.";
-				}
-			} else {
-				if (assertion.actualResult == 0) {
-					assertion.successResult = true;
-					assertion.explanation = "Rule [" + assertion.ruleName + "] was not activated.";
-				} else {
-					assertion.successResult = false;
-					assertion.explanation = "Rule [" + assertion.ruleName + "] was activated " + assertion.actualResult + " times, but expected none.";
-				}
-			}
-		}
+        assertion.actualResult = firingCounts.containsKey( assertion.ruleName ) ? firingCounts.get( assertion.ruleName ) : 0;
+        if ( assertion.expectedFire != null ) {
+            if ( assertion.expectedFire ) {
+                if ( assertion.actualResult > 0 ) {
+                    assertion.successResult = true;
+                    assertion.explanation = "Rule [" + assertion.ruleName + "] was actived " + assertion.actualResult + " times.";
+                } else {
+                    assertion.successResult = false;
+                    assertion.explanation = "Rule [" + assertion.ruleName + "] was not activated. Expected it to be activated.";
+                }
+            } else {
+                if ( assertion.actualResult == 0 ) {
+                    assertion.successResult = true;
+                    assertion.explanation = "Rule [" + assertion.ruleName + "] was not activated.";
+                } else {
+                    assertion.successResult = false;
+                    assertion.explanation = "Rule [" + assertion.ruleName + "] was activated " + assertion.actualResult + " times, but expected none.";
+                }
+            }
+        }
 
-		if (assertion.expectedCount != null) {
-			if (assertion.actualResult.equals(assertion.expectedCount)) {
-				assertion.successResult = true;
-				assertion.explanation = "Rule [" + assertion.ruleName + "] activated " + assertion.actualResult + " times.";
-			} else {
-				assertion.successResult = false;
-				assertion.explanation = "Rule [" + assertion.ruleName + "] activated " + assertion.actualResult + " times. Expected " + assertion.expectedCount + " times.";
-			}
-		}
-	}
+        if ( assertion.expectedCount != null ) {
+            if ( assertion.actualResult.equals( assertion.expectedCount ) ) {
+                assertion.successResult = true;
+                assertion.explanation = "Rule [" + assertion.ruleName + "] activated " + assertion.actualResult + " times.";
+            } else {
+                assertion.successResult = false;
+                assertion.explanation = "Rule [" + assertion.ruleName + "] activated " + assertion.actualResult + " times. Expected " + assertion.expectedCount + " times.";
+            }
+        }
+    }
 
+    void verify(VerifyFact value) {
 
-	void verify(VerifyFact value) {
+        if ( !value.anonymous ) {
+            Object factObject = this.populatedData.get( value.name );
+            if ( factObject == null ) factObject = this.globalData.get( value.name );
+            FactFieldValueVerifier fieldVerifier = new FactFieldValueVerifier( populatedData,
+                                                                               value.name,
+                                                                               factObject );
+            fieldVerifier.checkFields( value.fieldValues );
+        } else {
+            Iterator obs = this.workingMemory.iterateObjects();
+            while ( obs.hasNext() ) {
+                Object factObject = obs.next();
+                if ( factObject.getClass().getSimpleName().equals( value.name ) ) {
+                    FactFieldValueVerifier fieldVerifier = new FactFieldValueVerifier( populatedData,
+                                                                                       value.name,
+                                                                                       factObject );
+                    fieldVerifier.checkFields( value.fieldValues );
+                    if ( value.wasSuccessful() ) return;
+                }
+            }
+            for ( VerifyField vfl : value.fieldValues ) {
+                if ( vfl.successResult == null ) {
+                    vfl.successResult = Boolean.FALSE;
+                    vfl.actualResult = "No match";
+                }
+            }
+        }
+    }
 
-
-		if (!value.anonymous) {
-			Object fact = this.populatedData.get(value.name);
-			if (fact == null) fact = this.globalData.get(value.name);
-			checkFact(value, fact);
-		} else {
-			Iterator obs = this.workingMemory.iterateObjects();
-			while(obs.hasNext()) {
-				Object fact = obs.next();
-				if (fact.getClass().getSimpleName().equals(value.name)) {
-					checkFact(value, fact);
-					if (value.wasSuccessful()) return;
-				}
-			}
-			for (Iterator iterator = value.fieldValues.iterator(); iterator.hasNext();) {
-				VerifyField vfl = (VerifyField) iterator.next();
-				if (vfl.successResult == null) {
-					vfl.successResult = Boolean.FALSE;
-					vfl.actualResult = "No match";
-				}
-			}
- 		}
-	}
-
-	private void checkFact(VerifyFact value, Object fact) {
-		for (int i = 0; i < value.fieldValues.size(); i++) {
-			VerifyField fld = (VerifyField) value.fieldValues.get(i);
-			Map<String, Object> st = new HashMap<String, Object>();
-			st.put("__fact__", fact);
-			if (fld.expected != null) {
-				Object expectedVal = fld.expected.trim();
-				if (fld.expected.startsWith("=")) {
-					expectedVal = eval(fld.expected.substring(1), this.populatedData);
-				}
-				st.put("__expected__", expectedVal);
-				
-				
-				ParserContext ctx = new ParserContext();
-                for ( Map.Entry<String, Object> entry : st.entrySet() ) {
-                    ctx.addInput( entry.getKey(),
-                                  entry.getValue().getClass() );
+    Object populateFields(FactData fact,
+                          Map<String, Object> factData,
+                          Object factObject) {
+        for ( int i = 0; i < fact.fieldData.size(); i++ ) {
+            FieldData field = (FieldData) fact.fieldData.get( i );
+            Object val;
+            if ( field.value != null && !field.value.equals( "" ) ) {
+                if ( field.value.startsWith( "=" ) ) {
+                    // eval the val into existence
+                    val = eval( field.value.substring( 1 ),
+                                factData );
+                } else {
+                    val = field.value;
                 }
-                CompiledExpression expr = new ExpressionCompiler( "__fact__." + fld.fieldName
-                                                                  + " " + fld.operator  + " __expected__" ).compile( ctx );
+                Map<String, Object> vars = new HashMap<String, Object>();
+                vars.putAll( factData );
+                vars.put( "__val__",
+                          val );
+                vars.put( "__fact__",
+                          factObject );
+                eval( "__fact__." + field.name + " = __val__",
+                      vars );
+            }
+        }
+        return factObject;
+    }
 
-                fld.successResult = (Boolean) MVEL.executeExpression( expr,
-                                                                      st );
-				
-//				fld.successResult = (Boolean) eval("__fact__." + fld.fieldName
-//						+ " " + fld.operator  + " __expected__", st);
+    /**
+     * True if the scenario was run with 100% success.
+     */
+    public boolean wasSuccess() {
+        return this.scenario.wasSuccessful();
+    }
 
+    /**
+     * @return A pretty printed report detailing any failures that occured
+     * when running the scenario (unmet expectations).
+     */
+    public String getReport() {
+        return this.scenario.printFailureReport();
+    }
 
-				if (!fld.successResult) {
-//					Object actual = eval("__fact__." + fld.fieldName, st);
-
-				    Object actual = MVEL.executeExpression( new ExpressionCompiler( "__fact__." + fld.fieldName ).compile( ctx ),
-                                                            st );
-					
-					
-					fld.actualResult = (actual != null) ? actual.toString() : "";
-
-					if (fld.operator.equals("==")) {
-						fld.explanation = "[" + value.name + "] field [" + fld.fieldName + "] was [" + fld.actualResult
-											+ "] expected [" + fld.expected + "].";
-					} else {
-						fld.explanation = "[" + value.name + "] field [" + fld.fieldName + "] was not expected to be [" + fld.actualResult
-						+ "].";
-					}
-				} else {
-					if (fld.operator.equals("==")) {
-						fld.explanation = "[" + value.name + "] field [" + fld.fieldName + "] was [" + fld.expected + "].";
-					} else if (fld.operator.equals("!=")){
-						fld.explanation = "[" + value.name + "] field [" + fld.fieldName + "] was not [" + fld.expected + "].";
-					}
-				}
-			}
-
-		}
-	}
-
-
-
-	Object populateFields(FactData fact, Map<String, Object> factData, Object factObject) {
-		for (int i = 0; i < fact.fieldData.size(); i++) {
-			FieldData field = (FieldData) fact.fieldData.get(i);
-			Object val;
-			if (field.value != null && !field.value.equals("")) {
-				if (field.value.startsWith("=")) {
-					// eval the val into existence
-					val = eval(field.value.substring(1), factData);
-				} else {
-					val = field.value;
-				}
-				Map<String, Object> vars = new HashMap<String, Object>();
-				vars.putAll(factData);
-				vars.put("__val__", val);
-				vars.put("__fact__", factObject);
-				eval("__fact__." + field.name + " = __val__", vars);
-			}
-		}
-		return factObject;
-	}
-
-	/**
-	 * True if the scenario was run with 100% success.
-	 */
-	public boolean wasSuccess() {
-		return this.scenario.wasSuccessful();
-	}
-
-	/**
-	 * @return A pretty printed report detailing any failures that occured
-	 * when running the scenario (unmet expectations).
-	 */
-	public String getReport() {
-		return this.scenario.printFailureReport();
-	}
-
-
 }
-



More information about the jboss-svn-commits mailing list