001: package org.drools.integrationtests;
002:
003: import java.io.InputStreamReader;
004: import java.util.ArrayList;
005: import java.util.List;
006:
007: import org.drools.Cheese;
008: import org.drools.RuleBase;
009: import org.drools.RuleBaseConfiguration;
010: import org.drools.RuleBaseFactory;
011: import org.drools.StatelessSession;
012: import org.drools.StatelessSessionResult;
013: import org.drools.WorkingMemory;
014: import org.drools.compiler.PackageBuilder;
015: import org.drools.rule.Package;
016:
017: import junit.framework.TestCase;
018:
019: public class StatelessSessionTest extends TestCase {
020: final List list = new ArrayList();
021:
022: protected RuleBase getRuleBase() throws Exception {
023:
024: return RuleBaseFactory.newRuleBase(RuleBase.RETEOO, null);
025: }
026:
027: protected RuleBase getRuleBase(final RuleBaseConfiguration config)
028: throws Exception {
029:
030: return RuleBaseFactory.newRuleBase(RuleBase.RETEOO, config);
031: }
032:
033: public void testSingleObjectAssert() throws Exception {
034: StatelessSession session = getSession();
035:
036: final Cheese stilton = new Cheese("stilton", 5);
037:
038: session.execute(stilton);
039:
040: assertEquals("stilton", list.get(0));
041: }
042:
043: public void testArrayObjectAssert() throws Exception {
044: StatelessSession session = getSession();
045:
046: final Cheese stilton = new Cheese("stilton", 5);
047:
048: session.execute(new Object[] { stilton });
049:
050: assertEquals("stilton", list.get(0));
051: }
052:
053: public void testCollectionObjectAssert() throws Exception {
054: StatelessSession session = getSession();
055:
056: final Cheese stilton = new Cheese("stilton", 5);
057:
058: List collection = new ArrayList();
059: collection.add(stilton);
060: session.execute(collection);
061:
062: assertEquals("stilton", list.get(0));
063: }
064:
065: public void testSingleObjectAssertWithResults() throws Exception {
066: StatelessSession session = getSession();
067:
068: final Cheese stilton = new Cheese("stilton", 5);
069:
070: StatelessSessionResult result = session
071: .executeWithResults(stilton);
072:
073: assertSame(stilton, result.iterateObjects().next());
074: }
075:
076: public void testArrayObjectAssertWithResults() throws Exception {
077: StatelessSession session = getSession();
078:
079: final Cheese stilton = new Cheese("stilton", 5);
080:
081: StatelessSessionResult result = session
082: .executeWithResults(new Object[] { stilton });
083:
084: assertSame(stilton, result.iterateObjects().next());
085: }
086:
087: public void testCollectionObjectAssertWithResults()
088: throws Exception {
089: StatelessSession session = getSession();
090:
091: final Cheese stilton = new Cheese("stilton", 5);
092:
093: List collection = new ArrayList();
094: collection.add(stilton);
095: StatelessSessionResult result = session
096: .executeWithResults(collection);
097:
098: assertSame(stilton, result.iterateObjects().next());
099: }
100:
101: public void testAsynSingleOjbectcAssert() throws Exception {
102: StatelessSession session = getSession();
103:
104: final Cheese stilton = new Cheese("stilton", 5);
105:
106: session.asyncExecute(stilton);
107:
108: Thread.sleep(100);
109:
110: assertEquals("stilton", list.get(0));
111: }
112:
113: public void testAsynArrayOjbectcAssert() throws Exception {
114: StatelessSession session = getSession();
115:
116: final Cheese stilton = new Cheese("stilton", 5);
117:
118: session.asyncExecute(new Object[] { stilton });
119:
120: Thread.sleep(100);
121:
122: assertEquals("stilton", list.get(0));
123: }
124:
125: public void testAsynCollectionOjbectcAssert() throws Exception {
126: StatelessSession session = getSession();
127:
128: final Cheese stilton = new Cheese("stilton", 5);
129:
130: List collection = new ArrayList();
131: collection.add(stilton);
132: session.execute(collection);
133:
134: Thread.sleep(100);
135:
136: assertEquals("stilton", list.get(0));
137: }
138:
139: private StatelessSession getSession() throws Exception {
140: final PackageBuilder builder = new PackageBuilder();
141: builder.addPackageFromDrl(new InputStreamReader(getClass()
142: .getResourceAsStream("literal_rule_test.drl")));
143: final Package pkg = builder.getPackage();
144:
145: final RuleBase ruleBase = getRuleBase();
146: ruleBase.addPackage(pkg);
147: final StatelessSession session = ruleBase.newStatelessSession();
148:
149: session.setGlobal("list", this.list);
150: return session;
151: }
152: }
|