[jboss-svn-commits] JBL Code SVN: r19867 - labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/persister.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue May 6 01:16:32 EDT 2008


Author: mark.proctor at jboss.com
Date: 2008-05-06 01:16:30 -0400 (Tue, 06 May 2008)
New Revision: 19867

Modified:
   labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/persister/WorkingMemorySerialisationTest.java
Log:
JBRULES-1598 Efficient WorkingMemory serialization with binary protocol
-Added helper method to serialize WM and to do a round trip test at the same time, to make sure information is not lost on serialization.

Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/persister/WorkingMemorySerialisationTest.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/persister/WorkingMemorySerialisationTest.java	2008-05-06 04:37:59 UTC (rev 19866)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/persister/WorkingMemorySerialisationTest.java	2008-05-06 05:16:30 UTC (rev 19867)
@@ -61,25 +61,61 @@
 
         Person p = new Person( "bobba fet", 32);
         
-        session.insert( p );
+        session.insert( p );              
         
+        WorkingMemory wm2 = getSerialisedWorkingMemory( session );
+        
+        wm2.fireAllRules();
+
+        assertEquals( 1, ((List)wm2.getGlobal("list")).size());
+        assertEquals( p, ((List)wm2.getGlobal("list")).get(0));
+    }
+    
+    public WorkingMemory getSerialisedWorkingMemory(WorkingMemory wm) throws Exception {
+        RuleBase ruleBase = wm.getRuleBase();
+        
         PlaceholderResolverStrategyFactory factory = new PlaceholderResolverStrategyFactory();
         factory.addPlaceholderResolverStrategy( new SerializablePlaceholderResolverStrategy() );
+        
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        OutputPersister op = new OutputPersister( (InternalRuleBase) ruleBase, ( InternalWorkingMemory )session, new ObjectOutputStream( baos ), factory);
+        OutputPersister op = new OutputPersister( (InternalRuleBase) ruleBase, ( InternalWorkingMemory )wm, new ObjectOutputStream( baos ), factory);
         op.write();
         
-        ByteArrayInputStream bais = new ByteArrayInputStream( baos.toByteArray() );
+        byte[] b1 = baos.toByteArray();
+        ByteArrayInputStream bais = new ByteArrayInputStream( b1 );
         InputPersister ip = new InputPersister( (InternalRuleBase) ruleBase, new ObjectInputStream( bais ), factory);        
-        
         WorkingMemory wm2 = ip.read();
-        wm2.setGlobal( "list", list );
+        wm2.setGlobalResolver( wm.getGlobalResolver() );
         
-
-        wm2.fireAllRules();
-
-        assertEquals( 1, ((List)wm2.getGlobal("list")).size());
-        assertEquals( p, ((List)wm2.getGlobal("list")).get(0));
+        //this is to check round tripping
+        baos = new ByteArrayOutputStream();
+        op = new OutputPersister( (InternalRuleBase) ruleBase, ( InternalWorkingMemory )wm2, new ObjectOutputStream( baos ), factory);
+        op.write();
+        
+        byte[] b2 = baos.toByteArray();
+        // bytes should be the same.
+        assertTrue( areByteArraysEqual(b1, b2) );
+        
+        bais = new ByteArrayInputStream( b2 );
+        ip = new InputPersister( (InternalRuleBase) ruleBase, new ObjectInputStream( bais ), factory);        
+        wm2 = ip.read();                
+        wm2.setGlobalResolver( wm.getGlobalResolver() );
+        
+        return wm2;        
     }
+    
+    public static boolean areByteArraysEqual( byte[] b1, byte[] b2) {
+        if ( b1.length != b2.length ) {
+            return false;
+        }
+        
+        for ( int i = 0, length = b1.length; i < length; i++ ) {
+            if ( b1[i] != b2[i]) {
+                return false;
+            }
+        }
+        
+        return true;
+    }
 
 }
\ No newline at end of file




More information about the jboss-svn-commits mailing list