001: package org.drools.lang.dsl;
002:
003: import junit.framework.TestCase;
004:
005: public class DefaultDSLMappingEntryTest extends TestCase {
006:
007: private DSLMappingEntry entry;
008:
009: protected void setUp() throws Exception {
010: super .setUp();
011: final String inputKey = "String is \"{value}\"";
012: final String inputValue = "SomeFact(value==\"{value}\")";
013:
014: this .entry = new DefaultDSLMappingEntry(
015: DSLMappingEntry.CONDITION, null, inputKey, inputValue);
016: }
017:
018: protected void tearDown() throws Exception {
019: super .tearDown();
020: }
021:
022: public void testPatternCalculation() {
023: final String inputKey = "The Customer name is {name} and surname is {surname} and it has US$ 50,00 on his {pocket}";
024: final String inputValue = "Customer( name == \"{name}\", surname == \"{surname}\", money > $money )";
025:
026: final String expectedKeyP = "(\\W|^)The\\s+Customer\\s+name\\s+is\\s+(.*?)\\s+and\\s+surname\\s+is\\s+(.*?)\\s+and\\s+it\\s+has\\s+US\\$\\s+50,00\\s+on\\s+his\\s+(.*?)$";
027: final String expectedValP = "Customer( name == \"$2\", surname == \"$3\", money > \\$money )";
028:
029: final DSLMappingEntry entry = new DefaultDSLMappingEntry(
030: DSLMappingEntry.CONDITION, null, inputKey, inputValue);
031:
032: assertEquals(inputKey, entry.getMappingKey());
033: assertEquals(expectedKeyP, entry.getKeyPattern().pattern());
034: assertEquals(inputValue, entry.getMappingValue());
035: assertEquals(expectedValP, entry.getValuePattern());
036:
037: }
038:
039: public void testPatternCalculation2() {
040: final String inputKey = "-name is {name}";
041: final String inputValue = "name == \"{name}\"";
042:
043: final String expectedKeyP = "(\\W|^)-\\s*name\\s+is\\s+(.*?)$";
044: final String expectedValP = "name == \"$2\"";
045:
046: final DSLMappingEntry entry = new DefaultDSLMappingEntry(
047: DSLMappingEntry.CONDITION, null, inputKey, inputValue);
048:
049: assertEquals(inputKey, entry.getMappingKey());
050: assertEquals(expectedKeyP, entry.getKeyPattern().pattern());
051: assertEquals(inputValue, entry.getMappingValue());
052: assertEquals(expectedValP, entry.getValuePattern());
053:
054: }
055:
056: public void testPatternCalculation3() {
057: final String inputKey = "- name is {name}";
058: final String inputValue = "name == \"{name}\"";
059:
060: final String expectedKeyP = "(\\W|^)-\\s*name\\s+is\\s+(.*?)$";
061: final String expectedValP = "name == \"$2\"";
062:
063: final DSLMappingEntry entry = new DefaultDSLMappingEntry(
064: DSLMappingEntry.CONDITION, null, inputKey, inputValue);
065:
066: assertEquals(inputKey, entry.getMappingKey());
067: assertEquals(entry.getKeyPattern().pattern(), expectedKeyP,
068: entry.getKeyPattern().pattern());
069: assertEquals(inputValue, entry.getMappingValue());
070: assertEquals(expectedValP, entry.getValuePattern());
071: }
072:
073: public void testExpandNoSpaces() {
074: final String result = this .entry.getKeyPattern().matcher(
075: "String is \"blah\"").replaceAll(
076: this .entry.getValuePattern());
077:
078: assertEquals("SomeFact(value==\"blah\")", result);
079: }
080:
081: public void testExpandWithLeadingSpace() {
082: final String result = this .entry.getKeyPattern().matcher(
083: "String is \" blah\"").replaceAll(
084: this .entry.getValuePattern());
085:
086: assertEquals("SomeFact(value==\" blah\")", result);
087: }
088:
089: public void testExpandWithMultipleLeadingSpaces() {
090: final String result = this .entry.getKeyPattern().matcher(
091: "String is \" blah\"").replaceAll(
092: this .entry.getValuePattern());
093: assertEquals("SomeFact(value==\" blah\")", result);
094: }
095:
096: public void testExpandWithTrailingSpace() {
097: final String result = this .entry.getKeyPattern().matcher(
098: "String is \"blah \"").replaceAll(
099: this .entry.getValuePattern());
100: assertEquals("SomeFact(value==\"blah \")", result);
101: }
102:
103: public void testExpandWithMultipleTrailingSpaces() {
104: final String result = this .entry.getKeyPattern().matcher(
105: "String is \"blah \"").replaceAll(
106: this .entry.getValuePattern());
107: assertEquals("SomeFact(value==\"blah \")", result);
108: }
109:
110: public void testExpandWithInternalSpace() {
111: final String result = this .entry.getKeyPattern().matcher(
112: "String is \"bl ah\"").replaceAll(
113: this .entry.getValuePattern());
114: assertEquals("SomeFact(value==\"bl ah\")", result);
115: }
116:
117: public void testExpandWithMultipleSpaces() {
118: final String result = this .entry.getKeyPattern().matcher(
119: "String is \" bl ah \"").replaceAll(
120: this .entry.getValuePattern());
121: assertEquals("SomeFact(value==\" bl ah \")", result);
122: }
123:
124: public void testExpandWithDots() {
125: final String inputKey = "- {prop} is {val} ";
126: final String inputValue = "{prop} == {val}";
127:
128: this .entry = new DefaultDSLMappingEntry(
129: DSLMappingEntry.CONDITION, null, inputKey, inputValue);
130:
131: String result = this .entry.getKeyPattern().matcher(
132: "- type is ClientServiceType.TypeGOLD").replaceAll(
133: this .entry.getValuePattern());
134: assertEquals(result, "type == ClientServiceType.TypeGOLD",
135: result);
136: }
137:
138: public void testExpandPartialWords() {
139: final String inputKey = "- {prop} is {val} ";
140: final String inputValue = "{prop} == {val}";
141:
142: this .entry = new DefaultDSLMappingEntry(
143: DSLMappingEntry.CONDITION, null, inputKey, inputValue);
144: // not supposed to expand
145: String result = this .entry.getKeyPattern().matcher(
146: "- type is_not ClientServiceType.TypeGOLD").replaceAll(
147: this .entry.getValuePattern());
148: assertEquals(result,
149: "- type is_not ClientServiceType.TypeGOLD", result);
150: }
151:
152: public void testExpandPartialWords2() {
153: final String inputKey = "- {prop} is_not {val} ";
154: final String inputValue = "{prop} != {val}";
155:
156: this .entry = new DefaultDSLMappingEntry(
157: DSLMappingEntry.CONDITION, null, inputKey, inputValue);
158:
159: String result = this .entry.getKeyPattern().matcher(
160: "- type is_not ClientServiceType.TypeGOLD").replaceAll(
161: this .entry.getValuePattern());
162: assertEquals(result, "type != ClientServiceType.TypeGOLD",
163: result);
164: }
165:
166: public void testExpandPartialWords3() {
167: final String inputKey = "- {prop} is not {val} ";
168: final String inputValue = "{prop} != {val}";
169:
170: this .entry = new DefaultDSLMappingEntry(
171: DSLMappingEntry.CONDITION, null, inputKey, inputValue);
172:
173: String result = this .entry.getKeyPattern().matcher(
174: "- type is not ClientServiceType.TypeGOLD").replaceAll(
175: this .entry.getValuePattern());
176: assertEquals(result, "type != ClientServiceType.TypeGOLD",
177: result);
178: }
179:
180: public void testExpandWithBrackets() {
181: final String inputKey = "attr {attr_name} is in \\[ {values} \\]";
182: final String inputValue = "{attr_name} in ( {values} )";
183:
184: this .entry = new DefaultDSLMappingEntry(
185: DSLMappingEntry.CONDITION, null, inputKey, inputValue);
186:
187: String result = this .entry.getKeyPattern().matcher(
188: "attr name is in [ 'Edson', 'Bob' ]").replaceAll(
189: this .entry.getValuePattern());
190: assertEquals(result, "name in ( 'Edson', 'Bob' )", result);
191: }
192: }
|