001: package org.drools.brms.client.modeldriven;
002:
003: import java.util.ArrayList;
004: import java.util.HashMap;
005: import java.util.List;
006:
007: import junit.framework.TestCase;
008:
009: import org.drools.brms.server.rules.SuggestionCompletionLoader;
010:
011: public class SuggestionCompletionTest extends TestCase {
012:
013: public void testNestedImports() {
014: String pkg = "package org.test\n import org.drools.brms.client.modeldriven.SuggestionCompletionTest.NestedClass";
015:
016: SuggestionCompletionLoader loader = new SuggestionCompletionLoader();
017: SuggestionCompletionEngine engine = loader.getSuggestionEngine(
018: pkg, new ArrayList(), new ArrayList());
019:
020: assertEquals("String", engine.getFieldType(
021: "SuggestionCompletionTest$NestedClass", "name"));
022: }
023:
024: public void testStringNonNumeric() {
025: String pkg = "package org.test\n import org.drools.brms.client.modeldriven.Alert";
026:
027: SuggestionCompletionLoader loader = new SuggestionCompletionLoader();
028: SuggestionCompletionEngine engine = loader.getSuggestionEngine(
029: pkg, new ArrayList(), new ArrayList());
030:
031: assertEquals(SuggestionCompletionEngine.TYPE_STRING, engine
032: .getFieldType("Alert", "message"));
033:
034: }
035:
036: public void testDataEnums() {
037: String pkg = "package org.test\n import org.drools.brms.client.modeldriven.SuggestionCompletionTest.NestedClass";
038:
039: SuggestionCompletionLoader loader = new SuggestionCompletionLoader();
040:
041: List enums = new ArrayList();
042:
043: enums
044: .add("'Person.age' : [42, 43] \n 'Person.sex' : ['M', 'F']");
045: enums.add("'Driver.sex' : ['M', 'F']");
046:
047: SuggestionCompletionEngine engine = loader.getSuggestionEngine(
048: pkg, new ArrayList(), new ArrayList(), enums);
049: assertEquals("String", engine.getFieldType(
050: "SuggestionCompletionTest$NestedClass", "name"));
051:
052: assertEquals(3, engine.dataEnumLists.size());
053: String[] items = (String[]) engine.dataEnumLists
054: .get("Person.age");
055: assertEquals(2, items.length);
056: assertEquals("42", items[0]);
057: assertEquals("43", items[1]);
058: }
059:
060: public void testCompletions() {
061:
062: final SuggestionCompletionEngine com = new SuggestionCompletionEngine();
063:
064: com.factTypes = new String[] { "Person", "Vehicle" };
065: com.fieldsForType = new HashMap() {
066: {
067: put("Person", new String[] { "age", "name", "rank" });
068: put("Vehicle", new String[] { "type", "make" });
069: }
070: };
071: com.fieldTypes = new HashMap() {
072: {
073: put("Person.age", "Numeric");
074: put("Person.rank", "Comparable");
075: put("Person.name", "String");
076: put("Vehicle.make", "String");
077: put("Vehicle.type", "String");
078: }
079: };
080: com.globalTypes = new HashMap() {
081: {
082: put("bar", "Person");
083: put("baz", "Vehicle");
084: }
085: };
086:
087: String[] c = com.getConditionalElements();
088: assertEquals("not", c[0]);
089: assertEquals("exists", c[1]);
090: assertEquals("or", c[2]);
091:
092: c = com.getFactTypes();
093: assertEquals(2, c.length);
094: assertContains("Person", c);
095: assertContains("Vehicle", c);
096:
097: c = com.getFieldCompletions("Person");
098: assertEquals("age", c[0]);
099: assertEquals("name", c[1]);
100:
101: c = com.getFieldCompletions("Vehicle");
102: assertEquals("type", c[0]);
103: assertEquals("make", c[1]);
104:
105: c = com.getOperatorCompletions("Person", "name");
106: assertEquals(3, c.length);
107: assertEquals("==", c[0]);
108: assertEquals("!=", c[1]);
109: assertEquals("matches", c[2]);
110:
111: c = com.getOperatorCompletions("Person", "age");
112: assertEquals(6, c.length);
113: assertEquals(c[0], "==");
114: assertEquals(c[1], "!=");
115: assertEquals(c[2], "<");
116: assertEquals(c[3], ">");
117:
118: c = com.getOperatorCompletions("Person", "rank");
119: assertEquals(6, c.length);
120: assertEquals(c[0], "==");
121: assertEquals(c[1], "!=");
122: assertEquals(c[2], "<");
123: assertEquals(c[3], ">");
124:
125: c = com.getConnectiveOperatorCompletions("Vehicle", "make");
126: assertEquals(5, c.length);
127: assertEquals("|| ==", c[0]);
128:
129: c = com.getGlobalVariables();
130: assertEquals(2, c.length);
131: assertEquals("baz", c[0]);
132: assertEquals("bar", c[1]);
133:
134: c = com.getFieldCompletionsForGlobalVariable("bar");
135: assertEquals(3, c.length);
136: assertEquals("age", c[0]);
137: assertEquals("name", c[1]);
138:
139: c = com.getFieldCompletionsForGlobalVariable("baz");
140: assertEquals(2, c.length);
141: assertEquals("type", c[0]);
142: assertEquals("make", c[1]);
143:
144: //check that it has default operators for general objects
145: c = com.getOperatorCompletions("Person", "wankle");
146: assertEquals(2, c.length);
147:
148: assertEquals("Numeric", com.getFieldType("Person", "age"));
149:
150: }
151:
152: public void testAdd() {
153: final SuggestionCompletionEngine com = new SuggestionCompletionEngine();
154: com.factTypes = new String[] { "Foo" };
155: com.fieldsForType = new HashMap() {
156: {
157: put("Foo", new String[] { "a" });
158: }
159: };
160:
161: assertEquals(1, com.getFactTypes().length);
162: assertEquals("Foo", com.getFactTypes()[0]);
163:
164: assertEquals(1, com.getFieldCompletions("Foo").length);
165: assertEquals("a", com.getFieldCompletions("Foo")[0]);
166:
167: }
168:
169: private void assertContains(final String string, final String[] c) {
170:
171: for (int i = 0; i < c.length; i++) {
172: if (string.equals(c[i])) {
173: return;
174: }
175: }
176: fail("String array did not contain: " + string);
177:
178: }
179:
180: public void testGlobalAndFacts() {
181: final SuggestionCompletionEngine com = new SuggestionCompletionEngine();
182:
183: com.globalTypes = new HashMap() {
184: {
185: put("y", "Foo");
186: }
187: };
188: com.fieldsForType = new HashMap() {
189: {
190: put("Foo", new String[] { "a" });
191: }
192: };
193:
194: assertFalse(com.isGlobalVariable("x"));
195: assertTrue(com.isGlobalVariable("y"));
196: }
197:
198: public static class NestedClass {
199: private String name;
200:
201: public String getName() {
202: return name;
203: }
204:
205: public void setName(String name) {
206: this.name = name;
207: }
208: }
209: }
|