001: package org.drools.jsr94.rules;
002:
003: /*
004: * $Id: RuleRuntimeTestCase.java,v 1.7 2005/02/04 02:13:38 mproctor Exp $
005: *
006: * Copyright 2002-2004 (C) The Werken Company. All Rights Reserved.
007: *
008: * Redistribution and use of this software and associated documentation
009: * ("Software"), with or without modification, are permitted provided that the
010: * following conditions are met:
011: *
012: * 1. Redistributions of source code must retain copyright statements and
013: * notices. Redistributions must also contain a copy of this document.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright notice,
016: * this list of conditions and the following disclaimer in the documentation
017: * and/or other materials provided with the distribution.
018: *
019: * 3. The name "drools" must not be used to endorse or promote products derived
020: * from this Software without prior written permission of The Werken Company.
021: * For written permission, please contact bob@werken.com.
022: *
023: * 4. Products derived from this Software may not be called "drools" nor may
024: * "drools" appear in their names without prior written permission of The Werken
025: * Company. "drools" is a registered trademark of The Werken Company.
026: *
027: * 5. Due credit should be given to The Werken Company.
028: * (http://drools.werken.com/).
029: *
030: * THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS ``AS IS''
031: * AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
032: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
033: * ARE DISCLAIMED. IN NO EVENT SHALL THE WERKEN COMPANY OR ITS CONTRIBUTORS BE
034: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
035: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
036: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
037: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
038: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
039: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
040: * POSSIBILITY OF SUCH DAMAGE.
041: *
042: */
043:
044: import java.io.InputStreamReader;
045: import java.io.Reader;
046: import java.util.ArrayList;
047: import java.util.HashMap;
048: import java.util.List;
049: import java.util.Map;
050:
051: import javax.rules.RuleExecutionSetNotFoundException;
052: import javax.rules.RuleRuntime;
053: import javax.rules.RuleServiceProvider;
054: import javax.rules.RuleServiceProviderManager;
055: import javax.rules.StatefulRuleSession;
056: import javax.rules.StatelessRuleSession;
057: import javax.rules.admin.LocalRuleExecutionSetProvider;
058: import javax.rules.admin.RuleAdministrator;
059: import javax.rules.admin.RuleExecutionSet;
060:
061: import org.drools.RuleBaseConfiguration;
062: import org.drools.decisiontable.Cheese;
063: import org.drools.decisiontable.Person;
064: import org.drools.decisiontable.SpreadsheetIntegrationTest;
065:
066: /**
067: * Test the RuleRuntime implementation.
068: *
069: * @author N. Alex Rupp (n_alex <at>codehaus.org)
070: * @author <a href="mailto:thomas.diesler@softcon-itec.de">thomas diesler </a>
071: */
072: public class RuleRuntimeTest extends RuleEngineTestBase {
073: private LocalRuleExecutionSetProvider ruleSetProvider;
074:
075: private RuleAdministrator ruleAdministrator;
076:
077: private String RULES_RESOURCE;
078:
079: /**
080: * Setup the test case.
081: */
082: protected void setUp() throws Exception {
083: super .setUp();
084: this .RULES_RESOURCE = this .bindUri;
085: this .ruleAdministrator = this .ruleServiceProvider
086: .getRuleAdministrator();
087: this .ruleSetProvider = this .ruleAdministrator
088: .getLocalRuleExecutionSetProvider(null);
089: }
090:
091: /**
092: * Test createRuleSession.
093: */
094: public void testCreateRuleStatelessRuleSession() throws Exception {
095: final RuleRuntime ruleRuntime = this .ruleServiceProvider
096: .getRuleRuntime();
097: assertNotNull("cannot obtain RuleRuntime", ruleRuntime);
098:
099: // expect RuleExecutionSetNotFoundException
100: try {
101: ruleRuntime.createRuleSession("someUri", null,
102: RuleRuntime.STATELESS_SESSION_TYPE);
103: fail("RuleExecutionSetNotFoundException expected");
104: } catch (final RuleExecutionSetNotFoundException ex) {
105: // ignore exception
106: }
107:
108: // read rules and register with administrator
109: final Reader ruleReader = new InputStreamReader(
110: RuleRuntimeTest.class
111: .getResourceAsStream(this .RULES_RESOURCE));
112: final RuleExecutionSet ruleSet = this .ruleSetProvider
113: .createRuleExecutionSet(ruleReader, null);
114: this .ruleAdministrator.registerRuleExecutionSet(
115: this .RULES_RESOURCE, ruleSet, null);
116:
117: final StatelessRuleSession statelessRuleSession = (StatelessRuleSession) ruleRuntime
118: .createRuleSession(this .RULES_RESOURCE, null,
119: RuleRuntime.STATELESS_SESSION_TYPE);
120: assertNotNull("cannot obtain StatelessRuleSession",
121: statelessRuleSession);
122:
123: this .ruleAdministrator.deregisterRuleExecutionSet(
124: this .RULES_RESOURCE, null);
125: }
126:
127: /**
128: * Test createRuleSession.
129: */
130: public void testCreateRuleStatefulRuleSession() throws Exception {
131: final RuleRuntime ruleRuntime = this .ruleServiceProvider
132: .getRuleRuntime();
133: assertNotNull("cannot obtain RuleRuntime", ruleRuntime);
134:
135: // expect RuleExecutionSetNotFoundException
136: try {
137: ruleRuntime.createRuleSession("someUri", null,
138: RuleRuntime.STATEFUL_SESSION_TYPE);
139: fail("RuleExecutionSetNotFoundException expected");
140: } catch (final RuleExecutionSetNotFoundException ex) {
141: // ignore exception
142: }
143:
144: // read rules and register with administrator
145: final Reader ruleReader = new InputStreamReader(
146: RuleRuntimeTest.class
147: .getResourceAsStream(this .RULES_RESOURCE));
148: final RuleExecutionSet ruleSet = this .ruleSetProvider
149: .createRuleExecutionSet(ruleReader, null);
150: this .ruleAdministrator.registerRuleExecutionSet(
151: this .RULES_RESOURCE, ruleSet, null);
152:
153: final StatefulRuleSession statefulRuleSession = (StatefulRuleSession) ruleRuntime
154: .createRuleSession(this .RULES_RESOURCE, null,
155: RuleRuntime.STATEFUL_SESSION_TYPE);
156: assertNotNull("cannot obtain StatefulRuleSession",
157: statefulRuleSession);
158:
159: this .ruleAdministrator.deregisterRuleExecutionSet(
160: this .RULES_RESOURCE, null);
161: }
162:
163: /**
164: * Test getRegistrations.
165: */
166: public void testGetRegistrations() throws Exception {
167: final RuleRuntime ruleRuntime = this .ruleServiceProvider
168: .getRuleRuntime();
169: assertNotNull("cannot obtain RuleRuntime", ruleRuntime);
170:
171: // read rules and register with administrator
172: final Reader ruleReader = new InputStreamReader(
173: RuleRuntimeTest.class
174: .getResourceAsStream(this .RULES_RESOURCE));
175: final RuleExecutionSet ruleSet = this .ruleSetProvider
176: .createRuleExecutionSet(ruleReader, null);
177: this .ruleAdministrator.registerRuleExecutionSet(
178: this .RULES_RESOURCE, ruleSet, null);
179:
180: final List list = ruleRuntime.getRegistrations();
181: assertTrue("no registrations found", list.size() > 0);
182:
183: this .ruleAdministrator.deregisterRuleExecutionSet(
184: this .RULES_RESOURCE, null);
185: }
186:
187: public void testRuleBaseConfigurationConstant() throws Exception {
188: // JBRULES-1061
189:
190: Map properties = new HashMap();
191: properties.put(Constants.RES_SOURCE,
192: Constants.RES_SOURCE_TYPE_DECISION_TABLE);
193:
194: properties.put(Constants.RES_RULEBASE_CONFIG,
195: new RuleBaseConfiguration());
196:
197: RuleServiceProviderManager.registerRuleServiceProvider(
198: ExampleRuleEngineFacade.RULE_SERVICE_PROVIDER,
199: RuleServiceProviderImpl.class);
200:
201: RuleServiceProvider ruleServiceProvider = RuleServiceProviderManager
202: .getRuleServiceProvider(ExampleRuleEngineFacade.RULE_SERVICE_PROVIDER);
203: RuleAdministrator ruleAdministrator = ruleServiceProvider
204: .getRuleAdministrator();
205: LocalRuleExecutionSetProvider ruleSetProvider = ruleAdministrator
206: .getLocalRuleExecutionSetProvider(null);
207:
208: try {
209: RuleExecutionSet ruleExecutionSet = ruleSetProvider
210: .createRuleExecutionSet(
211: SpreadsheetIntegrationTest.class
212: .getResourceAsStream("IntegrationExampleTest.xls"),
213: properties);
214: } catch (Exception e) {
215: // fail should not throw an Excetpion
216: }
217: }
218: }
|