[jboss-svn-commits] JBL Code SVN: r12199 - in labs/jbossrules/trunk/experimental/drools-testing/src/org/drools/testing/core: model and 3 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue May 29 04:14:40 EDT 2007


Author: mshaw
Date: 2007-05-29 04:14:40 -0400 (Tue, 29 May 2007)
New Revision: 12199

Modified:
   labs/jbossrules/trunk/experimental/drools-testing/src/org/drools/testing/core/engine/TestRunner.java
   labs/jbossrules/trunk/experimental/drools-testing/src/org/drools/testing/core/model/Assertion.java
   labs/jbossrules/trunk/experimental/drools-testing/src/org/drools/testing/core/model/Fact.java
   labs/jbossrules/trunk/experimental/drools-testing/src/org/drools/testing/core/model/Outcome.java
   labs/jbossrules/trunk/experimental/drools-testing/src/org/drools/testing/core/model/Scenario.java
   labs/jbossrules/trunk/experimental/drools-testing/src/org/drools/testing/core/resources/drl/test.drl
   labs/jbossrules/trunk/experimental/drools-testing/src/org/drools/testing/core/test/APITester.java
   labs/jbossrules/trunk/experimental/drools-testing/src/org/drools/testing/core/utils/ObjectUtils.java
Log:


Modified: labs/jbossrules/trunk/experimental/drools-testing/src/org/drools/testing/core/engine/TestRunner.java
===================================================================
--- labs/jbossrules/trunk/experimental/drools-testing/src/org/drools/testing/core/engine/TestRunner.java	2007-05-29 07:42:55 UTC (rev 12198)
+++ labs/jbossrules/trunk/experimental/drools-testing/src/org/drools/testing/core/engine/TestRunner.java	2007-05-29 08:14:40 UTC (rev 12199)
@@ -4,6 +4,7 @@
 import java.util.Collection;
 import java.util.Iterator;
 
+import org.apache.commons.beanutils.ConvertUtils;
 import org.apache.commons.beanutils.PropertyUtils;
 import org.drools.WorkingMemory;
 import org.drools.testing.core.exception.RuleTestLanguageException;
@@ -104,27 +105,33 @@
 		Iterator i  = facts.iterator();
 		while (i.hasNext()) {
 			Fact factDefn = (Fact) i.next();
-			Class classDefn = ObjectUtils.getClassDefn(factDefn.getType(), pkg.getImports(),null);
+			Class classDefn; 
 			Object fact;
-			try {
-				fact = classDefn.newInstance(); 
-			}catch (Exception e) {
-				throw new RuleTestServiceUnavailableException("Exception ocurred",e);
-			}
-			
-			// get the fields to set from the fact definition
-			Field[] fields = factDefn.getFields();
-			for (int j=0; j<fields.length; j++) {
-				Field field = fields[j];
-				// set the property on our newly instantiated fact bean
+			if (factDefn != null) {
 				try {
-					PropertyUtils.setProperty(fact, field.getName(), field.getValue());
+					classDefn = ObjectUtils.getClassDefn(factDefn.getType());
+					fact = classDefn.newInstance(); 
 				}catch (Exception e) {
 					throw new RuleTestServiceUnavailableException("Exception ocurred",e);
 				}
+			
+			
+				// get the fields to set from the fact definition
+				Iterator j = factDefn.getFields().iterator();
+				while (j.hasNext()) {
+					Field field = (Field) j.next();
+					// set the property on our newly instantiated fact bean
+					try {
+						Object value = ConvertUtils.convert(field.getValue(), 
+								ObjectUtils.getClassDefn(field.getType()));
+						PropertyUtils.setProperty(fact, field.getName(), value);
+					}catch (Exception e) {
+						throw new RuleTestServiceUnavailableException("Exception ocurred",e);
+					}
+				}
+				// assert the fact into working memory
+				wm.assertObject(fact);
 			}
-			// assert the fact into working memory
-			wm.assertObject(fact);
 		}
 	}
 	

Modified: labs/jbossrules/trunk/experimental/drools-testing/src/org/drools/testing/core/model/Assertion.java
===================================================================
--- labs/jbossrules/trunk/experimental/drools-testing/src/org/drools/testing/core/model/Assertion.java	2007-05-29 07:42:55 UTC (rev 12198)
+++ labs/jbossrules/trunk/experimental/drools-testing/src/org/drools/testing/core/model/Assertion.java	2007-05-29 08:14:40 UTC (rev 12199)
@@ -9,6 +9,10 @@
 public class Assertion {
 
 	private String name;
+	private String beanName;
+	private String propertyName;
+	private String propertyValue;
+	private String expectedValue;
 	private boolean success;
 	
 	public Assertion () {}
@@ -25,4 +29,36 @@
 	public void setSuccess(boolean success) {
 		this.success = success;
 	}
+
+	public String getBeanName() {
+		return beanName;
+	}
+
+	public void setBeanName(String beanName) {
+		this.beanName = beanName;
+	}
+
+	public String getExpectedValue() {
+		return expectedValue;
+	}
+
+	public void setExpectedValue(String expectedValue) {
+		this.expectedValue = expectedValue;
+	}
+
+	public String getPropertyName() {
+		return propertyName;
+	}
+
+	public void setPropertyName(String propertyName) {
+		this.propertyName = propertyName;
+	}
+
+	public String getPropertyValue() {
+		return propertyValue;
+	}
+
+	public void setPropertyValue(String propertyValue) {
+		this.propertyValue = propertyValue;
+	}
 }

Modified: labs/jbossrules/trunk/experimental/drools-testing/src/org/drools/testing/core/model/Fact.java
===================================================================
--- labs/jbossrules/trunk/experimental/drools-testing/src/org/drools/testing/core/model/Fact.java	2007-05-29 07:42:55 UTC (rev 12198)
+++ labs/jbossrules/trunk/experimental/drools-testing/src/org/drools/testing/core/model/Fact.java	2007-05-29 08:14:40 UTC (rev 12199)
@@ -1,5 +1,8 @@
 package org.drools.testing.core.model;
 
