001: package org.drools.rule.builder.dialect.java;
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 junit.framework.TestCase;
020:
021: public class KnowledgeHelperFixerTest extends TestCase {
022:
023: private static final KnowledgeHelperFixer fixer = new KnowledgeHelperFixer();
024:
025: public void testAdd__Handle__Simple() {
026: String result = KnowledgeHelperFixerTest.fixer
027: .fix("update(myObject );");
028: assertEqualsIgnoreWhitespace("drools.update(myObject );",
029: result);
030:
031: result = KnowledgeHelperFixerTest.fixer
032: .fix("update ( myObject );");
033: assertEqualsIgnoreWhitespace("drools.update( myObject );",
034: result);
035: }
036:
037: public void testAdd__Handle__withNewLines() {
038: final String result = KnowledgeHelperFixerTest.fixer
039: .fix("\n\t\n\tupdate( myObject );");
040: assertEqualsIgnoreWhitespace(
041: "\n\t\n\tdrools.update( myObject );", result);
042: }
043:
044: public void testAdd__Handle__rComplex() {
045: String result = KnowledgeHelperFixerTest.fixer
046: .fix("something update( myObject); other");
047: assertEqualsIgnoreWhitespace(
048: "something drools.update( myObject); other", result);
049:
050: result = KnowledgeHelperFixerTest.fixer
051: .fix("something update ( myObject );");
052: assertEqualsIgnoreWhitespace(
053: "something drools.update( myObject );", result);
054:
055: result = KnowledgeHelperFixerTest.fixer
056: .fix(" update( myObject ); x");
057: assertEqualsIgnoreWhitespace(" drools.update( myObject ); x",
058: result);
059:
060: //should not touch, as it is not a stand alone word
061: result = KnowledgeHelperFixerTest.fixer
062: .fix("xxupdate(myObject ) x");
063: assertEqualsIgnoreWhitespace("xxupdate(myObject ) x", result);
064: }
065:
066: public void testMultipleMatches() {
067: String result = KnowledgeHelperFixerTest.fixer
068: .fix("update(myObject); update(myObject );");
069: assertEqualsIgnoreWhitespace(
070: "drools.update(myObject); drools.update(myObject );",
071: result);
072:
073: result = KnowledgeHelperFixerTest.fixer
074: .fix("xxx update(myObject ); update( myObject ); update( yourObject ); yyy");
075: assertEqualsIgnoreWhitespace(
076: "xxx drools.update(myObject ); drools.update( myObject ); drools.update( yourObject ); yyy",
077: result);
078:
079: }
080:
081: public void testAssert1() {
082: final String raw = "insert( foo );";
083: final String result = "drools.insert( foo );";
084: assertEqualsIgnoreWhitespace(result,
085: KnowledgeHelperFixerTest.fixer.fix(raw));
086: }
087:
088: public void testAssert2() {
089: final String raw = "some code; insert( new String(\"foo\") );\n More();";
090: final String result = "some code; drools.insert( new String(\"foo\") );\n More();";
091: assertEqualsIgnoreWhitespace(result,
092: KnowledgeHelperFixerTest.fixer.fix(raw));
093: }
094:
095: public void testAssertLogical() {
096: final String raw = "some code; insertLogical(new String(\"foo\"));\n More();";
097: final String result = "some code; drools.insertLogical(new String(\"foo\"));\n More();";
098: assertEqualsIgnoreWhitespace(result,
099: KnowledgeHelperFixerTest.fixer.fix(raw));
100: }
101:
102: public void testModifyRetractModifyInsert() {
103: final String raw = "some code; insert( bar ); modifyRetract( foo );\n More(); retract( bar ); modifyInsert( foo );";
104: final String result = "some code; drools.insert( bar ); drools.modifyRetract( foo );\n More(); drools.retract( bar ); drools.modifyInsert( foo );";
105: assertEqualsIgnoreWhitespace(result,
106: KnowledgeHelperFixerTest.fixer.fix(raw));
107: }
108:
109: public void testAllActionsMushedTogether() {
110: String result = KnowledgeHelperFixerTest.fixer
111: .fix("insert(myObject ); update(ourObject);\t retract(herObject);");
112: assertEqualsIgnoreWhitespace(
113: "drools.insert(myObject ); drools.update(ourObject);\t drools.retract(herObject);",
114: result);
115:
116: result = KnowledgeHelperFixerTest.fixer
117: .fix("insert( myObject ); update(ourObject);\t retract(herObject );\ninsert( myObject ); update(ourObject);\t retract( herObject );");
118: assertEqualsIgnoreWhitespace(
119: "drools.insert( myObject ); drools.update(ourObject);\t drools.retract(herObject );\ndrools.insert( myObject ); drools.update(ourObject);\t drools.retract( herObject );",
120: result);
121: }
122:
123: public void testLeaveLargeAlone() {
124: final String original = "yeah yeah yeah minsert( xxx ) this is a long() thing Person (name=='drools') modify a thing";
125: final String result = KnowledgeHelperFixerTest.fixer
126: .fix(original);
127: assertEqualsIgnoreWhitespace(original, result);
128: }
129:
130: public void testWithNull() {
131: final String original = null;
132: final String result = KnowledgeHelperFixerTest.fixer
133: .fix(original);
134: assertEqualsIgnoreWhitespace(original, result);
135: }
136:
137: public void testLeaveAssertAlone() {
138: final String original = "drools.insert(foo)";
139: assertEqualsIgnoreWhitespace(original,
140: KnowledgeHelperFixerTest.fixer.fix(original));
141: }
142:
143: public void testLeaveAssertLogicalAlone() {
144: final String original = "drools.insertLogical(foo)";
145: assertEqualsIgnoreWhitespace(original,
146: KnowledgeHelperFixerTest.fixer.fix(original));
147: }
148:
149: public void testWackyAssert() {
150: final String raw = "System.out.println($person1.getName() + \" and \" + $person2.getName() +\" are sisters\");\n"
151: + "insert($person1.getName(\"foo\") + \" and \" + $person2.getName() +\" are sisters\"); yeah();";
152: final String expected = "System.out.println($person1.getName() + \" and \" + $person2.getName() +\" are sisters\");\n"
153: + "drools.insert($person1.getName(\"foo\") + \" and \" + $person2.getName() +\" are sisters\"); yeah();";
154:
155: assertEqualsIgnoreWhitespace(expected,
156: KnowledgeHelperFixerTest.fixer.fix(raw));
157: }
158:
159: public void testMoreAssertCraziness() {
160: final String raw = "foobar(); (insert(new String(\"blah\").get()); bangBangYudoHono();)";
161: assertEqualsIgnoreWhitespace(
162: "foobar(); (drools.insert(new String(\"blah\").get()); bangBangYudoHono();)",
163: KnowledgeHelperFixerTest.fixer.fix(raw));
164: }
165:
166: private void assertEqualsIgnoreWhitespace(final String expected,
167: final String actual) {
168: if (expected == null || actual == null) {
169: assertEquals(expected, actual);
170: return;
171: }
172: final String cleanExpected = expected.replaceAll("\\s+", "");
173: final String cleanActual = actual.replaceAll("\\s+", "");
174:
175: assertEquals(cleanExpected, cleanActual);
176: }
177: }
|