001: package org.drools.agent;
002:
003: import java.io.File;
004: import java.io.FileOutputStream;
005: import java.io.ObjectOutputStream;
006: import java.util.Arrays;
007: import java.util.Collections;
008: import java.util.HashMap;
009: import java.util.Map;
010: import java.util.Properties;
011:
012: import org.drools.RuleBase;
013: import org.drools.RuleBaseFactory;
014: import org.drools.rule.Package;
015:
016: import junit.framework.TestCase;
017:
018: public class FileScannerTest extends TestCase {
019:
020: public void testHasChanged() {
021: Map lastMod = new HashMap();
022:
023: FileScanner scan = new FileScanner();
024: assertTrue(scan.hasChanged("/goo/ber", lastMod, 42));
025:
026: assertFalse(scan.hasChanged("/goo/ber", lastMod, 42));
027: assertTrue(scan.hasChanged("/goo/baaaa", lastMod, 42));
028:
029: assertFalse(scan.hasChanged("/goo/baaaa", lastMod, 42));
030: assertFalse(scan.hasChanged("/goo/ber", lastMod, 42));
031:
032: assertTrue(scan.hasChanged("/goo/ber", lastMod, 400));
033: assertFalse(scan.hasChanged("/goo/baaaa", lastMod, 42));
034: assertTrue(scan.hasChanged("/goo/baaaa", lastMod, 69));
035: }
036:
037: public void testScanAndLoad() throws Exception {
038: Package p1 = new Package("p1");
039: Package p2 = new Package("p2");
040:
041: File dir = RuleBaseAssemblerTest.getTempDirectory();
042: File p1f = new File(dir, "p1.pkg");
043: File p2f = new File(dir, "p2.pkg");
044:
045: RuleBaseAssemblerTest.writePackage(p1, p1f);
046: RuleBaseAssemblerTest.writePackage(p2, p2f);
047:
048: FileScanner scan = new FileScanner();
049: Properties props = new Properties();
050: props.setProperty(RuleAgent.FILES, p1f.getPath() + " "
051: + p2f.getPath());
052: scan.configure(props);
053:
054: RuleBase rb = RuleBaseFactory.newRuleBase();
055: PackageProvider.applyChanges(rb, true, scan
056: .loadPackageChanges(), new MockListener());
057:
058: assertEquals(2, rb.getPackages().length);
059: assertTrue("p1".equals(rb.getPackages()[0].getName())
060: || "p1".equals(rb.getPackages()[1].getName()));
061: assertTrue("p2".equals(rb.getPackages()[0].getName())
062: || "p2".equals(rb.getPackages()[1].getName()));
063:
064: PackageProvider.applyChanges(rb, true, scan
065: .loadPackageChanges(), new MockListener());
066: assertEquals(2, rb.getPackages().length);
067: assertTrue("p1".equals(rb.getPackages()[0].getName())
068: || "p1".equals(rb.getPackages()[1].getName()));
069: assertTrue("p2".equals(rb.getPackages()[0].getName())
070: || "p2".equals(rb.getPackages()[1].getName()));
071:
072: RuleBaseAssemblerTest.writePackage(p2, p2f);
073: PackageProvider.applyChanges(rb, true, scan
074: .loadPackageChanges(), new MockListener());
075:
076: assertEquals(2, rb.getPackages().length);
077: assertTrue("p1".equals(rb.getPackages()[0].getName())
078: || "p1".equals(rb.getPackages()[1].getName()));
079: assertTrue("p2".equals(rb.getPackages()[0].getName())
080: || "p2".equals(rb.getPackages()[1].getName()));
081:
082: }
083:
084: public void testEmptyList() throws Exception {
085: FileScanner scan = new FileScanner();
086: RuleBase rb = RuleBaseFactory.newRuleBase();
087:
088: PackageProvider.applyChanges(rb, true, scan
089: .loadPackageChanges(), new MockListener());
090:
091: assertEquals(0, rb.getPackages().length);
092: }
093:
094: public void testFileChanges() throws Exception {
095: File dir = RuleBaseAssemblerTest.getTempDirectory();
096: File t = new File(dir, "x.bar");
097:
098: Package x = new Package("x");
099: RuleBaseAssemblerTest.writePackage(x, t);
100:
101: FileScanner scan = new FileScanner();
102: Map updates = new HashMap();
103: assertTrue(scan.hasChanged("x", updates, t.lastModified()));
104: assertFalse(scan.hasChanged("x", updates, t.lastModified()));
105:
106: }
107:
108: }
|