⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smftestframework.java

📁 drools 一个开放源码的规则引擎
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

        DefaultConfiguration stringConfiguration = new DefaultConfiguration( "test2" );
        stringConfiguration.setText( String.class.getName( ) );
        ObjectType stringType = objectTypeFactory.newObjectType( rule,
                                                                 this.ruleBaseContext,
                                                                 stringConfiguration );
        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.setImporter( this.importer );
        tuple.setRule( rule );
        try
        {
            testCondition( testNumber++,
                           tuple,
                           rule );
            fail( "Condition should throw an exception" );
        }
        catch ( ConditionException e )
        {
            // expected
        }

        // need to add a test for declaration order

        // 20
        // test imports
        tuple = new MockTuple( );
        rule.setImporter( this.importer );
        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" );
        DefaultConfiguration conditionConfiguration = new DefaultConfiguration( "test" + testNumber );
        conditionConfiguration.setText( (String) tests.get( testNumber ) );
        Condition condition = conditionFactory.newCondition( rule,
                                                             this.ruleBaseContext,
                                                             conditionConfiguration )[0];
        return condition.isAllowed( tuple );
    }

    /**
     * Tests each of the extracted tests from consequences.data
     */
    public void testConsequences() throws Exception
    {
        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 );
        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( this.importer );
        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",
                         ruleSet );
        rule.setImporter( this.importer );
        tuple.setRule( rule );
        try
        {
            testConsequence( 6,
                             tuple,
                             rule );
            fail( "Consequence should throw an exception" );
        }
        catch ( ConsequenceException e )
        {
            // expected
        }

        // 7
        // test imports
        tuple = new MockTuple( );
        rule = new Rule( "Test Rule 1",
                         ruleSet );
        rule.setImporter( this.importer );
        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" );
        DefaultConfiguration consequenceConfiguration = new DefaultConfiguration( "test" + testNumber );
        consequenceConfiguration.setText( (String) tests.get( testNumber ) );
        Consequence consequence = consequenceFactory.newConsequence( rule,
                                                                     this.ruleBaseContext,
                                                                     consequenceConfiguration );
        consequence.invoke( tuple );
    }

    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 ( this == object )
            {
                return true;
            }

            if ( object == null || getClass( ) != object.getClass( ) )
            {
                return false;
            }

            return this.name.equals( ((Cheese) object).name );
        }

        public int hashCode()
        {
            return this.name.hashCode( );
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -