📄 smftestframework.java
字号:
package org.drools.smf;
/*
* $Id: SMFTestFrameWork.java,v 1.32 2005/07/30 16:37:34 brownj Exp $
*
* Copyright 2004 (C) The Werken Company. All Rights Reserved.
*
* Redistribution and use of this software and associated documentation
* ("Software"), with or without modification, are permitted provided that the
* following conditions are met:
*
* 1. Redistributions of source code must retain copyright statements and
* notices. Redistributions must also contain a copy of this document.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The name "drools" must not be used to endorse or promote products derived
* from this Software without prior written permission of The Werken Company.
* For written permission, please contact bob@werken.com.
*
* 4. Products derived from this Software may not be called "drools" nor may
* "drools" appear in their names without prior written permission of The Werken
* Company. "drools" is a trademark of The Werken Company.
*
* 5. Due credit should be given to The Werken Company. (http://werken.com/)
*
* THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS ``AS IS''
* AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE WERKEN COMPANY OR ITS CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import junit.framework.TestCase;
import org.drools.MockWorkingMemory;
import org.drools.WorkingMemory;
import org.drools.rule.Declaration;
import org.drools.rule.Rule;
import org.drools.rule.RuleSet;
import org.drools.spi.Condition;
import org.drools.spi.ConditionException;
import org.drools.spi.Consequence;
import org.drools.spi.ConsequenceException;
import org.drools.spi.Importer;
import org.drools.spi.MockTuple;
import org.drools.spi.ObjectType;
import org.drools.spi.RuleBaseContext;
import org.drools.spi.Tuple;
/**
* @author mproctor
*
* SMTTestFrameWork is a base class for unit testing Semantic Implementations
* The semantic implementation unit test simply needs to extend this class along
* with setup method that instructs SMFTEstFrameWork which semantic url to
* instantiate for testing. public class JavaSemanticTest extends
* SMFTestFrameWork { public JavaSemanticTest( String name ) { super( name ); }
*
* public void setUp() throws Exception { super.setUp("java"); } }
*
* Each class that extends SMFTestFrameWork must create 3 data files;
* conditions.data, consequences.data. Each file is read depending the testType,
* a List of the specified tests extracted from the file; usig the delimeter
* <!--drools-test--!>to seperate each test block.
*
* Each testType has a corresponding private helper method to instantiate a
* Condition, Consequence for each test using the specified parameters
*/
public abstract class SMFTestFrameWork extends TestCase
{
/** The List of tests extracted from the data file */
private List tests;
/** The test type; conditions, consequences */
private String testType;
/** The SemanticModule implementation return from the SemanticRepository */
private SemanticModule module;
/** The SemanticRepository */
private SemanticsRepository repository;
private String newline = System.getProperty( "line.separator" );
private Importer importer;
private RuleBaseContext ruleBaseContext;
public SMFTestFrameWork(String name)
{
super( name );
// use the method name, minus the "test" string, to specify the testType
this.testType = name.substring( 4 ).toLowerCase( );
}
/**
* Reads in the specified data file and extracts to a List of tests using
* the delimter <!--drools-test--!>
*/
public void setUp(String semantic,
Importer importer) throws Exception
{
if ( !"conditions".equals( testType ) && !"consequences".equals( testType ) )
{
return;
}
ClassLoader cl = Thread.currentThread( ).getContextClassLoader( );
URL semanticTests = cl.getResource( "data/" + semantic + "-" + testType + ".data" );
BufferedReader in = new BufferedReader( new InputStreamReader( semanticTests.openStream( ) ) );
StringBuffer buffer = new StringBuffer( );
String blockMarker = "<!--drools-test--!>";
String line;
tests = new ArrayList( );
while ( in.ready( ) )
{
line = in.readLine( ) + newline;
if ( line.startsWith( blockMarker ) )
{
tests.add( buffer.toString( ) );
buffer = new StringBuffer( );
}
else
{
buffer.append( line );
}
}
tests.add( buffer.toString( ) );
this.repository = DefaultSemanticsRepository.getInstance( );
module = this.repository.lookupSemanticModule( "http://drools.org/semantics/" + semantic );
this.importer = importer;
this.ruleBaseContext = new RuleBaseContext( );
}
/**
* Tests each of the extracted tests from conditions.data
*/
public void testConditions() throws Exception
{
// Setup
int testNumber = 0;
MockTuple tuple;
DefaultConfiguration cheeseConfiguration = new DefaultConfiguration( "test1" );
cheeseConfiguration.setText( Cheese.class.getName( ) );
ObjectTypeFactory objectTypeFactory = module.getObjectTypeFactory( "class" );
final RuleSet ruleSet = new RuleSet( "test RuleSet",
this.ruleBaseContext );
final Rule rule = new Rule( "Test Rule 1",
ruleSet );
rule.setImporter( new DefaultImporter( ) );
ObjectType cheeseType = objectTypeFactory.newObjectType( rule,
this.ruleBaseContext,
cheeseConfiguration );
tuple = new MockTuple( );
rule.setImporter( new DefaultImporter( ) );
tuple.setRule( rule );
tuple.setWorkingMemory( new MockWorkingMemory( ) );
// simple condition checks
assertTrue( testCondition( testNumber++,
tuple,
rule ) ); // 0
assertFalse( testCondition( testNumber++,
tuple,
rule ) ); // 1
assertTrue( testCondition( testNumber++,
tuple,
rule ) ); // 2
assertTrue( testCondition( testNumber++,
tuple,
rule ) ); // 3
assertTrue( testCondition( testNumber++,
tuple,
rule ) ); // 4
Declaration camembertDecl = rule.addParameterDeclaration( "camembert",
cheeseType );
Declaration stiltonDecl = rule.addParameterDeclaration( "stilton",
cheeseType );
rule.setImporter( this.importer );
// condition check with a single declaration
tuple.put( camembertDecl,
new Cheese( "camembert" ) );
assertTrue( testCondition( testNumber++,
tuple,
rule ) ); // 5
assertFalse( testCondition( testNumber++,
tuple,
rule ) ); // 6
// condition check with a single declaration
tuple = new MockTuple( );
rule.setImporter( this.importer );
tuple.setRule( rule );
tuple.setWorkingMemory( new MockWorkingMemory( ) );
tuple.put( stiltonDecl,
new Cheese( "stilton" ) );
assertTrue( testCondition( testNumber++,
tuple,
rule ) ); // 7
assertFalse( testCondition( testNumber++,
tuple,
rule ) ); // 8
// condition check with two declarations
tuple = new MockTuple( );
rule.setImporter( this.importer );
tuple.setRule( rule );
tuple.setWorkingMemory( new MockWorkingMemory( ) );
tuple.put( stiltonDecl,
new Cheese( "stilton" ) );
tuple.put( camembertDecl,
new Cheese( "camembert" ) );
assertFalse( testCondition( testNumber++,
tuple,
rule ) ); // 9
assertTrue( testCondition( testNumber++,
tuple,
rule ) ); // 10
assertTrue( testCondition( testNumber++,
tuple,
rule ) ); // 11
assertFalse( testCondition( testNumber++,
tuple,
rule ) ); // 12
// condition check with 2 declarations and application data
WorkingMemory workingMemory = new MockWorkingMemory( );
workingMemory.setApplicationData( "bites",
new Integer( 3 ) );
workingMemory.setApplicationData( "favouriteCheese",
new Cheese( "camembert" ) );
tuple.setWorkingMemory( workingMemory );
HashMap applicationData = new HashMap( );
applicationData.put( "bites",
Integer.class );
applicationData.put( "favouriteCheese",
Cheese.class );
rule.setApplicationData( applicationData );
assertTrue( testCondition( testNumber++,
tuple,
rule ) ); // 13
assertFalse( testCondition( testNumber++,
tuple,
rule ) ); // 14
assertTrue( testCondition( testNumber++,
tuple,
rule ) ); // 15
// test code works no matter what the order of decl are
tuple = new MockTuple( );
rule.setImporter( this.importer );
rule.setApplicationData( new HashMap( ) );
tuple.setRule( rule );
workingMemory = new MockWorkingMemory( );
tuple.setWorkingMemory( workingMemory );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -