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