001: package org.drools.decisiontable.model;
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 junit.framework.TestCase;
020:
021: /**
022: * @author <a href="mailto:michael.neale@gmail.com"> Michael Neale</a>
023: *
024: * Tests how the rule parse tree renders itself to a rule XML fragment.
025: */
026: public class RuleRenderTest extends TestCase {
027:
028: public void testRuleRender() {
029: final Rule rule = new Rule("myrule", new Integer(42), 1);
030: rule.setComment("rule comments");
031:
032: final Condition cond = new Condition();
033: cond.setComment("cond comment");
034: cond.setSnippet("cond snippet");
035: rule.addCondition(cond);
036:
037: final Consequence cons = new Consequence();
038: cons.setComment("cons comment");
039: cons.setSnippet("cons snippet;");
040: rule.addConsequence(cons);
041: rule.addConsequence(cons);
042:
043: final DRLOutput out = new DRLOutput();
044: rule.renderDRL(out);
045: final String drl = out.getDRL();
046: assertNotNull(drl);
047:
048: assertTrue(drl.indexOf("cond snippet") != -1);
049: assertTrue(drl.indexOf("cons snippet") != -1);
050: assertTrue(drl.indexOf("salience 42") != -1);
051: assertTrue(drl.indexOf("salience 42") < drl.indexOf("when"));
052: assertTrue(drl.indexOf("cond snippet") < drl.indexOf("then"));
053: assertTrue(drl.indexOf("cons snippet;") > drl.indexOf("then"));
054: assertTrue(drl.indexOf("rule") != -1);
055: assertTrue(drl.indexOf("end") > drl.indexOf("rule "));
056: assertTrue(drl.indexOf("#rule comments") > -1);
057:
058: }
059:
060: public void testAttributes() throws Exception {
061: Rule rule = new Rule("la", new Integer(42), 2);
062:
063: rule.setActivationGroup("foo");
064: rule.setNoLoop("true");
065: rule.setRuleFlowGroup("ruleflowgroup");
066:
067: DRLOutput out = new DRLOutput();
068: rule.renderDRL(out);
069:
070: String result = out.toString();
071:
072: assertTrue(result.indexOf("ruleflow-group \"ruleflowgroup\"") > -1);
073: assertTrue(result.indexOf("no-loop true") > -1);
074: assertTrue(result.indexOf("activation-group \"foo\"") > -1);
075:
076: }
077:
078: public void testSalienceCalculator() {
079: final int rowNumber = 2;
080: final int salience = Rule.calcSalience(rowNumber);
081: assertEquals(65533, salience);
082: }
083:
084: public void testColNumToColName() {
085: String colName = Rule.convertColNumToColName(1);
086: assertEquals("B", colName);
087:
088: colName = Rule.convertColNumToColName(10);
089: assertEquals("K", colName);
090:
091: colName = Rule.convertColNumToColName(42);
092: assertEquals("AQ", colName);
093:
094: colName = Rule.convertColNumToColName(27);
095: assertEquals("AB", colName);
096:
097: colName = Rule.convertColNumToColName(53);
098: assertEquals("BB", colName);
099:
100: }
101:
102: public void testNotEscapeChars() {
103: //bit of a legacy from the olde XML dayes of yesteryeare
104: final Condition cond = new Condition();
105: cond.setSnippet("a < b");
106: final DRLOutput out = new DRLOutput();
107: cond.renderDRL(out);
108:
109: assertTrue(out.toString().indexOf("a < b") != -1);
110:
111: }
112:
113: /**
114: * This checks that if the rule has "nil" salience, then
115: * no salience value should be put in the rule definition.
116: * This allows default salience to work as advertised.
117: *
118: */
119: public void testNilSalience() {
120: Rule rule = new Rule("MyRule", null, 1);
121:
122: DRLOutput out = new DRLOutput();
123: rule.renderDRL(out);
124: String xml = out.toString();
125: int idx = xml.indexOf("salience");
126: assertEquals(-1, idx);
127:
128: rule = new Rule("MyRule", new Integer(42), 1);
129: out = new DRLOutput();
130: rule.renderDRL(out);
131: xml = out.toString();
132: idx = xml.indexOf("salience");
133: assertTrue(idx > -1);
134: }
135:
136: }
|