1 package org.drools.jsr94.rules; 2 3 /* 4 $Id: StatefulRuleSessionTestCase.java,v 1.2 2003/06/18 17:04:36 tdiesler Exp $ 5 6 Copyright 2002 (C) The Werken Company. All Rights Reserved. 7 8 Redistribution and use of this software and associated documentation 9 ("Software"), with or without modification, are permitted provided 10 that the following conditions are met: 11 12 1. Redistributions of source code must retain copyright 13 statements and notices. Redistributions must also contain a 14 copy of this document. 15 16 2. Redistributions in binary form must reproduce the 17 above copyright notice, this list of conditions and the 18 following disclaimer in the documentation and/or other 19 materials provided with the distribution. 20 21 3. The name "drools" must not be used to endorse or promote 22 products derived from this Software without prior written 23 permission of The Werken Company. For written permission, 24 please contact bob@werken.com. 25 26 4. Products derived from this Software may not be called "drools" 27 nor may "drools" appear in their names without prior written 28 permission of The Werken Company. "drools" is a registered 29 trademark of The Werken Company. 30 31 5. Due credit should be given to The Werken Company. 32 (http://drools.werken.com/). 33 34 THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS 35 ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 36 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 37 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 38 THE WERKEN COMPANY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 39 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 40 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 41 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 42 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 43 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 44 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 45 OF THE POSSIBILITY OF SUCH DAMAGE. 46 47 */ 48 49 50 import javax.rules.Handle; 51 import javax.rules.ObjectFilter; 52 import javax.rules.RuleRuntime; 53 import javax.rules.StatefulRuleSession; 54 import javax.rules.admin.LocalRuleExecutionSetProvider; 55 import javax.rules.admin.RuleAdministrator; 56 import javax.rules.admin.RuleExecutionSet; 57 import java.io.InputStreamReader; 58 import java.io.Reader; 59 import java.util.ArrayList; 60 import java.util.List; 61 62 /*** 63 * Test the <code>StatefulRuleSession</code> implementation. 64 * 65 * @see StatefulRuleSession 66 * 67 * @author <a href="mailto:thomas.diesler@softcon-itec.de">thomas diesler</a> 68 */ 69 public class StatefulRuleSessionTestCase extends JSR94TestBase { 70 71 private StatefulRuleSession statefulSession; 72 73 /*** 74 * Setup the test case. 75 */ 76 protected void setUp() throws Exception { 77 super.setUp(); 78 RuleAdministrator ruleAdministrator = ruleServiceProvider.getRuleAdministrator(); 79 LocalRuleExecutionSetProvider ruleSetProvider = ruleAdministrator.getLocalRuleExecutionSetProvider(null); 80 RuleRuntime ruleRuntime = ruleServiceProvider.getRuleRuntime(); 81 82 // read rules and register with administrator 83 Reader ruleReader = new InputStreamReader(getResourceAsStream(RULES_RESOURCE)); 84 RuleExecutionSet ruleSet = ruleSetProvider.createRuleExecutionSet(ruleReader, null); 85 ruleAdministrator.registerRuleExecutionSet(RULES_RESOURCE, ruleSet, null); 86 87 // obtain the stateless rule session 88 statefulSession = (StatefulRuleSession)ruleRuntime.createRuleSession(RULES_RESOURCE, null, RuleRuntime.STATEFUL_SESSION_TYPE); 89 } 90 91 /*** 92 * Test containsObject. 93 */ 94 public void testContainsObject() throws Exception { 95 Person bob = new Person("bob"); 96 Handle handle = statefulSession.addObject(bob); 97 assertTrue("where is bob", statefulSession.containsObject(handle)); 98 } 99 100 /*** 101 * Test addObject. 102 */ 103 public void testAddObject() throws Exception { 104 // tested in testContainsObject 105 } 106 107 /*** 108 * Test addObjects. 109 */ 110 public void testAddObjects() throws Exception { 111 112 List inObjects = new ArrayList(); 113 114 Person bob = new Person("bob"); 115 inObjects.add(bob); 116 117 Person rebecca = new Person("rebecca"); 118 rebecca.addSister("jeannie"); 119 inObjects.add(rebecca); 120 121 Person jeannie = new Person("jeannie"); 122 jeannie.addSister("rebecca"); 123 inObjects.add(jeannie); 124 125 List handleList = statefulSession.addObjects(inObjects); 126 assertEquals("incorrect size", 3, handleList.size()); 127 assertEquals("where is bob", bob, statefulSession.getObject((Handle)handleList.get(0))); 128 assertEquals("where is rebecca", rebecca, statefulSession.getObject((Handle)handleList.get(1))); 129 assertEquals("where is jeannie", jeannie, statefulSession.getObject((Handle)handleList.get(2))); 130 } 131 132 /*** 133 * Test getObject. 134 */ 135 public void testGetObject() throws Exception { 136 // tested in testAddObjects 137 } 138 139 /*** 140 * Test updateObject. 141 */ 142 public void testUpdateObject() throws Exception { 143 Person bob = new Person("bob"); 144 Handle handle = statefulSession.addObject(bob); 145 statefulSession.updateObject(handle, bob = new Person("boby")); 146 assertEquals("where is boby", bob, statefulSession.getObject(handle)); 147 } 148 149 /*** 150 * Test removeObject. 151 */ 152 public void testRemoveObject() throws Exception { 153 Person bob = new Person("bob"); 154 Handle handle = statefulSession.addObject(bob); 155 assertTrue("where is bob", statefulSession.containsObject(handle)); 156 157 statefulSession.removeObject(handle); 158 assertTrue("bob still there", !statefulSession.containsObject(handle)); 159 } 160 161 /*** 162 * Test getObjects. 163 */ 164 public void testGetObjects() throws Exception { 165 166 Person bob = new Person("bob"); 167 statefulSession.addObject(bob); 168 169 Person rebecca = new Person("rebecca"); 170 rebecca.addSister("jeannie"); 171 statefulSession.addObject(rebecca); 172 173 Person jeannie = new Person("jeannie"); 174 jeannie.addSister("rebecca"); 175 statefulSession.addObject(jeannie); 176 177 // execute the rules 178 statefulSession.executeRules(); 179 List outList = statefulSession.getObjects(); 180 assertEquals("incorrect size", 7, outList.size()); 181 182 assertTrue("where is bob", outList.contains(bob)); 183 assertTrue("where is rebecca", outList.contains(rebecca)); 184 assertTrue("where is jeannie", outList.contains(jeannie)); 185 186 assertTrue(outList.contains("bob says: rebecca and jeannie are sisters")); 187 assertTrue(outList.contains("bob says: jeannie and rebecca are sisters")); 188 189 assertTrue(outList.contains("rebecca: I like cheese")); 190 assertTrue(outList.contains("jeannie: I like cheese")); 191 192 statefulSession.release(); 193 } 194 195 /*** 196 * Test getObjects with ObjectFilter. 197 */ 198 public void testGetObjectsWithFilter() throws Exception { 199 200 Person bob = new Person("bob"); 201 statefulSession.addObject(bob); 202 203 Person rebecca = new Person("rebecca"); 204 rebecca.addSister("jeannie"); 205 statefulSession.addObject(rebecca); 206 207 Person jeannie = new Person("jeannie"); 208 jeannie.addSister("rebecca"); 209 statefulSession.addObject(jeannie); 210 211 // execute the rules 212 statefulSession.executeRules(); 213 List outList = statefulSession.getObjects(new PersonFilter()); 214 assertEquals("incorrect size", 3, outList.size()); 215 216 assertTrue("where is bob", outList.contains(bob)); 217 assertTrue("where is rebecca", outList.contains(rebecca)); 218 assertTrue("where is jeannie", outList.contains(jeannie)); 219 220 statefulSession.release(); 221 } 222 223 /*** 224 * Test executeRules. 225 */ 226 public void testExecuteRules() throws Exception { 227 // tested in testGetObjects, testGetObjectsWithFilter 228 } 229 230 /*** 231 * Test reset. 232 */ 233 public void testReset() throws Exception { 234 Person bob = new Person("bob"); 235 Handle handle = statefulSession.addObject(bob); 236 assertTrue("where is bob", statefulSession.containsObject(handle)); 237 238 statefulSession.reset(); 239 assertTrue("bob still there", !statefulSession.containsObject(handle)); 240 } 241 242 243 /*** 244 * Filter accepts only objects of type Person. 245 */ 246 static class PersonFilter implements ObjectFilter { 247 248 public Object filter(Object object) { 249 return (object instanceof Person ? object : null); 250 } 251 252 public void reset() { 253 } 254 } 255 }

This page was automatically generated by Maven