1 package org.drools.semantics.java; 2 3 import org.drools.rule.Declaration; 4 import org.drools.spi.MockTuple; 5 import org.drools.spi.ObjectType; 6 7 import bsh.EvalError; 8 import bsh.NameSpace; 9 10 import junit.framework.TestCase; 11 12 public class InterpTest extends TestCase 13 { 14 private Interp interp; 15 16 public InterpTest(String name) 17 { 18 super( name ); 19 } 20 21 public void setUp() 22 { 23 this.interp = new Interp(); 24 } 25 26 public void tearDown() 27 { 28 this.interp = null; 29 } 30 31 public void testEvaluate() throws Exception 32 { 33 this.interp.setText( "42 + 12" ); 34 35 NameSpace ns = new NameSpace( "" ); 36 37 assertEquals( new Integer(54), 38 this.interp.evaluate( ns ) ); 39 } 40 41 public void testEvaluate_Variables() throws Exception 42 { 43 NameSpace ns = new NameSpace( "" ); 44 45 this.interp.setText( "a + b" ); 46 47 ns.setVariable( "a", 48 new Integer(42) ); 49 50 ns.setVariable( "b", 51 new Integer(12) ); 52 53 assertEquals( new Integer(54), 54 this.interp.evaluate( ns ) ); 55 } 56 57 public void testEvaluate_Tuple() throws Exception 58 { 59 MockTuple tuple = new MockTuple(); 60 61 tuple.put( new Declaration( new ClassObjectType( java.lang.Number.class ), 62 "a" ), 63 new Integer(42) ); 64 65 tuple.put( new Declaration( new ClassObjectType( java.lang.Number.class ), 66 "b" ), 67 new Integer(12) ); 68 69 this.interp.setText( "a + b" ); 70 71 assertEquals( new Integer(54), 72 this.interp.evaluate( tuple ) ); 73 74 tuple = new MockTuple(); 75 76 try 77 { 78 this.interp.evaluate( tuple ); 79 fail( "Should have thrown EvalError. No variables set." ); 80 } 81 catch (EvalError e) 82 { 83 // expected and correct 84 } 85 } 86 87 public void testSetUpNameSpace() throws Exception 88 { 89 MockTuple tuple = new MockTuple(); 90 91 tuple.put( new Declaration( new ClassObjectType( org.drools.DroolsException.class ), 92 "a" ), 93 null ); 94 95 tuple.put( new Declaration( new ClassObjectType( org.drools.AssertionException.class ), 96 "b" ), 97 null ); 98 99 tuple.put( new Declaration( new ObjectType() { public boolean matches(Object o) { return false; } }, 100 "c" ), 101 null ); 102 103 NameSpace ns = this.interp.setUpNameSpace( tuple ); 104 105 assertSame( org.drools.DroolsException.class, 106 ns.getClass( "DroolsException" ) ); 107 108 assertSame( org.drools.AssertionException.class, 109 ns.getClass( "AssertionException" ) ); 110 111 assertNull( ns.getClass( "RetractionException" ) ); 112 } 113 }

This page was automatically generated by Maven