📄 smftestframework.java
字号:
tuple.setWorkingMemory( workingMemory ); MockConfiguration stringConfiguration = new MockConfiguration( "test2" ); stringConfiguration.setText( String.class.getName( ) ); ObjectType stringType = objectTypeFactory.newObjectType( stringConfiguration, new HashSet( ) ); Declaration favouriteCheeseDecl = rule.addParameterDeclaration( "favouriteCheese", stringType ); tuple.put( favouriteCheeseDecl, "camembert" ); tuple.put( camembertDecl, new Cheese( "camembert" ) ); assertTrue( testCondition( testNumber++, tuple, rule ) ); // 16 assertTrue( testCondition( testNumber++, tuple, rule ) ); // 17 // test condition syntax with commas - Drools Issue #77 assertTrue( testCondition( testNumber++, tuple, rule ) ); // 18 // test exceptions rule.setImports( this.imports ); tuple.setRule( rule ); try { testCondition( testNumber++, tuple, rule ); fail( "Condition should throw an exception" ); } catch ( ConditionException e ) { assertEquals( rule, e.getRule( ) ); assertEquals( tests.get( testNumber - 1 ), e.getInfo( ) ); } // need to add a test for declaration order // 20 // test imports tuple = new MockTuple( ); rule.setImports( this.imports ); tuple.setRule( rule ); workingMemory = new MockWorkingMemory( ); tuple.setWorkingMemory( workingMemory ); assertTrue( testCondition( testNumber++, tuple, rule ) ); // 20 } /** * private helper method to test each of the extracted conditions */ private boolean testCondition(int testNumber, Tuple tuple, Rule rule) throws Exception { ConditionFactory conditionFactory = module.getConditionFactory( "condition" ); MockConfiguration conditionConfiguration = new MockConfiguration( "test" + testNumber ); conditionConfiguration.setText( (String) tests.get( testNumber ) ); Condition condition = conditionFactory.newCondition( conditionConfiguration, rule ); return condition.isAllowed( tuple ); } /** * Tests each of the extracted tests from consequences.data */ public void testConsequences() throws Exception { MockTuple tuple; MockConfiguration cheeseConfiguration = new MockConfiguration( "test1" ); cheeseConfiguration.setText( Cheese.class.getName( ) ); ObjectTypeFactory objectTypeFactory = module.getObjectTypeFactory( "class" ); ObjectType cheeseType = objectTypeFactory.newObjectType( cheeseConfiguration, null ); tuple = new MockTuple( ); Rule rule = new Rule( "Test Rule 1" ); rule.setImports( this.imports ); tuple.setRule( rule ); tuple.setWorkingMemory( new MockWorkingMemory( ) ); // simple condition, no declrations testConsequence( 0, tuple, rule ); // need to declare so that the tests have SMFTestFrameWork.Cheese // imported Declaration camembertDecl = rule.addParameterDeclaration( "camembert", cheeseType ); Declaration stiltonDecl = rule.addParameterDeclaration( "stilton", cheeseType ); Cheese camembert = new Cheese( "camembert" ); Cheese stilton = new Cheese( "stilton" ); tuple.put( camembertDecl, camembert ); tuple.put( stiltonDecl, stilton ); // tests nested classes, public static class SMFTestFrameWork.Cheese, // works testConsequence( 1, tuple, rule ); // now start doing tests with declarations // first confirm that biteLeft is 3 assertEquals( 3, camembert.getBitesLeft( ) ); assertEquals( 3, stilton.getBitesLeft( ) ); // execute consequence that calles eatCheese() testConsequence( 2, tuple, rule ); // camembert should be eaten once, and stilton twice assertEquals( 2, camembert.getBitesLeft( ) ); assertEquals( 1, stilton.getBitesLeft( ) ); // test condition with declarations and application data WorkingMemory workingMemory = new MockWorkingMemory( ); workingMemory.setApplicationData( "bites", new Integer( 3 ) ); workingMemory.setApplicationData( "cheeses", new HashMap( ) ); HashMap applicationData = new HashMap( ); applicationData.put( "bites", Integer.class ); applicationData.put( "cheeses", HashMap.class ); rule.setApplicationData( applicationData ); tuple.setWorkingMemory( workingMemory ); testConsequence( 3, tuple, rule ); assertEquals( 1, camembert.getBitesLeft( ) ); assertEquals( 0, stilton.getBitesLeft( ) ); Map map = (Map) workingMemory.getApplicationData( "cheeses" ); assertEquals( camembert, map.get( "favourite cheese" ) ); assertEquals( 3, ((Integer) map.get( "bites" )).intValue( ) ); // 4 // test exceptions rule = new Rule( "Test Rule 1" ); rule.setImports( this.imports ); tuple.setRule( rule ); try { testConsequence( 6, tuple, rule ); fail( "Consequence should throw an exception" ); } catch ( ConsequenceException e ) { assertEquals( rule, e.getRule( ) ); } // 7 // test imports tuple = new MockTuple( ); rule = new Rule( "Test Rule 1" ); rule.setImports( this.imports ); tuple.setRule( rule ); workingMemory = new MockWorkingMemory( ); tuple.setWorkingMemory( workingMemory ); try { testConsequence( 7, tuple, rule ); } catch ( ConsequenceException e ) { fail( "Consequence should execute without errors" ); } } /** * private helper method to test each of the extracted consequences */ private void testConsequence(int testNumber, Tuple tuple, Rule rule) throws Exception { ConsequenceFactory consequenceFactory = module.getConsequenceFactory( "consequence" ); MockConfiguration consequenceConfiguration = new MockConfiguration( "test" + testNumber ); consequenceConfiguration.setText( (String) tests.get( testNumber ) ); Consequence consequence = consequenceFactory.newConsequence( consequenceConfiguration, rule ); consequence.invoke( tuple, tuple.getWorkingMemory( ) ); } public static boolean conditionExceptionTest() throws Exception { throw new Exception( "this is a condition exception" ); } public static void consequenceExceptionTest() throws Exception { throw new Exception( "this is a consequence exception" ); } /** * Simple nested class used with testing */ public static class Cheese { private String name; private int bitesLeft = 3; public Cheese(String name) { this.name = name; } public String getName() { return this.name; } public void eatCheese() { bitesLeft--; } public int getBitesLeft() { return this.bitesLeft; } public boolean equals(Object object) { if ( object == null ) return false; if ( !(object instanceof Cheese) ) return false; Cheese otherCheese = (Cheese) object; return this.name.equals( otherCheese.getName( ) ); } public int hashCode() { return this.name.hashCode( ); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -