001: package org.drools.agent;
002:
003: import java.io.File;
004: import java.io.FileInputStream;
005: import java.io.FileNotFoundException;
006: import java.io.FileOutputStream;
007: import java.io.IOException;
008: import java.io.ObjectInputStream;
009: import java.io.ObjectOutputStream;
010:
011: import org.drools.RuleBase;
012: import org.drools.RuleBaseFactory;
013: import org.drools.common.DroolsObjectInputStream;
014: import org.drools.rule.Package;
015:
016: import junit.framework.TestCase;
017:
018: public class RuleBaseAssemblerTest extends TestCase {
019:
020: public void testAssemblePackages() throws Exception {
021: RuleBase rb = RuleBaseFactory.newRuleBase();
022: rb.addPackage(new Package("goober"));
023:
024: Package p1 = new Package("p1");
025:
026: File f = getTempDirectory();
027:
028: File p1file = new File(f, "p1.pkg");
029:
030: writePackage(p1, p1file);
031:
032: Package p1_ = readPackage(p1file);
033:
034: rb = RuleBaseFactory.newRuleBase();
035: rb.addPackage(p1_);
036:
037: }
038:
039: public static Package readPackage(File p1file) throws IOException,
040: FileNotFoundException, ClassNotFoundException {
041: ObjectInputStream in = new DroolsObjectInputStream(
042: new FileInputStream(p1file));
043: Package p1_ = (Package) in.readObject();
044: in.close();
045: return p1_;
046: }
047:
048: public static void writePackage(Package pkg, File p1file)
049: throws IOException, FileNotFoundException {
050: ObjectOutputStream out = new ObjectOutputStream(
051: new FileOutputStream(p1file));
052: out.writeObject(pkg);
053: out.flush();
054: out.close();
055: }
056:
057: public static File getTempDirectory() {
058: File f = tempDir();
059: if (f.exists()) {
060: if (f.isFile()) {
061: throw new IllegalStateException(
062: "The temp directory exists as a file. Nuke it now !");
063: }
064: deleteDir(f);
065: f.mkdir();
066: } else {
067: f.mkdir();
068: }
069: return f;
070: }
071:
072: private static File tempDir() {
073: File tmp = new File(System.getProperty("java.io.tmpdir"));
074:
075: return new File(tmp, "__temp_test_drools_packages");
076: }
077:
078: public static boolean deleteDir(File dir) {
079:
080: if (dir.isDirectory()) {
081: String[] children = dir.list();
082: for (int i = 0; i < children.length; i++) {
083: boolean success = deleteDir(new File(dir, children[i]));
084: if (!success) {
085: //throw new RuntimeException("Unable to delete !");
086: return false;
087: }
088: }
089: }
090:
091: // The directory is now empty so delete it
092: return dir.delete();
093: }
094:
095: public static void clearTempDirectory() {
096: deleteDir(tempDir());
097:
098: }
099:
100: }
|