001: package org.drools.eclipse.debug;
002:
003: import java.io.InputStreamReader;
004: import java.io.Reader;
005: import java.util.ArrayList;
006: import java.util.List;
007: import java.util.Map.Entry;
008:
009: import junit.framework.TestCase;
010:
011: import org.drools.RuleBase;
012: import org.drools.RuleBaseFactory;
013: import org.drools.base.MapGlobalResolver;
014: import org.drools.compiler.PackageBuilder;
015: import org.drools.reteoo.ReteooStatefulSession;
016: import org.drools.spi.Activation;
017: import org.drools.spi.AgendaGroup;
018:
019: /**
020: *
021: * WARNING: DO NOT CHANGE ANYTHING IN THIS TEST CLASS
022: * WITHOUT ALSO CHANGING THE IMPLEMENTATION IN THE DEBUG
023: * VIEWS (which are using reflection)
024: *
025: * @author Kris Verlaenen
026: */
027: public class DebugViewsTest extends TestCase {
028:
029: /*
030: * WARNING: DO NOT CHANGE ANYTHING IN THIS TEST CLASS
031: * WITHOUT ALSO CHANGING THE IMPLEMENTATION IN THE DEBUG
032: * VIEWS (which are using reflection)
033: */
034:
035: public void testApplicationDataView() throws Exception {
036: Reader source = new InputStreamReader(DebugViewsTest.class
037: .getResourceAsStream("/debug.drl"));
038: PackageBuilder builder = new PackageBuilder();
039: builder.addPackageFromDrl(source);
040: RuleBase ruleBase = RuleBaseFactory.newRuleBase();
041: ruleBase.addPackage(builder.getPackage());
042: ReteooStatefulSession session = (ReteooStatefulSession) ruleBase
043: .newStatefulSession();
044: session.setGlobal("s", "String");
045: List list = new ArrayList();
046: list.add("Value");
047: session.setGlobal("list", list);
048: Entry[] globals = ((MapGlobalResolver) session
049: .getGlobalResolver()).getGlobals();
050: assertEquals(2, globals.length);
051: assertEquals("list", globals[0].getKey());
052: assertEquals(list, globals[0].getValue());
053: assertEquals("s", globals[1].getKey());
054: assertEquals("String", globals[1].getValue());
055: }
056:
057: /*
058: * WARNING: DO NOT CHANGE ANYTHING IN THIS TEST CLASS
059: * WITHOUT ALSO CHANGING THE IMPLEMENTATION IN THE DEBUG
060: * VIEWS (which are using reflection)
061: */
062:
063: public void testAgendaView() throws Exception {
064: Reader source = new InputStreamReader(DebugViewsTest.class
065: .getResourceAsStream("/debug.drl"));
066: PackageBuilder builder = new PackageBuilder();
067: builder.addPackageFromDrl(source);
068: RuleBase ruleBase = RuleBaseFactory.newRuleBase();
069: ruleBase.addPackage(builder.getPackage());
070: ReteooStatefulSession session = (ReteooStatefulSession) ruleBase
071: .newStatefulSession();
072: List list = new ArrayList();
073: session.setGlobal("list", list);
074: session.insert("String1");
075: AgendaGroup focus = session.getAgenda().getFocus();
076: assertEquals("MAIN", focus.getName());
077: AgendaGroup[] agendaGroups = session.getAgenda()
078: .getAgendaGroups();
079: assertEquals(1, agendaGroups.length);
080: assertEquals("MAIN", agendaGroups[0].getName());
081: assertEquals(1, agendaGroups[0].getActivations().length);
082:
083: Activation activation = agendaGroups[0].getActivations()[0];
084: assertEquals("ActivationCreator", activation.getRule()
085: .getName());
086: Entry[] parameters = session.getActivationParameters(activation
087: .getActivationNumber());
088: assertEquals(1, parameters.length);
089: assertEquals("o", parameters[0].getKey());
090: assertEquals("String1", parameters[0].getValue());
091: }
092:
093: /*
094: * WARNING: DO NOT CHANGE ANYTHING IN THIS TEST CLASS
095: * WITHOUT ALSO CHANGING THE IMPLEMENTATION IN THE DEBUG
096: * VIEWS (which are using reflection)
097: */
098:
099: public void testWorkingMemoryView() throws Exception {
100: RuleBase ruleBase = RuleBaseFactory.newRuleBase();
101: ReteooStatefulSession session = (ReteooStatefulSession) ruleBase
102: .newStatefulSession();
103: session.insert("Test1");
104: session.insert("Test2");
105: Object[] objects = session.iterateObjectsToList().toArray();
106: assertEquals(2, objects.length);
107: assertTrue(("Test1".equals(objects[0]) && "Test2"
108: .equals(objects[1]))
109: || ("Test2".equals(objects[0]) && "Test1"
110: .equals(objects[1])));
111: }
112:
113: }
|