01: package org.drools.event;
02:
03: /*
04: * Copyright 2005 JBoss Inc
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: import java.io.Serializable;
20: import java.util.ArrayList;
21: import java.util.List;
22:
23: import org.drools.Cheese;
24: import org.drools.FactHandle;
25: import org.drools.RuleBase;
26: import org.drools.RuleBaseFactory;
27: import org.drools.WorkingMemory;
28:
29: import junit.framework.TestCase;
30:
31: /**
32: * @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris</a>
33: */
34: public class WorkingMemoryEventSupportTest extends TestCase {
35: public void testIsSerializable() {
36: assertTrue(Serializable.class
37: .isAssignableFrom(WorkingMemoryEventSupport.class));
38: }
39:
40: public void testWorkingMemoryEventListener() {
41: final RuleBase rb = RuleBaseFactory.newRuleBase();
42: final WorkingMemory wm = rb.newStatefulSession();
43:
44: final List wmList = new ArrayList();
45: final WorkingMemoryEventListener workingMemoryListener = new WorkingMemoryEventListener() {
46:
47: public void objectInserted(ObjectInsertedEvent event) {
48: wmList.add(event);
49: }
50:
51: public void objectUpdated(ObjectUpdatedEvent event) {
52: wmList.add(event);
53: }
54:
55: public void objectRetracted(ObjectRetractedEvent event) {
56: wmList.add(event);
57: }
58:
59: };
60:
61: wm.addEventListener(workingMemoryListener);
62:
63: final Cheese stilton = new Cheese("stilton", 15);
64: final Cheese cheddar = new Cheese("cheddar", 17);
65:
66: final FactHandle stiltonHandle = wm.insert(stilton);
67:
68: final ObjectInsertedEvent oae = (ObjectInsertedEvent) wmList
69: .get(0);
70: assertSame(stiltonHandle, oae.getFactHandle());
71:
72: wm.update(stiltonHandle, stilton);
73: final ObjectUpdatedEvent ome = (ObjectUpdatedEvent) wmList
74: .get(1);
75: assertSame(stiltonHandle, ome.getFactHandle());
76:
77: wm.retract(stiltonHandle);
78: final ObjectRetractedEvent ore = (ObjectRetractedEvent) wmList
79: .get(2);
80: assertSame(stiltonHandle, ore.getFactHandle());
81:
82: wm.insert(cheddar);
83: }
84: }
|