001: package org.drools.lang.dsl;
002:
003: import java.io.IOException;
004: import java.io.InputStreamReader;
005: import java.io.Reader;
006: import java.io.StringReader;
007:
008: import junit.framework.TestCase;
009:
010: public class DSLMappingFileTest extends TestCase {
011: private DSLMappingFile file = null;
012: private final String filename = "test_metainfo.dsl";
013:
014: protected void setUp() throws Exception {
015: super .setUp();
016: }
017:
018: protected void tearDown() throws Exception {
019: super .tearDown();
020: }
021:
022: public void testParseFile() {
023: try {
024: final Reader reader = new InputStreamReader(this .getClass()
025: .getResourceAsStream(this .filename));
026: this .file = new DSLMappingFile();
027:
028: final boolean parsingResult = this .file
029: .parseAndLoad(reader);
030: reader.close();
031:
032: assertTrue(this .file.getErrors().toString(), parsingResult);
033: assertTrue(this .file.getErrors().isEmpty());
034:
035: assertEquals(31, this .file.getMapping().getEntries().size());
036: } catch (final IOException e) {
037: e.printStackTrace();
038: fail("Should not raise exception ");
039: }
040:
041: }
042:
043: public void testParseFileWithBrackets() {
044: String file = "[when][]ATTRIBUTE \"{attr}\" IS IN [{list}]=Attribute( {attr} in ({list}) )";
045: try {
046: final Reader reader = new StringReader(file);
047: this .file = new DSLMappingFile();
048:
049: final boolean parsingResult = this .file
050: .parseAndLoad(reader);
051: reader.close();
052:
053: assertTrue(this .file.getErrors().toString(), parsingResult);
054: assertTrue(this .file.getErrors().isEmpty());
055:
056: assertEquals(1, this .file.getMapping().getEntries().size());
057:
058: DSLMappingEntry entry = (DSLMappingEntry) this .file
059: .getMapping().getEntries().get(0);
060:
061: assertEquals(DSLMappingEntry.CONDITION, entry.getSection());
062: assertEquals(DSLMappingEntry.EMPTY_METADATA, entry
063: .getMetaData());
064: assertEquals("ATTRIBUTE \"{attr}\" IS IN [{list}]", entry
065: .getMappingKey());
066: assertEquals("Attribute( {attr} in ({list}) )", entry
067: .getMappingValue());
068:
069: } catch (final IOException e) {
070: e.printStackTrace();
071: fail("Should not raise exception ");
072: }
073: }
074:
075: public void testParseFileWithEscaptedBrackets() {
076: String file = "[when][]ATTRIBUTE \"{attr}\" IS IN \\[{list}\\]=Attribute( {attr} in ({list}) )";
077: try {
078: final Reader reader = new StringReader(file);
079: this .file = new DSLMappingFile();
080:
081: final boolean parsingResult = this .file
082: .parseAndLoad(reader);
083: reader.close();
084:
085: assertTrue(this .file.getErrors().toString(), parsingResult);
086: assertTrue(this .file.getErrors().isEmpty());
087:
088: assertEquals(1, this .file.getMapping().getEntries().size());
089:
090: DSLMappingEntry entry = (DSLMappingEntry) this .file
091: .getMapping().getEntries().get(0);
092:
093: assertEquals(DSLMappingEntry.CONDITION, entry.getSection());
094: assertEquals(DSLMappingEntry.EMPTY_METADATA, entry
095: .getMetaData());
096: assertEquals("ATTRIBUTE \"{attr}\" IS IN \\[{list}\\]",
097: entry.getMappingKey());
098: assertEquals("Attribute( {attr} in ({list}) )", entry
099: .getMappingValue());
100:
101: } catch (final IOException e) {
102: e.printStackTrace();
103: fail("Should not raise exception ");
104: }
105:
106: }
107:
108: }
|