001: package org.drools.agent;
002:
003: import java.io.File;
004: import java.io.InputStream;
005: import java.util.HashMap;
006: import java.util.List;
007: import java.util.Map;
008: import java.util.Properties;
009: import java.util.Random;
010:
011: import org.drools.RuleBase;
012: import org.drools.RuleBaseConfiguration;
013: import org.drools.common.InternalRuleBase;
014: import org.drools.rule.Package;
015:
016: import junit.framework.TestCase;
017:
018: public class RuleAgentTest extends TestCase {
019:
020: public void testLists() {
021: String s = "\tfoo.bar\n baz.bar\t whee ";
022: List result = RuleAgent.list(s);
023: assertEquals(3, result.size());
024: assertEquals("foo.bar", result.get(0));
025: assertEquals("baz.bar", result.get(1));
026: assertEquals("whee", result.get(2));
027:
028: s = null;
029: result = RuleAgent.list(s);
030: assertNotNull(result);
031: assertEquals(0, result.size());
032: }
033:
034: public void testFiles() throws Exception {
035: File dir = RuleBaseAssemblerTest.getTempDirectory();
036:
037: Package p1 = new Package("p1");
038: File p1f = new File(dir, "p1.pkg");
039: RuleBaseAssemblerTest.writePackage(p1, p1f);
040:
041: Package p2 = new Package("p2");
042: File p2f = new File(dir, "p2.pkg");
043: RuleBaseAssemblerTest.writePackage(p2, p2f);
044:
045: String path = dir.getPath() + "/" + "p1.pkg " + dir.getPath()
046: + "/" + "p2.pkg";
047:
048: Properties props = new Properties();
049: props.setProperty("file", path);
050: RuleAgent ag = RuleAgent.newRuleAgent(props);
051: RuleBase rb = ag.getRuleBase();
052: assertNotNull(rb);
053: assertEquals(2, rb.getPackages().length);
054:
055: assertFalse(ag.isPolling());
056:
057: props.setProperty("poll", "1");
058: ag = RuleAgent.newRuleAgent(props);
059: assertTrue(ag.isPolling());
060:
061: ag.stopPolling();
062: assertFalse(ag.isPolling());
063:
064: }
065:
066: public void testPollingFilesRuleBaseUpdate() throws Exception {
067: //RuleBaseAssemblerTest.clearTempDirectory();
068: final File dir = RuleBaseAssemblerTest.getTempDirectory();
069:
070: Random rnd = new Random(System.currentTimeMillis());
071:
072: final Package p1 = new Package("p1");
073: final File p1f = new File(dir, rnd.nextLong() + ".pkg");
074: RuleBaseAssemblerTest.writePackage(p1, p1f);
075:
076: String path = p1f.getPath();
077:
078: Properties props = new Properties();
079: props.setProperty("file", path);
080:
081: RuleAgent ag = RuleAgent.newRuleAgent(props);
082:
083: RuleBase rb = ag.getRuleBase();
084: assertEquals(1, rb.getPackages().length);
085: assertEquals(0, rb.getPackages()[0].getGlobals().size());
086:
087: p1.addGlobal("goo", String.class);
088:
089: Thread.sleep(1000);
090:
091: RuleBaseAssemblerTest.writePackage(p1, p1f);
092:
093: RuleBase rb_ = ag.getRuleBase();
094: assertSame(rb, rb_);
095: assertEquals(1, rb.getPackages().length);
096: assertEquals(0, rb.getPackages()[0].getGlobals().size());
097:
098: Thread.sleep(1000);
099:
100: ag.refreshRuleBase();
101:
102: RuleBase rb2 = ag.getRuleBase();
103: assertSame(rb, rb2);
104:
105: assertEquals(1, rb2.getPackages().length);
106: assertEquals(1, rb2.getPackages()[0].getGlobals().size());
107:
108: //now check subsequent changes
109: p1.addGlobal("goo2", String.class);
110: System.err.println("-->WRITING CHANGE");
111: Thread.sleep(1000);
112: RuleBaseAssemblerTest.writePackage(p1, p1f);
113: System.err.println("-->WROTE CHANGE");
114: Thread.sleep(1000);
115: ag.refreshRuleBase();
116:
117: RuleBase rb2_ = ag.getRuleBase();
118: assertSame(rb2_, rb2);
119: assertEquals(1, rb2_.getPackages().length);
120: assertEquals(2, rb2_.getPackages()[0].getGlobals().size());
121:
122: ag.refreshRuleBase();
123:
124: RuleBase rb3 = ag.getRuleBase();
125: assertSame(rb3, rb2);
126:
127: assertEquals(1, rb3.getPackages().length);
128: assertEquals(2, rb3.getPackages()[0].getGlobals().size());
129:
130: ag.refreshRuleBase();
131: ag.refreshRuleBase();
132:
133: assertEquals(1, rb3.getPackages().length);
134: assertEquals(2, rb3.getPackages()[0].getGlobals().size());
135:
136: }
137:
138: public void testPollingFilesRuleBaseReplace() throws Exception {
139: File dir = RuleBaseAssemblerTest.getTempDirectory();
140:
141: Package p1 = new Package("p1");
142: File p1f = new File(dir, "p43_.pkg");
143: RuleBaseAssemblerTest.writePackage(p1, p1f);
144:
145: Package p2 = new Package("p2");
146: File p2f = new File(dir, "p44_.pkg");
147: RuleBaseAssemblerTest.writePackage(p2, p2f);
148:
149: String path = dir.getPath() + "/" + "p43_.pkg " + dir.getPath()
150: + "/p44_.pkg";
151:
152: Properties props = new Properties();
153: props.setProperty("file", path);
154:
155: props.setProperty("newInstance", "true");
156: RuleAgent ag = RuleAgent.newRuleAgent(props);
157:
158: assertTrue(ag.isNewInstance());
159:
160: RuleBase rb = ag.getRuleBase();
161: assertEquals(2, rb.getPackages().length);
162:
163: RuleBase rb_ = ag.getRuleBase();
164: assertSame(rb, rb_);
165:
166: ag.refreshRuleBase();
167:
168: assertSame(rb, ag.getRuleBase());
169: Thread.sleep(1000);
170: //only change one
171: RuleBaseAssemblerTest.writePackage(p1, p1f);
172: Thread.sleep(1000);
173: ag.refreshRuleBase();
174:
175: rb_ = ag.getRuleBase();
176:
177: assertNotSame(rb, rb_);
178:
179: //check we will have 2
180: assertEquals(2, rb_.getPackages().length);
181:
182: ag.refreshRuleBase();
183: ag.refreshRuleBase();
184:
185: RuleBase rb__ = ag.getRuleBase();
186: assertSame(rb_, rb__);
187:
188: }
189:
190: public void testDirectory() throws Exception {
191: File dir = RuleBaseAssemblerTest.getTempDirectory();
192:
193: Package p1 = new Package("p1");
194: File p1f = new File(dir, "p43_.pkg");
195: RuleBaseAssemblerTest.writePackage(p1, p1f);
196:
197: Properties props = new Properties();
198: props.setProperty(RuleAgent.DIRECTORY, dir.getPath());
199: props.setProperty(RuleAgent.CONFIG_NAME, "goo");
200:
201: RuleAgent ag = RuleAgent.newRuleAgent(props);
202:
203: ag.refreshRuleBase();
204:
205: RuleBase rb = ag.getRuleBase();
206: assertNotNull(rb);
207: assertEquals(1, rb.getPackages().length);
208: }
209:
210: public void testCustomRuleBaseConfiguration() throws Exception {
211: final File dir = RuleBaseAssemblerTest.getTempDirectory();
212:
213: Random rnd = new Random(System.currentTimeMillis());
214:
215: final Package p1 = new Package("p1");
216: final File p1f = new File(dir, rnd.nextLong() + ".pkg");
217: RuleBaseAssemblerTest.writePackage(p1, p1f);
218:
219: String path = p1f.getPath();
220:
221: Properties props = new Properties();
222: props.setProperty("file", path);
223:
224: // Check a default value for the RuleBase's RuleBaseConfiguration
225: RuleAgent agent = RuleAgent.newRuleAgent(props);
226: RuleBaseConfiguration conf = ((InternalRuleBase) agent
227: .getRuleBase()).getConfiguration();
228: assertEquals(false, conf.isSequential());
229:
230: // Pass in a RuleBaseConfiguration and make sure the RuleBase was created with it
231: conf = new RuleBaseConfiguration();
232: conf.setSequential(true);
233: agent = RuleAgent.newRuleAgent(props, conf);
234: conf = ((InternalRuleBase) agent.getRuleBase())
235: .getConfiguration();
236: assertEquals(true, conf.isSequential());
237: }
238:
239: public void testLoadSampleConfig() {
240: RuleAgent ag = new RuleAgent(new RuleBaseConfiguration());
241: Properties props = ag
242: .loadFromProperties("/sample-agent-config.properties");
243: assertEquals("10", props.getProperty(RuleAgent.POLL_INTERVAL));
244: assertEquals("/home/packages", props
245: .getProperty(RuleAgent.DIRECTORY));
246: assertEquals("true", props.getProperty(RuleAgent.NEW_INSTANCE));
247: assertEqualsIgnoreWhitespace(
248: "/foo/bar.pkg /wee/waa.pkg /wee/waa2.pkg", props
249: .getProperty(RuleAgent.FILES));
250: }
251:
252: private void assertEqualsIgnoreWhitespace(final String expected,
253: final String actual) {
254: final String cleanExpected = expected.replaceAll("\\s+", "");
255: final String cleanActual = actual.replaceAll("\\s+", "");
256:
257: assertEquals(cleanExpected, cleanActual);
258: }
259:
260: public void testEventListenerSetup() throws Exception {
261: RuleAgent ag = new RuleAgent(new RuleBaseConfiguration());
262: assertNotNull(ag.listener);
263:
264: final String[] name = new String[1];
265:
266: AgentEventListener list = new AgentEventListener() {
267: public void debug(String message) {
268: }
269:
270: public void exception(Exception e) {
271: }
272:
273: public void info(String message) {
274: }
275:
276: public void warning(String message) {
277: }
278:
279: public void setAgentName(String n) {
280: name[0] = n;
281: }
282: };
283:
284: File dir = RuleBaseAssemblerTest.getTempDirectory();
285:
286: Package p1 = new Package("p1");
287: File p1f = new File(dir, "p42_.pkg");
288: RuleBaseAssemblerTest.writePackage(p1, p1f);
289:
290: String path = dir.getPath() + "/" + "p42_.pkg";
291:
292: Properties props = new Properties();
293: props.setProperty("file", path);
294: props.setProperty("poll", "1");
295: props.setProperty("name", "poo");
296: ag = RuleAgent.newRuleAgent(props, list);
297:
298: assertEquals(list, ag.listener);
299: assertEquals("poo", name[0]);
300: ag.stopPolling();
301: }
302:
303: public void testPollSetup() throws Exception {
304: //this is the only method that will actually run the polling timer
305:
306: Properties props = new Properties();
307: //props.setProperty( "file", "/foo/bar" );
308: props.setProperty("poll", "1");
309: MockRuleAgent ag = new MockRuleAgent();
310: ag.init(props);
311:
312: assertTrue(ag.isPolling());
313: assertTrue(ag.refreshCalled);
314: ag.refreshCalled = false;
315: assertFalse(ag.refreshCalled);
316: Thread.sleep(100);
317: assertFalse(ag.refreshCalled);
318: Thread.sleep(1500);
319: assertTrue(ag.refreshCalled);
320: ag.refreshCalled = false;
321: Thread.sleep(100);
322: assertFalse(ag.refreshCalled);
323: Thread.sleep(1500);
324: assertTrue(ag.refreshCalled);
325: ag.stopPolling();
326:
327: }
328:
329: public void testProviderMap() throws Exception {
330:
331: assertEquals(3, RuleAgent.PACKAGE_PROVIDERS.size());
332: assertTrue(RuleAgent.PACKAGE_PROVIDERS.containsKey("url"));
333: assertTrue(RuleAgent.PACKAGE_PROVIDERS.containsKey("file"));
334: assertTrue(RuleAgent.PACKAGE_PROVIDERS.containsKey("dir"));
335: assertFalse(RuleAgent.PACKAGE_PROVIDERS.containsKey("XXX"));
336: assertTrue(RuleAgent.PACKAGE_PROVIDERS.get("url").equals(
337: URLScanner.class));
338:
339: }
340:
341: public void testLoadUpFromProperties() throws Exception {
342: AnotherRuleAgentMock ag = new AnotherRuleAgentMock();
343: Map oldMap = ag.PACKAGE_PROVIDERS;
344:
345: ag.PACKAGE_PROVIDERS = new HashMap();
346: ag.PACKAGE_PROVIDERS.put(RuleAgent.URLS, MockProvider.class);
347: ag.PACKAGE_PROVIDERS.put(RuleAgent.FILES, MockProvider.class);
348: ag.PACKAGE_PROVIDERS.put(RuleAgent.DIRECTORY,
349: MockProvider.class);
350:
351: Properties props = new Properties();
352: props.load(this .getClass().getResourceAsStream(
353: "/rule-agent-config.properties"));
354: MockEventListener evl = new MockEventListener();
355: ag.listener = evl;
356:
357: ag.init(props);
358:
359: assertTrue(ag.newInstance);
360: assertEquals(3, ag.provs.size());
361: assertEquals(30, ag.secondsToRefresh);
362: assertEquals("MyConfig", evl.name);
363: assertFalse(evl.exceptionCalled);
364: assertFalse(evl.warningCalled);
365: assertTrue(evl.infoCalled);
366:
367: ag.PACKAGE_PROVIDERS = oldMap;
368: }
369:
370: class AnotherRuleAgentMock extends RuleAgent {
371:
372: public int secondsToRefresh;
373: public List provs;
374: public boolean newInstance;
375:
376: public AnotherRuleAgentMock() {
377: super (new RuleBaseConfiguration());
378: }
379:
380: synchronized void configure(boolean newInstance, List provs,
381: int secondsToRefresh) {
382: this .newInstance = newInstance;
383: this .provs = provs;
384: this .secondsToRefresh = secondsToRefresh;
385: }
386:
387: }
388:
389: class MockEventListener implements AgentEventListener {
390:
391: public String name;
392: boolean exceptionCalled = false;
393: boolean infoCalled = false;
394: boolean warningCalled;
395:
396: public void debug(String message) {
397:
398: }
399:
400: public void exception(Exception e) {
401: this .exceptionCalled = true;
402: }
403:
404: public void info(String message) {
405: if (message != null)
406: this .infoCalled = true;
407: }
408:
409: public void setAgentName(String name) {
410: this .name = name;
411: }
412:
413: public void warning(String message) {
414: this .warningCalled = false;
415:
416: }
417:
418: }
419:
420: }
|