001: package org.drools.decisiontable.parser;
002:
003: import java.io.InputStream;
004: import java.util.List;
005: import java.util.Map;
006:
007: import junit.framework.TestCase;
008:
009: public class DefaultTemplateContainerTest extends TestCase {
010: public void testParseTemplate() {
011: InputStream is = DefaultTemplateContainerTest.class
012: .getResourceAsStream("/templates/test_template_simple.drl");
013: DefaultTemplateContainer t = new DefaultTemplateContainer(is);
014: assertEquals("package This_is_a_ruleset;\n", t.getHeader());
015: assertEquals(1, t.getColumns().length);
016: assertEquals("name", t.getColumns()[0].getName());
017: Map templates = t.getTemplates();
018: assertEquals(1, templates.size());
019: RuleTemplate template = (RuleTemplate) templates
020: .get("template1");
021: assertNotNull(template);
022: List columns = template.getColumns();
023: assertEquals(1, columns.size());
024: assertEquals("name", columns.get(0));
025: String contents = template.getContents();
026: assertTrue(contents
027: .startsWith("rule \"How cool is @{name} @{row.rowNumber}\""));
028: assertTrue(contents.endsWith("then\nend\n"));
029: }
030:
031: public void testParseTemplateNoHeader() {
032: try {
033: InputStream is = DefaultTemplateContainerTest.class
034: .getResourceAsStream("/templates/test_template_invalid1.drl");
035: new DefaultTemplateContainer(is);
036: fail("DecisionTableParseException expected");
037: } catch (DecisionTableParseException expected) {
038: assertEquals("Missing header", expected.getMessage());
039: }
040: }
041:
042: public void testParseTemplateNoHeaderColumns() {
043: try {
044: InputStream is = DefaultTemplateContainerTest.class
045: .getResourceAsStream("/templates/test_template_invalid2.drl");
046: new DefaultTemplateContainer(is);
047: fail("DecisionTableParseException expected");
048: } catch (DecisionTableParseException expected) {
049: assertEquals("Missing header columns", expected
050: .getMessage());
051: }
052: }
053:
054: public void testParseTemplateNoTemplates() {
055: try {
056: InputStream is = DefaultTemplateContainerTest.class
057: .getResourceAsStream("/templates/test_template_invalid3.drl");
058: new DefaultTemplateContainer(is);
059: fail("DecisionTableParseException expected");
060: } catch (DecisionTableParseException expected) {
061: assertEquals("Missing templates", expected.getMessage());
062: }
063: }
064:
065: public void testParseTemplateNoEndTemplate() {
066: try {
067: InputStream is = DefaultTemplateContainerTest.class
068: .getResourceAsStream("/templates/test_template_invalid4.drl");
069: new DefaultTemplateContainer(is);
070: fail("DecisionTableParseException expected");
071: } catch (DecisionTableParseException expected) {
072: assertEquals("Missing end template", expected.getMessage());
073: }
074: }
075:
076: public void testNullInputStream() {
077: try {
078: new DefaultTemplateContainer((InputStream) null);
079: fail("NullPointerException expected");
080: } catch (NullPointerException expected) {
081: }
082: }
083:
084: public void testInvalidTemplatePath() {
085: try {
086: new DefaultTemplateContainer("invalid path");
087: fail("NullPointerException expected");
088: } catch (NullPointerException expected) {
089: }
090: }
091:
092: public void testParseComplexTemplate() {
093: InputStream is = DefaultTemplateContainerTest.class
094: .getResourceAsStream("/templates/test_template_complex.drl");
095: DefaultTemplateContainer t = new DefaultTemplateContainer(is);
096: assertEquals("package This_is_a_ruleset;\n", t.getHeader());
097: Column[] columnList = t.getColumns();
098: assertEquals(5, columnList.length);
099: assertEquals("first_name", columnList[0].getName());
100: assertEquals("last_name", columnList[1].getName());
101: assertEquals("age", columnList[2].getName());
102: assertEquals("city", columnList[3].getName());
103: assertEquals("phone", columnList[4].getName());
104: Map templates = t.getTemplates();
105: assertEquals(2, templates.size());
106:
107: RuleTemplate template = (RuleTemplate) templates
108: .get("template1");
109: assertNotNull(template);
110: List columns = template.getColumns();
111: assertEquals(1, columns.size());
112: assertEquals("first_name", columns.get(0));
113: String contents = template.getContents();
114: assertTrue(contents
115: .startsWith("rule \"How cool is @{first_name} @{row.rowNumber}\""));
116: assertTrue(contents.endsWith("then\nend\n"));
117:
118: template = (RuleTemplate) templates.get("template2");
119: assertNotNull(template);
120: columns = template.getColumns();
121: assertEquals(2, columns.size());
122: assertEquals("first_name", columns.get(0));
123: assertEquals("last_name", columns.get(1));
124: contents = template.getContents();
125: assertTrue(contents
126: .startsWith("rule \"How uncool is @{first_name} @{row.rowNumber}\""));
127: assertTrue(contents.endsWith("then\nend\n"));
128:
129: }
130: }
|