001: package org.drools.lang.dsl;
002:
003: import java.io.InputStreamReader;
004: import java.io.Reader;
005: import java.io.StringReader;
006: import java.util.regex.Matcher;
007: import java.util.regex.Pattern;
008:
009: import junit.framework.TestCase;
010:
011: import org.drools.lang.ExpanderException;
012:
013: public class DefaultExpanderTest extends TestCase {
014: private DSLMappingFile file = null;
015: private DefaultExpander expander = null;
016:
017: protected void setUp() throws Exception {
018: final String filename = "test_metainfo.dsl";
019: final Reader reader = new InputStreamReader(this .getClass()
020: .getResourceAsStream(filename));
021: this .file = new DSLMappingFile();
022: this .file.parseAndLoad(reader);
023: reader.close();
024:
025: this .expander = new DefaultExpander();
026:
027: super .setUp();
028: }
029:
030: protected void tearDown() throws Exception {
031: super .tearDown();
032: }
033:
034: public void testAddDSLMapping() {
035: this .expander.addDSLMapping(this .file.getMapping());
036: // should not raise any exception
037: }
038:
039: public void testRegexp() throws Exception {
040: this .expander.addDSLMapping(this .file.getMapping());
041: final Reader rules = new InputStreamReader(this .getClass()
042: .getResourceAsStream("test_expansion.dslr"));
043: final String result = this .expander.expand(rules);
044: }
045:
046: public void testExpandParts() throws Exception {
047: DSLMappingFile file = new DSLMappingFile();
048: String dsl = "[when]foo=Foo()\n[then]bar {num}=baz({num});";
049: file.parseAndLoad(new StringReader(dsl));
050: assertEquals(0, file.getErrors().size());
051: DefaultExpander ex = new DefaultExpander();
052: ex.addDSLMapping(file.getMapping());
053:
054: System.err.println(ex
055: .expand("rule 'x' \n when \n foo \n then \n end"));
056: }
057:
058: public void testExpandFailure() throws Exception {
059:
060: DSLMappingFile file = new DSLMappingFile();
061: String dsl = "[when]foo=Foo()\n[then]bar {num}=baz({num});";
062: file.parseAndLoad(new StringReader(dsl));
063: assertEquals(0, file.getErrors().size());
064:
065: DefaultExpander ex = new DefaultExpander();
066: ex.addDSLMapping(file.getMapping());
067: String source = "rule 'q'\nagenda-group 'x'\nwhen\n foo \nthen\n bar 42\nend";
068: String drl = ex.expand(source);
069: assertFalse(ex.hasErrors());
070:
071: ex = new DefaultExpander();
072: ex.addDSLMapping(file.getMapping());
073:
074: source = "rule 'q' agenda-group 'x'\nwhen\n foos \nthen\n bar 42\n end";
075: drl = ex.expand(source);
076: //System.out.println( drl );
077: assertTrue(ex.hasErrors());
078: assertEquals(1, ex.getErrors().size());
079: //System.err.println(( (ExpanderException) ex.getErrors().get( 0 )).getMessage());
080: }
081:
082: public void testExpandWithKeywordClashes() throws Exception {
083:
084: DSLMappingFile file = new DSLMappingFile();
085: String dsl = "[when]Invoke rule executor=ruleExec: RuleExecutor()\n"
086: + "[then]Execute rule \"{id}\"=ruleExec.ExecuteSubRule( new Long({id}));";
087: file.parseAndLoad(new StringReader(dsl));
088: assertEquals(0, file.getErrors().size());
089:
090: DefaultExpander ex = new DefaultExpander();
091: ex.addDSLMapping(file.getMapping());
092: String source = "package something;\n\nrule \"1\"\nwhen\n Invoke rule executor\nthen\n Execute rule \"5\"\nend";
093: String expected = "package something;\n\nrule \"1\"\nwhen\n ruleExec: RuleExecutor()\nthen\n ruleExec.ExecuteSubRule( new Long(5));\nend\n";
094: String drl = ex.expand(source);
095: // System.out.println("["+drl+"]" );
096: // System.out.println("["+expected+"]" );
097: assertFalse(ex.hasErrors());
098: assertEquals(expected, drl);
099:
100: }
101:
102: public void testLineNumberError() throws Exception {
103: DSLMappingFile file = new DSLMappingFile();
104: String dsl = "[when]foo=Foo()\n[then]bar {num}=baz({num});";
105: file.parseAndLoad(new StringReader(dsl));
106:
107: DefaultExpander ex = new DefaultExpander();
108: ex.addDSLMapping(file.getMapping());
109: String source = "rule 'q'\nagenda-group 'x'\nwhen\n __ \nthen\n bar 42\n\tgoober\nend";
110: ex.expand(source);
111: assertTrue(ex.hasErrors());
112: assertEquals(2, ex.getErrors().size());
113: ExpanderException err = (ExpanderException) ex.getErrors().get(
114: 0);
115: assertEquals(4, err.getLine());
116: err = (ExpanderException) ex.getErrors().get(1);
117: assertEquals(7, err.getLine());
118:
119: }
120:
121: private boolean equalsIgnoreWhiteSpace(String expected,
122: String actual) {
123: String patternStr = expected.replaceAll("\\s+",
124: "(\\\\s|\\\\n|\\\\r)*");//.replaceAll( "\\n", "\\s*\\$" );
125: Pattern pattern = Pattern.compile(patternStr, Pattern.DOTALL);
126: Matcher m = pattern.matcher(actual);
127: return m.matches();
128: }
129: }
|