[jboss-svn-commits] JBL Code SVN: r17031 - in labs/jbossrules/trunk/drools-compiler/src: test/java/org/drools/integrationtests and 1 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Wed Dec 5 02:37:13 EST 2007
Author: michael.neale at jboss.com
Date: 2007-12-05 02:37:13 -0500 (Wed, 05 Dec 2007)
New Revision: 17031
Modified:
labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/testframework/ScenarioRunner.java
labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/PackageBuilderThreadSafetyTest.java
labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/testframework/ScenarioRunnerTest.java
Log:
JBRULES-1271 integrating with BRMS
Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/testframework/ScenarioRunner.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/testframework/ScenarioRunner.java 2007-12-05 06:22:36 UTC (rev 17030)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/testframework/ScenarioRunner.java 2007-12-05 07:37:13 UTC (rev 17031)
@@ -56,87 +56,94 @@
*
*/
public ScenarioRunner(final Scenario scenario, final TypeResolver resolver,
- final InternalWorkingMemory wm) throws ClassNotFoundException {
+ final InternalWorkingMemory wm, ClassLoader cl) throws ClassNotFoundException {
this.scenario = scenario;
this.workingMemory = wm;
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);
+ ClassLoader current = Thread.currentThread().getContextClassLoader();
- TestingEventListener listener = null;
-
- for (Iterator iterator = scenario.globals.iterator(); iterator.hasNext();) {
- FactData fact = (FactData) iterator.next();
- Object f = eval("new " + resolver.getFullTypeName(fact.type) + "()");
- populateFields(fact, globalData, f);
- globalData.put(fact.name, f);
- wm.setGlobal(fact.name, f);
+ if (cl != null) {
+ Thread.currentThread().setContextClassLoader(cl);
}
- for (Iterator<Fixture> iterator = scenario.fixtures.iterator(); iterator.hasNext();) {
- Fixture fx = iterator.next();
+ try {
+ //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);
- if (fx instanceof FactData) {
- //deal with facts and globals
- FactData fact = (FactData)fx;
- Object f = (fact.isModify)? this.populatedData.get(fact.name) : eval("new " + resolver.getFullTypeName(fact.type) + "()");
- if (fact.isModify) {
- if (!this.factHandles.containsKey(fact.name)) {
- throw new IllegalArgumentException("Was not a previously inserted fact. [" + fact.name + "]");
- }
- populateFields(fact, populatedData, f);
- this.workingMemory.update(this.factHandles.get(fact.name), f);
- } else /* a new one */ {
- populateFields(fact, populatedData, f);
- populatedData.put(fact.name, f);
- this.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 ExecutionTrace) {
- ExecutionTrace executionTrace = (ExecutionTrace)fx;
- //create the listener to trace rules
+ TestingEventListener listener = null;
- if (listener != null) wm.removeEventListener(listener); //remove the old
- listener = new TestingEventListener();
+ for (Iterator iterator = scenario.globals.iterator(); iterator.hasNext();) {
+ FactData fact = (FactData) iterator.next();
+ Object f = eval("new " + resolver.getFullTypeName(fact.type) + "()");
+ populateFields(fact, globalData, f);
+ globalData.put(fact.name, f);
+ wm.setGlobal(fact.name, f);
+ }
- wm.addEventListener(listener);
+ for (Iterator<Fixture> iterator = scenario.fixtures.iterator(); iterator.hasNext();) {
+ Fixture fx = iterator.next();
- //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;
-
- } else if (fx instanceof Expectation) {
- 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>());
+ if (fx instanceof FactData) {
+ //deal with facts and globals
+ FactData fact = (FactData)fx;
+ Object f = (fact.isModify)? this.populatedData.get(fact.name) : eval("new " + resolver.getFullTypeName(fact.type) + "()");
+ if (fact.isModify) {
+ if (!this.factHandles.containsKey(fact.name)) {
+ throw new IllegalArgumentException("Was not a previously inserted fact. [" + fact.name + "]");
+ }
+ populateFields(fact, populatedData, f);
+ this.workingMemory.update(this.factHandles.get(fact.name), f);
+ } else /* a new one */ {
+ populateFields(fact, populatedData, f);
+ populatedData.put(fact.name, f);
+ this.factHandles.put(fact.name, wm.insert(f));
}
- } else {
- throw new IllegalArgumentException("Not sure what to do with " + fx);
- }
+ } 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 ExecutionTrace) {
+ ExecutionTrace executionTrace = (ExecutionTrace)fx;
+ //create the listener to trace rules
+ if (listener != null) wm.removeEventListener(listener); //remove the old
+ listener = new TestingEventListener();
+ wm.addEventListener(listener);
- }
+ //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;
+ } else if (fx instanceof Expectation) {
+ 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);
+ }
+ }
+ } finally {
+ if (cl != null) {
+ Thread.currentThread().setContextClassLoader(current);
+ }
+ }
}
Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/PackageBuilderThreadSafetyTest.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/PackageBuilderThreadSafetyTest.java 2007-12-05 06:22:36 UTC (rev 17030)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/PackageBuilderThreadSafetyTest.java 2007-12-05 07:37:13 UTC (rev 17031)
@@ -1,6 +1,7 @@
package org.drools.integrationtests;
import java.util.ArrayList;
+import java.util.Iterator;
import java.util.List;
import junit.framework.TestCase;
@@ -24,7 +25,7 @@
}
public void testThreadSafetyEclipse() {
- execute( JavaDialectConfiguration.ECLIPSE );
+ execute( JavaDialectConfiguration.ECLIPSE );
}
public void testThreadSafetyJanino() {
@@ -33,8 +34,8 @@
public void execute(int compiler) {
final PackageBuilderConfiguration packageBuilderConfig = new PackageBuilderConfiguration();
- ((JavaDialectConfiguration) packageBuilderConfig.getDialectConfiguration( "java" )).setCompiler( compiler );
-
+ ((JavaDialectConfiguration) packageBuilderConfig.getDialectConfiguration( "java" )).setCompiler( compiler );
+
final List errors = new ArrayList();
final List exceptions = new ArrayList();
Thread[] threads = new Thread[_NUMBER_OF_THREADS];
@@ -54,8 +55,8 @@
PackageDescr packageDescr = new PackageDescr( "MyRulebase" );
addImports( packageDescr );
addFunctions( packageDescr );
- // added some arbitrary sleep statements to encourage
- // context switching and hope this provokes exceptions
+ // added some arbitrary sleep statements to encourage
+ // context switching and hope this provokes exceptions
sleep( _SLEEP_TIME_MS );
builder.addPackage( packageDescr );
sleep( _SLEEP_TIME_MS );
@@ -86,6 +87,12 @@
threads[i].interrupt();
}
}
+ if (!exceptions.isEmpty()) {
+ for (Iterator iterator = exceptions.iterator(); iterator.hasNext();) {
+ Exception name = (Exception) iterator.next();
+ System.err.println(name + name.getMessage());
+ }
+ }
assertTrue( "Exceptions during package compilation (number=" + exceptions.size() + ")",
exceptions.isEmpty() );
assertTrue( "PackageBuilderErrors during package compilation (number=" + errors.size() + ")",
Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/testframework/ScenarioRunnerTest.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/testframework/ScenarioRunnerTest.java 2007-12-05 06:22:36 UTC (rev 17030)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/testframework/ScenarioRunnerTest.java 2007-12-05 07:37:13 UTC (rev 17031)
@@ -43,7 +43,7 @@
resolver.addImport("org.drools.Cheese");
resolver.addImport("org.drools.Person");
ScenarioRunner runner = new ScenarioRunner(sc, resolver,
- new MockWorkingMemory());
+ new MockWorkingMemory(), null);
assertTrue(runner.populatedData.containsKey("c1"));
assertTrue(runner.populatedData.containsKey("p1"));
@@ -73,7 +73,7 @@
resolver.addImport("org.drools.Cheese");
ScenarioRunner runner = new ScenarioRunner(sc, resolver,
- new MockWorkingMemory());
+ new MockWorkingMemory(), null);
assertTrue(runner.populatedData.containsKey("c1"));
assertTrue(runner.populatedData.containsKey("c2"));
@@ -92,7 +92,7 @@
TypeResolver resolver = new ClassTypeResolver(new HashSet<Object>(),
Thread.currentThread().getContextClassLoader());
resolver.addImport("org.drools.Cheese");
- ScenarioRunner run = new ScenarioRunner(new Scenario(), resolver, new MockWorkingMemory());
+ ScenarioRunner run = new ScenarioRunner(new Scenario(), resolver, new MockWorkingMemory(), null);
run.populatedData.clear();
Cheese c = new Cheese();
c.setType("whee");
@@ -113,7 +113,7 @@
public void testVerifyFacts() throws Exception {
ScenarioRunner runner = new ScenarioRunner(new Scenario(), null,
- new MockWorkingMemory());
+ new MockWorkingMemory(), null);
Cheese f1 = new Cheese("cheddar", 42);
runner.populatedData.put("f1", f1);
@@ -176,7 +176,7 @@
public void testVerifyFactsWithOperator() throws Exception {
ScenarioRunner runner = new ScenarioRunner(new Scenario(), null,
- new MockWorkingMemory());
+ new MockWorkingMemory(), null);
Cheese f1 = new Cheese("cheddar", 42);
runner.populatedData.put("f1", f1);
@@ -219,7 +219,7 @@
resolver.addImport("org.drools.Cheese");
MockWorkingMemory wm = new MockWorkingMemory();
- ScenarioRunner runner = new ScenarioRunner(sc, resolver, wm);
+ ScenarioRunner runner = new ScenarioRunner(sc, resolver, wm, null);
assertEquals(1, wm.facts.size());
assertEquals(runner.populatedData.get("c1"), wm.facts.get(0));
@@ -239,7 +239,7 @@
// and baz, we leave out
ScenarioRunner runner = new ScenarioRunner(new Scenario(), null,
- new MockWorkingMemory());
+ new MockWorkingMemory(), null);
VerifyRuleFired v = new VerifyRuleFired();
v.ruleName = "foo";
v.expectedFire = true;
@@ -276,7 +276,7 @@
sc.fixtures.add(ext);
MockWorkingMemory wm = new MockWorkingMemory();
- ScenarioRunner run = new ScenarioRunner(sc, null, wm);
+ ScenarioRunner run = new ScenarioRunner(sc, null, wm, null);
assertEquals(wm, run.workingMemory);
assertNotNull(wm.agendaEventListener);
assertTrue(wm.agendaEventListener instanceof TestingEventListener);
@@ -300,7 +300,7 @@
resolver.addImport("org.drools.Cheese");
MockWorkingMemory wm = new MockWorkingMemory();
- ScenarioRunner run = new ScenarioRunner(sc, resolver, wm);
+ ScenarioRunner run = new ScenarioRunner(sc, resolver, wm, null);
assertEquals(1, wm.globals.size());
assertEquals(1, run.globalData.size());
assertEquals(1, run.populatedData.size());
@@ -318,7 +318,7 @@
public void testSimulatedDate() throws Exception {
Scenario sc = new Scenario();
MockWorkingMemory wm = new MockWorkingMemory();
- ScenarioRunner run = new ScenarioRunner(sc, null, wm);
+ ScenarioRunner run = new ScenarioRunner(sc, null, wm, null);
TimeMachine tm = run.workingMemory.getTimeMachine();
// love you
@@ -331,7 +331,7 @@
ExecutionTrace ext = new ExecutionTrace();
ext.scenarioSimulatedDate = new Date("10-Jul-1974");
sc.fixtures.add(ext);
- run = new ScenarioRunner(sc, null, wm);
+ run = new ScenarioRunner(sc, null, wm, null);
tm = run.workingMemory.getTimeMachine();
long expected = ext.scenarioSimulatedDate.getTime();
@@ -343,7 +343,7 @@
public void testVerifyRuleFired() throws Exception {
ScenarioRunner runner = new ScenarioRunner(new Scenario(), null,
- new MockWorkingMemory());
+ new MockWorkingMemory(), null);
VerifyRuleFired vr = new VerifyRuleFired("qqq", 42, null);
Map<String, Integer> f = new HashMap<String, Integer>();
@@ -431,7 +431,7 @@
WorkingMemory wm = getWorkingMemory("test_rules2.drl");
- ScenarioRunner run = new ScenarioRunner(sc, resolver, (InternalWorkingMemory) wm);
+ ScenarioRunner run = new ScenarioRunner(sc, resolver, (InternalWorkingMemory) wm, null);
assertEquals(3, executionTrace.numberOfRulesFired.intValue());
@@ -468,7 +468,7 @@
WorkingMemory wm = getWorkingMemory("test_stateful.drl");
- ScenarioRunner run = new ScenarioRunner(sc, resolver, (InternalWorkingMemory) wm);
+ ScenarioRunner run = new ScenarioRunner(sc, resolver, (InternalWorkingMemory) wm, null);
Cheese c1 = (Cheese) run.populatedData.get("c1");
Cheese c2 = (Cheese) run.populatedData.get("c2");
@@ -502,7 +502,7 @@
WorkingMemory wm = getWorkingMemory("test_stateful.drl");
- ScenarioRunner run = new ScenarioRunner(sc, resolver, (InternalWorkingMemory) wm);
+ ScenarioRunner run = new ScenarioRunner(sc, resolver, (InternalWorkingMemory) wm, null);
Cheese c1 = (Cheese) run.populatedData.get("c1");
@@ -534,7 +534,7 @@
WorkingMemory wm = getWorkingMemory("test_stateful.drl");
- ScenarioRunner run = new ScenarioRunner(sc, resolver, (InternalWorkingMemory) wm);
+ ScenarioRunner run = new ScenarioRunner(sc, resolver, (InternalWorkingMemory) wm, null);
Cheese c1 = (Cheese) run.populatedData.get("c1");
@@ -587,7 +587,7 @@
WorkingMemory wm = getWorkingMemory("test_rules2.drl");
- ScenarioRunner run = new ScenarioRunner(sc, resolver, (InternalWorkingMemory) wm);
+ ScenarioRunner run = new ScenarioRunner(sc, resolver, (InternalWorkingMemory) wm, null);
assertSame(run.scenario, sc);
More information about the jboss-svn-commits
mailing list