Author: davsclaus
Date: Wed Feb 24 08:01:11 2010
New Revision: 915716

URL: http://svn.apache.org/viewvc?rev=915716&view=rev
Log:
CAMEL-217: Fixed CS. Added more tests.

Added:
    
camel/trunk/components/camel-hawtdb/src/test/java/org/apache/camel/component/hawtdb/HawtDBAggregationRepositoryLoadExistingTest.java
      - copied, changed from r915673, 
camel/trunk/components/camel-hawtdb/src/test/java/org/apache/camel/component/hawtdb/HawtDBAggregationRepositoryTest.java
    
camel/trunk/components/camel-hawtdb/src/test/java/org/apache/camel/component/hawtdb/HawtDBAggregationRepositoryMultipleRepoTest.java
      - copied, changed from r915673, 
camel/trunk/components/camel-hawtdb/src/test/java/org/apache/camel/component/hawtdb/HawtDBAggregationRepositoryTest.java
Modified:
    
camel/trunk/components/camel-hawtdb/src/main/java/org/apache/camel/component/hawtdb/HawtDBFile.java

Modified: 
camel/trunk/components/camel-hawtdb/src/main/java/org/apache/camel/component/hawtdb/HawtDBFile.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-hawtdb/src/main/java/org/apache/camel/component/hawtdb/HawtDBFile.java?rev=915716&r1=915715&r2=915716&view=diff
==============================================================================
--- 
camel/trunk/components/camel-hawtdb/src/main/java/org/apache/camel/component/hawtdb/HawtDBFile.java
 (original)
+++ 
camel/trunk/components/camel-hawtdb/src/main/java/org/apache/camel/component/hawtdb/HawtDBFile.java
 Wed Feb 24 08:01:11 2010
