001: package org.drools.jsr94.rules.admin;
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 java.io.IOException;
020: import java.io.InputStream;
021: import java.io.InputStreamReader;
022: import java.util.ArrayList;
023:
024: import javax.rules.admin.RuleAdministrator;
025: import javax.rules.admin.RuleExecutionSet;
026: import javax.rules.admin.RuleExecutionSetProvider;
027:
028: import org.drools.compiler.PackageBuilder;
029: import org.drools.jsr94.rules.RuleEngineTestBase;
030: import org.drools.rule.Package;
031:
032: /**
033: * Test the RuleExecutionSetProvider implementation.
034: *
035: * @author N. Alex Rupp (n_alex <at>codehaus.org)
036: * @author <a href="mailto:michael.frandsen@syngenio.de">michael frandsen </a>
037: */
038: public class RuleExecutionSetProviderTest extends RuleEngineTestBase {
039: private RuleAdministrator ruleAdministrator;
040:
041: private RuleExecutionSetProvider ruleSetProvider;
042:
043: private Package pkg;
044:
045: /**
046: * Setup the test case.
047: */
048: protected void setUp() throws Exception {
049: super .setUp();
050: this .ruleAdministrator = this .ruleServiceProvider
051: .getRuleAdministrator();
052: this .ruleSetProvider = this .ruleAdministrator
053: .getRuleExecutionSetProvider(null);
054:
055: initPackage();
056: }
057:
058: private void initPackage() {
059: final InputStream resourceAsStream = null;
060: try {
061: final PackageBuilder builder = new PackageBuilder();
062: builder.addPackageFromDrl(new InputStreamReader(
063: RuleEngineTestBase.class
064: .getResourceAsStream(this .bindUri)));
065: final Package pkg = builder.getPackage();
066:
067: this .pkg = pkg;
068: } catch (final IOException e) {
069: throw new ExceptionInInitializerError(
070: "setUp() could not init the "
071: + "RuleSet due to an IOException in the InputStream: "
072: + e);
073: } catch (final Exception e) {
074: throw new ExceptionInInitializerError(
075: "setUp() could not init the RuleSet, " + e);
076: } finally {
077: if (resourceAsStream != null) {
078: try {
079: resourceAsStream.close();
080: } catch (final IOException e) {
081: e.printStackTrace();
082: }
083: }
084: }
085: }
086:
087: protected void tearDown() {
088: this .pkg = null;
089: }
090:
091: // Rule files are no XML
092: // /**
093: // * Test createRuleExecutionSet from DOM.
094: // */
095: // public void testCreateFromElement( ) throws Exception
096: // {
097: // DOMParser parser = new DOMParser( );
098: // Document doc = null;
099: // try
100: // {
101: // parser.parse( new InputSource(
102: // RuleEngineTestBase.class.getResourceAsStream( bindUri ) ) );
103: // doc = parser.getDocument( );
104: // }
105: // catch ( SAXException e )
106: // {
107: // fail( "could not parse incoming data stream: " + e );
108: // }
109: // catch ( IOException e )
110: // {
111: // fail( "could not open incoming data stream: " + e );
112: // }
113: // Element element = null;
114: // NodeList children = doc.getChildNodes( );
115: // if ( children != null )
116: // {
117: // for ( int i = 0; i < children.getLength( ); i++ )
118: // {
119: // Node child = children.item( i );
120: // if ( Node.ELEMENT_NODE == child.getNodeType( ) )
121: // {
122: // element = ( Element ) child;
123: // }
124: // }
125: // }
126: //
127: // if ( element != null )
128: // {
129: // RuleExecutionSet testRuleSet =
130: // ruleSetProvider.createRuleExecutionSet( element, null );
131: // assertEquals(
132: // "rule set name", "Sisters Rules", testRuleSet.getName( ) );
133: // assertEquals(
134: // "number of rules", 1, testRuleSet.getRules( ).size( ) );
135: // }
136: // else
137: // {
138: // fail( "could not build an org.w3c.dom.Element" );
139: // }
140: // }
141:
142: /**
143: * Test createRuleExecutionSet from Serializable.
144: */
145: public void testCreateFromSerializable() throws Exception {
146: final RuleExecutionSet ruleExecutionSet = this .ruleSetProvider
147: .createRuleExecutionSet(this .pkg, null);
148: assertEquals("rule set name", "SistersRules", ruleExecutionSet
149: .getName());
150: assertEquals("number of rules", 1, ruleExecutionSet.getRules()
151: .size());
152: }
153:
154: /**
155: * Test createRuleExecutionSet from URI.
156: */
157: public void testCreateFromURI() throws Exception {
158: final String rulesUri = RuleEngineTestBase.class.getResource(
159: this .bindUri).toExternalForm();
160: final RuleExecutionSet testRuleSet = this .ruleSetProvider
161: .createRuleExecutionSet(rulesUri, null);
162: assertEquals("rule set name", "SistersRules", testRuleSet
163: .getName());
164: assertEquals("number of rules", 1, testRuleSet.getRules()
165: .size());
166: }
167:
168: public void testIncompatibleSerializableCreation() throws Exception {
169: try {
170: final RuleExecutionSet testRuleSet = this .ruleSetProvider
171: .createRuleExecutionSet(new ArrayList(), null);
172: fail("Should have thrown an IllegalArgumentException. ArrayList "
173: + "objects are not valid AST representations. "
174: + testRuleSet);
175: } catch (final IllegalArgumentException e) {
176: /*
177: * this is supposed to happen if you pass in a serializable object
178: * that isn't a supported AST representation.
179: */
180: }
181: }
182: }
|