001: package org.drools.decisiontable;
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.InputStream;
020: import java.util.regex.Matcher;
021: import java.util.regex.Pattern;
022:
023: import junit.framework.TestCase;
024:
025: import org.drools.decisiontable.parser.RuleMatrixSheetListener;
026:
027: /**
028: * @author <a href="mailto:michael.neale@gmail.com"> Michael Neale</a>
029: *
030: * Some basic unit tests for converter utility.
031: * Note that some of this may still use the drools 2.x syntax, as it is not compiled,
032: * only tested that it generates DRL in the correct structure (not that the DRL itself
033: * is correct).
034: */
035: public class SpreadsheetCompilerUnitTest extends TestCase {
036:
037: public void testLoadFromClassPath() {
038: final SpreadsheetCompiler converter = new SpreadsheetCompiler();
039: final String drl = converter.compile("/data/MultiSheetDST.xls",
040: InputType.XLS);
041:
042: assertNotNull(drl);
043:
044: assertTrue(drl.indexOf("rule \"How cool am I_12\"") > drl
045: .indexOf("rule \"How cool am I_11\""));
046: assertTrue(drl.indexOf("import example.model.User;") > -1);
047: assertTrue(drl.indexOf("import example.model.Car;") > -1);
048: }
049:
050: public void testLoadSpecificWorksheet() {
051: final SpreadsheetCompiler converter = new SpreadsheetCompiler();
052: final InputStream stream = this .getClass().getResourceAsStream(
053: "/data/MultiSheetDST.xls");
054: final String drl = converter.compile(stream, "Another Sheet");
055: assertNotNull(drl);
056: }
057:
058: public void testLoadCustomListener() {
059: final SpreadsheetCompiler converter = new SpreadsheetCompiler();
060: final InputStream stream = this .getClass().getResourceAsStream(
061: "/data/CustomWorkbook.xls");
062: final String drl = converter.compile(stream, InputType.XLS,
063: new RuleMatrixSheetListener());
064: assertNotNull(drl);
065: assertTrue(drl.indexOf("\"matrix\"") != -1);
066: assertTrue(drl.indexOf("$v : FundVisibility") != -1);
067: assertTrue(drl.indexOf("FundType") != -1);
068: assertTrue(drl.indexOf("Role") != -1);
069: }
070:
071: public void testLoadCsv() {
072: final SpreadsheetCompiler converter = new SpreadsheetCompiler();
073: final InputStream stream = this .getClass().getResourceAsStream(
074: "/data/ComplexWorkbook.csv");
075: final String drl = converter.compile(stream, InputType.CSV);
076: assertNotNull(drl);
077:
078: System.out.println(drl);
079:
080: assertTrue(drl.indexOf("myObject.setIsValid(1, 2)") > 0);
081: assertTrue(drl.indexOf("myObject.size () > 50") > 0);
082:
083: assertTrue(drl
084: .indexOf("Foo(myObject.getColour().equals(red), myObject.size () > 1)") > 0);
085: }
086:
087: public void testLoadBasicWithMergedCells() {
088: final SpreadsheetCompiler converter = new SpreadsheetCompiler();
089: final InputStream stream = this .getClass().getResourceAsStream(
090: "/data/BasicWorkbook.xls");
091: final String drl = converter.compile(stream, InputType.XLS);
092:
093: assertNotNull(drl);
094:
095: Pattern p = Pattern
096: .compile(
097: ".*setIsValid\\(Y\\).*setIsValid\\(Y\\).*setIsValid\\(Y\\).*",
098: Pattern.DOTALL | Pattern.MULTILINE);
099: Matcher m = p.matcher(drl);
100: assertTrue(m.matches());
101:
102: assertTrue(drl.indexOf("This is a function block") > -1);
103: assertTrue(drl.indexOf("global Class1 obj1;") > -1);
104: assertTrue(drl.indexOf("myObject.setIsValid(10-Jul-1974)") > -1);
105: assertTrue(drl.indexOf("myObject.getColour().equals(blue)") > -1);
106: assertTrue(drl
107: .indexOf("Foo(myObject.getColour().equals(red), myObject.size () > 1)") > -1);
108:
109: assertTrue(drl.indexOf("b: Bar() eval(myObject.size() < 3)") > -1);
110: assertTrue(drl.indexOf("b: Bar() eval(myObject.size() < 9)") > -1);
111:
112: assertTrue(drl
113: .indexOf("Foo(myObject.getColour().equals(red), myObject.size () > 1)") < drl
114: .indexOf("b: Bar() eval(myObject.size() < 3)"));
115:
116: }
117:
118: }
|