@@ -30,31 +30,34 @@
 import org.fusesource.hawtdb.util.marshaller.VariableBufferMarshaller;
 
 /**
- * Manages access to a shared HawtDB file from multiple 
HawtDBAggregationRepository objects.
+ * Manages access to a shared HawtDB file.
+ * <p/>
+ * Will by default not sync writes which allows it to be faster.
+ * You can force syncing by setting sync=true.
  */
 public class HawtDBFile extends HawtPageFileFactory implements Service {
 
     private static final transient Log LOG = 
LogFactory.getLog(HawtDBFile.class);
 
     // the root which contains an index with name -> page for the real indexes
-    private final static BTreeIndexFactory<String, Integer> rootIndexesFactory 
= new BTreeIndexFactory<String, Integer>();
+    private static final BTreeIndexFactory<String, Integer> 
ROOT_INDEXES_FACTORY = new BTreeIndexFactory<String, Integer>();
     // the real indexes where we store persisted data in buffers
-    private final static BTreeIndexFactory<Buffer, Buffer> indexFactory = new 
BTreeIndexFactory<Buffer, Buffer>();
+    private static final BTreeIndexFactory<Buffer, Buffer> INDEX_FACTORY = new 
BTreeIndexFactory<Buffer, Buffer>();
 
-    public HawtDBFile() {
-        setSync(false);
-    }
+    private HawtPageFile pageFile;
 
     static {
-        rootIndexesFactory.setKeyMarshaller(StringMarshaller.INSTANCE);
-        rootIndexesFactory.setValueMarshaller(IntegerMarshaller.INSTANCE);
-        rootIndexesFactory.setDeferredEncoding(true);
-        indexFactory.setKeyMarshaller(VariableBufferMarshaller.INSTANCE);
-        indexFactory.setValueMarshaller(VariableBufferMarshaller.INSTANCE);
-        indexFactory.setDeferredEncoding(true);
+        ROOT_INDEXES_FACTORY.setKeyMarshaller(StringMarshaller.INSTANCE);
+        ROOT_INDEXES_FACTORY.setValueMarshaller(IntegerMarshaller.INSTANCE);
+        ROOT_INDEXES_FACTORY.setDeferredEncoding(true);
+        INDEX_FACTORY.setKeyMarshaller(VariableBufferMarshaller.INSTANCE);
+        INDEX_FACTORY.setValueMarshaller(VariableBufferMarshaller.INSTANCE);
+        INDEX_FACTORY.setDeferredEncoding(true);
     }
 
-    private HawtPageFile pageFile;
+    public HawtDBFile() {
+        setSync(false);
+    }
 
     public void start() {
         if (LOG.isDebugEnabled()) {
@@ -71,10 +74,10 @@
                     int page = tx.allocator().alloc(1);
                     // if we just created the file, first allocated page 
should be 0
                     assert page == 0;
-                    rootIndexesFactory.create(tx, 0);
+                    ROOT_INDEXES_FACTORY.create(tx, 0);
                     LOG.info("Aggregation repository data store created using 
file: " + getFile());
                 } else {
-                    Index<String, Integer> indexes = 
rootIndexesFactory.open(tx, 0);
+                    Index<String, Integer> indexes = 
ROOT_INDEXES_FACTORY.open(tx, 0);
                     LOG.info("Aggregation repository data store loaded using 
file: " + getFile()
                             + " containing " + indexes.size() + " 
repositories.");
                 }
@@ -112,13 +115,13 @@
     }
 
     public Index<Buffer, Buffer> getRepositoryIndex(Transaction tx, String 
name) {
-        Index<String, Integer> indexes = rootIndexesFactory.open(tx, 0);
+        Index<String, Integer> indexes = ROOT_INDEXES_FACTORY.open(tx, 0);
         Integer location = indexes.get(name);
 
         if (location == null) {
             // create it..
             int page = tx.allocator().alloc(1);
-            Index<Buffer, Buffer> created = indexFactory.create(tx, page);
+            Index<Buffer, Buffer> created = INDEX_FACTORY.create(tx, page);
 
             // add it to indexes so we can find it the next time
             indexes.put(name, page);
@@ -132,7 +135,7 @@
             if (LOG.isTraceEnabled()) {
                 LOG.trace("Repository index with name " + name + " at location 
" + location);
             }
-            return indexFactory.open(tx, location);
+            return INDEX_FACTORY.open(tx, location);
         }
     }
 

Copied: 
camel/trunk/components/camel-hawtdb/src/test/java/org/apache/camel/component/hawtdb/HawtDBAggregationRepositoryLoadExistingTest.java
 (from r915673, 
camel/trunk/components/camel-hawtdb/src/test/java/org/apache/camel/component/hawtdb/HawtDBAggregationRepositoryTest.java)
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-hawtdb/src/test/java/org/apache/camel/component/hawtdb/HawtDBAggregationRepositoryLoadExistingTest.java?p2=camel/trunk/components/camel-hawtdb/src/test/java/org/apache/camel/component/hawtdb/HawtDBAggregationRepositoryLoadExistingTest.java&p1=camel/trunk/components/camel-hawtdb/src/test/java/org/apache/camel/component/hawtdb/HawtDBAggregationRepositoryTest.java&r1=915673&r2=915716&rev=915716&view=diff
==============================================================================
--- 
camel/trunk/components/camel-hawtdb/src/test/java/org/apache/camel/component/hawtdb/HawtDBAggregationRepositoryTest.java
 (original)
+++ 
camel/trunk/components/camel-hawtdb/src/test/java/org/apache/camel/component/hawtdb/HawtDBAggregationRepositoryLoadExistingTest.java
 Wed Feb 24 08:01:11 2010
@@ -23,7 +23,7 @@
 import org.apache.camel.test.junit4.CamelTestSupport;
 import org.junit.Test;
 
-public class HawtDBAggregationRepositoryTest extends CamelTestSupport {
+public class HawtDBAggregationRepositoryLoadExistingTest extends 
CamelTestSupport {
 
     private HawtDBFile hawtDBFile;
 
@@ -44,21 +44,25 @@
     }
 
     @Test
-    public void testOperations() {
+    public void testExisting() throws Exception {
         HawtDBAggregationRepository<String> repo = new 
HawtDBAggregationRepository<String>();
         repo.setFile(hawtDBFile);
         repo.setName("repo1");
 
-        // Can't get something we have not put in...
-        Exchange actual = repo.get("missing");
-        assertEquals(null, actual);
-
         // Store it..
         Exchange exchange1 = new DefaultExchange(context);
         exchange1.getIn().setBody("counter:1");
-        actual = repo.add("foo", exchange1);
+        Exchange actual = repo.add("foo", exchange1);
         assertEquals(null, actual);
 
+        // stop the repo
+        hawtDBFile.stop();
+
+        Thread.sleep(1000);
+
+        // load the repo again
+        hawtDBFile.start();
+
         // Get it back..
         actual = repo.get("foo");
         assertEquals("counter:1", actual.getIn().getBody());
@@ -75,4 +79,4 @@
         assertEquals("counter:2", actual.getIn().getBody());
     }
 
-}
+}
\ No newline at end of file

Copied: 
camel/trunk/components/camel-hawtdb/src/test/java/org/apache/camel/component/hawtdb/HawtDBAggregationRepositoryMultipleRepoTest.java
 (from r915673, 
camel/trunk/components/camel-hawtdb/src/test/java/org/apache/camel/component/hawtdb/HawtDBAggregationRepositoryTest.java)
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-hawtdb/src/test/java/org/apache/camel/component/hawtdb/HawtDBAggregationRepositoryMultipleRepoTest.java?p2=camel/trunk/components/camel-hawtdb/src/test/java/org/apache/camel/component/hawtdb/HawtDBAggregationRepositoryMultipleRepoTest.java&p1=camel/trunk/components/camel-hawtdb/src/test/java/org/apache/camel/component/hawtdb/HawtDBAggregationRepositoryTest.java&r1=915673&r2=915716&rev=915716&view=diff
==============================================================================
--- 
camel/trunk/components/camel-hawtdb/src/test/java/org/apache/camel/component/hawtdb/HawtDBAggregationRepositoryTest.java
 (original)
+++ 
camel/trunk/components/camel-hawtdb/src/test/java/org/apache/camel/component/hawtdb/HawtDBAggregationRepositoryMultipleRepoTest.java
 Wed Feb 24 08:01:11 2010
@@ -23,7 +23,7 @@
 import org.apache.camel.test.junit4.CamelTestSupport;
 import org.junit.Test;
 
-public class HawtDBAggregationRepositoryTest extends CamelTestSupport {
+public class HawtDBAggregationRepositoryMultipleRepoTest extends 
CamelTestSupport {
 
     private HawtDBFile hawtDBFile;
 
@@ -44,35 +44,79 @@
     }
 
     @Test
-    public void testOperations() {
-        HawtDBAggregationRepository<String> repo = new 
HawtDBAggregationRepository<String>();
-        repo.setFile(hawtDBFile);
-        repo.setName("repo1");
+    public void testMultipeRepo() {
+        HawtDBAggregationRepository<String> repo1 = new 
HawtDBAggregationRepository<String>();
+        repo1.setFile(hawtDBFile);
+        repo1.setName("repo1");
+
+        HawtDBAggregationRepository<String> repo2 = new 
HawtDBAggregationRepository<String>();
+        repo2.setFile(hawtDBFile);
+        repo2.setName("repo2");
 
         // Can't get something we have not put in...
-        Exchange actual = repo.get("missing");
+        Exchange actual = repo1.get("missing");
+        assertEquals(null, actual);
+
+        actual = repo2.get("missing");
         assertEquals(null, actual);
 
         // Store it..
         Exchange exchange1 = new DefaultExchange(context);
         exchange1.getIn().setBody("counter:1");
-        actual = repo.add("foo", exchange1);
+        actual = repo1.add("foo", exchange1);
         assertEquals(null, actual);
 
         // Get it back..
-        actual = repo.get("foo");
+        actual = repo1.get("foo");
         assertEquals("counter:1", actual.getIn().getBody());
+        assertEquals(null, repo2.get("foo"));
 
         // Change it..
         Exchange exchange2 = new DefaultExchange(context);
         exchange2.getIn().setBody("counter:2");
-        actual = repo.add("foo", exchange2);
+        actual = repo1.add("foo", exchange2);
         // the old one
         assertEquals("counter:1", actual.getIn().getBody());
 
+        // add to repo2
+        Exchange exchange3 = new DefaultExchange(context);
+        exchange3.getIn().setBody("Hello World");
+        actual = repo2.add("bar", exchange3);
+        assertEquals(null, actual);
+        assertEquals(null, repo1.get("bar"));
+
         // Get it back..
-        actual = repo.get("foo");
+        actual = repo1.get("foo");
         assertEquals("counter:2", actual.getIn().getBody());
+        assertEquals(null, repo2.get("foo"));
+
+        actual = repo2.get("bar");
+        assertEquals("Hello World", actual.getIn().getBody());
+        assertEquals(null, repo1.get("bar"));
+    }
+
+    @Test
+    public void testMultipeRepoSameKeyDifferentContent() {
+        HawtDBAggregationRepository<String> repo1 = new 
HawtDBAggregationRepository<String>();
+        repo1.setFile(hawtDBFile);
+        repo1.setName("repo1");
+
+        HawtDBAggregationRepository<String> repo2 = new 
HawtDBAggregationRepository<String>();
+        repo2.setFile(hawtDBFile);
+        repo2.setName("repo2");
+
+        Exchange exchange1 = new DefaultExchange(context);
+        exchange1.getIn().setBody("Hello World");
+        repo1.add("foo", exchange1);
+
+        Exchange exchange2 = new DefaultExchange(context);
+        exchange2.getIn().setBody("Bye World");
+        repo2.add("foo", exchange2);
+
+        Exchange actual = repo1.get("foo");
+        assertEquals("Hello World", actual.getIn().getBody());
+        actual = repo2.get("foo");
+        assertEquals("Bye World", actual.getIn().getBody());
     }
 
-}
+}
\ No newline at end of file


Reply via email to