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