001: package org.drools.jsr94.rules;
002:
003: /*
004: * $Id: RuleEngineTestBase.java,v 1.4 2004/11/17 03:09:50 dbarnett 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.IOException;
045: import java.io.InputStream;
046: import java.io.InputStreamReader;
047: import java.io.Reader;
048: import java.net.URL;
049: import java.util.HashMap;
050: import java.util.Map;
051:
052: import javax.rules.RuleServiceProvider;
053: import javax.rules.StatefulRuleSession;
054: import javax.rules.StatelessRuleSession;
055:
056: import junit.framework.TestCase;
057:
058: /**
059: * Base class for all drools JSR94 test cases.
060: *
061: * @author N. Alex Rupp (n_alex <at>codehaus.org)
062: * @author <a href="mailto:thomas.diesler@softcon-itec.de">thomas diesler </a>
063: * @author <a href="mailto:michael.frandsen@syngenio.de">Michael Frandsen </a>
064: */
065: public abstract class RuleEngineTestBase extends TestCase {
066: protected StatefulRuleSession statefulSession;
067:
068: protected StatelessRuleSession statelessSession;
069:
070: protected ExampleRuleEngineFacade engine;
071:
072: protected String bindUri = "sisters.drl";
073: protected String bindUri_drl = "sisters_expander.drl";
074: protected String bindUri_dsl = "sisters_expander.dsl";
075: protected String bindUri_globals = "sisters_globals.drl";
076:
077: protected RuleServiceProvider ruleServiceProvider;
078:
079: /**
080: * Setup the test case.
081: */
082: protected void setUp() throws Exception {
083: super .setUp();
084: this .engine = new ExampleRuleEngineFacade();
085: this .engine.addRuleExecutionSet(this .bindUri,
086: RuleEngineTestBase.class
087: .getResourceAsStream(this .bindUri));
088:
089: final Map map = new HashMap();
090: final Reader reader = new InputStreamReader(
091: RuleEngineTestBase.class
092: .getResourceAsStream(this .bindUri_dsl));
093:
094: map.put("dsl", this .getDSLText(reader).toString());
095: this .engine.addRuleExecutionSet(this .bindUri_drl,
096: RuleEngineTestBase.class
097: .getResourceAsStream(this .bindUri_drl), map);
098:
099: this .engine.addRuleExecutionSet(this .bindUri_globals,
100: RuleEngineTestBase.class
101: .getResourceAsStream(this .bindUri_globals));
102:
103: this .ruleServiceProvider = this .engine.getRuleServiceProvider();
104: // this.statelessSession = engine.getStatelessRuleSession( bindUri );
105: // this.statefulSession = engine.getStatefulRuleSession( bindUri );
106:
107: }
108:
109: /*
110: * Taken from DRLParser
111: */
112: private StringBuffer getDSLText(final Reader reader)
113: throws IOException {
114: final StringBuffer text = new StringBuffer();
115:
116: final char[] buf = new char[1024];
117: int len = 0;
118:
119: while ((len = reader.read(buf)) >= 0) {
120: text.append(buf, 0, len);
121: }
122: return text;
123: }
124:
125: /**
126: * Get the requested resource from the ClassLoader.
127: *
128: * @see ClassLoader#getResource
129: */
130: protected URL getResource(final String res) {
131: return getClass().getClassLoader().getResource(res);
132: }
133:
134: /**
135: * Get the requested resource from the ClassLoader.
136: *
137: * @see ClassLoader#getResourceAsStream
138: */
139: protected InputStream getResourceAsStream(final String res) {
140: return getClass().getClassLoader().getResourceAsStream(res);
141: }
142: }
|