001: package org.drools.reteoo;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import org.drools.DroolsTestCase;
020: import org.drools.RuleBaseFactory;
021: import org.drools.StatefulSession;
022: import org.drools.WorkingMemory;
023:
024: public class ReteooRuleBaseTest extends DroolsTestCase {
025: ReteooRuleBase ruleBase;
026:
027: StatefulSession wm1;
028: StatefulSession wm2;
029: StatefulSession wm3;
030: StatefulSession wm4;
031:
032: public void setUp() {
033: this .ruleBase = (ReteooRuleBase) RuleBaseFactory.newRuleBase();
034:
035: this .wm1 = this .ruleBase.newStatefulSession();
036: this .wm2 = this .ruleBase.newStatefulSession();
037: this .wm3 = this .ruleBase.newStatefulSession();
038: this .wm4 = this .ruleBase.newStatefulSession();
039: }
040:
041: public void testKeepReference() throws Exception {
042: /* Make sure the RuleBase is referencing all 4 Working Memories */
043: assertLength(4, this .ruleBase.getStatefulSessions());
044: assertContains(this .wm1, this .ruleBase.getStatefulSessions());
045: assertContains(this .wm2, this .ruleBase.getStatefulSessions());
046: assertContains(this .wm3, this .ruleBase.getStatefulSessions());
047: assertContains(this .wm4, this .ruleBase.getStatefulSessions());
048: }
049:
050: public void testDispose() throws Exception {
051: /*
052: * Now lets test the dispose method on the WorkingMemory itself. dispose
053: * doesn't need GC
054: */
055: this .wm3.dispose();
056:
057: /* Check only wm3 was removed */
058: assertLength(3, this .ruleBase.getStatefulSessions());
059: assertNotContains(this .wm3, this .ruleBase.getStatefulSessions());
060: }
061:
062: public void testNoKeepReference() throws Exception {
063: final WorkingMemory wm5 = this .ruleBase
064: .newStatefulSession(false);
065: final WorkingMemory wm6 = this .ruleBase
066: .newStatefulSession(false);
067: assertLength(4, this .ruleBase.getStatefulSessions());
068: assertNotContains(wm5, this .ruleBase.getStatefulSessions());
069: assertNotContains(wm6, this .ruleBase.getStatefulSessions());
070: }
071:
072: public void testAddPackage() throws Exception {
073: final org.drools.rule.Package pkg1 = new org.drools.rule.Package(
074: "org.droos.test");
075: pkg1.addGlobal("global1", Object.class);
076: pkg1.addGlobal("global2", Object.class);
077:
078: final org.drools.rule.Package pkg2 = new org.drools.rule.Package(
079: "org.droos.test");
080: pkg2.addGlobal("global1", Object.class);
081: pkg2.addGlobal("global3", Object.class);
082:
083: final org.drools.rule.Package pkg3 = new org.drools.rule.Package(
084: "org.droos.test2");
085: pkg3.addGlobal("global3", Object.class);
086: pkg3.addGlobal("global4", Object.class);
087:
088: this .ruleBase.addPackage(pkg1);
089: // one package
090: assertLength(1, this .ruleBase.getPackages());
091: // two globals
092: assertLength(2, this .ruleBase.getGlobals().values());
093: // two globals in the package also
094: assertLength(2, this .ruleBase.getPackages()[0].getGlobals()
095: .values());
096:
097: this .ruleBase.addPackage(pkg2);
098: // packages merged, so still 1 package
099: assertLength(1, this .ruleBase.getPackages());
100: // globals merged, so 3 globals total
101: assertLength(3, this .ruleBase.getGlobals().values());
102: // three globals in the package also
103: assertLength(3, this .ruleBase.getPackages()[0].getGlobals()
104: .values());
105:
106: this .ruleBase.addPackage(pkg3);
107: // new package, so now we have 2 package
108: assertLength(2, this .ruleBase.getPackages());
109: // globals partially merged, so 4 globals total
110: assertLength(4, this .ruleBase.getGlobals().values());
111: // two globals in the package
112: final org.drools.rule.Package[] pkgs = this .ruleBase
113: .getPackages();
114: for (int i = 0; i < pkgs.length; i++) {
115: if (pkgs[i].getName().equals(pkg3.getName())) {
116: assertLength(2, pkgs[i].getGlobals().values());
117: }
118: }
119: }
120:
121: public void testRemovePackage() throws Exception {
122: final org.drools.rule.Package pkg1 = new org.drools.rule.Package(
123: "org.droos.test");
124: pkg1.addGlobal("global1", Object.class);
125: pkg1.addGlobal("global2", Object.class);
126:
127: final org.drools.rule.Package pkg2 = new org.drools.rule.Package(
128: "org.droos.test");
129: pkg2.addGlobal("global1", Object.class);
130: pkg2.addGlobal("global3", Object.class);
131:
132: final org.drools.rule.Package pkg3 = new org.drools.rule.Package(
133: "org.droos.test2");
134: pkg3.addGlobal("global3", Object.class);
135: pkg3.addGlobal("global4", Object.class);
136:
137: this .ruleBase.addPackage(pkg1);
138: this .ruleBase.addPackage(pkg2);
139: this .ruleBase.addPackage(pkg3);
140:
141: this .ruleBase.removePackage(pkg1.getName());
142: // packages were partially merged when adding, so removal
143: // shall left only package 3 behind
144: assertLength(1, this .ruleBase.getPackages());
145: assertLength(2, this .ruleBase.getGlobals().values());
146:
147: this .ruleBase.removePackage(pkg3.getName());
148: assertLength(0, this .ruleBase.getPackages());
149: assertLength(0, this.ruleBase.getGlobals().values());
150:
151: }
152:
153: }
|