001: package org.drools.jsr94.rules;
002:
003: /*
004: * $Id: StatefulRuleSessionTestCase.java,v 1.7 2005/05/06 19:54:49 dbarnett Exp $
005: *
006: * Copyright 2002-2004 (C) The Werken Company. All Rights Reserved.
007: *
008: * Redistribution and use of this software and associated documentation
009: * ("Software"), with or without modification, are permitted provided that the
010: * following conditions are met:
011: *
012: * 1. Redistributions of source code must retain copyright statements and
013: * notices. Redistributions must also contain a copy of this document.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright notice,
016: * this list of conditions and the following disclaimer in the documentation
017: * and/or other materials provided with the distribution.
018: *
019: * 3. The name "drools" must not be used to endorse or promote products derived
020: * from this Software without prior written permission of The Werken Company.
021: * For written permission, please contact bob@werken.com.
022: *
023: * 4. Products derived from this Software may not be called "drools" nor may
024: * "drools" appear in their names without prior written permission of The Werken
025: * Company. "drools" is a registered trademark of The Werken Company.
026: *
027: * 5. Due credit should be given to The Werken Company.
028: * (http://drools.werken.com/).
029: *
030: * THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS ``AS IS''
031: * AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
032: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
033: * ARE DISCLAIMED. IN NO EVENT SHALL THE WERKEN COMPANY OR ITS CONTRIBUTORS BE
034: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
035: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
036: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
037: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
038: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
039: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
040: * POSSIBILITY OF SUCH DAMAGE.
041: *
042: */
043:
044: import java.util.ArrayList;
045: import java.util.HashMap;
046: import java.util.List;
047:
048: import javax.rules.Handle;
049: import javax.rules.ObjectFilter;
050:
051: /**
052: * Test the <code>StatefulRuleSession</code> implementation.
053: *
054: * @author N. Alex Rupp (n_alex <at>codehaus.org)
055: * @author <a href="mailto:thomas.diesler@softcon-itec.de">thomas diesler </a>
056: * @author <a href="mailto:michael.frandsen@syngenio.de">Michael Frandsen </a>
057: * @see javax.rules.StatefulRuleSession
058: */
059: public class StatefulRuleSessionTest extends RuleEngineTestBase {
060: /**
061: * Setup the test case.
062: */
063: protected void setUp() throws Exception {
064: super .setUp();
065: }
066:
067: /**
068: * Test containsObject.
069: */
070: public void testContainsObject() throws Exception {
071: this .statefulSession = this .engine
072: .getStatefulRuleSession(this .bindUri);
073: final Person bob = new Person("bob");
074: final Handle handle = this .statefulSession.addObject(bob);
075: assertTrue("where is bob", this .statefulSession
076: .containsObject(handle));
077: }
078:
079: /**
080: * Test addObject.
081: */
082: public void testAddObject() throws Exception {
083: // tested in testContainsObject
084: }
085:
086: public void testJsr94FactHandleFactoryAvailable()
087: throws ClassNotFoundException {
088: this .getClass().getClassLoader().loadClass(
089: "org.drools.jsr94.rules.Jsr94FactHandleFactory");
090: }
091:
092: /**
093: * Test addObjects.
094: */
095: public void testAddObjects() throws Exception {
096: this .statefulSession = this .engine
097: .getStatefulRuleSession(this .bindUri);
098: final List inObjects = new ArrayList();
099:
100: final Person bob = new Person("bob");
101: inObjects.add(bob);
102:
103: final Person rebecca = new Person("rebecca");
104: rebecca.addSister("jeannie");
105: inObjects.add(rebecca);
106:
107: final Person jeannie = new Person("jeannie");
108: jeannie.addSister("rebecca");
109: inObjects.add(jeannie);
110:
111: final List handleList = this .statefulSession
112: .addObjects(inObjects);
113: assertEquals("incorrect size", 3, handleList.size());
114: assertEquals("where is bob", bob, this .statefulSession
115: .getObject((Handle) handleList.get(0)));
116: assertEquals("where is rebecca", rebecca, this .statefulSession
117: .getObject((Handle) handleList.get(1)));
118: assertEquals("where is jeannie", jeannie, this .statefulSession
119: .getObject((Handle) handleList.get(2)));
120: }
121:
122: /**
123: * Test getObject.
124: */
125: public void testGetObject() throws Exception {
126: // tested in testAddObjects
127: }
128:
129: /**
130: * Test updateObject.
131: */
132: public void testUpdateObject() throws Exception {
133: this .statefulSession = this .engine
134: .getStatefulRuleSession(this .bindUri);
135: Person bob = new Person("bob");
136: final Handle handle = this .statefulSession.addObject(bob);
137: this .statefulSession.updateObject(handle, bob = new Person(
138: "boby"));
139: assertEquals("where is boby", bob, this .statefulSession
140: .getObject(handle));
141: }
142:
143: /**
144: * Test removeObject.
145: */
146: public void testRemoveObject() throws Exception {
147: this .statefulSession = this .engine
148: .getStatefulRuleSession(this .bindUri);
149: final Person bob = new Person("bob");
150: final Handle handle = this .statefulSession.addObject(bob);
151: assertTrue("where is bob", this .statefulSession
152: .containsObject(handle));
153:
154: this .statefulSession.removeObject(handle);
155: assertTrue("bob still there", !this .statefulSession
156: .containsObject(handle));
157: }
158:
159: /**
160: * Test getObjects.
161: */
162: public void testGetObjects() throws Exception {
163: this .statefulSession = this .engine
164: .getStatefulRuleSession(this .bindUri);
165:
166: final Person bob = new Person("bob");
167: this .statefulSession.addObject(bob);
168:
169: final Person rebecca = new Person("rebecca");
170: rebecca.addSister("jeannie");
171: this .statefulSession.addObject(rebecca);
172:
173: final Person jeannie = new Person("jeannie");
174: jeannie.addSister("rebecca");
175: this .statefulSession.addObject(jeannie);
176:
177: // execute the rules
178: this .statefulSession.executeRules();
179: final List outList = this .statefulSession.getObjects();
180: assertEquals("incorrect size", 5, 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("rebecca and jeannie are sisters"));
187: assertTrue(outList.contains("jeannie and rebecca are sisters"));
188:
189: this .statefulSession.release();
190: }
191:
192: /**
193: * Test getObjects with ObjectFilter.
194: */
195: public void testGetObjectsWithFilter() throws Exception {
196: this .statefulSession = this .engine
197: .getStatefulRuleSession(this .bindUri);
198:
199: final Person bob = new Person("bob");
200: this .statefulSession.addObject(bob);
201:
202: final Person rebecca = new Person("rebecca");
203: rebecca.addSister("jeannie");
204: this .statefulSession.addObject(rebecca);
205:
206: final Person jeannie = new Person("jeannie");
207: jeannie.addSister("rebecca");
208: this .statefulSession.addObject(jeannie);
209:
210: // execute the rules
211: this .statefulSession.executeRules();
212: final List outList = this .statefulSession
213: .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: this .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: this .statefulSession = this .engine
235: .getStatefulRuleSession(this .bindUri);
236:
237: final Person bob = new Person("bob");
238: final Handle handle = this .statefulSession.addObject(bob);
239: assertTrue("where is bob", this .statefulSession
240: .containsObject(handle));
241:
242: this .statefulSession.reset();
243: assertTrue("bob still there", !this .statefulSession
244: .containsObject(handle));
245: }
246:
247: /**
248: * Test executeRules with globals.
249: */
250: public void testExecuteRulesGlobals() throws Exception {
251: final java.util.Map map = new HashMap();
252: java.util.Vector v = new java.util.Vector();
253: map.put("vector", v);
254: this .statefulSession = this .engine.getStatefulRuleSession(
255: this .bindUri_globals, map);
256:
257: final Person bob = new Person("bob");
258: this .statefulSession.addObject(bob);
259:
260: final Person rebecca = new Person("rebecca");
261: rebecca.addSister("jeannie");
262: this .statefulSession.addObject(rebecca);
263:
264: final Person jeannie = new Person("jeannie");
265: jeannie.addSister("rebecca");
266: this .statefulSession.addObject(jeannie);
267:
268: // execute the rules
269: this .statefulSession.executeRules();
270:
271: final List outList = this .statefulSession.getObjects();
272:
273: assertEquals("incorrect size", 5, outList.size());
274:
275: assertContains(outList, bob);
276:
277: assertContains(outList, rebecca);
278:
279: assertContains(outList, jeannie);
280:
281: assertContains(outList, "rebecca and jeannie are sisters");
282:
283: assertContains(outList, "jeannie and rebecca are sisters");
284:
285: v = (java.util.Vector) map.get("vector");
286:
287: assertNotNull("Global Vector null", v);
288:
289: assertContains(v, "rebecca and jeannie are sisters");
290:
291: assertContains(v, "jeannie and rebecca are sisters");
292:
293: assertEquals("Vector v incorrect size", 2, v.size());
294:
295: this .statefulSession.release();
296: }
297:
298: /**
299: * Test executeRules drl with dsl.
300: */
301: public void xxxtestExecuteRules_dsl() throws Exception {
302: // @FIXME
303: this .statefulSession = this .engine
304: .getStatefulRuleSession(this .bindUri_drl);
305:
306: final Person bob = new Person("bob");
307: this .statefulSession.addObject(bob);
308:
309: final Person rebecca = new Person("rebecca");
310: rebecca.addSister("jeannie");
311: this .statefulSession.addObject(rebecca);
312:
313: final Person jeannie = new Person("jeannie");
314: jeannie.addSister("rebecca");
315: this .statefulSession.addObject(jeannie);
316:
317: // execute the rules
318: this .statefulSession.executeRules();
319:
320: final List outList = this .statefulSession.getObjects();
321:
322: assertEquals("incorrect size", 5, outList.size());
323:
324: assertContains(outList, bob);
325:
326: assertContains(outList, rebecca);
327:
328: assertContains(outList, jeannie);
329:
330: assertContains(outList, "rebecca and jeannie are sisters");
331:
332: assertContains(outList, "jeannie and rebecca are sisters");
333:
334: this .statefulSession.release();
335: }
336:
337: protected void assertContains(final List expected,
338: final Object object) {
339: if (expected.contains(object)) {
340: return;
341: }
342:
343: fail(object + " not in " + expected);
344: }
345:
346: /**
347: * Filter accepts only objects of type Person.
348: */
349: static class PersonFilter implements ObjectFilter {
350: public Object filter(final Object object) {
351: return (object instanceof Person ? object : null);
352: }
353:
354: public void reset() {
355: // nothing to reset
356: }
357: }
358:
359: }
|