001: package org.drools.base;
002:
003: import java.lang.reflect.Field;
004:
005: import junit.framework.TestCase;
006:
007: import org.drools.RuleBaseFactory;
008: import org.drools.WorkingMemory;
009:
010: public class DelegateJavaFactHandlerTest extends TestCase {
011:
012: public void test1Entry() throws Exception {
013: final DelegateJavaFactHandler handler = new DelegateJavaFactHandler();
014:
015: final Field field = handler.getClass().getDeclaredField(
016: "entries");
017: field.setAccessible(true);
018:
019: final WorkingMemory wm1 = RuleBaseFactory.newRuleBase()
020: .newStatefulSession();
021: handler.register(wm1);
022:
023: JavaFactRegistryEntry[] entries = (JavaFactRegistryEntry[]) field
024: .get(handler);
025: assertEquals(1, entries.length);
026:
027: assertTrue(handler.isRegistered(wm1));
028:
029: assertEquals(1, handler.listWorkingMemories().length);
030: assertSame(wm1, handler.listWorkingMemories()[0]
031: .getWorkingMemory());
032:
033: final WorkingMemory wm2 = RuleBaseFactory.newRuleBase()
034: .newStatefulSession();
035: assertFalse(handler.isRegistered(wm2));
036:
037: handler.unregister(wm1);
038: assertFalse(handler.isRegistered(wm1));
039: entries = (JavaFactRegistryEntry[]) field.get(handler);
040: assertNull(entries);
041: }
042:
043: public void test2Entries() throws Exception {
044: final DelegateJavaFactHandler handler = new DelegateJavaFactHandler();
045:
046: final Field field = handler.getClass().getDeclaredField(
047: "entries");
048: field.setAccessible(true);
049:
050: final WorkingMemory wm1 = RuleBaseFactory.newRuleBase()
051: .newStatefulSession();
052: final WorkingMemory wm2 = RuleBaseFactory.newRuleBase()
053: .newStatefulSession();
054: handler.register(wm1);
055: handler.register(wm2);
056:
057: JavaFactRegistryEntry[] entries = (JavaFactRegistryEntry[]) field
058: .get(handler);
059: assertEquals(2, entries.length);
060:
061: assertTrue(handler.isRegistered(wm1));
062: assertTrue(handler.isRegistered(wm2));
063:
064: assertEquals(2, handler.listWorkingMemories().length);
065: assertSame(wm1, handler.listWorkingMemories()[0]
066: .getWorkingMemory());
067: assertSame(wm2, handler.listWorkingMemories()[1]
068: .getWorkingMemory());
069:
070: handler.unregister(wm1);
071: assertFalse(handler.isRegistered(wm1));
072: entries = (JavaFactRegistryEntry[]) field.get(handler);
073: assertEquals(1, entries.length);
074:
075: handler.unregister(wm2);
076: assertFalse(handler.isRegistered(wm2));
077: entries = (JavaFactRegistryEntry[]) field.get(handler);
078: assertNull(entries);
079:
080: // check revererse
081: handler.register(wm1);
082: handler.register(wm2);
083:
084: handler.unregister(wm2);
085: assertFalse(handler.isRegistered(wm2));
086: entries = (JavaFactRegistryEntry[]) field.get(handler);
087: assertEquals(1, entries.length);
088:
089: handler.unregister(wm1);
090: assertFalse(handler.isRegistered(wm1));
091: entries = (JavaFactRegistryEntry[]) field.get(handler);
092: assertNull(entries);
093: }
094:
095: public void test3Entries() throws Exception {
096: final DelegateJavaFactHandler handler = new DelegateJavaFactHandler();
097:
098: final Field field = handler.getClass().getDeclaredField(
099: "entries");
100: field.setAccessible(true);
101:
102: final WorkingMemory wm1 = RuleBaseFactory.newRuleBase()
103: .newStatefulSession();
104: final WorkingMemory wm2 = RuleBaseFactory.newRuleBase()
105: .newStatefulSession();
106: final WorkingMemory wm3 = RuleBaseFactory.newRuleBase()
107: .newStatefulSession();
108: handler.register(wm1);
109: handler.register(wm2);
110: handler.register(wm3);
111:
112: JavaFactRegistryEntry[] entries = (JavaFactRegistryEntry[]) field
113: .get(handler);
114: assertEquals(3, entries.length);
115:
116: assertTrue(handler.isRegistered(wm1));
117: assertTrue(handler.isRegistered(wm2));
118: assertTrue(handler.isRegistered(wm3));
119:
120: assertEquals(3, handler.listWorkingMemories().length);
121: assertSame(wm1, handler.listWorkingMemories()[0]
122: .getWorkingMemory());
123: assertSame(wm2, handler.listWorkingMemories()[1]
124: .getWorkingMemory());
125: assertSame(wm3, handler.listWorkingMemories()[2]
126: .getWorkingMemory());
127:
128: handler.unregister(wm2);
129: assertFalse(handler.isRegistered(wm2));
130: entries = (JavaFactRegistryEntry[]) field.get(handler);
131: assertEquals(2, entries.length);
132:
133: handler.unregister(wm1);
134: assertFalse(handler.isRegistered(wm1));
135: entries = (JavaFactRegistryEntry[]) field.get(handler);
136: assertEquals(1, entries.length);
137:
138: handler.unregister(wm3);
139: assertFalse(handler.isRegistered(wm3));
140: entries = (JavaFactRegistryEntry[]) field.get(handler);
141: assertNull(entries);
142: }
143: }
|