+import java.util.ArrayList;
+import java.util.Collection;
+
 /**
  * 
  * @author Matt
@@ -13,16 +16,10 @@
 
 	private Integer id;
 	private String type;
-	private Field[] fields;
+	private Collection fields = new ArrayList();
 	
 	public Fact () {}
 	
-	public Field[] getFields() {
-		return fields;
-	}
-	public void setFields(Field[] fields) {
-		this.fields = fields;
-	}
 	public Integer getId() {
 		return id;
 	}
@@ -35,4 +32,12 @@
 	public void setType(String type) {
 		this.type = type;
 	}
+
+	public Collection getFields() {
+		return fields;
+	}
+
+	public void setFields(Collection fields) {
+		this.fields = fields;
+	}
 }

Modified: labs/jbossrules/trunk/experimental/drools-testing/src/org/drools/testing/core/model/Outcome.java
===================================================================
--- labs/jbossrules/trunk/experimental/drools-testing/src/org/drools/testing/core/model/Outcome.java	2007-05-29 07:42:55 UTC (rev 12198)
+++ labs/jbossrules/trunk/experimental/drools-testing/src/org/drools/testing/core/model/Outcome.java	2007-05-29 08:14:40 UTC (rev 12199)
@@ -1,5 +1,8 @@
 package org.drools.testing.core.model;
 
+import java.util.ArrayList;
+import java.util.Collection;
+
 /**
  * 
  * @author Matt
@@ -8,21 +11,26 @@
  */
 public class Outcome {
 
-	public Rule[] rules;
-	public Assertion[] assertions;
+	private Collection rules = new ArrayList();
+	public Collection assertions = new ArrayList();
 	
 	public Outcome () {}
-	
-	public Assertion[] getAssertions() {
+
+	public Collection getAssertions() {
 		return assertions;
 	}
-	public void setAssertions(Assertion[] assertions) {
+
+	public void setAssertions(Collection assertions) {
 		this.assertions = assertions;
 	}
-	public Rule[] getRules() {
+
+	public Collection getRules() {
 		return rules;
 	}
-	public void setRules(Rule[] rules) {
+
+	public void setRules(Collection rules) {
 		this.rules = rules;
 	}
+	
+	
 }

Modified: labs/jbossrules/trunk/experimental/drools-testing/src/org/drools/testing/core/model/Scenario.java
===================================================================
--- labs/jbossrules/trunk/experimental/drools-testing/src/org/drools/testing/core/model/Scenario.java	2007-05-29 07:42:55 UTC (rev 12198)
+++ labs/jbossrules/trunk/experimental/drools-testing/src/org/drools/testing/core/model/Scenario.java	2007-05-29 08:14:40 UTC (rev 12199)
@@ -1,5 +1,6 @@
 package org.drools.testing.core.model;
 
+import java.util.ArrayList;
 import java.util.Collection;
 
 /**
@@ -10,9 +11,9 @@
  */
 public class Scenario {
 
-	private Collection facts;
-	private Collection outcomes;
-	private Collection rules;
+	private Collection facts = new ArrayList();
+	private Collection outcomes = new ArrayList();
+	private Collection rules = new ArrayList();
 	
 	public Scenario () {}
 

Modified: labs/jbossrules/trunk/experimental/drools-testing/src/org/drools/testing/core/resources/drl/test.drl
===================================================================
--- labs/jbossrules/trunk/experimental/drools-testing/src/org/drools/testing/core/resources/drl/test.drl	2007-05-29 07:42:55 UTC (rev 12198)
+++ labs/jbossrules/trunk/experimental/drools-testing/src/org/drools/testing/core/resources/drl/test.drl	2007-05-29 08:14:40 UTC (rev 12199)
@@ -5,7 +5,6 @@
 rule rule1
 	when 
 		$acc : Account (status == "active", balance == 0)
-		$person : Person ( age < 60 )
 	then
 		$acc.setBalance(new Integer(10));
 		modify($acc);

Modified: labs/jbossrules/trunk/experimental/drools-testing/src/org/drools/testing/core/test/APITester.java
===================================================================
--- labs/jbossrules/trunk/experimental/drools-testing/src/org/drools/testing/core/test/APITester.java	2007-05-29 07:42:55 UTC (rev 12198)
+++ labs/jbossrules/trunk/experimental/drools-testing/src/org/drools/testing/core/test/APITester.java	2007-05-29 08:14:40 UTC (rev 12199)
@@ -1,26 +1,68 @@
 package org.drools.testing.core.test;
 
+import java.io.InputStreamReader;
+import java.lang.reflect.Field;
+import java.util.Calendar;
+import java.util.Iterator;
+import java.util.Random;
+
+import org.apache.commons.beanutils.BeanUtils;
+import org.drools.compiler.PackageBuilder;
+import org.drools.rule.Package;
+import org.drools.testing.core.engine.TestRunner;
+import org.drools.testing.core.model.Assertion;
+import org.drools.testing.core.model.Fact;
+import org.drools.testing.core.model.Outcome;
 import org.drools.testing.core.model.Rule;
 import org.drools.testing.core.model.Scenario;
 import org.drools.testing.core.model.TestSuite;
+import org.drools.testing.core.rules.model.Account;
+import org.drools.testing.core.utils.ObjectUtils;
 import org.drools.testing.core.utils.OperatorUtils;
 
 import junit.framework.TestCase;
 
+/**
+ * 
+ * @author Matt
+ *
+ * Make sure you have the drools libs on your classpath before running
+ * these tests!
+ *
+ * (c) Matt Shaw
+ */
 public class APITester extends TestCase {
 
-	TestSuite testSuite;
-	
-	public void testMockTestSuite () {
+	public void testMockTestSuite () throws Exception {
 		
-		testSuite = new TestSuite();
+		// get the package
+		PackageBuilder builder = new PackageBuilder();
+		builder.addPackageFromDrl(new InputStreamReader( getClass().getResourceAsStream( "/org/drools/testing/core/resources/drl/test.drl" ) ));
+		Package pkg = builder.getPackage();
 		
+		// generate test suite
+		TestSuite testSuite = new TestSuite();
+		testSuite.setName("Mock Test Suite");
+		testSuite.getScenarios().add(addScenario(pkg));
+		assertTrue(testSuite.getScenarios().size() > 0);
+		
+		// run the test
+		TestRunner testRunner = new TestRunner(testSuite);
+		testRunner.run(pkg);
 	}
 	
-	private void addScenario () {
-	
+	private Scenario addScenario (Package pkg) throws Exception {
 		Scenario scenario = new Scenario();
-		addRule("testrule1");
+		scenario.getRules().add(addRule("test1"));
+		scenario.getOutcomes().add(addOutcome());
+		
+		
+		Iterator i = pkg.getImports().iterator();
+		while (i.hasNext()) {
+			String importName = (String) i.next();
+			scenario.getFacts().add(addFact(importName));
+		}
+		return scenario;
 	}
 	
 	private Rule addRule (String name) {
@@ -30,4 +72,52 @@
 		rule.setFire(true);
 		return rule;
 	}
+	
+	private Outcome addOutcome () {
+		Outcome outcome = new Outcome();
+		outcome.getAssertions().add(addAssertion());
+		
+		return outcome;
+	}
+	
+	private Assertion addAssertion () {
+		Assertion assertion = new Assertion();
+		assertion.setName("assertion1");
+		assertion.setBeanName("Account");
+		assertion.setPropertyName("balance");
+		assertion.setExpectedValue("10");
+		return assertion;
+	}
+	
+	private Fact addFact (String importName) {
+		Fact fact = new Fact();
+		fact.setType(importName);
+		Random rand = new Random();
+		rand.setSeed(Calendar.getInstance().getTimeInMillis());
+		fact.setId(new Integer(rand.nextInt()));
+		Class classDefn;
+		Object object;
+		try {
+			classDefn = ObjectUtils.getClassDefn(importName);
+		}catch (Exception e) {
+			return null;
+		}
+		Field[] fields = classDefn.getDeclaredFields();
+		for (int i=0; i<fields.length; i++) {
+			Field fieldDefn = fields[i];
+			fact.getFields().add(addField(fieldDefn));
+		}
+		return fact;
+	}
+	
+	private org.drools.testing.core.model.Field addField (Field fieldDefn) {
+		org.drools.testing.core.model.Field field = new org.drools.testing.core.model.Field();
+		field.setName(fieldDefn.getName());
+		field.setType(fieldDefn.getType().getName());
+		if (field.getName().equalsIgnoreCase("status"))
+			field.setValue("active");
+		if (field.getName().equalsIgnoreCase("balance"))
+			field.setValue("0");
+		return field;
+	}
 }

Modified: labs/jbossrules/trunk/experimental/drools-testing/src/org/drools/testing/core/utils/ObjectUtils.java
===================================================================
--- labs/jbossrules/trunk/experimental/drools-testing/src/org/drools/testing/core/utils/ObjectUtils.java	2007-05-29 07:42:55 UTC (rev 12198)
+++ labs/jbossrules/trunk/experimental/drools-testing/src/org/drools/testing/core/utils/ObjectUtils.java	2007-05-29 08:14:40 UTC (rev 12199)
@@ -6,27 +6,20 @@
 
 public final class ObjectUtils {
 	
-	public static Object createObject(String className) {
+	public static Object createObject(String className) throws Exception {
 	      Object object = null;
-	      try {
-	    	  //Class classDefinition = Class.forName(className, false, ClassLoader.getSystemClassLoader());
+	        //Class classDefinition = Class.forName(className, false, ClassLoader.getSystemClassLoader());
 	    	  Class classDefinition = Class.forName(className);
 	          object = classDefinition.newInstance();
-	      } catch (Exception e) {
-	          System.out.println(e);
-	      }
+	      
 	      return object;
 	}
 	
-	public static Class getClassDefn (String className) {
+	public static Class getClassDefn (String className) throws ClassNotFoundException {
 		Class classDefinition = null;
-		try {
 			//classDefinition = Class.forName(className, false, ClassLoader.getSystemClassLoader());
-	        classDefinition = Class.forName(className);
-	      } catch (Exception e) {
-	          System.out.println(e);
-	      }
-	      return classDefinition;
+	    classDefinition = Class.forName(className);
+	    return classDefinition;
 	}
 	
 	public static Class getClassDefn (String className, List imports, ClassLoader classLoader) {




More information about the jboss-svn-commits mailing list