[jboss-svn-commits] JBL Code SVN: r23525 - in labs/jbossrules/trunk: drools-compiler/src/test/java/org/drools/testframework and 1 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Oct 20 18:32:35 EDT 2008


Author: Rikkola
Date: 2008-10-20 18:32:35 -0400 (Mon, 20 Oct 2008)
New Revision: 23525

Modified:
   labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/guvnor/client/modeldriven/testing/Scenario.java
   labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/testframework/ScenarioTest.java
   labs/jbossrules/trunk/drools-guvnor/src/main/java/org/drools/guvnor/client/qa/ScenarioWidget.java
Log:
GUVNOR-28: Scenerio Manager: Unable to Remove Expectation

Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/guvnor/client/modeldriven/testing/Scenario.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/guvnor/client/modeldriven/testing/Scenario.java	2008-10-20 21:13:26 UTC (rev 23524)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/guvnor/client/modeldriven/testing/Scenario.java	2008-10-20 22:32:35 UTC (rev 23525)
@@ -1,6 +1,7 @@
 package org.drools.guvnor.client.modeldriven.testing;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -15,249 +16,292 @@
  *
  * @author Michael Neale
  */
-public class Scenario implements PortableObject {
+public class Scenario
+    implements
+    PortableObject {
 
-	/**
-	 * The maximum number of rules to fire so we don't recurse for ever.
-	 */
-	public int		maxRuleFirings = 100000;
+    /**
+     * The maximum number of rules to fire so we don't recurse for ever.
+     */
+    public int            maxRuleFirings = 100000;
 
-	/**
-	 * global data which must be setup before hand.
-	 */
-	public List<FactData> globals = new ArrayList<FactData>();
+    /**
+     * global data which must be setup before hand.
+     */
+    public List<FactData> globals        = new ArrayList<FactData>();
 
-	/**
-	 * Fixtures are parts of the test. They may be assertions, globals, data, execution runs etc.
-	 * Anything really.
-	 *
-	 */
-	public List<Fixture> fixtures = new ArrayList<Fixture>();
+    /**
+     * Fixtures are parts of the test. They may be assertions, globals, data, execution runs etc.
+     * Anything really.
+     *
+     */
+    public List<Fixture>  fixtures       = new ArrayList<Fixture>();
 
-	/**
-	 * This is the date the last time the scenario was run (and what the results apply to).
-	 */
-	public Date lastRunResult;
+    /**
+     * This is the date the last time the scenario was run (and what the results apply to).
+     */
+    public Date           lastRunResult;
 
-	/**
-	 * the rules to include or exclude
-	 */
-	public List<String> rules = new ArrayList<String>();
+    /**
+     * the rules to include or exclude
+     */
+    public List<String>   rules          = new ArrayList<String>();
 
-	/**
-	 * true if only the rules in the list should be allowed to fire. Otherwise
-	 * it is exclusive (ie all rules can fire BUT the ones in the list).
-	 */
-	public boolean inclusive = false;
+    /**
+     * true if only the rules in the list should be allowed to fire. Otherwise
+     * it is exclusive (ie all rules can fire BUT the ones in the list).
+     */
+    public boolean        inclusive      = false;
 
+    /**
+     * Returns true if this was a totally successful scenario, based on the results contained.
+     */
+    public boolean wasSuccessful() {
+        for ( Iterator iterator = fixtures.iterator(); iterator.hasNext(); ) {
+            Fixture f = (Fixture) iterator.next();
+            if ( f instanceof Expectation ) {
+                if ( !((Expectation) f).wasSuccessful() ) {
+                    return false;
+                }
+            }
 
+        }
+        return true;
+    }
 
-	/**
-	 * Returns true if this was a totally successful scenario, based on the results contained.
-	 */
-	public boolean wasSuccessful() {
-		for (Iterator iterator = fixtures.iterator(); iterator.hasNext();) {
-			Fixture f= (Fixture) iterator.next();
-			if (f instanceof Expectation) {
-				if (! ((Expectation) f ).wasSuccessful()) {
-					return false;
-				}
-			}
+    /**
+     * Will slip in a fixture after the specified one, but before the next execution trace.
+     */
+    public void insertBetween(Fixture fix,
+                              Fixture toAdd) {
 
-		}
-		return true;
-	}
+        boolean inserted = false;
+        int start = (fix == null) ? 0 : fixtures.indexOf( fix ) + 1;
+        for ( int j = start; j < fixtures.size(); j++ ) {
+            Fixture f = (Fixture) fixtures.get( j );
+            if ( f instanceof ExecutionTrace ) {
+                fixtures.add( j,
+                              toAdd );
+                return;
+            }
+        }
 
-	/**
-	 * Will slip in a fixture after the specified one, but before the next execution trace.
-	 */
-	public void insertBetween(Fixture fix, Fixture toAdd) {
+        if ( !inserted ) {
+            //fixtures.add( fixtures.indexOf(fix) + 1, toAdd);
+            fixtures.add( toAdd );
+        }
+    }
 
-			boolean inserted = false;
-			int start = (fix == null) ? 0 : fixtures.indexOf(fix) + 1;
-			for (int j = start; j < fixtures.size(); j++) {
-				Fixture f = (Fixture) fixtures.get(j);
-				if (f instanceof ExecutionTrace) {
-					fixtures.add(j, toAdd);
-					return;
-				}
-			}
+    /**
+     * Remove the specified fixture.
+     */
+    public void removeFixture(Fixture f) {
+        this.fixtures.remove( f );
+        this.globals.remove( f );
+    }
 
-			if (!inserted) {
-			  //fixtures.add( fixtures.indexOf(fix) + 1, toAdd);
-				fixtures.add(toAdd);
-			}
-	}
+    /**
+     * Remove fixtures between this ExecutionTrace and the previous one.
+     */
+    public void removeExecutionTrace(ExecutionTrace et) {
 
-	/**
-	 * Remove the specified fixture.
-	 */
-	public void removeFixture(Fixture f) {
-		this.fixtures.remove(f);
-		this.globals.remove(f);
-	}
+        boolean remove = false;
+        for ( Iterator<Fixture> iterator = fixtures.iterator(); iterator.hasNext(); ) {
+            Fixture f = iterator.next();
 
+            if ( f.equals( et ) ) {
+                remove = true;
+                continue;
+            } else if ( remove && (f instanceof ExecutionTrace || (f instanceof FactData)) ) {
+                break;
+            }
 
-	/**
-	 *
-	 * @return A mapping of variable names to their fact type.
-	 */
-	public Map getVariableTypes() {
-		Map m = new HashMap();
-		for (Iterator iterator = fixtures.iterator(); iterator.hasNext();) {
-			Fixture f = (Fixture) iterator.next();
-			if (f instanceof FactData) {
-				FactData fd = (FactData)f;
-				m.put(fd.name, fd.type);
-			}
-		}
-		for (Iterator iterator = globals.iterator(); iterator.hasNext();) {
-			FactData fd = (FactData) iterator.next();
-			m.put(fd.name, fd.type);
-		}
-		return m;
-	}
+            if ( remove ) {
+                iterator.remove();
+                this.globals.remove( f );
+            }
+        }
 
-	/**
-	 * This will return a list of fact names that are in scope (including globals).
-	 * @return List<String>
-	 */
-	public List getFactNamesInScope(ExecutionTrace ex, boolean includeGlobals) {
-		if (ex == null) return new ArrayList();
-		List l = new ArrayList();
-		int p = this.fixtures.indexOf(ex);
-		for (int i = 0; i < p; i++) {
-			Fixture f = (Fixture) fixtures.get(i);
-			if (f instanceof FactData) {
-				FactData fd = (FactData) f;
-				l.add(fd.name);
-			} else if (f instanceof RetractFact) {
-				RetractFact rf = (RetractFact) f;
-				l.remove(rf.name);
-			}
-		}
+        Collections.reverse( fixtures );
 
-		if (includeGlobals) {
-			for (Iterator iterator = globals.iterator(); iterator.hasNext();) {
-				FactData f = (FactData) iterator.next();
-				l.add(f.name);
-			}
-		}
-		return l;
-	}
+        remove = false;
+        for ( Iterator<Fixture> iterator = fixtures.iterator(); iterator.hasNext(); ) {
+            Fixture f = iterator.next();
 
-	/**
-	 * @return true iff a fact name is already in use.
-	 */
-	public boolean isFactNameExisting(String factName) {
-		for (Iterator iterator = globals.iterator(); iterator.hasNext();) {
-			FactData fd = (FactData) iterator.next();
-			if (fd.name.equals(factName)) {
-				return true;
-			}
-		}
-		for (Iterator iterator = fixtures.iterator(); iterator.hasNext();) {
-			Fixture f = (Fixture) iterator.next();
-			if (f instanceof FactData) {
-				FactData fd = (FactData) f;
-				if (fd.name.equals(factName)) {
-					return true;
-				}
-			}
-		}
-		return false;
-	}
+            // Catch the first or next ExecutionTrace.
+            if ( f.equals( et ) ) {
+                remove = true;
+            } else if ( remove && (f instanceof ExecutionTrace || (f instanceof VerifyFact)) ) {
+                break;
+            }
 
-	/**
-	 * @return true iff a fact is actually used (ie if its not, its safe to remove it).
-	 */
-	public boolean isFactNameUsed(FactData fd) {
-		int start = this.fixtures.indexOf(fd);
-		for (int i = start + 1; i < fixtures.size(); i++) {
-			Fixture f = (Fixture) fixtures.get(i);
-			if (f instanceof RetractFact) {
-				 if (((RetractFact)f).name.equals(fd.name)) {
-					 return true;
-				 }
-			} else if (f instanceof VerifyFact) {
-				 if (((VerifyFact)f).name.equals(fd.name)) {
-					 return true;
-				 }
-			} else if (f instanceof FactData) {
-				 if (((FactData)f).name.equals(fd.name)) {
-					 return true;
-				 }
-			}
-		}
-		return false;
-	}
+            if ( remove ) {
+                iterator.remove();
+                this.globals.remove( f );
+            }
+        }
 
+        Collections.reverse( fixtures );
+    }
 
-	/**
-	 *
-	 * @return int[0] = failures, int[1] = total;
-	 */
-	public int[] countFailuresTotal() {
-		int total = 0;
-		int failures = 0;
-		for (Iterator iterator = fixtures.iterator(); iterator.hasNext();) {
-			Fixture f = (Fixture) iterator.next();
-			if (f instanceof VerifyRuleFired) {
-				total++;
-				VerifyRuleFired vr = (VerifyRuleFired) f;
-				if (vr.successResult != null && !vr.successResult.booleanValue()) {
-					failures++;
-				}
-			} else if (f instanceof VerifyFact) {
-				VerifyFact vf = (VerifyFact) f;
-				for (Iterator it = vf.fieldValues.iterator(); it
-						.hasNext();) {
-					VerifyField vfl = (VerifyField) it.next();
-					if (vfl.successResult != null && !vfl.successResult.booleanValue()) {
-						failures++;
-					}
-					total++;
-				}
-			}
-		}
-		return new int[] {failures, total};
-	}
+    /**
+     *
+     * @return A mapping of variable names to their fact type.
+     */
+    public Map getVariableTypes() {
+        Map m = new HashMap();
+        for ( Iterator iterator = fixtures.iterator(); iterator.hasNext(); ) {
+            Fixture f = (Fixture) iterator.next();
+            if ( f instanceof FactData ) {
+                FactData fd = (FactData) f;
+                m.put( fd.name,
+                       fd.type );
+            }
+        }
+        for ( Iterator iterator = globals.iterator(); iterator.hasNext(); ) {
+            FactData fd = (FactData) iterator.next();
+            m.put( fd.name,
+                   fd.type );
+        }
+        return m;
+    }
 
-	public String printFailureReport() {
-		int total = 0;
-		int failures = 0;
-		StringBuffer buf = new StringBuffer();
-		buf.append("------- Unmet expectations: -------\n");
-		for (Iterator iterator = fixtures.iterator(); iterator.hasNext();) {
-			Fixture f = (Fixture) iterator.next();
-			if (f instanceof VerifyRuleFired) {
-				total++;
-				VerifyRuleFired vr = (VerifyRuleFired) f;
-				if (vr.successResult != null && !vr.successResult.booleanValue()) {
-					failures++;
-					buf.append(vr.explanation);
-					buf.append('\n');
-				}
-			} else if (f instanceof VerifyFact) {
-				VerifyFact vf = (VerifyFact) f;
-				for (Iterator it = vf.fieldValues.iterator(); it
-						.hasNext();) {
-					VerifyField vfl = (VerifyField) it.next();
-					if (vfl.successResult != null && !vfl.successResult.booleanValue()) {
-						failures++;
-						buf.append(vfl.explanation);
-						buf.append('\n');
+    /**
+     * This will return a list of fact names that are in scope (including globals).
+     * @return List<String>
+     */
+    public List getFactNamesInScope(ExecutionTrace ex,
+                                    boolean includeGlobals) {
+        if ( ex == null ) return new ArrayList();
+        List l = new ArrayList();
+        int p = this.fixtures.indexOf( ex );
+        for ( int i = 0; i < p; i++ ) {
+            Fixture f = (Fixture) fixtures.get( i );
+            if ( f instanceof FactData ) {
+                FactData fd = (FactData) f;
+                l.add( fd.name );
+            } else if ( f instanceof RetractFact ) {
+                RetractFact rf = (RetractFact) f;
+                l.remove( rf.name );
+            }
+        }
 
-					}
-					total++;
-				}
-			}
-		}
-		buf.append("\n------- Summary: ------\n");
-		buf.append(failures + " failures out of " + total + " expectations.");
-		return buf.toString();
-	}
+        if ( includeGlobals ) {
+            for ( Iterator iterator = globals.iterator(); iterator.hasNext(); ) {
+                FactData f = (FactData) iterator.next();
+                l.add( f.name );
+            }
+        }
+        return l;
+    }
 
-}
+    /**
+     * @return true iff a fact name is already in use.
+     */
+    public boolean isFactNameExisting(String factName) {
+        for ( Iterator iterator = globals.iterator(); iterator.hasNext(); ) {
+            FactData fd = (FactData) iterator.next();
+            if ( fd.name.equals( factName ) ) {
+                return true;
+            }
+        }
+        for ( Iterator iterator = fixtures.iterator(); iterator.hasNext(); ) {
+            Fixture f = (Fixture) iterator.next();
+            if ( f instanceof FactData ) {
+                FactData fd = (FactData) f;
+                if ( fd.name.equals( factName ) ) {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
 
+    /**
+     * @return true iff a fact is actually used (ie if its not, its safe to remove it).
+     */
+    public boolean isFactNameUsed(FactData fd) {
+        int start = this.fixtures.indexOf( fd );
+        for ( int i = start + 1; i < fixtures.size(); i++ ) {
+            Fixture f = (Fixture) fixtures.get( i );
+            if ( f instanceof RetractFact ) {
+                if ( ((RetractFact) f).name.equals( fd.name ) ) {
+                    return true;
+                }
+            } else if ( f instanceof VerifyFact ) {
+                if ( ((VerifyFact) f).name.equals( fd.name ) ) {
+                    return true;
+                }
+            } else if ( f instanceof FactData ) {
+                if ( ((FactData) f).name.equals( fd.name ) ) {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
 
+    /**
+     *
+     * @return int[0] = failures, int[1] = total;
+     */
+    public int[] countFailuresTotal() {
+        int total = 0;
+        int failures = 0;
+        for ( Iterator iterator = fixtures.iterator(); iterator.hasNext(); ) {
+            Fixture f = (Fixture) iterator.next();
+            if ( f instanceof VerifyRuleFired ) {
+                total++;
+                VerifyRuleFired vr = (VerifyRuleFired) f;
+                if ( vr.successResult != null && !vr.successResult.booleanValue() ) {
+                    failures++;
+                }
+            } else if ( f instanceof VerifyFact ) {
+                VerifyFact vf = (VerifyFact) f;
+                for ( Iterator it = vf.fieldValues.iterator(); it.hasNext(); ) {
+                    VerifyField vfl = (VerifyField) it.next();
+                    if ( vfl.successResult != null && !vfl.successResult.booleanValue() ) {
+                        failures++;
+                    }
+                    total++;
+                }
+            }
+        }
+        return new int[]{failures, total};
+    }
+
+    public String printFailureReport() {
+        int total = 0;
+        int failures = 0;
+        StringBuffer buf = new StringBuffer();
+        buf.append( "------- Unmet expectations: -------\n" );
+        for ( Iterator iterator = fixtures.iterator(); iterator.hasNext(); ) {
+            Fixture f = (Fixture) iterator.next();
+            if ( f instanceof VerifyRuleFired ) {
+                total++;
+                VerifyRuleFired vr = (VerifyRuleFired) f;
+                if ( vr.successResult != null && !vr.successResult.booleanValue() ) {
+                    failures++;
+                    buf.append( vr.explanation );
+                    buf.append( '\n' );
+                }
+            } else if ( f instanceof VerifyFact ) {
+                VerifyFact vf = (VerifyFact) f;
+                for ( Iterator it = vf.fieldValues.iterator(); it.hasNext(); ) {
+                    VerifyField vfl = (VerifyField) it.next();
+                    if ( vfl.successResult != null && !vfl.successResult.booleanValue() ) {
+                        failures++;
+                        buf.append( vfl.explanation );
+                        buf.append( '\n' );
+
+                    }
+                    total++;
+                }
+            }
+        }
+        buf.append( "\n------- Summary: ------\n" );
+        buf.append( failures + " failures out of " + total + " expectations." );
+        return buf.toString();
+    }
+
+}

Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/testframework/ScenarioTest.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/testframework/ScenarioTest.java	2008-10-20 21:13:26 UTC (rev 23524)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/testframework/ScenarioTest.java	2008-10-20 22:32:35 UTC (rev 23525)
@@ -108,6 +108,40 @@
 
 
 
+	public void testExecutionTrace() {
+        Scenario sc = new Scenario();
+
+        sc.globals.add(new FactData("A", "A", new ArrayList(), false));
+        sc.fixtures.add(new FactData("B", "B", new ArrayList(), true));
+        sc.fixtures.add(new FactData("C", "C", new ArrayList(), true));
+        ExecutionTrace ex1 = new ExecutionTrace();
+        sc.fixtures.add(ex1);
+        sc.fixtures.add(new VerifyFact());
+        sc.fixtures.add(new RetractFact());
+        sc.fixtures.add(new FactData("D", "D", new ArrayList(), false));
+        sc.fixtures.add(new FactData("E", "E", new ArrayList(), false));
+        ExecutionTrace ex2 = new ExecutionTrace();
+        sc.fixtures.add(ex2);
+        sc.fixtures.add(new VerifyFact());
+        sc.fixtures.add(new FactData("F", "F", new ArrayList(), false));
+        ExecutionTrace ex3 = new ExecutionTrace();
+        sc.fixtures.add(ex3);
+
+        assertEquals( 11,
+                      sc.fixtures.size() );
+
+        sc.removeExecutionTrace( ex2 );
+
+        assertEquals( 6,
+                      sc.fixtures.size() );
+        assertTrue( sc.isFactNameExisting( "A" ) );
+        assertTrue( sc.isFactNameExisting( "B" ) );
+        assertTrue( sc.isFactNameExisting( "C" ) );
+        assertFalse( sc.isFactNameExisting( "D" ) );
+        assertFalse( sc.isFactNameExisting( "E" ) );
+        assertTrue( sc.isFactNameExisting( "F" ) );
+	}
+
 	public void testRemoveFixture() {
 		Scenario sc = new Scenario();
 

Modified: labs/jbossrules/trunk/drools-guvnor/src/main/java/org/drools/guvnor/client/qa/ScenarioWidget.java
===================================================================
--- labs/jbossrules/trunk/drools-guvnor/src/main/java/org/drools/guvnor/client/qa/ScenarioWidget.java	2008-10-20 21:13:26 UTC (rev 23524)
+++ labs/jbossrules/trunk/drools-guvnor/src/main/java/org/drools/guvnor/client/qa/ScenarioWidget.java	2008-10-20 22:32:35 UTC (rev 23525)
@@ -126,11 +126,11 @@
 		ScenarioHelper hlp = new ScenarioHelper();
 		List fixtures = hlp.lumpyMap(scenario.fixtures);
 
-
+		
         int layoutRow = 1;
         ExecutionTrace previousEx = null;
         for (int i = 0; i < fixtures.size(); i++) {
-			Object f = fixtures.get(i);
+			final Object f = fixtures.get(i);
 			if (f instanceof ExecutionTrace) {
 				previousEx = (ExecutionTrace) f;
 				HorizontalPanel h = new HorizontalPanel();
@@ -138,7 +138,17 @@
 				h.add(new SmallLabel("EXPECT"));
 				editorLayout.setWidget(layoutRow, 0, h);
 
-
+                final ExecutionTrace et = (ExecutionTrace) previousEx;
+                Image del = new ImageButton("images/delete_item_small.gif", "Delete item.", new ClickListener() {
+                    public void onClick(Widget w) {
+                         if ( Window.confirm( "Are you sure you want to remove this item?" ) ) {
+                             scenario.removeExecutionTrace( et );
+                             renderEditor();
+                         }
+                    }
+                });
+                h.add(del);
+				
 				editorLayout.setWidget(layoutRow, 1, new ExecutionWidget(previousEx, showResults));
 				//layout.setWidget(layoutRow, 2, getNewExpectationButton(previousEx, scenario, availableRules));
 				editorLayout.getFlexCellFormatter().setHorizontalAlignment(layoutRow, 2, HasHorizontalAlignment.ALIGN_LEFT);
@@ -148,6 +158,7 @@
 				h.add(getNewDataButton(previousEx, scenario));
 				h.add(new SmallLabel("GIVEN"));
 
+                
 				editorLayout.setWidget(layoutRow, 0, h);
 
 				layoutRow++;
@@ -719,7 +730,7 @@
                     t.setWidget(idx, 0, new SmallLabel(fd.name + ":"));
                     Image del = new ImageButton("images/delete_item_small.gif", "Remove this row.", new ClickListener() {
         				public void onClick(Widget w) {
-        					if (Window.confirm("Are you sure you want to remove this row ?")) {
+        					if (Window.confirm("Are you sure you want to remove this row?")) {
         						ScenarioHelper.removeFields(defList, fd.name);
         						outer.setWidget(1, 0, render(defList));
 




More information about the jboss-svn-commits mailing list