[jboss-svn-commits] JBL Code SVN: r12773 - in labs/jbossrules/trunk/drools-core/src: test/java/org/drools/agent and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri Jun 22 03:41:16 EDT 2007


Author: michael.neale at jboss.com
Date: 2007-06-22 03:41:16 -0400 (Fri, 22 Jun 2007)
New Revision: 12773

Modified:
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/RuleAgent.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/URLScanner.java
   labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/URLScannerTest.java
Log:
JBRULES-752 w00t ! all done bar for event logging

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/RuleAgent.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/RuleAgent.java	2007-06-22 06:54:09 UTC (rev 12772)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/RuleAgent.java	2007-06-22 07:41:16 UTC (rev 12773)
@@ -179,8 +179,12 @@
             timer = new Timer( true );
             timer.schedule( new TimerTask() {
                                 public void run() {
-                                    refreshRuleBase();
-
+                                    try {
+                                        refreshRuleBase();
+                                    } catch (Exception e) {
+                                        //don't want to stop execution here.
+                                        listener.exception( e );
+                                    }
                                 }
                             },
                             interval,

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/URLScanner.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/URLScanner.java	2007-06-22 06:54:09 UTC (rev 12772)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/URLScanner.java	2007-06-22 07:41:16 UTC (rev 12773)
@@ -33,7 +33,7 @@
     //a record of the last updated URL timestamps
     Map         lastUpdated = new HashMap();
 
-    private File localCacheDir;
+    File        localCacheDir;
 
     void configure(Properties config) {
         List uriList = RuleAgent.list( config.getProperty( RuleAgent.URLS ) );
@@ -70,7 +70,7 @@
         return fs;
     }
 
-    private File getLocalCacheFileForURL(File cacheDir, URL u) {
+    static File getLocalCacheFileForURL(File cacheDir, URL u) {
         File f;
         try {
             f = new File( cacheDir,

Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/URLScannerTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/URLScannerTest.java	2007-06-22 06:54:09 UTC (rev 12772)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/agent/URLScannerTest.java	2007-06-22 07:41:16 UTC (rev 12773)
@@ -2,12 +2,15 @@
 
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.IOException;
 import java.net.URL;
 import java.net.URLEncoder;
 import java.util.Properties;
 
 import junit.framework.TestCase;
 
+import org.drools.RuleBase;
+import org.drools.RuleBaseFactory;
 import org.drools.common.DroolsObjectInputStream;
 import org.drools.rule.Package;
 
@@ -15,87 +18,262 @@
 
     public void testFileURLCache() throws Exception {
         File dir = RuleBaseAssemblerTest.getTempDirectory();
-        
+
         String url = "http://localhost:8080/foo/bar.bar/packages/IMINYRURL/LATEST";
         String fileName = URLEncoder.encode( url, "UTF-8" );
-        
-        File f = new File(dir, fileName);
-        
-        Package p = new Package("x");
-        
-        
+
+        File f = new File( dir,
+                           fileName );
+
+        Package p = new Package( "x" );
+
         RuleBaseAssemblerTest.writePackage( p, f );
-        
-        DroolsObjectInputStream in = new DroolsObjectInputStream(new FileInputStream(f));
+
+        DroolsObjectInputStream in = new DroolsObjectInputStream( new FileInputStream( f ) );
         Package p_ = (Package) in.readObject();
-        assertEquals("x", p_.getName());
+        assertEquals( "x", p_.getName() );
 
+    }
 
-        
-    }
-    
-//    public void testGetURL() throws Exception {
-//        String url = "http://localhost:8080/foo/bar.bar/packages/IMINYRURL/LATEST";
-//        URL u = new URL(url);
-//        assertEquals(url, URLScanner.getURL( u ));
-//        //URLConnection con = u.openConnection();
-//        //con.connect();
-//    }
-    
+    //    public void testGetURL() throws Exception {
+    //        String url = "http://localhost:8080/foo/bar.bar/packages/IMINYRURL/LATEST";
+    //        URL u = new URL(url);
+    //        assertEquals(url, URLScanner.getURL( u ));
+    //        //URLConnection con = u.openConnection();
+    //        //con.connect();
+    //    }
+
     public void testGetFiles() throws Exception {
-        
-        URL u1 = new URL("http://localhost:8080/foo/bar.bar/packages/IMINYRURL/LATEST");
-        URL u2 = new URL("http://localhost:8080/foo/bar.bar/packages/IMINYRURL/PROD");
+
+        URL u1 = new URL( "http://localhost:8080/foo/bar.bar/packages/IMINYRURL/LATEST" );
+        URL u2 = new URL( "http://localhost:8080/foo/bar.bar/packages/IMINYRURL/PROD" );
         URLScanner scan = new URLScanner();
-        
-        File dir = RuleBaseAssemblerTest.getTempDirectory();        
-        File[] result = scan.getFiles( new URL[] {u1, u2}, dir );
-        
-        assertEquals(2, result.length);
-        assertEquals(dir.getPath(), result[0].getParent());
-        
+
+        File dir = RuleBaseAssemblerTest.getTempDirectory();
+        File[] result = scan.getFiles( new URL[]{u1, u2}, dir );
+
+        assertEquals( 2, result.length );
+        assertEquals( dir.getPath(), result[0].getParent() );
+
         File f1 = result[0];
         File f2 = result[1];
-        assertEquals("http%3A%2F%2Flocalhost%3A8080%2Ffoo%2Fbar.bar%2Fpackages%2FIMINYRURL%2FLATEST", f1.getName());
-        assertEquals("http%3A%2F%2Flocalhost%3A8080%2Ffoo%2Fbar.bar%2Fpackages%2FIMINYRURL%2FPROD", f2.getName());
-        
+        assertEquals( "http%3A%2F%2Flocalhost%3A8080%2Ffoo%2Fbar.bar%2Fpackages%2FIMINYRURL%2FLATEST", f1.getName() );
+        assertEquals( "http%3A%2F%2Flocalhost%3A8080%2Ffoo%2Fbar.bar%2Fpackages%2FIMINYRURL%2FPROD", f2.getName() );
+
     }
-    
+
     public void testConfig() throws Exception {
         URLScanner scan = new URLScanner();
-        
+
         File dir = RuleBaseAssemblerTest.getTempDirectory();
-        
+
         Properties config = new Properties();
         config.setProperty( RuleAgent.LOCAL_URL_CACHE, dir.getPath() );
         config.setProperty( RuleAgent.URLS, "http://goo.ber http://wee.waa" );
-        
+
         scan.configure( config );
+
+        assertNotNull( scan.lastUpdated );
+        assertEquals( 2, scan.urls.length );
+        assertEquals( "http://goo.ber", scan.urls[0].toExternalForm() );
+        assertEquals( "http://wee.waa", scan.urls[1].toExternalForm() );
+        assertNotNull( scan.localCacheFileScanner );
+
+        assertEquals( 2, scan.localCacheFileScanner.files.length );
+
+        assertEquals( "http%3A%2F%2Fgoo.ber", scan.localCacheFileScanner.files[0].getName() );
+        assertEquals( "http%3A%2F%2Fwee.waa", scan.localCacheFileScanner.files[1].getName() );
+
+    }
+
+    public void testLastUpdatedError() {
+        LastUpdatedPing ping = new LastUpdatedPing();
+        assertTrue( ping.isError() );
+        ping.responseMessage = "ABC";
+        ping.lastUpdated = 42;
+        assertTrue( ping.isError() );
+        ping.responseMessage = "200 OK";
+        assertFalse( ping.isError() );
+    }
+
+    public void testUpdateNoLocalCache() throws Exception {
+        URLScanner scan = new URLScanner();
+        File dir = RuleBaseAssemblerTest.getTempDirectory();
+
+        Properties config = new Properties();
+        //config.setProperty( RuleAgent.LOCAL_URL_CACHE, dir.getPath() );
+        config.setProperty( RuleAgent.URLS, "http://goo.ber http://wee.waa" );
+
+        scan.configure( config );
+
+        scan.httpClient = new IHttpClient() {
+
+            public LastUpdatedPing checkLastUpdated(URL url) throws IOException {
+                LastUpdatedPing ping = new LastUpdatedPing();
+                ping.lastUpdated = 123;
+                ping.responseMessage = "200 OK";
+                return ping;
+            }
+
+            public Package fetchPackage(URL url) throws IOException {
+                if ( url.toExternalForm().equals( "http://goo.ber" ) ) {
+                    return new Package( "goo.ber" );
+                } else {
+                    return new Package( "wee.waa" );
+                }
+            }
+
+        };
+
+        assertNull( scan.localCacheFileScanner );
+        assertNull( scan.localCacheDir );
+
+        RuleBase rb = RuleBaseFactory.newRuleBase();
+        scan.updateRuleBase( rb, false );
+
+        assertEquals( 2, rb.getPackages().length );
+        assertEquals( "goo.ber", rb.getPackages()[0].getName() );
+        assertEquals( "wee.waa", rb.getPackages()[1].getName() );
+
+        assertEquals( 0, dir.list().length );
+    }
+
+    public void testUpdateWithLocalCache() {
+        URLScanner scan = new URLScanner();
+        File dir = RuleBaseAssemblerTest.getTempDirectory();
+
+        Properties config = new Properties();
+        config.setProperty( RuleAgent.LOCAL_URL_CACHE, dir.getPath() );
+        config.setProperty( RuleAgent.URLS, "http://goo.ber http://wee.waa" );
+
+        scan.configure( config );
+
+        scan.httpClient = new IHttpClient() {
+
+            public LastUpdatedPing checkLastUpdated(URL url) throws IOException {
+                LastUpdatedPing ping = new LastUpdatedPing();
+                ping.lastUpdated = 123;
+                ping.responseMessage = "200 OK";
+                return ping;
+            }
+
+            public Package fetchPackage(URL url) throws IOException {
+                if ( url.toExternalForm().equals( "http://goo.ber" ) ) {
+                    return new Package( "goo.ber" );
+                } else {
+                    return new Package( "wee.waa" );
+                }
+            }
+
+        };
+
+        assertNotNull( scan.localCacheFileScanner );
+        assertNotNull( scan.localCacheDir );
+
+        RuleBase rb = RuleBaseFactory.newRuleBase();
+        scan.updateRuleBase( rb, false );
+
+        assertEquals( 2, rb.getPackages().length );
+        assertEquals( "goo.ber", rb.getPackages()[0].getName() );
+        assertEquals( "wee.waa", rb.getPackages()[1].getName() );
+
+        assertEquals( 2, dir.list().length );
+
+        //now check with failures:
+        scan.httpClient = new IHttpClient() {
+
+            public LastUpdatedPing checkLastUpdated(URL url) throws IOException {
+                LastUpdatedPing ping = new LastUpdatedPing();
+
+                if (url.toExternalForm().equals( "http://wee.waa" )) {
+                    ping.lastUpdated = -1;
+                    ping.responseMessage = "XXX";
+                    
+                } else {
+                    ping.lastUpdated = 123;
+                    ping.responseMessage = "200 OK";
+                }
+                return ping;
+            }
+
+            public Package fetchPackage(URL url) throws IOException {
+                throw new IOException("poo");
+            }
+
+        };      
         
-        assertNotNull(scan.lastUpdated);
-        assertEquals(2, scan.urls.length);
-        assertEquals("http://goo.ber", scan.urls[0].toExternalForm());
-        assertEquals("http://wee.waa", scan.urls[1].toExternalForm());
-        assertNotNull(scan.localCacheFileScanner);
+        rb = RuleBaseFactory.newRuleBase();
+        assertEquals(0, rb.getPackages().length);
+        scan.updateRuleBase( rb, true );
+        assertEquals(2, rb.getPackages().length);
         
-        assertEquals(2, scan.localCacheFileScanner.files.length);
+        //now check with IOExceptions
+        scan.httpClient = new IHttpClient() {
+
+            public LastUpdatedPing checkLastUpdated(URL url) throws IOException {
+                LastUpdatedPing ping = new LastUpdatedPing();
+                ping.lastUpdated = 1234;
+                ping.responseMessage = "200 OK";
+                return ping;
+            }
+
+            public Package fetchPackage(URL url) throws IOException {
+                throw new IOException("poo");
+            }
+
+        };          
         
-        assertEquals("http%3A%2F%2Fgoo.ber", scan.localCacheFileScanner.files[0].getName());
-        assertEquals("http%3A%2F%2Fwee.waa", scan.localCacheFileScanner.files[1].getName());
+        scan.updateRuleBase( rb, true );
+        assertEquals(2, rb.getPackages().length);
         
+        rb = RuleBaseFactory.newRuleBase();
+        scan.updateRuleBase( rb, true );
+        assertEquals(2, rb.getPackages().length);
         
-        
     }
     
-    public void testLastUpdatedError() {
-        LastUpdatedPing ping = new LastUpdatedPing();
-        assertTrue(ping.isError());
-        ping.responseMessage = "ABC";
-        ping.lastUpdated = 42;
-        assertTrue(ping.isError());
-        ping.responseMessage = "200 OK";
-        assertFalse(ping.isError());
+    public void testColdStartWithError() throws Exception {
+        //this will show starting up and reading packages from the dir when the remote one doesn't respond
+        URLScanner scan = new URLScanner();
+        File dir = RuleBaseAssemblerTest.getTempDirectory();
+
+        Package p1 = new Package("goo.ber");
+        Package p2 = new Package("wee.waa");
+        
+        File f1 = URLScanner.getLocalCacheFileForURL( dir, new URL("http://goo.ber") );        
+        File f2 = URLScanner.getLocalCacheFileForURL( dir, new URL("http://wee.waa") );
+        
+        RuleBaseAssemblerTest.writePackage( p1, f1 );
+        RuleBaseAssemblerTest.writePackage( p2, f2 );
+        
+        Properties config = new Properties();
+        config.setProperty( RuleAgent.LOCAL_URL_CACHE, dir.getPath() );
+        config.setProperty( RuleAgent.URLS, "http://goo.ber http://wee.waa" );
+
+        scan.configure( config );
+
+        scan.httpClient = new IHttpClient() {
+
+            public LastUpdatedPing checkLastUpdated(URL url) throws IOException {
+                throw new IOException();
+            }
+
+            public Package fetchPackage(URL url) throws IOException {
+                throw new IOException();
+            }
+
+        };
+
+        assertNotNull( scan.localCacheFileScanner );
+        assertNotNull( scan.localCacheDir );
+        
+        RuleBase rb = RuleBaseFactory.newRuleBase();
+        scan.updateRuleBase( rb, true );
+        assertEquals(2, rb.getPackages().length);
+        
+        
     }
-    
-    
+
+
+
 }




More information about the jboss-svn-commits mailing list