001: package org.drools.brms.server.rules;
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 java.util.Collections;
020:
021: import junit.framework.TestCase;
022:
023: import org.drools.brms.client.common.AssetFormats;
024: import org.drools.brms.client.modeldriven.SuggestionCompletionEngine;
025: import org.drools.brms.server.util.BRMSSuggestionCompletionLoader;
026: import org.drools.brms.server.util.TestEnvironmentSessionHelper;
027: import org.drools.repository.AssetItem;
028: import org.drools.repository.PackageItem;
029: import org.drools.repository.RulesRepository;
030:
031: public class BRMSSuggestionCompletionLoaderTest extends TestCase {
032:
033: public void testLoader() throws Exception {
034:
035: RulesRepository repo = new RulesRepository(
036: TestEnvironmentSessionHelper.getSession());
037: PackageItem item = repo.createPackage("testLoader",
038: "to test the loader");
039: item.updateHeader("import java.util.Date");
040: repo.save();
041:
042: BRMSSuggestionCompletionLoader loader = new BRMSSuggestionCompletionLoader();
043: String header = item.getHeader();
044:
045: SuggestionCompletionEngine engine = loader
046: .getSuggestionEngine(item);
047: assertNotNull(engine);
048:
049: }
050:
051: public void testStripUnNeededFields() {
052: SuggestionCompletionLoader loader = new SuggestionCompletionLoader();
053: String[] result = loader.removeIrrelevantFields(new String[] {
054: "foo", "toString", "class", "hashCode" });
055: assertEquals(1, result.length);
056: assertEquals("foo", result[0]);
057: }
058:
059: public void testGetShortNameOfClass() {
060: SuggestionCompletionLoader loader = new SuggestionCompletionLoader();
061:
062: assertEquals("Object", loader.getShortNameOfClass(Object.class
063: .getName()));
064:
065: assertEquals("Foo", loader.getShortNameOfClass("Foo"));
066: }
067:
068: public void testFactTemplates() throws Exception {
069:
070: RulesRepository repo = new RulesRepository(
071: TestEnvironmentSessionHelper.getSession());
072: PackageItem item = repo.createPackage("testLoader2",
073: "to test the loader for fact templates");
074: item
075: .updateHeader("import java.util.Date\ntemplate Person\njava.lang.String name\nDate birthDate\nend");
076: repo.save();
077:
078: BRMSSuggestionCompletionLoader loader = new BRMSSuggestionCompletionLoader();
079:
080: SuggestionCompletionEngine engine = loader
081: .getSuggestionEngine(item);
082: assertNotNull(engine);
083: String[] factTypes = engine.getFactTypes();
084:
085: assertEquals(2, factTypes.length);
086: assertEquals("Date", factTypes[0]);
087: assertEquals("Person", factTypes[1]);
088:
089: String[] fieldsForType = engine.getFieldCompletions("Person");
090: assertEquals(2, fieldsForType.length);
091: assertEquals("name", fieldsForType[0]);
092: assertEquals("birthDate", fieldsForType[1]);
093:
094: String fieldType = engine.getFieldType("Person", "name");
095: assertEquals(SuggestionCompletionEngine.TYPE_STRING, fieldType);
096: fieldType = engine.getFieldType("Person", "birthDate");
097: assertEquals(SuggestionCompletionEngine.TYPE_COMPARABLE,
098: fieldType);
099: }
100:
101: public void testLoadDSLs() throws Exception {
102: String dsl = "[when]The agents rating is {rating}=doNothing()\n[then]Send a notification to manufacturing '{message}'=foo()";
103: RulesRepository repo = new RulesRepository(
104: TestEnvironmentSessionHelper.getSession());
105: PackageItem item = repo.createPackage("testLoadDSLs",
106: "to test the loader for DSLs");
107: AssetItem asset = item.addAsset("mydsl", "");
108: asset.updateFormat(AssetFormats.DSL);
109: asset.updateContent(dsl);
110: asset.checkin("ok");
111:
112: item = repo.loadPackage("testLoadDSLs");
113: BRMSSuggestionCompletionLoader loader = new BRMSSuggestionCompletionLoader();
114:
115: SuggestionCompletionEngine eng = loader
116: .getSuggestionEngine(item);
117: assertNotNull(eng.dataEnumLists);
118: assertEquals(Collections.EMPTY_MAP, eng.dataEnumLists);
119: assertFalse(loader.hasErrors());
120: assertEquals(1, eng.actionDSLSentences.length);
121: assertEquals(1, eng.conditionDSLSentences.length);
122:
123: assertEquals("The agents rating is {rating}",
124: eng.conditionDSLSentences[0].sentence);
125: assertEquals(
126: "Send a notification to manufacturing '{message}'",
127: eng.actionDSLSentences[0].sentence);
128:
129: dsl = "koo kooo ca choo";
130: asset.updateContent(dsl);
131: asset.checkin("boo");
132:
133: item = repo.loadPackage("testLoadDSLs");
134: loader = new BRMSSuggestionCompletionLoader();
135: loader.getSuggestionEngine(item);
136: assertTrue(loader.hasErrors());
137:
138: }
139:
140: public void testLoadEnumerations() throws Exception {
141: String enumeration = "'Person.sex' : ['M', 'F']";
142: RulesRepository repo = new RulesRepository(
143: TestEnvironmentSessionHelper.getSession());
144: PackageItem item = repo.createPackage("testLoadEnums",
145: "to test the loader for enums");
146: AssetItem asset = item.addAsset("myenum", "");
147: asset.updateFormat(AssetFormats.ENUMERATION);
148: asset.updateContent(enumeration);
149: asset.checkin("ok");
150:
151: item = repo.loadPackage("testLoadEnums");
152: BRMSSuggestionCompletionLoader loader = new BRMSSuggestionCompletionLoader();
153: SuggestionCompletionEngine sce = loader
154: .getSuggestionEngine(item);
155:
156: assertFalse(loader.hasErrors());
157: assertEquals(1, sce.dataEnumLists.size());
158:
159: asset.updateContent("goober boy");
160: asset.checkin("yeah");
161: item = repo.loadPackage("testLoadEnums");
162: loader = new BRMSSuggestionCompletionLoader();
163: sce = loader.getSuggestionEngine(item);
164: assertTrue(loader.hasErrors());
165:
166: }
167:
168: public void testErrors() throws Exception {
169: RulesRepository repo = new RulesRepository(
170: TestEnvironmentSessionHelper.getSession());
171: PackageItem item = repo.createPackage("testErrorsInPackage",
172: "to test error handling");
173:
174: BRMSSuggestionCompletionLoader loader = new BRMSSuggestionCompletionLoader();
175:
176: assertNotNull(loader.getSuggestionEngine(item));
177: assertFalse(loader.hasErrors());
178:
179: item.updateHeader("gooble de gook");
180: loader = new BRMSSuggestionCompletionLoader();
181: loader.getSuggestionEngine(item);
182: assertTrue(loader.hasErrors());
183:
184: item.updateHeader("import foo.bar; \nglobal goo.Bar baz;");
185: loader = new BRMSSuggestionCompletionLoader();
186: loader.getSuggestionEngine(item);
187: assertTrue(loader.hasErrors());
188:
189: }
190:
191: /**
192: * This shows we need to load up the model without anything attached yet.
193: */
194: public void testModelWithNoAttachment() throws Exception {
195: RulesRepository repo = new RulesRepository(
196: TestEnvironmentSessionHelper.getSession());
197: PackageItem item = repo.createPackage(
198: "testmodelWithNoAttachment", "to test model loading");
199:
200: item.addAsset("testModel", "", null, AssetFormats.MODEL);
201: repo.save();
202:
203: BRMSSuggestionCompletionLoader loader = new BRMSSuggestionCompletionLoader();
204:
205: assertNotNull(loader.getSuggestionEngine(item));
206: assertFalse(loader.hasErrors());
207:
208: }
209:
210: }
|