[jboss-cvs] JBoss Messaging SVN: r3464 - in branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl: bdbje/integration/test and 6 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Mon Dec 10 12:26:06 EST 2007
Author: timfox
Date: 2007-12-10 12:26:06 -0500 (Mon, 10 Dec 2007)
New Revision: 3464
Added:
branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/integration/test/unit/
branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/integration/test/unit/RealBDBJEEnvironmentTest.java
branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/unit/
branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/unit/BDBJEEnvironmentTestBase.java
branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/unit/BDBJEPersistenceManagerTest.java
branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/unit/BDBSpeedTest.java
branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/unit/FakeBDBJEEnvironmentTest.java
branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/unit/fakes/
branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/unit/fakes/FakeBDBJEEnvironment.java
branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/test/unit/RoundRobinDistributionPolicyTest.java
Removed:
branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/integration/test/RealBDBJEEnvironmentTest.java
branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/BDBJEEnvironmentTestBase.java
branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/BDBJEPersistenceManagerTest.java
branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/BDBSpeedTest.java
branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/FakeBDBJEEnvironmentTest.java
branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/fakes/FakeBDBJEEnvironment.java
Modified:
branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/RoundRobinDistributionPolicy.java
Log:
More moving of tests
Modified: branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/RoundRobinDistributionPolicy.java
===================================================================
--- branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/RoundRobinDistributionPolicy.java 2007-12-10 17:09:58 UTC (rev 3463)
+++ branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/RoundRobinDistributionPolicy.java 2007-12-10 17:26:06 UTC (rev 3464)
@@ -36,7 +36,7 @@
public class RoundRobinDistributionPolicy implements DistributionPolicy
{
public int select(List<Consumer> consumers, int pos)
- {
+ {
if (pos == -1)
{
//First time
Deleted: branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/integration/test/RealBDBJEEnvironmentTest.java
===================================================================
--- branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/integration/test/RealBDBJEEnvironmentTest.java 2007-12-10 17:09:58 UTC (rev 3463)
+++ branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/integration/test/RealBDBJEEnvironmentTest.java 2007-12-10 17:26:06 UTC (rev 3464)
@@ -1,48 +0,0 @@
-package org.jboss.messaging.newcore.impl.bdbje.integration.test;
-
-import java.io.File;
-
-import org.jboss.messaging.newcore.impl.bdbje.BDBJEEnvironment;
-import org.jboss.messaging.newcore.impl.bdbje.integration.RealBDBJEEnvironment;
-import org.jboss.messaging.newcore.impl.bdbje.test.BDBJEEnvironmentTestBase;
-
-/**
- *
- * A RealBDBJEEnvironmentTest
- *
- * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
- *
- */
-public class RealBDBJEEnvironmentTest extends BDBJEEnvironmentTestBase
-{
- protected void setUp() throws Exception
- {
- createDir(ENV_DIR);
-
- env = createEnvironment();
-
- env.setEnvironmentPath(ENV_DIR);
-
- env.start();
-
- database = env.getDatabase("test-db");
- }
-
- protected BDBJEEnvironment createEnvironment() throws Exception
- {
- BDBJEEnvironment env = new RealBDBJEEnvironment(true);
-
- env.setTransacted(true);
-
- return env;
- }
-
- protected void createDir(String path)
- {
- File file = new File(path);
-
- deleteDirectory(file);
-
- file.mkdir();
- }
-}
Copied: branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/integration/test/unit/RealBDBJEEnvironmentTest.java (from rev 3462, branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/integration/test/RealBDBJEEnvironmentTest.java)
===================================================================
--- branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/integration/test/unit/RealBDBJEEnvironmentTest.java (rev 0)
+++ branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/integration/test/unit/RealBDBJEEnvironmentTest.java 2007-12-10 17:26:06 UTC (rev 3464)
@@ -0,0 +1,48 @@
+package org.jboss.messaging.newcore.impl.bdbje.integration.test.unit;
+
+import java.io.File;
+
+import org.jboss.messaging.newcore.impl.bdbje.BDBJEEnvironment;
+import org.jboss.messaging.newcore.impl.bdbje.integration.RealBDBJEEnvironment;
+import org.jboss.messaging.newcore.impl.bdbje.test.unit.BDBJEEnvironmentTestBase;
+
+/**
+ *
+ * A RealBDBJEEnvironmentTest
+ *
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ *
+ */
+public class RealBDBJEEnvironmentTest extends BDBJEEnvironmentTestBase
+{
+ protected void setUp() throws Exception
+ {
+ createDir(ENV_DIR);
+
+ env = createEnvironment();
+
+ env.setEnvironmentPath(ENV_DIR);
+
+ env.start();
+
+ database = env.getDatabase("test-db");
+ }
+
+ protected BDBJEEnvironment createEnvironment() throws Exception
+ {
+ BDBJEEnvironment env = new RealBDBJEEnvironment(true);
+
+ env.setTransacted(true);
+
+ return env;
+ }
+
+ protected void createDir(String path)
+ {
+ File file = new File(path);
+
+ deleteDirectory(file);
+
+ file.mkdir();
+ }
+}
Deleted: branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/BDBJEEnvironmentTestBase.java
===================================================================
--- branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/BDBJEEnvironmentTestBase.java 2007-12-10 17:09:58 UTC (rev 3463)
+++ branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/BDBJEEnvironmentTestBase.java 2007-12-10 17:26:06 UTC (rev 3464)
@@ -1,1726 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2005, JBoss Inc., and individual contributors as indicated
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.jboss.messaging.newcore.impl.bdbje.test;
-
-import java.util.List;
-
-import javax.transaction.xa.Xid;
-
-import org.jboss.messaging.newcore.impl.bdbje.BDBJEDatabase;
-import org.jboss.messaging.newcore.impl.bdbje.BDBJEEnvironment;
-import org.jboss.messaging.newcore.impl.bdbje.BDBJETransaction;
-import org.jboss.messaging.test.unit.UnitTestCase;
-
-/**
- *
- * Base for tests for BDBJEEnvironment and BDBJEDatabase
- *
- * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
- *
- */
-public abstract class BDBJEEnvironmentTestBase extends UnitTestCase
-{
- protected BDBJEEnvironment env;
-
- protected BDBJEDatabase database;
-
- protected static final String ENV_DIR = "test-bdb-environment";
-
- protected static final String DB_NAME = "test-db";
-
- @Override
- protected void setUp() throws Exception
- {
- env = createEnvironment();
-
- env.setEnvironmentPath(ENV_DIR);
-
- env.start();
-
- database = env.getDatabase(DB_NAME);
- }
-
- protected abstract void createDir(String path);
-
- protected void tearDown() throws Exception
- {
- database.close();
-
- env.stop();
- }
-
- protected abstract BDBJEEnvironment createEnvironment() throws Exception;
-
- // The tests ----------------------------------------------------------------
-
- public void testGetInDoubtXidsCompleteWithCommit() throws Exception
- {
- List<Xid> xids = env.getInDoubtXids();
-
- assertTrue(xids.isEmpty());
-
- Xid xid1 = generateXid();
-
- env.startWork(xid1);
-
- database.put(null, 1, new byte[10], 0, 10);
-
- env.endWork(xid1, false);
-
- env.prepare(xid1);
-
- xids = env.getInDoubtXids();
-
- assertEquals(xid1, xids.get(0));
-
- env.commit(xid1);
-
- xids = env.getInDoubtXids();
-
- assertTrue(xids.isEmpty());
- }
-
- public void testGetInDoubtXidsCompleteWithRollback() throws Exception
- {
- List<Xid> xids = env.getInDoubtXids();
-
- assertTrue(xids.isEmpty());
-
- Xid xid1 = generateXid();
-
- env.startWork(xid1);
-
- database.put(null, 1, new byte[10], 0, 10);
-
- env.endWork(xid1, false);
-
- env.prepare(xid1);
-
- xids = env.getInDoubtXids();
-
- assertEquals(xid1, xids.get(0));
-
- env.rollback(xid1);
-
- xids = env.getInDoubtXids();
-
- assertTrue(xids.isEmpty());
- }
-
-
- public void testGetInDoubtXidsMultiple() throws Exception
- {
- List<Xid> xids = env.getInDoubtXids();
-
- assertTrue(xids.isEmpty());
-
- Xid xid1 = generateXid();
- env.startWork(xid1);
- database.put(null, 1, new byte[10], 0, 10);
- env.endWork(xid1, false);
-
- env.prepare(xid1);
- xids = env.getInDoubtXids();
- assertEquals(xid1, xids.get(0));
-
-
- Xid xid2 = generateXid();
- env.startWork(xid2);
- database.put(null, 1, new byte[10], 0, 10);
- env.endWork(xid2, false);
-
- env.prepare(xid2);
- xids = env.getInDoubtXids();
- assertTrue(xids.contains(xid1));
- assertTrue(xids.contains(xid2));
-
- Xid xid3 = generateXid();
- env.startWork(xid3);
- database.put(null, 1, new byte[10], 0, 10);
- env.endWork(xid3, false);
-
- env.prepare(xid3);
- xids = env.getInDoubtXids();
- assertTrue(xids.contains(xid1));
- assertTrue(xids.contains(xid2));
- assertTrue(xids.contains(xid3));
-
- }
-
- public void testGetInDoubtXidsMultipleWithRestart() throws Exception
- {
- List<Xid> xids = env.getInDoubtXids();
-
- assertTrue(xids.isEmpty());
-
- Xid xid1 = generateXid();
- env.startWork(xid1);
- database.put(null, 1, new byte[10], 0, 10);
- env.endWork(xid1, false);
-
- env.prepare(xid1);
- xids = env.getInDoubtXids();
- assertEquals(xid1, xids.get(0));
-
-
- Xid xid2 = generateXid();
- env.startWork(xid2);
- database.put(null, 1, new byte[10], 0, 10);
- env.endWork(xid2, false);
-
- env.prepare(xid2);
- xids = env.getInDoubtXids();
- assertTrue(xids.contains(xid1));
- assertTrue(xids.contains(xid2));
-
- Xid xid3 = generateXid();
- env.startWork(xid3);
- database.put(null, 1, new byte[10], 0, 10);
- env.endWork(xid3, false);
-
- env.prepare(xid3);
- xids = env.getInDoubtXids();
- assertTrue(xids.contains(xid1));
- assertTrue(xids.contains(xid2));
- assertTrue(xids.contains(xid3));
-
- database.close();
-
- env.stop();
-
- env.start();
-
- database = env.getDatabase(DB_NAME);
-
- xids = env.getInDoubtXids();
- assertTrue(xids.contains(xid1));
- assertTrue(xids.contains(xid2));
- assertTrue(xids.contains(xid3));
-
- env.commit(xid1);
- env.commit(xid2);
- env.commit(xid3);
-
- xids = env.getInDoubtXids();
-
- assertTrue(xids.isEmpty());
- }
-
- public void testPutAndRemoveNonTransactional() throws Exception
- {
- byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
-
- long id = 1;
-
- int offset = 0;
-
- database.put(null, id, bytes, offset, bytes.length);
-
- assertContainsPair(id, bytes, 1);
-
- database.remove(null, 1);
-
- assertStoreEmpty();
- }
-
- public void testPutAndRemoveNonTransactionalWithRestart() throws Exception
- {
- byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
-
- long id = 1;
-
- int offset = 0;
-
- database.put(null, id, bytes, offset, bytes.length);
-
- assertContainsPair(id, bytes, 1);
-
- database.close();
-
- env.stop();
-
- env.start();
-
- database = env.getDatabase(DB_NAME);
-
- assertContainsPair(id, bytes, 1);
- }
-
- public void testPutAndRemoveMultipleNonTransactional() throws Exception
- {
- byte[] bytes1 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
-
- byte[] bytes2 = new byte[] { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
-
- byte[] bytes3 = new byte[] { 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 };
-
- long id1 = 1;
-
- long id2 = 2;
-
- long id3 = 3;
-
- int offset = 0;
-
- database.put(null, id1, bytes1, offset, bytes1.length);
-
- database.put(null, id2, bytes2, offset, bytes2.length);
-
- database.put(null, id3, bytes3, offset, bytes3.length);
-
- assertContainsPair(id1, bytes1, 3);
-
- assertContainsPair(id2, bytes2, 3);
-
- assertContainsPair(id3, bytes3, 3);
-
- database.remove(null, id2);
-
- assertContainsPair(id1, bytes1, 2);
-
- assertContainsPair(id3, bytes3, 2);
-
- database.remove(null, id3);
-
- assertContainsPair(id1, bytes1, 1);
-
- database.remove(null, id1);
-
- assertStoreEmpty();
- }
-
- public void testPutTransactionalCommit() throws Exception
- {
- byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
-
- long id = 1;
-
- int offset = 0;
-
- BDBJETransaction tx = env.createTransaction();
-
- database.put(tx, id, bytes, offset, bytes.length);
-
- tx.commit();
-
- assertContainsPair(id, bytes, 1);
-
- database.remove(null, 1);
-
- assertStoreEmpty();
- }
-
- public void testPutTransactionalWithRestart() throws Exception
- {
- byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
-
- long id = 1;
-
- int offset = 0;
-
- BDBJETransaction tx = env.createTransaction();
-
- database.put(tx, id, bytes, offset, bytes.length);
-
- //Now restart before committing
-
- database.close();
-
- env.stop();
-
- env.start();
-
- database = env.getDatabase(DB_NAME);
-
- assertStoreEmpty();
- }
-
- public void testPutXACommit() throws Exception
- {
- byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
-
- long id = 1;
-
- int offset = 0;
-
- Xid xid = generateXid();
-
- env.startWork(xid);
-
- database.put(null, id, bytes, offset, bytes.length);
-
- env.endWork(xid, false);
-
- env.prepare(xid);
-
- env.commit(xid);
-
- assertContainsPair(id, bytes, 1);
-
- database.remove(null, 1);
-
- assertStoreEmpty();
- }
-
- public void testPutXAWithRestart() throws Exception
- {
- byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
-
- long id = 1;
-
- int offset = 0;
-
- Xid xid = generateXid();
-
- env.startWork(xid);
-
- database.put(null, id, bytes, offset, bytes.length);
-
- env.endWork(xid, false);
-
- // Now restart
-
- database.close();
-
- env.stop();
-
- env.start();
-
- database = env.getDatabase(DB_NAME);
-
- assertStoreEmpty();
- }
-
-
- public void testPutXAWithRestartAfterPrepare() throws Exception
- {
- byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
-
- long id = 1;
-
- int offset = 0;
-
- Xid xid = generateXid();
-
- env.startWork(xid);
-
- database.put(null, id, bytes, offset, bytes.length);
-
- env.endWork(xid, false);
-
- env.prepare(xid);
-
- // Now restart
-
- database.close();
-
- env.stop();
-
- env.start();
-
- database = env.getDatabase(DB_NAME);
-
- assertStoreEmpty();
- }
-
- public void testRemoveTransactional() throws Exception
- {
- byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
-
- long id = 1;
-
- int offset = 0;
-
- database.put(null, id, bytes, offset, bytes.length);
-
- assertContainsPair(id, bytes, 1);
-
- BDBJETransaction tx = env.createTransaction();
-
- database.remove(tx, id);
-
- tx.commit();
-
- assertStoreEmpty();
- }
-
- public void testRemoveTransactionalWithRestart() throws Exception
- {
- byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
-
- long id = 1;
-
- int offset = 0;
-
- database.put(null, id, bytes, offset, bytes.length);
-
- assertContainsPair(id, bytes, 1);
-
- BDBJETransaction tx = env.createTransaction();
-
- database.remove(tx, id);
-
- // Now restart
-
- database.close();
-
- env.stop();
-
- env.start();
-
- database = env.getDatabase(DB_NAME);
-
- assertContainsPair(id, bytes, 1);
- }
-
- public void testRemoveXACommit() throws Exception
- {
- byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
-
- long id = 1;
-
- int offset = 0;
-
- database.put(null, id, bytes, offset, bytes.length);
-
- assertContainsPair(id, bytes, 1);
-
- Xid xid = generateXid();
-
- env.startWork(xid);
-
- database.remove(null, id);
-
- env.endWork(xid, false);
-
- env.prepare(xid);
-
- env.commit(xid);
-
- assertStoreEmpty();
- }
-
- public void testRemoveXAWithRestart() throws Exception
- {
- byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
-
- long id = 1;
-
- int offset = 0;
-
- database.put(null, id, bytes, offset, bytes.length);
-
- assertContainsPair(id, bytes, 1);
-
- Xid xid = generateXid();
-
- env.startWork(xid);
-
- database.remove(null, id);
-
- env.endWork(xid, false);
-
- // Now restart
-
- database.close();
-
- env.stop();
-
- env.start();
-
- database = env.getDatabase(DB_NAME);
-
- assertContainsPair(id, bytes, 1);
- }
-
- public void testRemoveXAWithRestartAfterPrepare() throws Exception
- {
- byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
-
- long id = 1;
-
- int offset = 0;
-
- database.put(null, id, bytes, offset, bytes.length);
-
- assertContainsPair(id, bytes, 1);
-
- Xid xid = generateXid();
-
- env.startWork(xid);
-
- database.remove(null, id);
-
- env.endWork(xid, false);
-
- env.prepare(xid);
-
- // Now restart
-
- database.close();
-
- env.stop();
-
- env.start();
-
- database = env.getDatabase(DB_NAME);
-
- assertContainsPair(id, bytes, 1);
- }
-
- public void testPutTransactionalRollback() throws Exception
- {
- byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
-
- long id = 1;
-
- int offset = 0;
-
- BDBJETransaction tx = env.createTransaction();
-
- database.put(tx, id, bytes, offset, bytes.length);
-
- tx.rollback();
-
- assertStoreEmpty();
- }
-
- public void testPutXARollback() throws Exception
- {
- byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
-
- long id = 1;
-
- int offset = 0;
-
- Xid xid = generateXid();
-
- env.startWork(xid);
-
- database.put(null, id, bytes, offset, bytes.length);
-
- env.endWork(xid, false);
-
- env.prepare(xid);
-
- env.rollback(xid);
-
- assertStoreEmpty();
- }
-
- public void testRemoveTransactionalRollback() throws Exception
- {
- byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
-
- long id = 1;
-
- int offset = 0;
-
- database.put(null, id, bytes, offset, bytes.length);
-
- assertContainsPair(id, bytes, 1);
-
- BDBJETransaction tx = env.createTransaction();
-
- database.remove(tx, id);
-
- tx.rollback();
-
- assertContainsPair(id, bytes, 1);
- }
-
- public void testRemoveXARollback() throws Exception
- {
- byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
-
- long id = 1;
-
- int offset = 0;
-
- database.put(null, id, bytes, offset, bytes.length);
-
- assertContainsPair(id, bytes, 1);
-
- Xid xid = generateXid();
-
- env.startWork(xid);
-
- database.remove(null, id);
-
- env.endWork(xid, false);
-
- env.prepare(xid);
-
- env.rollback(xid);
-
- assertContainsPair(id, bytes, 1);
- }
-
-
- public void testPutAndRemoveMultipleTransactionalCommit() throws Exception
- {
- byte[] bytes1 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
-
- byte[] bytes2 = new byte[] { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
-
- byte[] bytes3 = new byte[] { 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 };
-
- byte[] bytes4 = new byte[] { 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 };
-
- long id1 = 1;
-
- long id2 = 2;
-
- long id3 = 3;
-
- long id4 = 4;
-
- int offset = 0;
-
- database.put(null, id1, bytes1, offset, bytes1.length);
-
- database.put(null, id2, bytes2, offset, bytes2.length);
-
- assertContainsPair(id1, bytes1, 2);
-
- assertContainsPair(id2, bytes2, 2);
-
- BDBJETransaction tx = env.createTransaction();
-
- database.put(tx, id3, bytes3, offset, bytes3.length);
-
- database.put(tx, id4, bytes4, offset, bytes4.length);
-
- database.remove(tx, id1);
-
- database.remove(tx, id2);
-
- tx.commit();
-
- assertContainsPair(id3, bytes3, 2);
-
- assertContainsPair(id4, bytes4, 2);
- }
-
- public void testPutAndRemoveMultipleXACommit() throws Exception
- {
- byte[] bytes1 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
-
- byte[] bytes2 = new byte[] { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
-
- byte[] bytes3 = new byte[] { 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 };
-
- byte[] bytes4 = new byte[] { 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 };
-
- long id1 = 1;
-
- long id2 = 2;
-
- long id3 = 3;
-
- long id4 = 4;
-
- int offset = 0;
-
- database.put(null, id1, bytes1, offset, bytes1.length);
-
- database.put(null, id2, bytes2, offset, bytes2.length);
-
- assertContainsPair(id1, bytes1, 2);
-
- assertContainsPair(id2, bytes2, 2);
-
- Xid xid = generateXid();
-
- env.startWork(xid);
-
- database.put(null, id3, bytes3, offset, bytes3.length);
-
- database.put(null, id4, bytes4, offset, bytes4.length);
-
- database.remove(null, id1);
-
- database.remove(null, id2);
-
- env.endWork(xid, false);
-
- env.prepare(xid);
-
- env.commit(xid);
-
- assertContainsPair(id3, bytes3, 2);
-
- assertContainsPair(id4, bytes4, 2);
- }
-
- public void testPutAndRemoveMultipleXAWithRestart() throws Exception
- {
- byte[] bytes1 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
-
- byte[] bytes2 = new byte[] { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
-
- byte[] bytes3 = new byte[] { 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 };
-
- byte[] bytes4 = new byte[] { 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 };
-
- long id1 = 1;
-
- long id2 = 2;
-
- long id3 = 3;
-
- long id4 = 4;
-
- int offset = 0;
-
- database.put(null, id1, bytes1, offset, bytes1.length);
-
- database.put(null, id2, bytes2, offset, bytes2.length);
-
- assertContainsPair(id1, bytes1, 2);
-
- assertContainsPair(id2, bytes2, 2);
-
- Xid xid = generateXid();
-
- env.startWork(xid);
-
- database.put(null, id3, bytes3, offset, bytes3.length);
-
- database.put(null, id4, bytes4, offset, bytes4.length);
-
- database.remove(null, id1);
-
- database.remove(null, id2);
-
- env.endWork(xid, false);
-
- // Now restart
-
- database.close();
-
- env.stop();
-
- env.start();
-
- database = env.getDatabase(DB_NAME);
-
- assertContainsPair(id1, bytes1, 2);
-
- assertContainsPair(id2, bytes2, 2);
- }
-
- public void testPutAndRemoveMultipleTransactionalRollback() throws Exception
- {
- byte[] bytes1 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
-
- byte[] bytes2 = new byte[] { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
-
- byte[] bytes3 = new byte[] { 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 };
-
- byte[] bytes4 = new byte[] { 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 };
-
- long id1 = 1;
-
- long id2 = 2;
-
- long id3 = 3;
-
- long id4 = 4;
-
- int offset = 0;
-
- database.put(null, id1, bytes1, offset, bytes1.length);
-
- database.put(null, id2, bytes2, offset, bytes2.length);
-
- assertContainsPair(id1, bytes1, 2);
-
- assertContainsPair(id2, bytes2, 2);
-
- BDBJETransaction tx = env.createTransaction();
-
- database.put(tx, id3, bytes3, offset, bytes3.length);
-
- database.put(tx, id4, bytes4, offset, bytes4.length);
-
- database.remove(tx, id1);
-
- database.remove(tx, id2);
-
- tx.rollback();
-
- assertContainsPair(id1, bytes1, 2);
-
- assertContainsPair(id2, bytes2, 2);
- }
-
- public void testPutAndRemoveMultipleXARollback() throws Exception
- {
- byte[] bytes1 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
-
- byte[] bytes2 = new byte[] { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
-
- byte[] bytes3 = new byte[] { 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 };
-
- byte[] bytes4 = new byte[] { 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 };
-
- long id1 = 1;
-
- long id2 = 2;
-
- long id3 = 3;
-
- long id4 = 4;
-
- int offset = 0;
-
- database.put(null, id1, bytes1, offset, bytes1.length);
-
- database.put(null, id2, bytes2, offset, bytes2.length);
-
- assertContainsPair(id1, bytes1, 2);
-
- assertContainsPair(id2, bytes2, 2);
-
- Xid xid = generateXid();
-
- env.startWork(xid);
-
- database.put(null, id3, bytes3, offset, bytes3.length);
-
- database.put(null, id4, bytes4, offset, bytes4.length);
-
- database.remove(null, id1);
-
- database.remove(null, id2);
-
- env.endWork(xid, false);
-
- env.prepare(xid);
-
- env.rollback(xid);
-
- assertContainsPair(id1, bytes1, 2);
-
- assertContainsPair(id2, bytes2, 2);
- }
-
- public void testOverwiteNonTransactional() throws Exception
- {
- byte[] bytes1 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
-
- byte[] bytes2 = new byte[] { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
-
- long id = 1;
-
- int offset = 0;
-
- database.put(null, id, bytes1, offset, bytes1.length);
-
- assertContainsPair(id, bytes1, 1);
-
- database.put(null, id, bytes2, offset, bytes1.length);
-
- assertContainsPair(id, bytes2, 1);
-
- database.remove(null, 1);
-
- assertStoreEmpty();
- }
-
- public void testOverwiteTransactionalCommit() throws Exception
- {
- byte[] bytes1 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
-
- byte[] bytes2 = new byte[] { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
-
- long id = 1;
-
- int offset = 0;
-
- BDBJETransaction tx = env.createTransaction();
-
- database.put(tx, id, bytes1, offset, bytes1.length);
-
- database.put(tx, id, bytes2, offset, bytes1.length);
-
- tx.commit();
-
- assertContainsPair(id, bytes2, 1);
-
- database.remove(null, 1);
-
- assertStoreEmpty();
- }
-
- public void testOverwiteXACommit() throws Exception
- {
- byte[] bytes1 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
-
- byte[] bytes2 = new byte[] { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
-
- long id = 1;
-
- int offset = 0;
-
- Xid xid = generateXid();
-
- env.startWork(xid);
-
- database.put(null, id, bytes1, offset, bytes1.length);
-
- database.put(null, id, bytes2, offset, bytes1.length);
-
- env.endWork(xid, false);
-
- env.prepare(xid);
-
- env.commit(xid);
-
- assertContainsPair(id, bytes2, 1);
-
- database.remove(null, 1);
-
- assertStoreEmpty();
- }
-
- public void testOverwiteTransactionalRollback() throws Exception
- {
- byte[] bytes1 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
-
- byte[] bytes2 = new byte[] { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
-
- long id = 1;
-
- int offset = 0;
-
- BDBJETransaction tx = env.createTransaction();
-
- database.put(tx, id, bytes1, offset, bytes1.length);
-
- database.put(tx, id, bytes2, offset, bytes1.length);
-
- tx.rollback();
-
- assertStoreEmpty();
- }
-
- public void testOverwiteXARollback() throws Exception
- {
- byte[] bytes1 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
-
- byte[] bytes2 = new byte[] { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
-
- long id = 1;
-
- int offset = 0;
-
- Xid xid = generateXid();
-
- env.startWork(xid);
-
- database.put(null, id, bytes1, offset, bytes1.length);
-
- database.put(null, id, bytes2, offset, bytes1.length);
-
- env.endWork(xid, false);
-
- env.prepare(xid);
-
- env.rollback(xid);
-
- assertStoreEmpty();
- }
-
- public void testPutAndRemovePartialNonTransactional() throws Exception
- {
- byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
-
- byte[] bytes2 = new byte[] { 20, 21, 22, 23 };
-
- long id = 1;
-
- int offset = 0;
-
- database.put(null, id, bytes, offset, bytes.length);
-
- assertContainsPair(id, bytes, 1);
-
- database.put(null, id, bytes2, 10, bytes2.length);
-
- byte[] bytes3 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 21, 22, 23 };
-
- assertContainsPair(id, bytes3, 1);
-
- database.put(null, id, bytes2, 3, bytes2.length);
-
- byte[] bytes4 = new byte[] { 1, 2, 3, 20, 21, 22, 23, 8, 9, 10, 20, 21, 22, 23 };
-
- assertContainsPair(id, bytes4, 1);
-
- byte[] bytes5 = new byte[0];
-
- //blank out 4 bytes
- database.put(null, id, bytes5, 5, 4);
-
- byte[] bytes6 = new byte[] { 1, 2, 3, 20, 21, 10, 20, 21, 22, 23 };
-
- assertContainsPair(id, bytes6, 1);
-
-
- database.put(null, id, new byte[0], 0, 4);
-
- byte[] bytes7 = new byte[] { 21, 10, 20, 21, 22, 23 };
-
- assertContainsPair(id, bytes7, 1);
-
-
- database.remove(null, 1);
-
- assertStoreEmpty();
- }
-
- public void testPutAndRemovePartialTransactional() throws Exception
- {
- byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
-
- byte[] bytes2 = new byte[] { 20, 21, 22, 23 };
-
- long id = 1;
-
- int offset = 0;
-
- BDBJETransaction tx = env.createTransaction();
-
- database.put(tx, id, bytes, offset, bytes.length);
-
- tx.commit();
-
- assertContainsPair(id, bytes, 1);
-
- tx = env.createTransaction();
-
- database.put(tx, id, bytes2, 10, bytes2.length);
-
- tx.commit();
-
- byte[] bytes3 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 21, 22, 23 };
-
- assertContainsPair(id, bytes3, 1);
-
- tx = env.createTransaction();
-
- database.put(tx, id, bytes2, 3, bytes2.length);
-
- tx.commit();
-
- byte[] bytes4 = new byte[] { 1, 2, 3, 20, 21, 22, 23, 8, 9, 10, 20, 21, 22, 23 };
-
- assertContainsPair(id, bytes4, 1);
-
- byte[] bytes5 = new byte[0];
-
- tx = env.createTransaction();
-
- //blank out 4 bytes
- database.put(tx, id, bytes5, 5, 4);
-
- tx.commit();
-
- byte[] bytes6 = new byte[] { 1, 2, 3, 20, 21, 10, 20, 21, 22, 23 };
-
- assertContainsPair(id, bytes6, 1);
-
- database.put(null, id, new byte[0], 0, 4);
-
- byte[] bytes7 = new byte[] { 21, 10, 20, 21, 22, 23 };
-
- assertContainsPair(id, bytes7, 1);
-
- database.remove(null, 1);
-
- assertStoreEmpty();
- }
-
- public void testSetAndGetEnvironment() throws Exception
- {
- BDBJEEnvironment bdb = createEnvironment();
-
- final String path = "/home/tim/test-path123";
-
- createDir(path);
-
- assertNull(bdb.getEnvironmentPath());
-
- bdb.setEnvironmentPath(path);
-
- assertEquals(path, bdb.getEnvironmentPath());
-
- bdb.start();
-
- try
- {
- bdb.setEnvironmentPath("blah");
-
- fail("Should throw exception");
- }
- catch (IllegalStateException e)
- {
- //OK
- }
-
- assertEquals(path, bdb.getEnvironmentPath());
-
- bdb.stop();
-
- final String path2 = "test-path123651";
-
- bdb.setEnvironmentPath(path2);
-
- assertEquals(path2, bdb.getEnvironmentPath());
- }
-
-
- public void testSetAndGetTransacted() throws Exception
- {
- BDBJEEnvironment bdb = createEnvironment();
-
- final String path = "/home/tim/test-path123";
-
- createDir(path);
-
- bdb.setEnvironmentPath(path);
-
- bdb.setTransacted(false);
-
- assertFalse(bdb.isTransacted());
-
- bdb.setTransacted(true);
-
- assertTrue(bdb.isTransacted());
-
- bdb.start();
-
- try
- {
- bdb.setTransacted(true);
-
- fail("Should throw exception");
- }
- catch (IllegalStateException e)
- {
- //OK
- }
-
- assertTrue(bdb.isTransacted());
-
- bdb.stop();
-
- bdb.setTransacted(false);
-
- assertFalse(bdb.isTransacted());
- }
-
- public void testSetAndGetSyncOS() throws Exception
- {
- BDBJEEnvironment bdb = createEnvironment();
-
- final String path = "/home/tim/test-path123";
-
- createDir(path);
-
- bdb.setEnvironmentPath(path);
-
- assertFalse(bdb.isSyncOS());
-
- bdb.setSyncOS(true);
-
- assertTrue(bdb.isSyncOS());
-
- bdb.start();
-
- try
- {
- bdb.setSyncOS(true);
-
- fail("Should throw exception");
- }
- catch (IllegalStateException e)
- {
- //OK
- }
-
- assertTrue(bdb.isSyncOS());
-
- bdb.stop();
-
- bdb.setSyncOS(false);
-
- assertFalse(bdb.isSyncOS());
- }
-
- public void testSetAndGetSyncVM() throws Exception
- {
- BDBJEEnvironment bdb = createEnvironment();
-
- final String path = "/home/tim/test-path123";
-
- bdb.setEnvironmentPath(path);
-
- createDir(path);
-
- assertFalse(bdb.isSyncVM());
-
- bdb.setSyncVM(true);
-
- assertTrue(bdb.isSyncVM());
-
- bdb.start();
-
- try
- {
- bdb.setSyncVM(true);
-
- fail("Should throw exception");
- }
- catch (IllegalStateException e)
- {
- //OK
- }
-
- assertTrue(bdb.isSyncVM());
-
- bdb.stop();
-
- bdb.setSyncVM(false);
-
- assertFalse(bdb.isSyncVM());
- }
-
- public void testSetAndGetMemoryCacheSize() throws Exception
- {
- BDBJEEnvironment bdb = createEnvironment();
-
- final String path = "/home/tim/test-path123";
-
- createDir(path);
-
- bdb.setEnvironmentPath(path);
-
- assertEquals(-1, bdb.getMemoryCacheSize());
-
- final long size = 16251762;
-
- bdb.setMemoryCacheSize(size);
-
- assertEquals(size, bdb.getMemoryCacheSize());
-
- bdb.start();
-
- try
- {
- bdb.setMemoryCacheSize(1897291289);
-
- fail("Should throw exception");
- }
- catch (IllegalStateException e)
- {
- //OK
- }
-
- assertEquals(size, bdb.getMemoryCacheSize());
-
- bdb.stop();
-
- final long size2 = 1625534783;
-
- bdb.setMemoryCacheSize(size2);
-
- assertEquals(size2, bdb.getMemoryCacheSize());
- }
-
-
- public void testStartAndStop() throws Exception
- {
- BDBJEEnvironment bdb = createEnvironment();
-
- try
- {
- bdb.start();
- fail("Should throw exception");
- }
- catch (IllegalStateException e)
- {
- //OK
- }
-
- final String path = "/home/tim/test-path123";
-
- createDir(path);
-
- bdb.setEnvironmentPath(path);
-
- bdb.start();
-
- try
- {
- bdb.start();
-
- fail("Should throw exception");
- }
- catch (IllegalStateException e)
- {
- //OK
- }
-
- bdb.stop();
-
- try
- {
- bdb.stop();
-
- fail("Should throw exception");
- }
- catch (IllegalStateException e)
- {
- //OK
- }
- }
-
- public void testWrongOrderCommit() throws Exception
- {
- testXAWrongOrder(true);
- }
-
- public void testWrongOrderRollback() throws Exception
- {
- testXAWrongOrder(false);
- }
-
- public void testXAWrongXidCommit() throws Exception
- {
- testXAWrongXid(true);
- }
-
- public void testXAWrongXidRollback() throws Exception
- {
- testXAWrongXid(false);
- }
-
- private void testXAWrongXid(boolean commit) throws Exception
- {
- Xid xid = generateXid();
-
- env.startWork(xid);
-
- Xid xid2 = generateXid();
-
- try
- {
- env.endWork(xid2, false);
- fail("Should throw exception");
- }
- catch (IllegalStateException e)
- {
- //Ok
- }
-
- //do some work
-
- database.put(null, 23, new byte[10], 0, 10);
-
- env.endWork(xid, false);
-
- try
- {
- env.prepare(xid2);
- fail("Should throw exception");
- }
- catch (IllegalStateException e)
- {
- //Ok
- }
-
- env.prepare(xid);
-
- if (commit)
- {
- try
- {
- env.commit(xid2);
- fail("Should throw exception");
- }
- catch (IllegalStateException e)
- {
- //Ok
- }
- env.commit(xid);
- }
- else
- {
- try
- {
- env.rollback(xid2);
- fail("Should throw exception");
- }
- catch (IllegalStateException e)
- {
- //Ok
- }
- env.rollback(xid);
- }
- }
-
- private void testXAWrongOrder(boolean commit) throws Exception
- {
- Xid xid = generateXid();
-
- try
- {
- env.endWork(xid, false);
- fail("Should throw exception");
- }
- catch (IllegalStateException e)
- {
- //Ok
- }
-
- try
- {
- env.prepare(xid);
- fail("Should throw exception");
- }
- catch (IllegalStateException e)
- {
- //Ok
- }
-
- try
- {
- env.commit(xid);
- fail("Should throw exception");
- }
- catch (IllegalStateException e)
- {
- //Ok
- }
-
- try
- {
- env.rollback(xid);
- fail("Should throw exception");
- }
- catch (IllegalStateException e)
- {
- //Ok
- }
-
- env.startWork(xid);
-
- //do some work
-
- database.put(null, 23, new byte[10], 0, 10);
-
- try
- {
- env.startWork(xid);
- fail("Should throw exception");
- }
- catch (IllegalStateException e)
- {
- //Ok
- }
-
- try
- {
- env.prepare(xid);
- fail("Should throw exception");
- }
- catch (IllegalStateException e)
- {
- //Ok
- }
-
- try
- {
- env.commit(xid);
- fail("Should throw exception");
- }
- catch (IllegalStateException e)
- {
- //Ok
- }
-
- try
- {
- env.rollback(xid);
- fail("Should throw exception");
- }
- catch (IllegalStateException e)
- {
- //Ok
- }
-
- env.endWork(xid, false);
-
- try
- {
- env.startWork(xid);
- fail("Should throw exception");
- }
- catch (IllegalStateException e)
- {
- //Ok
- }
-
- try
- {
- env.endWork(xid, false);
- fail("Should throw exception");
- }
- catch (IllegalStateException e)
- {
- //Ok
- }
-
- try
- {
- env.commit(xid);
- fail("Should throw exception");
- }
- catch (IllegalStateException e)
- {
- //Ok
- }
-
- try
- {
- env.rollback(xid);
- fail("Should throw exception");
- }
- catch (IllegalStateException e)
- {
- //Ok
- }
-
- env.prepare(xid);
-
- try
- {
- env.startWork(xid);
- fail("Should throw exception");
- }
- catch (IllegalStateException e)
- {
- //Ok
- }
-
- try
- {
- env.endWork(xid, false);
- fail("Should throw exception");
- }
- catch (IllegalStateException e)
- {
- //Ok
- }
-
- if (commit)
- {
- env.commit(xid);
- }
- else
- {
- env.rollback(xid);
- }
-
- try
- {
- env.endWork(xid, false);
- fail("Should throw exception");
- }
- catch (IllegalStateException e)
- {
- //Ok
- }
-
- try
- {
- env.prepare(xid);
- fail("Should throw exception");
- }
- catch (IllegalStateException e)
- {
- //Ok
- }
-
- try
- {
- env.commit(xid);
- fail("Should throw exception");
- }
- catch (IllegalStateException e)
- {
- //Ok
- }
-
- try
- {
- env.rollback(xid);
- fail("Should throw exception");
- }
- catch (IllegalStateException e)
- {
- //Ok
- }
-
- env.startWork(xid);
-
- database.put(null, 23, new byte[10], 0, 10);
-
- env.endWork(xid, false);
-
- env.prepare(xid);
-
- env.rollback(xid);
-
- }
-
- // Private -------------------------------------------------------------------------------------
-
- private void assertContainsPair(long id, byte[] bytes, long size) throws Exception
- {
- byte[] b = database.get(id);
-
- assertNotNull(b);
-
- assertByteArraysEquivalent(bytes, b);
-
- assertEquals(size, database.size());
- }
-
- private void assertStoreEmpty() throws Exception
- {
- assertEquals(0, database.size());
- }
-}
Deleted: branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/BDBJEPersistenceManagerTest.java
===================================================================
--- branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/BDBJEPersistenceManagerTest.java 2007-12-10 17:09:58 UTC (rev 3463)
+++ branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/BDBJEPersistenceManagerTest.java 2007-12-10 17:26:06 UTC (rev 3464)
@@ -1,786 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2005, JBoss Inc., and individual contributors as indicated
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.jboss.messaging.newcore.impl.bdbje.test;
-
-import java.nio.ByteBuffer;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import javax.transaction.xa.Xid;
-
-import org.jboss.messaging.newcore.impl.MessageImpl;
-import org.jboss.messaging.newcore.impl.QueueImpl;
-import org.jboss.messaging.newcore.impl.bdbje.BDBJEDatabase;
-import org.jboss.messaging.newcore.impl.bdbje.BDBJEEnvironment;
-import org.jboss.messaging.newcore.impl.bdbje.BDBJEPersistenceManager;
-import org.jboss.messaging.newcore.impl.bdbje.test.fakes.FakeBDBJEEnvironment;
-import org.jboss.messaging.newcore.intf.Message;
-import org.jboss.messaging.newcore.intf.MessageReference;
-import org.jboss.messaging.newcore.intf.Queue;
-import org.jboss.messaging.test.unit.UnitTestCase;
-
-/**
- *
- * A BDBJEPersistenceManagerTest
- *
- * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
- *
- */
-public class BDBJEPersistenceManagerTest extends UnitTestCase
-{
- protected static final String ENV_DIR = "test-env";
-
- protected BDBJEPersistenceManager pm;
-
- protected BDBJEEnvironment bdb;
-
- protected void setUp() throws Exception
- {
- super.setUp();
-
- bdb = new FakeBDBJEEnvironment();
-
- pm = new BDBJEPersistenceManager(bdb, ENV_DIR);
-
- pm.start();
- }
-
- protected void tearDown() throws Exception
- {
- super.tearDown();
-
- pm.stop();
- }
-
- // The tests ----------------------------------------------------------------
-
- public void testAddMessage() throws Exception
- {
- Queue queue = new QueueImpl(67);
-
- Message m = createMessageWithRefs(1, queue);
-
- pm.addMessage(m);
-
- assertMessageInStore(m, queue);
- }
-
- public void testDeleteReference() throws Exception
- {
- Queue queue = new QueueImpl(67);
-
- Message m = createMessageWithRefs(1, queue);
-
- List<MessageReference> refs = new ArrayList<MessageReference>(m.getReferences());
-
-
- pm.addMessage(m);
-
- assertMessageInStore(m, queue);
-
-
- pm.deleteReference(refs.get(2));
-
- assertMessageInStore(m, queue);
-
- assertEquals(3, m.getReferences().size());
-
- assertTrue(m.getReferences().contains(refs.get(0)));
- assertTrue(m.getReferences().contains(refs.get(1)));
- assertTrue(m.getReferences().contains(refs.get(3)));
-
-
- pm.deleteReference(refs.get(1));
-
- assertMessageInStore(m, queue);
-
- assertEquals(2, m.getReferences().size());
-
- assertTrue(m.getReferences().contains(refs.get(0)));
- assertTrue(m.getReferences().contains(refs.get(3)));
-
-
-
- pm.deleteReference(refs.get(3));
-
- assertMessageInStore(m, queue);
-
- assertEquals(1, m.getReferences().size());
-
- assertTrue(m.getReferences().contains(refs.get(0)));
-
-
- pm.deleteReference(refs.get(0));
-
- assertMessageNotInStore(m);
-
- assertStoreEmpty();
- }
-
- public void testCommitTransaction() throws Exception
- {
- List<Message> msgs = new ArrayList<Message>();
-
- Queue queue = new QueueImpl(67);
-
- Message m1 = createMessageWithRefs(1, queue);
- List<MessageReference> m1Refs = new ArrayList<MessageReference>(m1.getReferences());
-
- msgs.add(m1);
-
- Message m2 = createMessageWithRefs(2, queue);
-
- msgs.add(m2);
-
- Message m3 = createMessageWithRefs(3, queue);
- List<MessageReference> m3Refs = new ArrayList<MessageReference>(m3.getReferences());
-
- msgs.add(m3);
-
- pm.commitTransaction(msgs, null);
-
- assertMessageInStore(m1, queue);
-
- assertMessageInStore(m2, queue);
-
- assertMessageInStore(m3, queue);
-
-
- //Add a couple more
-
- List<Message> msgsMore = new ArrayList<Message>();
-
- Message m4 = createMessageWithRefs(4, queue);
- msgsMore.add(m4);
-
- Message m5 = createMessageWithRefs(5, queue);
- msgsMore.add(m5);
-
- //Delete some refs
-
- List<MessageReference> refsToRemove = new ArrayList<MessageReference>();
-
- refsToRemove.add(m1.getReferences().get(0));
- refsToRemove.add(m1.getReferences().get(3));
-
- refsToRemove.add(m2.getReferences().get(0));
- refsToRemove.add(m2.getReferences().get(1));
- refsToRemove.add(m2.getReferences().get(2));
- refsToRemove.add(m2.getReferences().get(3));
-
- refsToRemove.add(m3.getReferences().get(2));
-
- pm.commitTransaction(msgsMore, refsToRemove);
-
- assertMessageInStore(m1, queue);
- assertEquals(2, m1.getReferences().size());
- assertTrue(m1.getReferences().contains(m1Refs.get(1)));
- assertTrue(m1.getReferences().contains(m1Refs.get(2)));
-
- assertMessageNotInStore(m2);
-
- assertMessageInStore(m3, queue);
- assertEquals(3, m3.getReferences().size());
- assertTrue(m3.getReferences().contains(m3Refs.get(0)));
- assertTrue(m3.getReferences().contains(m3Refs.get(1)));
- assertTrue(m3.getReferences().contains(m3Refs.get(3)));
-
- assertMessageInStore(m4, queue);
- assertEquals(4, m4.getReferences().size());
-
- assertMessageInStore(m5, queue);
- assertEquals(4, m5.getReferences().size());
-
- //Delete the rest
- refsToRemove.clear();
- refsToRemove.addAll(m1.getReferences());
- refsToRemove.addAll(m3.getReferences());
- refsToRemove.addAll(m4.getReferences());
- refsToRemove.addAll(m5.getReferences());
-
- pm.commitTransaction(null, refsToRemove);
-
- assertMessageNotInStore(m1);
- assertMessageNotInStore(m2);
- assertMessageNotInStore(m4);
- assertMessageNotInStore(m5);
- assertMessageNotInStore(m5);
-
- //try with nulls
- pm.commitTransaction(null, null);
-
- }
-
- public void testPrepareAndCommitTransaction() throws Exception
- {
- List<Message> msgs = new ArrayList<Message>();
-
- Queue queue = new QueueImpl(67);
-
- Message m1 = createMessageWithRefs(1, queue);
- List<MessageReference> m1Refs = new ArrayList<MessageReference>(m1.getReferences());
-
- msgs.add(m1);
-
- Message m2 = createMessageWithRefs(2, queue);
-
- msgs.add(m2);
-
- Message m3 = createMessageWithRefs(3, queue);
- List<MessageReference> m3Refs = new ArrayList<MessageReference>(m3.getReferences());
-
- msgs.add(m3);
-
- pm.commitTransaction(msgs, null);
-
- assertMessageInStore(m1, queue);
-
- assertMessageInStore(m2, queue);
-
- assertMessageInStore(m3, queue);
-
-
- //Add a couple more
-
- List<Message> msgsMore = new ArrayList<Message>();
-
- Message m4 = createMessageWithRefs(4, queue);
- msgsMore.add(m4);
-
- Message m5 = createMessageWithRefs(5, queue);
-
- msgsMore.add(m5);
-
- //Delete some refs
-
- List<MessageReference> refsToRemove = new ArrayList<MessageReference>();
-
- refsToRemove.add(m1.getReferences().get(0));
- refsToRemove.add(m1.getReferences().get(3));
-
- refsToRemove.add(m2.getReferences().get(0));
- refsToRemove.add(m2.getReferences().get(1));
- refsToRemove.add(m2.getReferences().get(2));
- refsToRemove.add(m2.getReferences().get(3));
-
- refsToRemove.add(m3.getReferences().get(2));
-
- Xid xid = generateXid();
-
- pm.prepareTransaction(xid, msgsMore, refsToRemove);
-
- pm.commitPreparedTransaction(xid);
-
- assertMessageInStore(m1, queue);
- assertEquals(2, m1.getReferences().size());
- assertTrue(m1.getReferences().contains(m1Refs.get(1)));
- assertTrue(m1.getReferences().contains(m1Refs.get(2)));
-
- assertMessageNotInStore(m2);
-
- assertMessageInStore(m3, queue);
- assertEquals(3, m3.getReferences().size());
- assertTrue(m3.getReferences().contains(m3Refs.get(0)));
- assertTrue(m3.getReferences().contains(m3Refs.get(1)));
- assertTrue(m3.getReferences().contains(m3Refs.get(3)));
-
- assertMessageInStore(m4, queue);
- assertEquals(4, m4.getReferences().size());
-
- assertMessageInStore(m5, queue);
- assertEquals(4, m5.getReferences().size());
-
- //Delete the rest
- refsToRemove.clear();
- refsToRemove.addAll(m1.getReferences());
- refsToRemove.addAll(m3.getReferences());
- refsToRemove.addAll(m4.getReferences());
- refsToRemove.addAll(m5.getReferences());
-
- xid = generateXid();
-
- pm.prepareTransaction(xid, null, refsToRemove);
-
- pm.commitPreparedTransaction(xid);
-
- assertMessageNotInStore(m1);
- assertMessageNotInStore(m2);
- assertMessageNotInStore(m4);
- assertMessageNotInStore(m5);
- assertMessageNotInStore(m5);
-
- //try with nulls
- xid = generateXid();
- pm.prepareTransaction(xid, null, null);
- pm.commitPreparedTransaction(xid);
-
- }
-
- public void testPrepareAndUnprepareTransaction() throws Exception
- {
- List<Message> msgs = new ArrayList<Message>();
-
- Queue queue = new QueueImpl(67);
-
- Message m1 = createMessageWithRefs(1, queue);
-
- msgs.add(m1);
-
- Message m2 = createMessageWithRefs(2, queue);
-
- msgs.add(m2);
-
- Message m3 = createMessageWithRefs(3, queue);
-
- msgs.add(m3);
-
- pm.commitTransaction(msgs, null);
-
- assertMessageInStore(m1, queue);
-
- assertMessageInStore(m2, queue);
-
- assertMessageInStore(m3, queue);
-
-
- //Add a couple more
-
- List<Message> msgsMore = new ArrayList<Message>();
-
- Message m4 = createMessageWithRefs(4, queue);
- msgsMore.add(m4);
-
- Message m5 = createMessageWithRefs(5, queue);
- msgsMore.add(m5);
-
- //Delete some refs
-
- List<MessageReference> refsToRemove = new ArrayList<MessageReference>();
-
- refsToRemove.add(m1.getReferences().get(0));
- refsToRemove.add(m1.getReferences().get(3));
-
- refsToRemove.add(m2.getReferences().get(0));
- refsToRemove.add(m2.getReferences().get(1));
- refsToRemove.add(m2.getReferences().get(2));
- refsToRemove.add(m2.getReferences().get(3));
-
- refsToRemove.add(m3.getReferences().get(2));
-
- Xid xid = generateXid();
-
- pm.prepareTransaction(xid, msgsMore, refsToRemove);
-
- pm.unprepareTransaction(xid, msgsMore, refsToRemove);
-
- assertNumMessagesInStore(3);
- }
-
- public void testUpdateDeliveryCount() throws Exception
- {
- Queue queue = new QueueImpl(67);
-
- Message m1 = createMessageWithRefs(1, queue);
-
- assertEquals(0, m1.getReferences().get(0).getDeliveryCount());
- assertEquals(0, m1.getReferences().get(1).getDeliveryCount());
- assertEquals(0, m1.getReferences().get(2).getDeliveryCount());
- assertEquals(0, m1.getReferences().get(3).getDeliveryCount());
-
- pm.addMessage(m1);
-
- final int delCount = 77;
- m1.getReferences().get(1).setDeliveryCount(delCount);
- pm.updateDeliveryCount(queue, m1.getReferences().get(1));
-
- final int delCount2 = 423;
-
- m1.getReferences().get(3).setDeliveryCount(delCount2);
- pm.updateDeliveryCount(queue, m1.getReferences().get(3));
-
- assertMessageInStore(m1, queue);
- }
-
- public void testRefsWithDifferentQueues() throws Exception
- {
- final int numQueues = 10;
-
- List<Message> msgs = new ArrayList<Message>();
-
- for (int i = 0; i < numQueues; i++)
- {
- Queue queue = new QueueImpl(i);
-
- MessageReference ref = generateReference(queue, i);
-
- msgs.add(ref.getMessage());
-
- pm.addMessage(ref.getMessage());
-
- assertEquals(queue, ref.getQueue());
- }
-
- for (Message msg: msgs)
- {
- assertMessageInStore(msg, msg.getReferences().get(0).getQueue());
- }
- }
-
- public void testLoadQueues() throws Exception
- {
- Map<Long, Queue> queues = new HashMap<Long, Queue>();
-
- final int numQueues = 10;
-
- final int numMessages = 10;
-
- for (int i = 0; i < numQueues; i++)
- {
- Queue queue = new QueueImpl(i);
-
- queues.put(queue.getID(), queue);
- }
-
- List<Message> msgs = new ArrayList<Message>();
-
- for (int i = 0; i < numMessages; i++)
- {
- Message msg = this.generateMessage(i);
-
- msgs.add(msg);
-
- for (long j = 0; j < numQueues; j++)
- {
- Queue queue = queues.get(j);
-
- msg.createReference(queue);
- }
-
- pm.addMessage(msg);
- }
-
-
- pm.loadQueues(queues);
-
- for (Queue queue: queues.values())
- {
- assertEquals(numMessages, queue.getMessageCount());
-
- List<MessageReference> refs = queue.list(null);
-
- int i = 0;
- for (MessageReference ref: refs)
- {
- this.assertEquivalent(msgs.get(i++), ref.getMessage());
- }
- }
- }
-
- public void testGetInDoubtXids() throws Exception
- {
- Queue queue = new QueueImpl(12);
-
- Message message1 = createMessageWithRefs(1, queue);
-
- List<Message> msgs = new ArrayList<Message>();
-
- msgs.add(message1);
-
- Xid xid1 = generateXid();
-
- pm.prepareTransaction(xid1, msgs, null);
-
- pm.setInRecoveryMode(true);
-
- List<Xid> xids = pm.getInDoubtXids();
-
- assertNotNull(xids);
-
- assertEquals(1, xids.size());
-
- assertEquals(xid1, xids.get(0));
-
-
-
- Message message2 = createMessageWithRefs(2, queue);
-
- msgs.clear();
-
- msgs.add(message2);
-
- Xid xid2 = generateXid();
-
- pm.prepareTransaction(xid2, msgs, null);
-
- xids = pm.getInDoubtXids();
-
- assertNotNull(xids);
-
- assertEquals(2, xids.size());
-
- assertTrue(xids.contains(xid1));
-
- assertTrue(xids.contains(xid2));
-
-
- pm.commitPreparedTransaction(xid1);
-
- pm.commitPreparedTransaction(xid2);
-
- xids = pm.getInDoubtXids();
-
- assertNotNull(xids);
-
- assertEquals(0, xids.size());
- }
-
- public void testGetInDoubtXidsWithRestart() throws Exception
- {
- Queue queue = new QueueImpl(12);
-
- Message message1 = createMessageWithRefs(1, queue);
-
- List<Message> msgs = new ArrayList<Message>();
-
- msgs.add(message1);
-
- Xid xid1 = generateXid();
-
- pm.prepareTransaction(xid1, msgs, null);
-
- pm.setInRecoveryMode(true);
-
- List<Xid> xids = pm.getInDoubtXids();
-
- assertNotNull(xids);
-
- assertEquals(1, xids.size());
-
- assertEquals(xid1, xids.get(0));
-
-
-
- Message message2 = createMessageWithRefs(2, queue);
-
- msgs.clear();
-
- msgs.add(message2);
-
- Xid xid2 = generateXid();
-
- pm.prepareTransaction(xid2, msgs, null);
-
- xids = pm.getInDoubtXids();
-
- assertNotNull(xids);
-
- assertEquals(2, xids.size());
-
- assertTrue(xids.contains(xid1));
-
- assertTrue(xids.contains(xid2));
-
- pm.stop();
-
- pm.start();
-
- pm.setInRecoveryMode(true);
-
- xids = pm.getInDoubtXids();
-
- assertNotNull(xids);
-
- assertEquals(2, xids.size());
-
- assertTrue(xids.contains(xid1));
-
- assertTrue(xids.contains(xid2));
-
-
- pm.commitPreparedTransaction(xid1);
-
- pm.commitPreparedTransaction(xid2);
-
- xids = pm.getInDoubtXids();
-
- assertNotNull(xids);
-
- assertEquals(0, xids.size());
- }
-
- public void testSetGetRecoveryMode() throws Exception
- {
- assertFalse(pm.isInRecoveryMode());
-
- try
- {
- pm.getInDoubtXids();
- fail("Should throw exception");
- }
- catch (IllegalStateException e)
- {
- //Ok
- }
-
- pm.setInRecoveryMode(true);
-
- assertTrue(pm.isInRecoveryMode());
-
- pm.getInDoubtXids();
-
- pm.setInRecoveryMode(false);
-
- assertFalse(pm.isInRecoveryMode());
- }
-
- // Private --------------------------------------------------------------------
-
- private Message extractMessage(Map<Long, Queue> queues, long id, byte[] msgBytes, byte[] refBytes) throws Exception
- {
- ByteBuffer buffer = ByteBuffer.wrap(msgBytes);
-
- int type = buffer.getInt();
-
- long expiration = buffer.getLong();
-
- long timestamp = buffer.getLong();
-
- byte priority = buffer.get();
-
- int headerSize = buffer.getInt();
-
- byte[] headers = new byte[headerSize];
-
- buffer.get(headers);
-
- int payloadSize = buffer.getInt();
-
- byte[] payload = null;
-
- if (payloadSize != 0)
- {
- payload = new byte[payloadSize];
-
- buffer.get(payload);
- }
-
- Message message = new MessageImpl(id, type, true, expiration, timestamp, priority,
- headers, payload);
-
- buffer = ByteBuffer.wrap(refBytes);
-
- while (buffer.hasRemaining())
- {
- long queueID = buffer.getLong();
-
- int deliveryCount = buffer.getInt();
-
- long scheduledDeliveryTime = buffer.getLong();
-
- MessageReference reference = message.createReference(queues.get(queueID));
-
- reference.setDeliveryCount(deliveryCount);
-
- reference.setScheduledDeliveryTime(scheduledDeliveryTime);
- }
-
- return message;
- }
-
- private void assertMessageInStore(Message m, Queue queue) throws Exception
- {
- BDBJEDatabase msgDB = bdb.getDatabase(BDBJEPersistenceManager.MESSAGE_DB_NAME);
-
- BDBJEDatabase refDB = bdb.getDatabase(BDBJEPersistenceManager.REFERENCE_DB_NAME);
-
- byte[] msgBytes = msgDB.get(m.getMessageID());
-
- assertNotNull(msgBytes);
-
- byte[] refBytes = refDB.get(m.getMessageID());
-
- assertNotNull(refBytes);
-
- Map<Long, Queue> queues = new HashMap<Long, Queue>();
-
- queues.put(queue.getID(), queue);
-
- Message m2 = extractMessage(queues, m.getMessageID(), msgBytes, refBytes);
-
- assertEquivalent(m, m2);
- }
-
- private void assertNumMessagesInStore(int num) throws Exception
- {
- BDBJEDatabase msgDB = bdb.getDatabase(BDBJEPersistenceManager.MESSAGE_DB_NAME);
-
- assertEquals(num, msgDB.size());
- }
-
- private void assertMessageNotInStore(Message m) throws Exception
- {
- BDBJEDatabase msgDB = bdb.getDatabase(BDBJEPersistenceManager.MESSAGE_DB_NAME);
-
- BDBJEDatabase refDB = bdb.getDatabase(BDBJEPersistenceManager.REFERENCE_DB_NAME);
-
-
- byte[] msgBytes = msgDB.get(m.getMessageID());
-
- assertNull(msgBytes);
-
- byte[] refBytes = refDB.get(m.getMessageID());
-
- assertNull(refBytes);
- }
-
- private void assertStoreEmpty() throws Exception
- {
- BDBJEDatabase msgDB = bdb.getDatabase(BDBJEPersistenceManager.MESSAGE_DB_NAME);
-
- BDBJEDatabase refDB = bdb.getDatabase(BDBJEPersistenceManager.REFERENCE_DB_NAME);
-
- assertEquals(0, msgDB.size());
-
- assertEquals(0, refDB.size());
- }
-
- private Message createMessageWithRefs(long id, Queue queue)
- {
- Message m = generateMessage(id);
-
- m.createReference(queue);
-
- m.createReference(queue);
-
- m.createReference(queue);
-
- m.createReference(queue);
-
- return m;
- }
-
-
- // Inner classes ---------------------------------------------------------------
-
-}
Deleted: branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/BDBSpeedTest.java
===================================================================
--- branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/BDBSpeedTest.java 2007-12-10 17:09:58 UTC (rev 3463)
+++ branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/BDBSpeedTest.java 2007-12-10 17:26:06 UTC (rev 3464)
@@ -1,656 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2005, JBoss Inc., and individual contributors as indicated
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.jboss.messaging.newcore.impl.bdbje.test;
-
-import java.io.File;
-import java.util.ArrayList;
-import java.util.List;
-
-import javax.transaction.xa.Xid;
-
-import org.jboss.jms.tx.MessagingXid;
-import org.jboss.messaging.newcore.impl.MessageImpl;
-import org.jboss.messaging.newcore.impl.QueueImpl;
-import org.jboss.messaging.newcore.impl.bdbje.BDBJEPersistenceManager;
-import org.jboss.messaging.newcore.intf.Message;
-import org.jboss.messaging.newcore.intf.MessageReference;
-import org.jboss.messaging.newcore.intf.Queue;
-import org.jboss.messaging.test.unit.UnitTestCase;
-
-/**
- *
- * A BDBSpeedTest
- *
- * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
- *
- */
-public class BDBSpeedTest extends UnitTestCase
-{
-
-// public void testCommitMessage() throws Exception
-// {
-// String envPath = "/home/tim/test-env";
-//
-// File file = new File(envPath);
-//
-// deleteDirectory(file);
-//
-// file.mkdir();
-//
-// final int numMessages = 5000;
-//
-// final int numRefs = 10;
-//
-// BDBJEPersistenceManager2 bdb = new BDBJEPersistenceManager2(envPath);
-//
-// bdb.start();
-//
-// Queue queue = new QueueImpl(1);
-//
-// //List<Message> messages = new ArrayList<Message>();
-//
-// long start = System.currentTimeMillis();
-//
-// byte[] payload = new byte[1 * 1024];
-//
-// for (int i = 0; i < numMessages; i++)
-// {
-// Message message = new MessageImpl(i, 0, true, 0, System.currentTimeMillis(), (byte)4);
-//
-// for (int j = 0; j < numRefs; j++)
-// {
-// message.createReference(queue);
-//
-// message.setPayload(payload);
-// }
-//
-// //messages.add(message);
-//
-// bdb.commitMessage(message);
-// }
-//
-// long end = System.currentTimeMillis();
-//
-// double rate = 1000 * (double)numMessages / (end - start);
-//
-// System.out.println("Insert Rate: " + rate);
-//
-// }
-//
-// public void testCommitMessages() throws Exception
-// {
-// String envPath = "/home/tim/test-env";
-//
-// File file = new File(envPath);
-//
-// deleteDirectory(file);
-//
-// file.mkdir();
-//
-// final int numMessages = 5000;
-//
-// final int numRefs = 10;
-//
-// BDBJEPersistenceManager2 bdb = new BDBJEPersistenceManager2(envPath);
-//
-// bdb.start();
-//
-// Queue queue = new QueueImpl(1);
-//
-// List<Message> messages = new ArrayList<Message>();
-//
-// long start = System.currentTimeMillis();
-//
-// byte[] payload = new byte[1 * 1024];
-//
-// for (int i = 0; i < numMessages; i++)
-// {
-// Message message = new MessageImpl(i, 0, true, 0, System.currentTimeMillis(), (byte)4);
-//
-// for (int j = 0; j < numRefs; j++)
-// {
-// message.createReference(queue);
-//
-// message.setPayload(payload);
-// }
-//
-// messages.add(message);
-//
-// if (i % 100 == 0)
-// {
-// bdb.commitMessages(messages);
-//
-// messages.clear();
-// }
-// }
-// bdb.commitMessages(messages);
-//
-// long end = System.currentTimeMillis();
-//
-// double rate = 1000 * (double)numMessages / (end - start);
-//
-// System.out.println("Insert Rate: " + rate);
-//
-// }
-//
-// public void testDeleteReferences() throws Exception
-// {
-// String envPath = "/home/tim/test-env";
-//
-// File file = new File(envPath);
-//
-// deleteDirectory(file);
-//
-// file.mkdir();
-//
-// final int numMessages = 5000;
-//
-// final int numRefs = 10;
-//
-// BDBJEPersistenceManager2 bdb = new BDBJEPersistenceManager2(envPath);
-//
-// bdb.start();
-//
-// Queue queue = new QueueImpl(1);
-//
-// List<Message> messages = new ArrayList<Message>();
-//
-// byte[] payload = new byte[1 * 1024];
-//
-// List<MessageReference> refs = new ArrayList<MessageReference>();
-//
-// for (int i = 0; i < numMessages; i++)
-// {
-// Message message = new MessageImpl(i, 0, true, 0, System.currentTimeMillis(), (byte)4);
-//
-// for (int j = 0; j < numRefs; j++)
-// {
-// MessageReference ref = message.createReference(queue);
-//
-// message.setPayload(payload);
-//
-// refs.add(ref);
-// }
-//
-// messages.add(message);
-//
-// if (i % 100 == 0)
-// {
-// bdb.commitMessages(messages);
-//
-// messages.clear();
-// }
-// }
-// bdb.commitMessages(messages);
-//
-// long start = System.currentTimeMillis();
-//
-// //Now delete them
-//
-// bdb.deleteReferences(refs);
-//
-// long end = System.currentTimeMillis();
-//
-// double rate = 1000 * (double)numMessages / (end - start);
-//
-// System.out.println("Delete Rate: " + rate);
-//
-// }
-//
-// public void testDeleteReference() throws Exception
-// {
-// String envPath = "/home/tim/test-env";
-//
-// File file = new File(envPath);
-//
-// deleteDirectory(file);
-//
-// file.mkdir();
-//
-// final int numMessages = 5000;
-//
-// final int numRefs = 1;
-//
-// BDBJEPersistenceManager bdb = new BDBJEPersistenceManager(envPath);
-//
-// bdb.start();
-//
-// Queue queue = new QueueImpl(1);
-//
-// List<Message> messages = new ArrayList<Message>();
-//
-// byte[] payload = new byte[1 * 1024];
-//
-// List<MessageReference> refs = new ArrayList<MessageReference>();
-//
-// for (int i = 0; i < numMessages; i++)
-// {
-// Message message = new MessageImpl(i, 0, true, 0, System.currentTimeMillis(), (byte)4);
-//
-// for (int j = 0; j < numRefs; j++)
-// {
-// MessageReference ref = message.createReference(queue);
-//
-// message.setPayload(payload);
-//
-// refs.add(ref);
-// }
-//
-// messages.add(message);
-//
-// if (i % 100 == 0)
-// {
-// bdb.commitMessages(messages);
-//
-// messages.clear();
-// }
-// }
-// bdb.commitMessages(messages);
-//
-// System.out.println("Added them");
-//
-// long start = System.currentTimeMillis();
-//
-// //Now delete them
-//
-// for (MessageReference ref: refs)
-// {
-// bdb.deleteReference(ref);
-// }
-//
-// long end = System.currentTimeMillis();
-//
-// double rate = 1000 * (double)numMessages / (end - start);
-//
-// System.out.println("Delete Rate: " + rate);
-//
-// }
-//
-//
-// public void testPrepareMessages() throws Exception
-// {
-// String envPath = "/home/tim/test-env";
-//
-// File file = new File(envPath);
-//
-// deleteDirectory(file);
-//
-// file.mkdir();
-//
-// final int numMessages = 5000;
-//
-// final int numRefs = 10;
-//
-// BDBJEPersistenceManager2 bdb = new BDBJEPersistenceManager2(envPath);
-//
-// bdb.start();
-//
-// Queue queue = new QueueImpl(1);
-//
-// List<Message> messages = new ArrayList<Message>();
-//
-// long start = System.currentTimeMillis();
-//
-// byte[] payload = new byte[1 * 1024];
-//
-// for (int i = 0; i < numMessages; i++)
-// {
-// Message message = new MessageImpl(i, 0, true, 0, System.currentTimeMillis(), (byte)4);
-//
-// for (int j = 0; j < numRefs; j++)
-// {
-// message.createReference(queue);
-//
-// message.setPayload(payload);
-// }
-//
-// messages.add(message);
-//
-// if (i % 100 == 0)
-// {
-// Xid xid = this.generateXid();
-//
-// bdb.prepareMessages(xid, messages);
-//
-// bdb.commitPreparedMessages(xid);
-//
-// messages.clear();
-// }
-// }
-//
-// Xid xid = this.generateXid();
-//
-// bdb.prepareMessages(xid, messages);
-//
-// bdb.commitPreparedMessages(xid);
-//
-// long end = System.currentTimeMillis();
-//
-// double rate = 1000 * (double)numMessages / (end - start);
-//
-// System.out.println("Insert Rate: " + rate);
-//
-// }
-
-// /*
-// * Test with message and ref in separate databases
-// */
-// public void testMsgandRefsDifferentDBSNoXA() throws Exception
-// {
-// File file = new File("/home/tim/test-env");
-//
-// deleteDirectory(file);
-//
-// file.mkdir();
-//
-// EnvironmentConfig envConfig = new EnvironmentConfig();
-//
-// envConfig.setAllowCreate(true);
-//
-// envConfig.setTransactional(true);
-//
-//// envConfig.setTxnNoSync(true);
-////
-//// envConfig.setTxnWriteNoSync(true);
-//
-// Environment env1 = new Environment(file, envConfig);
-//
-// DatabaseConfig dbConfig1 = new DatabaseConfig();
-//
-// dbConfig1.setTransactional(true);
-//
-// dbConfig1.setAllowCreate(true);
-//
-// Database msgDB = env1.openDatabase(null, "message", dbConfig1);
-//
-// DatabaseConfig dbConfig2 = new DatabaseConfig();
-//
-// dbConfig2.setTransactional(true);
-//
-// dbConfig2.setAllowCreate(true);
-//
-// Database refDB = env1.openDatabase(null, "ref", dbConfig2);
-//
-//
-// DatabaseConfig dbConfig3 = new DatabaseConfig();
-//
-// dbConfig3.setTransactional(true);
-//
-// dbConfig3.setAllowCreate(true);
-//
-// Database txDB = env1.openDatabase(null, "tx", dbConfig3);
-//
-//
-//
-// final int numMessages = 5000;
-//
-// byte[] msgBytes = new byte[256];
-//
-// final int numRefs = 10;
-//
-// byte[] refBytes = new byte[numRefs * 20];
-//
-// byte[] txBytes = new byte[numRefs * 50];
-//
-//
-// long start = System.currentTimeMillis();
-//
-// for (int i = 0; i < numMessages; i++)
-// {
-// byte[] keyBytes = new byte[8];
-//
-// ByteBuffer buffer = ByteBuffer.wrap(keyBytes);
-//
-// buffer.putLong(i);
-//
-// DatabaseEntry key = new DatabaseEntry(keyBytes);
-//
-//
-// //Prepare refs
-//
-// DatabaseEntry refVal = new DatabaseEntry(refBytes);
-//
-// Transaction tx = env1.beginTransaction(null, null);
-//
-// refDB.put(tx, key, refVal);
-//
-// // Message
-//
-// DatabaseEntry msgVal = new DatabaseEntry(msgBytes);
-//
-// msgDB.put(tx, key, msgVal);
-//
-// //Write a tx record
-//
-// DatabaseEntry txVal = new DatabaseEntry(txBytes);
-//
-// txDB.put(tx, key, txVal);
-//
-// //Commit the prepare
-//
-// tx.commit();
-//
-// tx = env1.beginTransaction(null, null);
-//
-// //Now commit the refs
-//
-// refVal = new DatabaseEntry(refBytes);
-//
-// refDB.put(tx, key, refVal);
-//
-// //And delete the tx record
-//
-// txDB.delete(tx, key);
-//
-// tx.commit();
-// }
-//
-// long end = System.currentTimeMillis();
-//
-// double rate = 1000 * (double)numMessages / (end - start);
-//
-// System.out.println("Insert Rate: " + rate);
-//
-//
-// }
-//
-// public void testMsgandRefsDifferentDBSXA() throws Exception
-// {
-// File file = new File("/home/tim/test-env");
-//
-// deleteDirectory(file);
-//
-// file.mkdir();
-//
-// EnvironmentConfig envConfig = new EnvironmentConfig();
-//
-// envConfig.setAllowCreate(true);
-//
-// envConfig.setTransactional(true);
-//
-// XAEnvironment env1 = new XAEnvironment(file, envConfig);
-//
-// DatabaseConfig dbConfig1 = new DatabaseConfig();
-//
-// dbConfig1.setTransactional(true);
-//
-// dbConfig1.setAllowCreate(true);
-//
-// Database msgDB = env1.openDatabase(null, "message", dbConfig1);
-//
-// DatabaseConfig dbConfig2 = new DatabaseConfig();
-//
-// dbConfig2.setTransactional(true);
-//
-// dbConfig2.setAllowCreate(true);
-//
-// Database refDB = env1.openDatabase(null, "ref", dbConfig2);
-//
-//
-//
-// final int numMessages = 5000;
-//
-// byte[] msgBytes = new byte[256];
-//
-// final int numRefs = 10;
-//
-// byte[] refBytes = new byte[numRefs * 20];
-//
-// long start = System.currentTimeMillis();
-//
-// for (int i = 0; i < numMessages; i++)
-// {
-// Xid xid = generateXid();
-//
-// env1.start(xid, XAResource.TMNOFLAGS);
-//
-// byte[] keyBytes = new byte[8];
-//
-// ByteBuffer buffer = ByteBuffer.wrap(keyBytes);
-//
-// buffer.putLong(i);
-//
-// DatabaseEntry key = new DatabaseEntry(keyBytes);
-//
-// DatabaseEntry msgVal = new DatabaseEntry(msgBytes);
-//
-// Transaction tx = env1.beginTransaction(null, null);
-//
-// msgDB.put(null, key, msgVal);
-//
-// //Now the refs
-//
-// DatabaseEntry refVal = new DatabaseEntry(refBytes);
-//
-// refDB.put(null, key, refVal);
-//
-// env1.end(xid, XAResource.TMSUCCESS);
-//
-// env1.prepare(xid);
-//
-// env1.commit(xid, false);
-//
-// // tx.commit();
-// }
-//
-// long end = System.currentTimeMillis();
-//
-// double rate = 1000 * (double)numMessages / (end - start);
-//
-// System.out.println("Insert Rate: " + rate);
-//
-// start = System.currentTimeMillis();
-// }
-//
-// private Xid generateXid()
-// {
-// String id = java.util.UUID.randomUUID().toString();
-//
-// Xid xid = new MessagingXid("blah".getBytes(), 123, id.getBytes());
-//
-// return xid;
-// }
-//
-//
-//// This is very slow
-//// public void testMsgandRefsSameDBSNoXA() throws Exception
-//// {
-//// File file = new File("/home/tim/test-env");
-////
-//// deleteDirectory(file);
-////
-//// file.mkdir();
-////
-//// EnvironmentConfig envConfig = new EnvironmentConfig();
-////
-//// envConfig.setAllowCreate(true);
-////
-//// envConfig.setTransactional(true);
-////
-//// Environment env1 = new Environment(file, envConfig);
-////
-//// DatabaseConfig dbConfig1 = new DatabaseConfig();
-////
-//// dbConfig1.setTransactional(true);
-////
-//// dbConfig1.setAllowCreate(true);
-////
-//// Database msgDB = env1.openDatabase(null, "message", dbConfig1);
-////
-//// final int numMessages = 5000;
-////
-//// byte[] msgBytes = new byte[10 * 1024];
-////
-//// byte[] refBytes = new byte[24];
-////
-//// long start = System.currentTimeMillis();
-////
-//// for (int i = 0; i < numMessages; i++)
-//// {
-//// byte[] keyBytes = new byte[8];
-////
-//// ByteBuffer buffer = ByteBuffer.wrap(keyBytes);
-////
-//// buffer.putLong(i);
-////
-//// DatabaseEntry key = new DatabaseEntry(keyBytes);
-////
-//// DatabaseEntry msgVal = new DatabaseEntry(msgBytes);
-////
-//// Transaction tx = env1.beginTransaction(null, null);
-////
-//// msgDB.put(tx, key, msgVal);
-////
-//// //Now the refs
-////
-//// final int numRefs = 50;
-////
-//// for (int j = 0; j < numRefs; j++)
-//// {
-//// keyBytes = new byte[8];
-////
-//// buffer = ByteBuffer.wrap(keyBytes);
-////
-//// buffer.putLong(j);
-////
-//// key = new DatabaseEntry(keyBytes);
-////
-//// DatabaseEntry value = new DatabaseEntry();
-////
-//// value.setPartial(0, refBytes.length, true);
-////
-//// value.setData(refBytes);
-////
-//// msgDB.put(tx, key, value);
-//// }
-////
-//// tx.commit();
-////
-//// }
-////
-//// long end = System.currentTimeMillis();
-////
-//// double rate = 1000 * (double)numMessages / (end - start);
-////
-//// System.out.println("Rate: " + rate);
-//// }
-}
Deleted: branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/FakeBDBJEEnvironmentTest.java
===================================================================
--- branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/FakeBDBJEEnvironmentTest.java 2007-12-10 17:09:58 UTC (rev 3463)
+++ branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/FakeBDBJEEnvironmentTest.java 2007-12-10 17:26:06 UTC (rev 3464)
@@ -1,25 +0,0 @@
-package org.jboss.messaging.newcore.impl.bdbje.test;
-
-import org.jboss.messaging.newcore.impl.bdbje.BDBJEEnvironment;
-import org.jboss.messaging.newcore.impl.bdbje.test.fakes.FakeBDBJEEnvironment;
-
-/**
- *
- * A FakeBDBJEEnvironmentTest
- *
- * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
- *
- */
-public class FakeBDBJEEnvironmentTest extends BDBJEEnvironmentTestBase
-{
- protected BDBJEEnvironment createEnvironment() throws Exception
- {
- BDBJEEnvironment env = new FakeBDBJEEnvironment();
-
- return env;
- }
-
- protected void createDir(String path)
- {
- }
-}
Deleted: branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/fakes/FakeBDBJEEnvironment.java
===================================================================
--- branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/fakes/FakeBDBJEEnvironment.java 2007-12-10 17:09:58 UTC (rev 3463)
+++ branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/fakes/FakeBDBJEEnvironment.java 2007-12-10 17:26:06 UTC (rev 3464)
@@ -1,501 +0,0 @@
-package org.jboss.messaging.newcore.impl.bdbje.test.fakes;
-
-import java.nio.ByteBuffer;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.atomic.AtomicInteger;
-
-import javax.transaction.xa.Xid;
-
-import org.jboss.messaging.newcore.impl.bdbje.BDBJECursor;
-import org.jboss.messaging.newcore.impl.bdbje.BDBJEDatabase;
-import org.jboss.messaging.newcore.impl.bdbje.BDBJEEnvironment;
-import org.jboss.messaging.newcore.impl.bdbje.BDBJETransaction;
-import org.jboss.messaging.util.Pair;
-
-/**
- *
- * A FakeBDBJEEnvironment
- *
- * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
- *
- */
-public class FakeBDBJEEnvironment implements BDBJEEnvironment
-{
- private String environmentPath;
-
- private Map<String, FakeBDBJETransaction> transactions = new HashMap<String, FakeBDBJETransaction>();
-
- private boolean transacted;
-
- private boolean syncOS;
-
- private boolean syncVM;
-
- private long memoryCacheSize = -1;
-
- private boolean started;
-
- private Map<Thread, FakeBDBJETransaction> implicitTXs = new ConcurrentHashMap<Thread, FakeBDBJETransaction>();
-
- private Map<String, BDBJEDatabase> databases = new HashMap<String, BDBJEDatabase>();
-
- public boolean isSyncOS()
- {
- return syncOS;
- }
-
- public void setSyncOS(boolean sync)
- {
- if (started)
- {
- throw new IllegalStateException("Cannot set SyncOS when started");
- }
- syncOS = sync;
- }
-
- public boolean isSyncVM()
- {
- return syncVM;
- }
-
- public void setSyncVM(boolean sync)
- {
- if (started)
- {
- throw new IllegalStateException("Cannot set SyncVM when started");
- }
- syncVM = sync;
- }
-
- public boolean isTransacted()
- {
- return transacted;
- }
-
- public void setTransacted(boolean transacted)
- {
- if (started)
- {
- throw new IllegalStateException("Cannot set transacted when started");
- }
- this.transacted = transacted;
- }
-
- public long getMemoryCacheSize()
- {
- return memoryCacheSize;
- }
-
- public void setMemoryCacheSize(long size)
- {
- if (started)
- {
- throw new IllegalStateException("Cannot set memory cache size when started");
- }
- this.memoryCacheSize = size;
- }
-
- public void setEnvironmentPath(String environmentPath)
- {
- if (started)
- {
- throw new IllegalStateException("Cannot set environmentPath when started");
- }
- this.environmentPath = environmentPath;
- }
-
- public String getEnvironmentPath()
- {
- return environmentPath;
- }
-
- public void start() throws Exception
- {
- if (environmentPath == null)
- {
- throw new IllegalStateException("Must set environmentPath before starting");
- }
- if (started)
- {
- throw new IllegalStateException("Already started");
- }
- started = true;
- }
-
- public void stop() throws Exception
- {
- if (!started)
- {
- throw new IllegalStateException("Not started");
- }
- started = false;
- }
-
- public BDBJETransaction createTransaction() throws Exception
- {
- return createTransactionInternal(null);
- }
-
- public synchronized BDBJEDatabase getDatabase(String databaseName) throws Exception
- {
- BDBJEDatabase database = databases.get(databaseName);
-
- if (database == null)
- {
- database = new FakeBDBJEDatabase();
-
- databases.put(databaseName, database);
- }
-
- return database;
- }
-
- public List<Xid> getInDoubtXids() throws Exception
- {
- List<Xid> xids = new ArrayList<Xid>();
-
- for (FakeBDBJETransaction tx: transactions.values())
- {
- if (tx.isPrepared())
- {
- xids.add(tx.getXid());
- }
- }
-
- return xids;
- }
-
- public void startWork(Xid xid) throws Exception
- {
- if (implicitTXs.get(Thread.currentThread()) != null)
- {
- throw new IllegalStateException("Already implicit transaction");
- }
-
- implicitTXs.put(Thread.currentThread(), createTransactionInternal(xid));
- }
-
- public void endWork(Xid xid, boolean fail) throws Exception
- {
- FakeBDBJETransaction removed = implicitTXs.remove(Thread.currentThread());
-
- if (removed == null)
- {
- throw new IllegalStateException("No implicit tx");
- }
- }
-
- private FakeBDBJETransaction findTXForXid(Xid xid)
- {
- for (FakeBDBJETransaction tx: transactions.values())
- {
- if (xid.equals(tx.xid))
- {
- return tx;
- }
- }
- return null;
- }
-
- public void prepare(Xid xid) throws Exception
- {
- FakeBDBJETransaction tx = findTXForXid(xid);
-
- if (tx == null)
- {
- throw new IllegalStateException("Cannot find tx for xid " + xid);
- }
-
- if (implicitTXs.containsKey(Thread.currentThread()))
- {
- throw new IllegalStateException("Work not ended");
- }
-
- tx.prepare();
- }
-
- public void commit(Xid xid) throws Exception
- {
- FakeBDBJETransaction tx = findTXForXid(xid);
-
- if (tx == null)
- {
- throw new IllegalStateException("Cannot find tx for xid " + xid);
- }
-
- if (implicitTXs.containsKey(Thread.currentThread()))
- {
- throw new IllegalStateException("Work not ended");
- }
-
- tx.commit();
- }
-
- public void rollback(Xid xid) throws Exception
- {
- FakeBDBJETransaction tx = findTXForXid(xid);
-
- if (tx == null)
- {
- throw new IllegalStateException("Cannot find tx for xid " + xid);
- }
-
- if (implicitTXs.containsKey(Thread.currentThread()))
- {
- throw new IllegalStateException("Work not ended");
- }
-
- tx.rollback();
- }
-
- // Private -----------------------------------------------------------------------
-
- private FakeBDBJETransaction createTransactionInternal(Xid xid) throws Exception
- {
- FakeBDBJETransaction tx = new FakeBDBJETransaction(xid);
-
- transactions.put(tx.id, tx);
-
- return tx;
- }
-
- // Inner classes ------------------------------------------------------------------
-
- private class FakeBDBJETransaction implements BDBJETransaction
- {
- private String id = java.util.UUID.randomUUID().toString();
-
- private Xid xid;
-
- private List<Action> actions = new ArrayList<Action>();
-
- private boolean prepared;
-
- FakeBDBJETransaction(Xid xid)
- {
- this.xid = xid;
- }
-
- public void commit() throws Exception
- {
- for (Action action : actions)
- {
- if (action.put)
- {
- action.database.put(null, action.id, action.bytes, action.offset, action.length);
- }
- else
- {
- action.database.remove(null, action.id);
- }
- }
-
- actions.clear();
-
- transactions.remove(id);
- }
-
- public void prepare() throws Exception
- {
- prepared = true;
- }
-
- public void rollback() throws Exception
- {
- actions.clear();
-
- transactions.remove(id);
- }
-
- public boolean isPrepared()
- {
- return prepared;
- }
-
- public Xid getXid()
- {
- return xid;
- }
-
- }
-
- private class Action
- {
- public BDBJEDatabase database;
-
- public boolean put;
-
- public long id;
-
- public byte[] bytes;
-
- public int offset;
-
- public int length;
-
- public Action(BDBJEDatabase database, boolean put, long id, byte[] bytes, int offset, int length)
- {
- this.database = database;
-
- this.put = put;
-
- this.id = id;
-
- this.bytes = bytes;
-
- this.offset = offset;
-
- this.length = length;
- }
- }
-
- private class FakeBDBJEDatabase implements BDBJEDatabase
- {
- private Map<Long, byte[]> store = new LinkedHashMap<Long, byte[]>();
-
- private AtomicInteger cursorCount = new AtomicInteger(0);
-
- public void close() throws Exception
- {
- if (cursorCount.get() != 0)
- {
- throw new IllegalStateException("Cannot close. There are cursors open");
- }
- }
-
- public BDBJECursor cursor() throws Exception
- {
- return new FakeBDBJECursor();
- }
-
- private BDBJETransaction getTx(BDBJETransaction tx)
- {
- if (tx == null)
- {
- tx = implicitTXs.get(Thread.currentThread());
- }
- return tx;
- }
-
- public void put(BDBJETransaction tx, long id, byte[] bytes, int offset,
- int length) throws Exception
- {
- tx = getTx(tx);
-
- if (tx == null)
- {
- if (offset == 0 && bytes.length == length)
- {
- store.put(id, bytes);
- }
- else
- {
- byte[] currentBytes = store.get(id);
-
- if (offset == currentBytes.length)
- {
- byte[] newBytes = new byte[currentBytes.length + bytes.length];
-
- ByteBuffer buffer = ByteBuffer.wrap(newBytes);
-
- buffer.put(currentBytes);
-
- buffer.put(bytes);
-
- store.put(id, newBytes);
- }
- else if (offset < currentBytes.length)
- {
- if (offset + length > currentBytes.length)
- {
- throw new IllegalStateException("Invalid offset/length");
- }
-
- byte[] newBytes = new byte[currentBytes.length - (length - bytes.length)];
-
- System.arraycopy(currentBytes, 0, newBytes, 0, offset);
-
- System.arraycopy(bytes, 0, newBytes, offset, bytes.length);
-
- System.arraycopy(currentBytes, offset + length, newBytes,
- offset + bytes.length, currentBytes.length - (offset + length));
-
- store.put(id, newBytes);
- }
- else
- {
- throw new IllegalStateException("Invalid offset " + offset);
- }
- }
- }
- else
- {
- FakeBDBJETransaction ftx = (FakeBDBJETransaction)tx;
-
- ftx.actions.add(new Action(this, true, id, bytes, offset, length));
- }
-
- }
-
- public void remove(BDBJETransaction tx, long id) throws Exception
- {
- tx = getTx(tx);
-
- if (tx == null)
- {
- store.remove(id);
- }
- else
- {
- FakeBDBJETransaction ftx = (FakeBDBJETransaction)tx;
-
- ftx.actions.add(new Action(this, false, id, null, -1, -1));
- }
- }
-
- public byte[] get(long id) throws Exception
- {
- return store.get(id);
- }
-
- public long size() throws Exception
- {
- return store.size();
- }
-
- private class FakeBDBJECursor implements BDBJECursor
- {
- private Iterator<Map.Entry<Long,byte[]>> iterator = store.entrySet().iterator();
-
- FakeBDBJECursor()
- {
- cursorCount.incrementAndGet();
- }
-
- public void close() throws Exception
- {
- cursorCount.decrementAndGet();
- }
-
- public Pair<Long, byte[]> getNext() throws Exception
- {
- if (iterator.hasNext())
- {
- Map.Entry<Long,byte[]> entry = iterator.next();
-
- return new Pair<Long, byte[]>(entry.getKey(), entry.getValue());
- }
- else
- {
- return null;
- }
- }
- }
- }
-}
Copied: branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/unit/BDBJEEnvironmentTestBase.java (from rev 3462, branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/BDBJEEnvironmentTestBase.java)
===================================================================
--- branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/unit/BDBJEEnvironmentTestBase.java (rev 0)
+++ branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/unit/BDBJEEnvironmentTestBase.java 2007-12-10 17:26:06 UTC (rev 3464)
@@ -0,0 +1,1726 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.messaging.newcore.impl.bdbje.test.unit;
+
+import java.util.List;
+
+import javax.transaction.xa.Xid;
+
+import org.jboss.messaging.newcore.impl.bdbje.BDBJEDatabase;
+import org.jboss.messaging.newcore.impl.bdbje.BDBJEEnvironment;
+import org.jboss.messaging.newcore.impl.bdbje.BDBJETransaction;
+import org.jboss.messaging.test.unit.UnitTestCase;
+
+/**
+ *
+ * Base for tests for BDBJEEnvironment and BDBJEDatabase
+ *
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ *
+ */
+public abstract class BDBJEEnvironmentTestBase extends UnitTestCase
+{
+ protected BDBJEEnvironment env;
+
+ protected BDBJEDatabase database;
+
+ protected static final String ENV_DIR = "test-bdb-environment";
+
+ protected static final String DB_NAME = "test-db";
+
+ @Override
+ protected void setUp() throws Exception
+ {
+ env = createEnvironment();
+
+ env.setEnvironmentPath(ENV_DIR);
+
+ env.start();
+
+ database = env.getDatabase(DB_NAME);
+ }
+
+ protected abstract void createDir(String path);
+
+ protected void tearDown() throws Exception
+ {
+ database.close();
+
+ env.stop();
+ }
+
+ protected abstract BDBJEEnvironment createEnvironment() throws Exception;
+
+ // The tests ----------------------------------------------------------------
+
+ public void testGetInDoubtXidsCompleteWithCommit() throws Exception
+ {
+ List<Xid> xids = env.getInDoubtXids();
+
+ assertTrue(xids.isEmpty());
+
+ Xid xid1 = generateXid();
+
+ env.startWork(xid1);
+
+ database.put(null, 1, new byte[10], 0, 10);
+
+ env.endWork(xid1, false);
+
+ env.prepare(xid1);
+
+ xids = env.getInDoubtXids();
+
+ assertEquals(xid1, xids.get(0));
+
+ env.commit(xid1);
+
+ xids = env.getInDoubtXids();
+
+ assertTrue(xids.isEmpty());
+ }
+
+ public void testGetInDoubtXidsCompleteWithRollback() throws Exception
+ {
+ List<Xid> xids = env.getInDoubtXids();
+
+ assertTrue(xids.isEmpty());
+
+ Xid xid1 = generateXid();
+
+ env.startWork(xid1);
+
+ database.put(null, 1, new byte[10], 0, 10);
+
+ env.endWork(xid1, false);
+
+ env.prepare(xid1);
+
+ xids = env.getInDoubtXids();
+
+ assertEquals(xid1, xids.get(0));
+
+ env.rollback(xid1);
+
+ xids = env.getInDoubtXids();
+
+ assertTrue(xids.isEmpty());
+ }
+
+
+ public void testGetInDoubtXidsMultiple() throws Exception
+ {
+ List<Xid> xids = env.getInDoubtXids();
+
+ assertTrue(xids.isEmpty());
+
+ Xid xid1 = generateXid();
+ env.startWork(xid1);
+ database.put(null, 1, new byte[10], 0, 10);
+ env.endWork(xid1, false);
+
+ env.prepare(xid1);
+ xids = env.getInDoubtXids();
+ assertEquals(xid1, xids.get(0));
+
+
+ Xid xid2 = generateXid();
+ env.startWork(xid2);
+ database.put(null, 1, new byte[10], 0, 10);
+ env.endWork(xid2, false);
+
+ env.prepare(xid2);
+ xids = env.getInDoubtXids();
+ assertTrue(xids.contains(xid1));
+ assertTrue(xids.contains(xid2));
+
+ Xid xid3 = generateXid();
+ env.startWork(xid3);
+ database.put(null, 1, new byte[10], 0, 10);
+ env.endWork(xid3, false);
+
+ env.prepare(xid3);
+ xids = env.getInDoubtXids();
+ assertTrue(xids.contains(xid1));
+ assertTrue(xids.contains(xid2));
+ assertTrue(xids.contains(xid3));
+
+ }
+
+ public void testGetInDoubtXidsMultipleWithRestart() throws Exception
+ {
+ List<Xid> xids = env.getInDoubtXids();
+
+ assertTrue(xids.isEmpty());
+
+ Xid xid1 = generateXid();
+ env.startWork(xid1);
+ database.put(null, 1, new byte[10], 0, 10);
+ env.endWork(xid1, false);
+
+ env.prepare(xid1);
+ xids = env.getInDoubtXids();
+ assertEquals(xid1, xids.get(0));
+
+
+ Xid xid2 = generateXid();
+ env.startWork(xid2);
+ database.put(null, 1, new byte[10], 0, 10);
+ env.endWork(xid2, false);
+
+ env.prepare(xid2);
+ xids = env.getInDoubtXids();
+ assertTrue(xids.contains(xid1));
+ assertTrue(xids.contains(xid2));
+
+ Xid xid3 = generateXid();
+ env.startWork(xid3);
+ database.put(null, 1, new byte[10], 0, 10);
+ env.endWork(xid3, false);
+
+ env.prepare(xid3);
+ xids = env.getInDoubtXids();
+ assertTrue(xids.contains(xid1));
+ assertTrue(xids.contains(xid2));
+ assertTrue(xids.contains(xid3));
+
+ database.close();
+
+ env.stop();
+
+ env.start();
+
+ database = env.getDatabase(DB_NAME);
+
+ xids = env.getInDoubtXids();
+ assertTrue(xids.contains(xid1));
+ assertTrue(xids.contains(xid2));
+ assertTrue(xids.contains(xid3));
+
+ env.commit(xid1);
+ env.commit(xid2);
+ env.commit(xid3);
+
+ xids = env.getInDoubtXids();
+
+ assertTrue(xids.isEmpty());
+ }
+
+ public void testPutAndRemoveNonTransactional() throws Exception
+ {
+ byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ database.put(null, id, bytes, offset, bytes.length);
+
+ assertContainsPair(id, bytes, 1);
+
+ database.remove(null, 1);
+
+ assertStoreEmpty();
+ }
+
+ public void testPutAndRemoveNonTransactionalWithRestart() throws Exception
+ {
+ byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ database.put(null, id, bytes, offset, bytes.length);
+
+ assertContainsPair(id, bytes, 1);
+
+ database.close();
+
+ env.stop();
+
+ env.start();
+
+ database = env.getDatabase(DB_NAME);
+
+ assertContainsPair(id, bytes, 1);
+ }
+
+ public void testPutAndRemoveMultipleNonTransactional() throws Exception
+ {
+ byte[] bytes1 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ byte[] bytes2 = new byte[] { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
+
+ byte[] bytes3 = new byte[] { 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 };
+
+ long id1 = 1;
+
+ long id2 = 2;
+
+ long id3 = 3;
+
+ int offset = 0;
+
+ database.put(null, id1, bytes1, offset, bytes1.length);
+
+ database.put(null, id2, bytes2, offset, bytes2.length);
+
+ database.put(null, id3, bytes3, offset, bytes3.length);
+
+ assertContainsPair(id1, bytes1, 3);
+
+ assertContainsPair(id2, bytes2, 3);
+
+ assertContainsPair(id3, bytes3, 3);
+
+ database.remove(null, id2);
+
+ assertContainsPair(id1, bytes1, 2);
+
+ assertContainsPair(id3, bytes3, 2);
+
+ database.remove(null, id3);
+
+ assertContainsPair(id1, bytes1, 1);
+
+ database.remove(null, id1);
+
+ assertStoreEmpty();
+ }
+
+ public void testPutTransactionalCommit() throws Exception
+ {
+ byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ BDBJETransaction tx = env.createTransaction();
+
+ database.put(tx, id, bytes, offset, bytes.length);
+
+ tx.commit();
+
+ assertContainsPair(id, bytes, 1);
+
+ database.remove(null, 1);
+
+ assertStoreEmpty();
+ }
+
+ public void testPutTransactionalWithRestart() throws Exception
+ {
+ byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ BDBJETransaction tx = env.createTransaction();
+
+ database.put(tx, id, bytes, offset, bytes.length);
+
+ //Now restart before committing
+
+ database.close();
+
+ env.stop();
+
+ env.start();
+
+ database = env.getDatabase(DB_NAME);
+
+ assertStoreEmpty();
+ }
+
+ public void testPutXACommit() throws Exception
+ {
+ byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ Xid xid = generateXid();
+
+ env.startWork(xid);
+
+ database.put(null, id, bytes, offset, bytes.length);
+
+ env.endWork(xid, false);
+
+ env.prepare(xid);
+
+ env.commit(xid);
+
+ assertContainsPair(id, bytes, 1);
+
+ database.remove(null, 1);
+
+ assertStoreEmpty();
+ }
+
+ public void testPutXAWithRestart() throws Exception
+ {
+ byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ Xid xid = generateXid();
+
+ env.startWork(xid);
+
+ database.put(null, id, bytes, offset, bytes.length);
+
+ env.endWork(xid, false);
+
+ // Now restart
+
+ database.close();
+
+ env.stop();
+
+ env.start();
+
+ database = env.getDatabase(DB_NAME);
+
+ assertStoreEmpty();
+ }
+
+
+ public void testPutXAWithRestartAfterPrepare() throws Exception
+ {
+ byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ Xid xid = generateXid();
+
+ env.startWork(xid);
+
+ database.put(null, id, bytes, offset, bytes.length);
+
+ env.endWork(xid, false);
+
+ env.prepare(xid);
+
+ // Now restart
+
+ database.close();
+
+ env.stop();
+
+ env.start();
+
+ database = env.getDatabase(DB_NAME);
+
+ assertStoreEmpty();
+ }
+
+ public void testRemoveTransactional() throws Exception
+ {
+ byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ database.put(null, id, bytes, offset, bytes.length);
+
+ assertContainsPair(id, bytes, 1);
+
+ BDBJETransaction tx = env.createTransaction();
+
+ database.remove(tx, id);
+
+ tx.commit();
+
+ assertStoreEmpty();
+ }
+
+ public void testRemoveTransactionalWithRestart() throws Exception
+ {
+ byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ database.put(null, id, bytes, offset, bytes.length);
+
+ assertContainsPair(id, bytes, 1);
+
+ BDBJETransaction tx = env.createTransaction();
+
+ database.remove(tx, id);
+
+ // Now restart
+
+ database.close();
+
+ env.stop();
+
+ env.start();
+
+ database = env.getDatabase(DB_NAME);
+
+ assertContainsPair(id, bytes, 1);
+ }
+
+ public void testRemoveXACommit() throws Exception
+ {
+ byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ database.put(null, id, bytes, offset, bytes.length);
+
+ assertContainsPair(id, bytes, 1);
+
+ Xid xid = generateXid();
+
+ env.startWork(xid);
+
+ database.remove(null, id);
+
+ env.endWork(xid, false);
+
+ env.prepare(xid);
+
+ env.commit(xid);
+
+ assertStoreEmpty();
+ }
+
+ public void testRemoveXAWithRestart() throws Exception
+ {
+ byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ database.put(null, id, bytes, offset, bytes.length);
+
+ assertContainsPair(id, bytes, 1);
+
+ Xid xid = generateXid();
+
+ env.startWork(xid);
+
+ database.remove(null, id);
+
+ env.endWork(xid, false);
+
+ // Now restart
+
+ database.close();
+
+ env.stop();
+
+ env.start();
+
+ database = env.getDatabase(DB_NAME);
+
+ assertContainsPair(id, bytes, 1);
+ }
+
+ public void testRemoveXAWithRestartAfterPrepare() throws Exception
+ {
+ byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ database.put(null, id, bytes, offset, bytes.length);
+
+ assertContainsPair(id, bytes, 1);
+
+ Xid xid = generateXid();
+
+ env.startWork(xid);
+
+ database.remove(null, id);
+
+ env.endWork(xid, false);
+
+ env.prepare(xid);
+
+ // Now restart
+
+ database.close();
+
+ env.stop();
+
+ env.start();
+
+ database = env.getDatabase(DB_NAME);
+
+ assertContainsPair(id, bytes, 1);
+ }
+
+ public void testPutTransactionalRollback() throws Exception
+ {
+ byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ BDBJETransaction tx = env.createTransaction();
+
+ database.put(tx, id, bytes, offset, bytes.length);
+
+ tx.rollback();
+
+ assertStoreEmpty();
+ }
+
+ public void testPutXARollback() throws Exception
+ {
+ byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ Xid xid = generateXid();
+
+ env.startWork(xid);
+
+ database.put(null, id, bytes, offset, bytes.length);
+
+ env.endWork(xid, false);
+
+ env.prepare(xid);
+
+ env.rollback(xid);
+
+ assertStoreEmpty();
+ }
+
+ public void testRemoveTransactionalRollback() throws Exception
+ {
+ byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ database.put(null, id, bytes, offset, bytes.length);
+
+ assertContainsPair(id, bytes, 1);
+
+ BDBJETransaction tx = env.createTransaction();
+
+ database.remove(tx, id);
+
+ tx.rollback();
+
+ assertContainsPair(id, bytes, 1);
+ }
+
+ public void testRemoveXARollback() throws Exception
+ {
+ byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ database.put(null, id, bytes, offset, bytes.length);
+
+ assertContainsPair(id, bytes, 1);
+
+ Xid xid = generateXid();
+
+ env.startWork(xid);
+
+ database.remove(null, id);
+
+ env.endWork(xid, false);
+
+ env.prepare(xid);
+
+ env.rollback(xid);
+
+ assertContainsPair(id, bytes, 1);
+ }
+
+
+ public void testPutAndRemoveMultipleTransactionalCommit() throws Exception
+ {
+ byte[] bytes1 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ byte[] bytes2 = new byte[] { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
+
+ byte[] bytes3 = new byte[] { 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 };
+
+ byte[] bytes4 = new byte[] { 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 };
+
+ long id1 = 1;
+
+ long id2 = 2;
+
+ long id3 = 3;
+
+ long id4 = 4;
+
+ int offset = 0;
+
+ database.put(null, id1, bytes1, offset, bytes1.length);
+
+ database.put(null, id2, bytes2, offset, bytes2.length);
+
+ assertContainsPair(id1, bytes1, 2);
+
+ assertContainsPair(id2, bytes2, 2);
+
+ BDBJETransaction tx = env.createTransaction();
+
+ database.put(tx, id3, bytes3, offset, bytes3.length);
+
+ database.put(tx, id4, bytes4, offset, bytes4.length);
+
+ database.remove(tx, id1);
+
+ database.remove(tx, id2);
+
+ tx.commit();
+
+ assertContainsPair(id3, bytes3, 2);
+
+ assertContainsPair(id4, bytes4, 2);
+ }
+
+ public void testPutAndRemoveMultipleXACommit() throws Exception
+ {
+ byte[] bytes1 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ byte[] bytes2 = new byte[] { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
+
+ byte[] bytes3 = new byte[] { 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 };
+
+ byte[] bytes4 = new byte[] { 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 };
+
+ long id1 = 1;
+
+ long id2 = 2;
+
+ long id3 = 3;
+
+ long id4 = 4;
+
+ int offset = 0;
+
+ database.put(null, id1, bytes1, offset, bytes1.length);
+
+ database.put(null, id2, bytes2, offset, bytes2.length);
+
+ assertContainsPair(id1, bytes1, 2);
+
+ assertContainsPair(id2, bytes2, 2);
+
+ Xid xid = generateXid();
+
+ env.startWork(xid);
+
+ database.put(null, id3, bytes3, offset, bytes3.length);
+
+ database.put(null, id4, bytes4, offset, bytes4.length);
+
+ database.remove(null, id1);
+
+ database.remove(null, id2);
+
+ env.endWork(xid, false);
+
+ env.prepare(xid);
+
+ env.commit(xid);
+
+ assertContainsPair(id3, bytes3, 2);
+
+ assertContainsPair(id4, bytes4, 2);
+ }
+
+ public void testPutAndRemoveMultipleXAWithRestart() throws Exception
+ {
+ byte[] bytes1 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ byte[] bytes2 = new byte[] { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
+
+ byte[] bytes3 = new byte[] { 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 };
+
+ byte[] bytes4 = new byte[] { 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 };
+
+ long id1 = 1;
+
+ long id2 = 2;
+
+ long id3 = 3;
+
+ long id4 = 4;
+
+ int offset = 0;
+
+ database.put(null, id1, bytes1, offset, bytes1.length);
+
+ database.put(null, id2, bytes2, offset, bytes2.length);
+
+ assertContainsPair(id1, bytes1, 2);
+
+ assertContainsPair(id2, bytes2, 2);
+
+ Xid xid = generateXid();
+
+ env.startWork(xid);
+
+ database.put(null, id3, bytes3, offset, bytes3.length);
+
+ database.put(null, id4, bytes4, offset, bytes4.length);
+
+ database.remove(null, id1);
+
+ database.remove(null, id2);
+
+ env.endWork(xid, false);
+
+ // Now restart
+
+ database.close();
+
+ env.stop();
+
+ env.start();
+
+ database = env.getDatabase(DB_NAME);
+
+ assertContainsPair(id1, bytes1, 2);
+
+ assertContainsPair(id2, bytes2, 2);
+ }
+
+ public void testPutAndRemoveMultipleTransactionalRollback() throws Exception
+ {
+ byte[] bytes1 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ byte[] bytes2 = new byte[] { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
+
+ byte[] bytes3 = new byte[] { 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 };
+
+ byte[] bytes4 = new byte[] { 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 };
+
+ long id1 = 1;
+
+ long id2 = 2;
+
+ long id3 = 3;
+
+ long id4 = 4;
+
+ int offset = 0;
+
+ database.put(null, id1, bytes1, offset, bytes1.length);
+
+ database.put(null, id2, bytes2, offset, bytes2.length);
+
+ assertContainsPair(id1, bytes1, 2);
+
+ assertContainsPair(id2, bytes2, 2);
+
+ BDBJETransaction tx = env.createTransaction();
+
+ database.put(tx, id3, bytes3, offset, bytes3.length);
+
+ database.put(tx, id4, bytes4, offset, bytes4.length);
+
+ database.remove(tx, id1);
+
+ database.remove(tx, id2);
+
+ tx.rollback();
+
+ assertContainsPair(id1, bytes1, 2);
+
+ assertContainsPair(id2, bytes2, 2);
+ }
+
+ public void testPutAndRemoveMultipleXARollback() throws Exception
+ {
+ byte[] bytes1 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ byte[] bytes2 = new byte[] { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
+
+ byte[] bytes3 = new byte[] { 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 };
+
+ byte[] bytes4 = new byte[] { 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 };
+
+ long id1 = 1;
+
+ long id2 = 2;
+
+ long id3 = 3;
+
+ long id4 = 4;
+
+ int offset = 0;
+
+ database.put(null, id1, bytes1, offset, bytes1.length);
+
+ database.put(null, id2, bytes2, offset, bytes2.length);
+
+ assertContainsPair(id1, bytes1, 2);
+
+ assertContainsPair(id2, bytes2, 2);
+
+ Xid xid = generateXid();
+
+ env.startWork(xid);
+
+ database.put(null, id3, bytes3, offset, bytes3.length);
+
+ database.put(null, id4, bytes4, offset, bytes4.length);
+
+ database.remove(null, id1);
+
+ database.remove(null, id2);
+
+ env.endWork(xid, false);
+
+ env.prepare(xid);
+
+ env.rollback(xid);
+
+ assertContainsPair(id1, bytes1, 2);
+
+ assertContainsPair(id2, bytes2, 2);
+ }
+
+ public void testOverwiteNonTransactional() throws Exception
+ {
+ byte[] bytes1 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ byte[] bytes2 = new byte[] { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ database.put(null, id, bytes1, offset, bytes1.length);
+
+ assertContainsPair(id, bytes1, 1);
+
+ database.put(null, id, bytes2, offset, bytes1.length);
+
+ assertContainsPair(id, bytes2, 1);
+
+ database.remove(null, 1);
+
+ assertStoreEmpty();
+ }
+
+ public void testOverwiteTransactionalCommit() throws Exception
+ {
+ byte[] bytes1 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ byte[] bytes2 = new byte[] { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ BDBJETransaction tx = env.createTransaction();
+
+ database.put(tx, id, bytes1, offset, bytes1.length);
+
+ database.put(tx, id, bytes2, offset, bytes1.length);
+
+ tx.commit();
+
+ assertContainsPair(id, bytes2, 1);
+
+ database.remove(null, 1);
+
+ assertStoreEmpty();
+ }
+
+ public void testOverwiteXACommit() throws Exception
+ {
+ byte[] bytes1 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ byte[] bytes2 = new byte[] { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ Xid xid = generateXid();
+
+ env.startWork(xid);
+
+ database.put(null, id, bytes1, offset, bytes1.length);
+
+ database.put(null, id, bytes2, offset, bytes1.length);
+
+ env.endWork(xid, false);
+
+ env.prepare(xid);
+
+ env.commit(xid);
+
+ assertContainsPair(id, bytes2, 1);
+
+ database.remove(null, 1);
+
+ assertStoreEmpty();
+ }
+
+ public void testOverwiteTransactionalRollback() throws Exception
+ {
+ byte[] bytes1 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ byte[] bytes2 = new byte[] { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ BDBJETransaction tx = env.createTransaction();
+
+ database.put(tx, id, bytes1, offset, bytes1.length);
+
+ database.put(tx, id, bytes2, offset, bytes1.length);
+
+ tx.rollback();
+
+ assertStoreEmpty();
+ }
+
+ public void testOverwiteXARollback() throws Exception
+ {
+ byte[] bytes1 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ byte[] bytes2 = new byte[] { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ Xid xid = generateXid();
+
+ env.startWork(xid);
+
+ database.put(null, id, bytes1, offset, bytes1.length);
+
+ database.put(null, id, bytes2, offset, bytes1.length);
+
+ env.endWork(xid, false);
+
+ env.prepare(xid);
+
+ env.rollback(xid);
+
+ assertStoreEmpty();
+ }
+
+ public void testPutAndRemovePartialNonTransactional() throws Exception
+ {
+ byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ byte[] bytes2 = new byte[] { 20, 21, 22, 23 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ database.put(null, id, bytes, offset, bytes.length);
+
+ assertContainsPair(id, bytes, 1);
+
+ database.put(null, id, bytes2, 10, bytes2.length);
+
+ byte[] bytes3 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 21, 22, 23 };
+
+ assertContainsPair(id, bytes3, 1);
+
+ database.put(null, id, bytes2, 3, bytes2.length);
+
+ byte[] bytes4 = new byte[] { 1, 2, 3, 20, 21, 22, 23, 8, 9, 10, 20, 21, 22, 23 };
+
+ assertContainsPair(id, bytes4, 1);
+
+ byte[] bytes5 = new byte[0];
+
+ //blank out 4 bytes
+ database.put(null, id, bytes5, 5, 4);
+
+ byte[] bytes6 = new byte[] { 1, 2, 3, 20, 21, 10, 20, 21, 22, 23 };
+
+ assertContainsPair(id, bytes6, 1);
+
+
+ database.put(null, id, new byte[0], 0, 4);
+
+ byte[] bytes7 = new byte[] { 21, 10, 20, 21, 22, 23 };
+
+ assertContainsPair(id, bytes7, 1);
+
+
+ database.remove(null, 1);
+
+ assertStoreEmpty();
+ }
+
+ public void testPutAndRemovePartialTransactional() throws Exception
+ {
+ byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ byte[] bytes2 = new byte[] { 20, 21, 22, 23 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ BDBJETransaction tx = env.createTransaction();
+
+ database.put(tx, id, bytes, offset, bytes.length);
+
+ tx.commit();
+
+ assertContainsPair(id, bytes, 1);
+
+ tx = env.createTransaction();
+
+ database.put(tx, id, bytes2, 10, bytes2.length);
+
+ tx.commit();
+
+ byte[] bytes3 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 21, 22, 23 };
+
+ assertContainsPair(id, bytes3, 1);
+
+ tx = env.createTransaction();
+
+ database.put(tx, id, bytes2, 3, bytes2.length);
+
+ tx.commit();
+
+ byte[] bytes4 = new byte[] { 1, 2, 3, 20, 21, 22, 23, 8, 9, 10, 20, 21, 22, 23 };
+
+ assertContainsPair(id, bytes4, 1);
+
+ byte[] bytes5 = new byte[0];
+
+ tx = env.createTransaction();
+
+ //blank out 4 bytes
+ database.put(tx, id, bytes5, 5, 4);
+
+ tx.commit();
+
+ byte[] bytes6 = new byte[] { 1, 2, 3, 20, 21, 10, 20, 21, 22, 23 };
+
+ assertContainsPair(id, bytes6, 1);
+
+ database.put(null, id, new byte[0], 0, 4);
+
+ byte[] bytes7 = new byte[] { 21, 10, 20, 21, 22, 23 };
+
+ assertContainsPair(id, bytes7, 1);
+
+ database.remove(null, 1);
+
+ assertStoreEmpty();
+ }
+
+ public void testSetAndGetEnvironment() throws Exception
+ {
+ BDBJEEnvironment bdb = createEnvironment();
+
+ final String path = "/home/tim/test-path123";
+
+ createDir(path);
+
+ assertNull(bdb.getEnvironmentPath());
+
+ bdb.setEnvironmentPath(path);
+
+ assertEquals(path, bdb.getEnvironmentPath());
+
+ bdb.start();
+
+ try
+ {
+ bdb.setEnvironmentPath("blah");
+
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //OK
+ }
+
+ assertEquals(path, bdb.getEnvironmentPath());
+
+ bdb.stop();
+
+ final String path2 = "test-path123651";
+
+ bdb.setEnvironmentPath(path2);
+
+ assertEquals(path2, bdb.getEnvironmentPath());
+ }
+
+
+ public void testSetAndGetTransacted() throws Exception
+ {
+ BDBJEEnvironment bdb = createEnvironment();
+
+ final String path = "/home/tim/test-path123";
+
+ createDir(path);
+
+ bdb.setEnvironmentPath(path);
+
+ bdb.setTransacted(false);
+
+ assertFalse(bdb.isTransacted());
+
+ bdb.setTransacted(true);
+
+ assertTrue(bdb.isTransacted());
+
+ bdb.start();
+
+ try
+ {
+ bdb.setTransacted(true);
+
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //OK
+ }
+
+ assertTrue(bdb.isTransacted());
+
+ bdb.stop();
+
+ bdb.setTransacted(false);
+
+ assertFalse(bdb.isTransacted());
+ }
+
+ public void testSetAndGetSyncOS() throws Exception
+ {
+ BDBJEEnvironment bdb = createEnvironment();
+
+ final String path = "/home/tim/test-path123";
+
+ createDir(path);
+
+ bdb.setEnvironmentPath(path);
+
+ assertFalse(bdb.isSyncOS());
+
+ bdb.setSyncOS(true);
+
+ assertTrue(bdb.isSyncOS());
+
+ bdb.start();
+
+ try
+ {
+ bdb.setSyncOS(true);
+
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //OK
+ }
+
+ assertTrue(bdb.isSyncOS());
+
+ bdb.stop();
+
+ bdb.setSyncOS(false);
+
+ assertFalse(bdb.isSyncOS());
+ }
+
+ public void testSetAndGetSyncVM() throws Exception
+ {
+ BDBJEEnvironment bdb = createEnvironment();
+
+ final String path = "/home/tim/test-path123";
+
+ bdb.setEnvironmentPath(path);
+
+ createDir(path);
+
+ assertFalse(bdb.isSyncVM());
+
+ bdb.setSyncVM(true);
+
+ assertTrue(bdb.isSyncVM());
+
+ bdb.start();
+
+ try
+ {
+ bdb.setSyncVM(true);
+
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //OK
+ }
+
+ assertTrue(bdb.isSyncVM());
+
+ bdb.stop();
+
+ bdb.setSyncVM(false);
+
+ assertFalse(bdb.isSyncVM());
+ }
+
+ public void testSetAndGetMemoryCacheSize() throws Exception
+ {
+ BDBJEEnvironment bdb = createEnvironment();
+
+ final String path = "/home/tim/test-path123";
+
+ createDir(path);
+
+ bdb.setEnvironmentPath(path);
+
+ assertEquals(-1, bdb.getMemoryCacheSize());
+
+ final long size = 16251762;
+
+ bdb.setMemoryCacheSize(size);
+
+ assertEquals(size, bdb.getMemoryCacheSize());
+
+ bdb.start();
+
+ try
+ {
+ bdb.setMemoryCacheSize(1897291289);
+
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //OK
+ }
+
+ assertEquals(size, bdb.getMemoryCacheSize());
+
+ bdb.stop();
+
+ final long size2 = 1625534783;
+
+ bdb.setMemoryCacheSize(size2);
+
+ assertEquals(size2, bdb.getMemoryCacheSize());
+ }
+
+
+ public void testStartAndStop() throws Exception
+ {
+ BDBJEEnvironment bdb = createEnvironment();
+
+ try
+ {
+ bdb.start();
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //OK
+ }
+
+ final String path = "/home/tim/test-path123";
+
+ createDir(path);
+
+ bdb.setEnvironmentPath(path);
+
+ bdb.start();
+
+ try
+ {
+ bdb.start();
+
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //OK
+ }
+
+ bdb.stop();
+
+ try
+ {
+ bdb.stop();
+
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //OK
+ }
+ }
+
+ public void testWrongOrderCommit() throws Exception
+ {
+ testXAWrongOrder(true);
+ }
+
+ public void testWrongOrderRollback() throws Exception
+ {
+ testXAWrongOrder(false);
+ }
+
+ public void testXAWrongXidCommit() throws Exception
+ {
+ testXAWrongXid(true);
+ }
+
+ public void testXAWrongXidRollback() throws Exception
+ {
+ testXAWrongXid(false);
+ }
+
+ private void testXAWrongXid(boolean commit) throws Exception
+ {
+ Xid xid = generateXid();
+
+ env.startWork(xid);
+
+ Xid xid2 = generateXid();
+
+ try
+ {
+ env.endWork(xid2, false);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ //do some work
+
+ database.put(null, 23, new byte[10], 0, 10);
+
+ env.endWork(xid, false);
+
+ try
+ {
+ env.prepare(xid2);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ env.prepare(xid);
+
+ if (commit)
+ {
+ try
+ {
+ env.commit(xid2);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+ env.commit(xid);
+ }
+ else
+ {
+ try
+ {
+ env.rollback(xid2);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+ env.rollback(xid);
+ }
+ }
+
+ private void testXAWrongOrder(boolean commit) throws Exception
+ {
+ Xid xid = generateXid();
+
+ try
+ {
+ env.endWork(xid, false);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ try
+ {
+ env.prepare(xid);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ try
+ {
+ env.commit(xid);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ try
+ {
+ env.rollback(xid);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ env.startWork(xid);
+
+ //do some work
+
+ database.put(null, 23, new byte[10], 0, 10);
+
+ try
+ {
+ env.startWork(xid);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ try
+ {
+ env.prepare(xid);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ try
+ {
+ env.commit(xid);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ try
+ {
+ env.rollback(xid);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ env.endWork(xid, false);
+
+ try
+ {
+ env.startWork(xid);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ try
+ {
+ env.endWork(xid, false);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ try
+ {
+ env.commit(xid);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ try
+ {
+ env.rollback(xid);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ env.prepare(xid);
+
+ try
+ {
+ env.startWork(xid);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ try
+ {
+ env.endWork(xid, false);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ if (commit)
+ {
+ env.commit(xid);
+ }
+ else
+ {
+ env.rollback(xid);
+ }
+
+ try
+ {
+ env.endWork(xid, false);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ try
+ {
+ env.prepare(xid);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ try
+ {
+ env.commit(xid);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ try
+ {
+ env.rollback(xid);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ env.startWork(xid);
+
+ database.put(null, 23, new byte[10], 0, 10);
+
+ env.endWork(xid, false);
+
+ env.prepare(xid);
+
+ env.rollback(xid);
+
+ }
+
+ // Private -------------------------------------------------------------------------------------
+
+ private void assertContainsPair(long id, byte[] bytes, long size) throws Exception
+ {
+ byte[] b = database.get(id);
+
+ assertNotNull(b);
+
+ assertByteArraysEquivalent(bytes, b);
+
+ assertEquals(size, database.size());
+ }
+
+ private void assertStoreEmpty() throws Exception
+ {
+ assertEquals(0, database.size());
+ }
+}
Copied: branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/unit/BDBJEPersistenceManagerTest.java (from rev 3462, branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/BDBJEPersistenceManagerTest.java)
===================================================================
--- branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/unit/BDBJEPersistenceManagerTest.java (rev 0)
+++ branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/unit/BDBJEPersistenceManagerTest.java 2007-12-10 17:26:06 UTC (rev 3464)
@@ -0,0 +1,786 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.messaging.newcore.impl.bdbje.test.unit;
+
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.transaction.xa.Xid;
+
+import org.jboss.messaging.newcore.impl.MessageImpl;
+import org.jboss.messaging.newcore.impl.QueueImpl;
+import org.jboss.messaging.newcore.impl.bdbje.BDBJEDatabase;
+import org.jboss.messaging.newcore.impl.bdbje.BDBJEEnvironment;
+import org.jboss.messaging.newcore.impl.bdbje.BDBJEPersistenceManager;
+import org.jboss.messaging.newcore.impl.bdbje.test.unit.fakes.FakeBDBJEEnvironment;
+import org.jboss.messaging.newcore.intf.Message;
+import org.jboss.messaging.newcore.intf.MessageReference;
+import org.jboss.messaging.newcore.intf.Queue;
+import org.jboss.messaging.test.unit.UnitTestCase;
+
+/**
+ *
+ * A BDBJEPersistenceManagerTest
+ *
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ *
+ */
+public class BDBJEPersistenceManagerTest extends UnitTestCase
+{
+ protected static final String ENV_DIR = "test-env";
+
+ protected BDBJEPersistenceManager pm;
+
+ protected BDBJEEnvironment bdb;
+
+ protected void setUp() throws Exception
+ {
+ super.setUp();
+
+ bdb = new FakeBDBJEEnvironment();
+
+ pm = new BDBJEPersistenceManager(bdb, ENV_DIR);
+
+ pm.start();
+ }
+
+ protected void tearDown() throws Exception
+ {
+ super.tearDown();
+
+ pm.stop();
+ }
+
+ // The tests ----------------------------------------------------------------
+
+ public void testAddMessage() throws Exception
+ {
+ Queue queue = new QueueImpl(67);
+
+ Message m = createMessageWithRefs(1, queue);
+
+ pm.addMessage(m);
+
+ assertMessageInStore(m, queue);
+ }
+
+ public void testDeleteReference() throws Exception
+ {
+ Queue queue = new QueueImpl(67);
+
+ Message m = createMessageWithRefs(1, queue);
+
+ List<MessageReference> refs = new ArrayList<MessageReference>(m.getReferences());
+
+
+ pm.addMessage(m);
+
+ assertMessageInStore(m, queue);
+
+
+ pm.deleteReference(refs.get(2));
+
+ assertMessageInStore(m, queue);
+
+ assertEquals(3, m.getReferences().size());
+
+ assertTrue(m.getReferences().contains(refs.get(0)));
+ assertTrue(m.getReferences().contains(refs.get(1)));
+ assertTrue(m.getReferences().contains(refs.get(3)));
+
+
+ pm.deleteReference(refs.get(1));
+
+ assertMessageInStore(m, queue);
+
+ assertEquals(2, m.getReferences().size());
+
+ assertTrue(m.getReferences().contains(refs.get(0)));
+ assertTrue(m.getReferences().contains(refs.get(3)));
+
+
+
+ pm.deleteReference(refs.get(3));
+
+ assertMessageInStore(m, queue);
+
+ assertEquals(1, m.getReferences().size());
+
+ assertTrue(m.getReferences().contains(refs.get(0)));
+
+
+ pm.deleteReference(refs.get(0));
+
+ assertMessageNotInStore(m);
+
+ assertStoreEmpty();
+ }
+
+ public void testCommitTransaction() throws Exception
+ {
+ List<Message> msgs = new ArrayList<Message>();
+
+ Queue queue = new QueueImpl(67);
+
+ Message m1 = createMessageWithRefs(1, queue);
+ List<MessageReference> m1Refs = new ArrayList<MessageReference>(m1.getReferences());
+
+ msgs.add(m1);
+
+ Message m2 = createMessageWithRefs(2, queue);
+
+ msgs.add(m2);
+
+ Message m3 = createMessageWithRefs(3, queue);
+ List<MessageReference> m3Refs = new ArrayList<MessageReference>(m3.getReferences());
+
+ msgs.add(m3);
+
+ pm.commitTransaction(msgs, null);
+
+ assertMessageInStore(m1, queue);
+
+ assertMessageInStore(m2, queue);
+
+ assertMessageInStore(m3, queue);
+
+
+ //Add a couple more
+
+ List<Message> msgsMore = new ArrayList<Message>();
+
+ Message m4 = createMessageWithRefs(4, queue);
+ msgsMore.add(m4);
+
+ Message m5 = createMessageWithRefs(5, queue);
+ msgsMore.add(m5);
+
+ //Delete some refs
+
+ List<MessageReference> refsToRemove = new ArrayList<MessageReference>();
+
+ refsToRemove.add(m1.getReferences().get(0));
+ refsToRemove.add(m1.getReferences().get(3));
+
+ refsToRemove.add(m2.getReferences().get(0));
+ refsToRemove.add(m2.getReferences().get(1));
+ refsToRemove.add(m2.getReferences().get(2));
+ refsToRemove.add(m2.getReferences().get(3));
+
+ refsToRemove.add(m3.getReferences().get(2));
+
+ pm.commitTransaction(msgsMore, refsToRemove);
+
+ assertMessageInStore(m1, queue);
+ assertEquals(2, m1.getReferences().size());
+ assertTrue(m1.getReferences().contains(m1Refs.get(1)));
+ assertTrue(m1.getReferences().contains(m1Refs.get(2)));
+
+ assertMessageNotInStore(m2);
+
+ assertMessageInStore(m3, queue);
+ assertEquals(3, m3.getReferences().size());
+ assertTrue(m3.getReferences().contains(m3Refs.get(0)));
+ assertTrue(m3.getReferences().contains(m3Refs.get(1)));
+ assertTrue(m3.getReferences().contains(m3Refs.get(3)));
+
+ assertMessageInStore(m4, queue);
+ assertEquals(4, m4.getReferences().size());
+
+ assertMessageInStore(m5, queue);
+ assertEquals(4, m5.getReferences().size());
+
+ //Delete the rest
+ refsToRemove.clear();
+ refsToRemove.addAll(m1.getReferences());
+ refsToRemove.addAll(m3.getReferences());
+ refsToRemove.addAll(m4.getReferences());
+ refsToRemove.addAll(m5.getReferences());
+
+ pm.commitTransaction(null, refsToRemove);
+
+ assertMessageNotInStore(m1);
+ assertMessageNotInStore(m2);
+ assertMessageNotInStore(m4);
+ assertMessageNotInStore(m5);
+ assertMessageNotInStore(m5);
+
+ //try with nulls
+ pm.commitTransaction(null, null);
+
+ }
+
+ public void testPrepareAndCommitTransaction() throws Exception
+ {
+ List<Message> msgs = new ArrayList<Message>();
+
+ Queue queue = new QueueImpl(67);
+
+ Message m1 = createMessageWithRefs(1, queue);
+ List<MessageReference> m1Refs = new ArrayList<MessageReference>(m1.getReferences());
+
+ msgs.add(m1);
+
+ Message m2 = createMessageWithRefs(2, queue);
+
+ msgs.add(m2);
+
+ Message m3 = createMessageWithRefs(3, queue);
+ List<MessageReference> m3Refs = new ArrayList<MessageReference>(m3.getReferences());
+
+ msgs.add(m3);
+
+ pm.commitTransaction(msgs, null);
+
+ assertMessageInStore(m1, queue);
+
+ assertMessageInStore(m2, queue);
+
+ assertMessageInStore(m3, queue);
+
+
+ //Add a couple more
+
+ List<Message> msgsMore = new ArrayList<Message>();
+
+ Message m4 = createMessageWithRefs(4, queue);
+ msgsMore.add(m4);
+
+ Message m5 = createMessageWithRefs(5, queue);
+
+ msgsMore.add(m5);
+
+ //Delete some refs
+
+ List<MessageReference> refsToRemove = new ArrayList<MessageReference>();
+
+ refsToRemove.add(m1.getReferences().get(0));
+ refsToRemove.add(m1.getReferences().get(3));
+
+ refsToRemove.add(m2.getReferences().get(0));
+ refsToRemove.add(m2.getReferences().get(1));
+ refsToRemove.add(m2.getReferences().get(2));
+ refsToRemove.add(m2.getReferences().get(3));
+
+ refsToRemove.add(m3.getReferences().get(2));
+
+ Xid xid = generateXid();
+
+ pm.prepareTransaction(xid, msgsMore, refsToRemove);
+
+ pm.commitPreparedTransaction(xid);
+
+ assertMessageInStore(m1, queue);
+ assertEquals(2, m1.getReferences().size());
+ assertTrue(m1.getReferences().contains(m1Refs.get(1)));
+ assertTrue(m1.getReferences().contains(m1Refs.get(2)));
+
+ assertMessageNotInStore(m2);
+
+ assertMessageInStore(m3, queue);
+ assertEquals(3, m3.getReferences().size());
+ assertTrue(m3.getReferences().contains(m3Refs.get(0)));
+ assertTrue(m3.getReferences().contains(m3Refs.get(1)));
+ assertTrue(m3.getReferences().contains(m3Refs.get(3)));
+
+ assertMessageInStore(m4, queue);
+ assertEquals(4, m4.getReferences().size());
+
+ assertMessageInStore(m5, queue);
+ assertEquals(4, m5.getReferences().size());
+
+ //Delete the rest
+ refsToRemove.clear();
+ refsToRemove.addAll(m1.getReferences());
+ refsToRemove.addAll(m3.getReferences());
+ refsToRemove.addAll(m4.getReferences());
+ refsToRemove.addAll(m5.getReferences());
+
+ xid = generateXid();
+
+ pm.prepareTransaction(xid, null, refsToRemove);
+
+ pm.commitPreparedTransaction(xid);
+
+ assertMessageNotInStore(m1);
+ assertMessageNotInStore(m2);
+ assertMessageNotInStore(m4);
+ assertMessageNotInStore(m5);
+ assertMessageNotInStore(m5);
+
+ //try with nulls
+ xid = generateXid();
+ pm.prepareTransaction(xid, null, null);
+ pm.commitPreparedTransaction(xid);
+
+ }
+
+ public void testPrepareAndUnprepareTransaction() throws Exception
+ {
+ List<Message> msgs = new ArrayList<Message>();
+
+ Queue queue = new QueueImpl(67);
+
+ Message m1 = createMessageWithRefs(1, queue);
+
+ msgs.add(m1);
+
+ Message m2 = createMessageWithRefs(2, queue);
+
+ msgs.add(m2);
+
+ Message m3 = createMessageWithRefs(3, queue);
+
+ msgs.add(m3);
+
+ pm.commitTransaction(msgs, null);
+
+ assertMessageInStore(m1, queue);
+
+ assertMessageInStore(m2, queue);
+
+ assertMessageInStore(m3, queue);
+
+
+ //Add a couple more
+
+ List<Message> msgsMore = new ArrayList<Message>();
+
+ Message m4 = createMessageWithRefs(4, queue);
+ msgsMore.add(m4);
+
+ Message m5 = createMessageWithRefs(5, queue);
+ msgsMore.add(m5);
+
+ //Delete some refs
+
+ List<MessageReference> refsToRemove = new ArrayList<MessageReference>();
+
+ refsToRemove.add(m1.getReferences().get(0));
+ refsToRemove.add(m1.getReferences().get(3));
+
+ refsToRemove.add(m2.getReferences().get(0));
+ refsToRemove.add(m2.getReferences().get(1));
+ refsToRemove.add(m2.getReferences().get(2));
+ refsToRemove.add(m2.getReferences().get(3));
+
+ refsToRemove.add(m3.getReferences().get(2));
+
+ Xid xid = generateXid();
+
+ pm.prepareTransaction(xid, msgsMore, refsToRemove);
+
+ pm.unprepareTransaction(xid, msgsMore, refsToRemove);
+
+ assertNumMessagesInStore(3);
+ }
+
+ public void testUpdateDeliveryCount() throws Exception
+ {
+ Queue queue = new QueueImpl(67);
+
+ Message m1 = createMessageWithRefs(1, queue);
+
+ assertEquals(0, m1.getReferences().get(0).getDeliveryCount());
+ assertEquals(0, m1.getReferences().get(1).getDeliveryCount());
+ assertEquals(0, m1.getReferences().get(2).getDeliveryCount());
+ assertEquals(0, m1.getReferences().get(3).getDeliveryCount());
+
+ pm.addMessage(m1);
+
+ final int delCount = 77;
+ m1.getReferences().get(1).setDeliveryCount(delCount);
+ pm.updateDeliveryCount(queue, m1.getReferences().get(1));
+
+ final int delCount2 = 423;
+
+ m1.getReferences().get(3).setDeliveryCount(delCount2);
+ pm.updateDeliveryCount(queue, m1.getReferences().get(3));
+
+ assertMessageInStore(m1, queue);
+ }
+
+ public void testRefsWithDifferentQueues() throws Exception
+ {
+ final int numQueues = 10;
+
+ List<Message> msgs = new ArrayList<Message>();
+
+ for (int i = 0; i < numQueues; i++)
+ {
+ Queue queue = new QueueImpl(i);
+
+ MessageReference ref = generateReference(queue, i);
+
+ msgs.add(ref.getMessage());
+
+ pm.addMessage(ref.getMessage());
+
+ assertEquals(queue, ref.getQueue());
+ }
+
+ for (Message msg: msgs)
+ {
+ assertMessageInStore(msg, msg.getReferences().get(0).getQueue());
+ }
+ }
+
+ public void testLoadQueues() throws Exception
+ {
+ Map<Long, Queue> queues = new HashMap<Long, Queue>();
+
+ final int numQueues = 10;
+
+ final int numMessages = 10;
+
+ for (int i = 0; i < numQueues; i++)
+ {
+ Queue queue = new QueueImpl(i);
+
+ queues.put(queue.getID(), queue);
+ }
+
+ List<Message> msgs = new ArrayList<Message>();
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ Message msg = this.generateMessage(i);
+
+ msgs.add(msg);
+
+ for (long j = 0; j < numQueues; j++)
+ {
+ Queue queue = queues.get(j);
+
+ msg.createReference(queue);
+ }
+
+ pm.addMessage(msg);
+ }
+
+
+ pm.loadQueues(queues);
+
+ for (Queue queue: queues.values())
+ {
+ assertEquals(numMessages, queue.getMessageCount());
+
+ List<MessageReference> refs = queue.list(null);
+
+ int i = 0;
+ for (MessageReference ref: refs)
+ {
+ this.assertEquivalent(msgs.get(i++), ref.getMessage());
+ }
+ }
+ }
+
+ public void testGetInDoubtXids() throws Exception
+ {
+ Queue queue = new QueueImpl(12);
+
+ Message message1 = createMessageWithRefs(1, queue);
+
+ List<Message> msgs = new ArrayList<Message>();
+
+ msgs.add(message1);
+
+ Xid xid1 = generateXid();
+
+ pm.prepareTransaction(xid1, msgs, null);
+
+ pm.setInRecoveryMode(true);
+
+ List<Xid> xids = pm.getInDoubtXids();
+
+ assertNotNull(xids);
+
+ assertEquals(1, xids.size());
+
+ assertEquals(xid1, xids.get(0));
+
+
+
+ Message message2 = createMessageWithRefs(2, queue);
+
+ msgs.clear();
+
+ msgs.add(message2);
+
+ Xid xid2 = generateXid();
+
+ pm.prepareTransaction(xid2, msgs, null);
+
+ xids = pm.getInDoubtXids();
+
+ assertNotNull(xids);
+
+ assertEquals(2, xids.size());
+
+ assertTrue(xids.contains(xid1));
+
+ assertTrue(xids.contains(xid2));
+
+
+ pm.commitPreparedTransaction(xid1);
+
+ pm.commitPreparedTransaction(xid2);
+
+ xids = pm.getInDoubtXids();
+
+ assertNotNull(xids);
+
+ assertEquals(0, xids.size());
+ }
+
+ public void testGetInDoubtXidsWithRestart() throws Exception
+ {
+ Queue queue = new QueueImpl(12);
+
+ Message message1 = createMessageWithRefs(1, queue);
+
+ List<Message> msgs = new ArrayList<Message>();
+
+ msgs.add(message1);
+
+ Xid xid1 = generateXid();
+
+ pm.prepareTransaction(xid1, msgs, null);
+
+ pm.setInRecoveryMode(true);
+
+ List<Xid> xids = pm.getInDoubtXids();
+
+ assertNotNull(xids);
+
+ assertEquals(1, xids.size());
+
+ assertEquals(xid1, xids.get(0));
+
+
+
+ Message message2 = createMessageWithRefs(2, queue);
+
+ msgs.clear();
+
+ msgs.add(message2);
+
+ Xid xid2 = generateXid();
+
+ pm.prepareTransaction(xid2, msgs, null);
+
+ xids = pm.getInDoubtXids();
+
+ assertNotNull(xids);
+
+ assertEquals(2, xids.size());
+
+ assertTrue(xids.contains(xid1));
+
+ assertTrue(xids.contains(xid2));
+
+ pm.stop();
+
+ pm.start();
+
+ pm.setInRecoveryMode(true);
+
+ xids = pm.getInDoubtXids();
+
+ assertNotNull(xids);
+
+ assertEquals(2, xids.size());
+
+ assertTrue(xids.contains(xid1));
+
+ assertTrue(xids.contains(xid2));
+
+
+ pm.commitPreparedTransaction(xid1);
+
+ pm.commitPreparedTransaction(xid2);
+
+ xids = pm.getInDoubtXids();
+
+ assertNotNull(xids);
+
+ assertEquals(0, xids.size());
+ }
+
+ public void testSetGetRecoveryMode() throws Exception
+ {
+ assertFalse(pm.isInRecoveryMode());
+
+ try
+ {
+ pm.getInDoubtXids();
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ pm.setInRecoveryMode(true);
+
+ assertTrue(pm.isInRecoveryMode());
+
+ pm.getInDoubtXids();
+
+ pm.setInRecoveryMode(false);
+
+ assertFalse(pm.isInRecoveryMode());
+ }
+
+ // Private --------------------------------------------------------------------
+
+ private Message extractMessage(Map<Long, Queue> queues, long id, byte[] msgBytes, byte[] refBytes) throws Exception
+ {
+ ByteBuffer buffer = ByteBuffer.wrap(msgBytes);
+
+ int type = buffer.getInt();
+
+ long expiration = buffer.getLong();
+
+ long timestamp = buffer.getLong();
+
+ byte priority = buffer.get();
+
+ int headerSize = buffer.getInt();
+
+ byte[] headers = new byte[headerSize];
+
+ buffer.get(headers);
+
+ int payloadSize = buffer.getInt();
+
+ byte[] payload = null;
+
+ if (payloadSize != 0)
+ {
+ payload = new byte[payloadSize];
+
+ buffer.get(payload);
+ }
+
+ Message message = new MessageImpl(id, type, true, expiration, timestamp, priority,
+ headers, payload);
+
+ buffer = ByteBuffer.wrap(refBytes);
+
+ while (buffer.hasRemaining())
+ {
+ long queueID = buffer.getLong();
+
+ int deliveryCount = buffer.getInt();
+
+ long scheduledDeliveryTime = buffer.getLong();
+
+ MessageReference reference = message.createReference(queues.get(queueID));
+
+ reference.setDeliveryCount(deliveryCount);
+
+ reference.setScheduledDeliveryTime(scheduledDeliveryTime);
+ }
+
+ return message;
+ }
+
+ private void assertMessageInStore(Message m, Queue queue) throws Exception
+ {
+ BDBJEDatabase msgDB = bdb.getDatabase(BDBJEPersistenceManager.MESSAGE_DB_NAME);
+
+ BDBJEDatabase refDB = bdb.getDatabase(BDBJEPersistenceManager.REFERENCE_DB_NAME);
+
+ byte[] msgBytes = msgDB.get(m.getMessageID());
+
+ assertNotNull(msgBytes);
+
+ byte[] refBytes = refDB.get(m.getMessageID());
+
+ assertNotNull(refBytes);
+
+ Map<Long, Queue> queues = new HashMap<Long, Queue>();
+
+ queues.put(queue.getID(), queue);
+
+ Message m2 = extractMessage(queues, m.getMessageID(), msgBytes, refBytes);
+
+ assertEquivalent(m, m2);
+ }
+
+ private void assertNumMessagesInStore(int num) throws Exception
+ {
+ BDBJEDatabase msgDB = bdb.getDatabase(BDBJEPersistenceManager.MESSAGE_DB_NAME);
+
+ assertEquals(num, msgDB.size());
+ }
+
+ private void assertMessageNotInStore(Message m) throws Exception
+ {
+ BDBJEDatabase msgDB = bdb.getDatabase(BDBJEPersistenceManager.MESSAGE_DB_NAME);
+
+ BDBJEDatabase refDB = bdb.getDatabase(BDBJEPersistenceManager.REFERENCE_DB_NAME);
+
+
+ byte[] msgBytes = msgDB.get(m.getMessageID());
+
+ assertNull(msgBytes);
+
+ byte[] refBytes = refDB.get(m.getMessageID());
+
+ assertNull(refBytes);
+ }
+
+ private void assertStoreEmpty() throws Exception
+ {
+ BDBJEDatabase msgDB = bdb.getDatabase(BDBJEPersistenceManager.MESSAGE_DB_NAME);
+
+ BDBJEDatabase refDB = bdb.getDatabase(BDBJEPersistenceManager.REFERENCE_DB_NAME);
+
+ assertEquals(0, msgDB.size());
+
+ assertEquals(0, refDB.size());
+ }
+
+ private Message createMessageWithRefs(long id, Queue queue)
+ {
+ Message m = generateMessage(id);
+
+ m.createReference(queue);
+
+ m.createReference(queue);
+
+ m.createReference(queue);
+
+ m.createReference(queue);
+
+ return m;
+ }
+
+
+ // Inner classes ---------------------------------------------------------------
+
+}
Copied: branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/unit/BDBSpeedTest.java (from rev 3462, branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/BDBSpeedTest.java)
===================================================================
--- branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/unit/BDBSpeedTest.java (rev 0)
+++ branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/unit/BDBSpeedTest.java 2007-12-10 17:26:06 UTC (rev 3464)
@@ -0,0 +1,656 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.messaging.newcore.impl.bdbje.test.unit;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.transaction.xa.Xid;
+
+import org.jboss.jms.tx.MessagingXid;
+import org.jboss.messaging.newcore.impl.MessageImpl;
+import org.jboss.messaging.newcore.impl.QueueImpl;
+import org.jboss.messaging.newcore.impl.bdbje.BDBJEPersistenceManager;
+import org.jboss.messaging.newcore.intf.Message;
+import org.jboss.messaging.newcore.intf.MessageReference;
+import org.jboss.messaging.newcore.intf.Queue;
+import org.jboss.messaging.test.unit.UnitTestCase;
+
+/**
+ *
+ * A BDBSpeedTest
+ *
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ *
+ */
+public class BDBSpeedTest extends UnitTestCase
+{
+
+// public void testCommitMessage() throws Exception
+// {
+// String envPath = "/home/tim/test-env";
+//
+// File file = new File(envPath);
+//
+// deleteDirectory(file);
+//
+// file.mkdir();
+//
+// final int numMessages = 5000;
+//
+// final int numRefs = 10;
+//
+// BDBJEPersistenceManager2 bdb = new BDBJEPersistenceManager2(envPath);
+//
+// bdb.start();
+//
+// Queue queue = new QueueImpl(1);
+//
+// //List<Message> messages = new ArrayList<Message>();
+//
+// long start = System.currentTimeMillis();
+//
+// byte[] payload = new byte[1 * 1024];
+//
+// for (int i = 0; i < numMessages; i++)
+// {
+// Message message = new MessageImpl(i, 0, true, 0, System.currentTimeMillis(), (byte)4);
+//
+// for (int j = 0; j < numRefs; j++)
+// {
+// message.createReference(queue);
+//
+// message.setPayload(payload);
+// }
+//
+// //messages.add(message);
+//
+// bdb.commitMessage(message);
+// }
+//
+// long end = System.currentTimeMillis();
+//
+// double rate = 1000 * (double)numMessages / (end - start);
+//
+// System.out.println("Insert Rate: " + rate);
+//
+// }
+//
+// public void testCommitMessages() throws Exception
+// {
+// String envPath = "/home/tim/test-env";
+//
+// File file = new File(envPath);
+//
+// deleteDirectory(file);
+//
+// file.mkdir();
+//
+// final int numMessages = 5000;
+//
+// final int numRefs = 10;
+//
+// BDBJEPersistenceManager2 bdb = new BDBJEPersistenceManager2(envPath);
+//
+// bdb.start();
+//
+// Queue queue = new QueueImpl(1);
+//
+// List<Message> messages = new ArrayList<Message>();
+//
+// long start = System.currentTimeMillis();
+//
+// byte[] payload = new byte[1 * 1024];
+//
+// for (int i = 0; i < numMessages; i++)
+// {
+// Message message = new MessageImpl(i, 0, true, 0, System.currentTimeMillis(), (byte)4);
+//
+// for (int j = 0; j < numRefs; j++)
+// {
+// message.createReference(queue);
+//
+// message.setPayload(payload);
+// }
+//
+// messages.add(message);
+//
+// if (i % 100 == 0)
+// {
+// bdb.commitMessages(messages);
+//
+// messages.clear();
+// }
+// }
+// bdb.commitMessages(messages);
+//
+// long end = System.currentTimeMillis();
+//
+// double rate = 1000 * (double)numMessages / (end - start);
+//
+// System.out.println("Insert Rate: " + rate);
+//
+// }
+//
+// public void testDeleteReferences() throws Exception
+// {
+// String envPath = "/home/tim/test-env";
+//
+// File file = new File(envPath);
+//
+// deleteDirectory(file);
+//
+// file.mkdir();
+//
+// final int numMessages = 5000;
+//
+// final int numRefs = 10;
+//
+// BDBJEPersistenceManager2 bdb = new BDBJEPersistenceManager2(envPath);
+//
+// bdb.start();
+//
+// Queue queue = new QueueImpl(1);
+//
+// List<Message> messages = new ArrayList<Message>();
+//
+// byte[] payload = new byte[1 * 1024];
+//
+// List<MessageReference> refs = new ArrayList<MessageReference>();
+//
+// for (int i = 0; i < numMessages; i++)
+// {
+// Message message = new MessageImpl(i, 0, true, 0, System.currentTimeMillis(), (byte)4);
+//
+// for (int j = 0; j < numRefs; j++)
+// {
+// MessageReference ref = message.createReference(queue);
+//
+// message.setPayload(payload);
+//
+// refs.add(ref);
+// }
+//
+// messages.add(message);
+//
+// if (i % 100 == 0)
+// {
+// bdb.commitMessages(messages);
+//
+// messages.clear();
+// }
+// }
+// bdb.commitMessages(messages);
+//
+// long start = System.currentTimeMillis();
+//
+// //Now delete them
+//
+// bdb.deleteReferences(refs);
+//
+// long end = System.currentTimeMillis();
+//
+// double rate = 1000 * (double)numMessages / (end - start);
+//
+// System.out.println("Delete Rate: " + rate);
+//
+// }
+//
+// public void testDeleteReference() throws Exception
+// {
+// String envPath = "/home/tim/test-env";
+//
+// File file = new File(envPath);
+//
+// deleteDirectory(file);
+//
+// file.mkdir();
+//
+// final int numMessages = 5000;
+//
+// final int numRefs = 1;
+//
+// BDBJEPersistenceManager bdb = new BDBJEPersistenceManager(envPath);
+//
+// bdb.start();
+//
+// Queue queue = new QueueImpl(1);
+//
+// List<Message> messages = new ArrayList<Message>();
+//
+// byte[] payload = new byte[1 * 1024];
+//
+// List<MessageReference> refs = new ArrayList<MessageReference>();
+//
+// for (int i = 0; i < numMessages; i++)
+// {
+// Message message = new MessageImpl(i, 0, true, 0, System.currentTimeMillis(), (byte)4);
+//
+// for (int j = 0; j < numRefs; j++)
+// {
+// MessageReference ref = message.createReference(queue);
+//
+// message.setPayload(payload);
+//
+// refs.add(ref);
+// }
+//
+// messages.add(message);
+//
+// if (i % 100 == 0)
+// {
+// bdb.commitMessages(messages);
+//
+// messages.clear();
+// }
+// }
+// bdb.commitMessages(messages);
+//
+// System.out.println("Added them");
+//
+// long start = System.currentTimeMillis();
+//
+// //Now delete them
+//
+// for (MessageReference ref: refs)
+// {
+// bdb.deleteReference(ref);
+// }
+//
+// long end = System.currentTimeMillis();
+//
+// double rate = 1000 * (double)numMessages / (end - start);
+//
+// System.out.println("Delete Rate: " + rate);
+//
+// }
+//
+//
+// public void testPrepareMessages() throws Exception
+// {
+// String envPath = "/home/tim/test-env";
+//
+// File file = new File(envPath);
+//
+// deleteDirectory(file);
+//
+// file.mkdir();
+//
+// final int numMessages = 5000;
+//
+// final int numRefs = 10;
+//
+// BDBJEPersistenceManager2 bdb = new BDBJEPersistenceManager2(envPath);
+//
+// bdb.start();
+//
+// Queue queue = new QueueImpl(1);
+//
+// List<Message> messages = new ArrayList<Message>();
+//
+// long start = System.currentTimeMillis();
+//
+// byte[] payload = new byte[1 * 1024];
+//
+// for (int i = 0; i < numMessages; i++)
+// {
+// Message message = new MessageImpl(i, 0, true, 0, System.currentTimeMillis(), (byte)4);
+//
+// for (int j = 0; j < numRefs; j++)
+// {
+// message.createReference(queue);
+//
+// message.setPayload(payload);
+// }
+//
+// messages.add(message);
+//
+// if (i % 100 == 0)
+// {
+// Xid xid = this.generateXid();
+//
+// bdb.prepareMessages(xid, messages);
+//
+// bdb.commitPreparedMessages(xid);
+//
+// messages.clear();
+// }
+// }
+//
+// Xid xid = this.generateXid();
+//
+// bdb.prepareMessages(xid, messages);
+//
+// bdb.commitPreparedMessages(xid);
+//
+// long end = System.currentTimeMillis();
+//
+// double rate = 1000 * (double)numMessages / (end - start);
+//
+// System.out.println("Insert Rate: " + rate);
+//
+// }
+
+// /*
+// * Test with message and ref in separate databases
+// */
+// public void testMsgandRefsDifferentDBSNoXA() throws Exception
+// {
+// File file = new File("/home/tim/test-env");
+//
+// deleteDirectory(file);
+//
+// file.mkdir();
+//
+// EnvironmentConfig envConfig = new EnvironmentConfig();
+//
+// envConfig.setAllowCreate(true);
+//
+// envConfig.setTransactional(true);
+//
+//// envConfig.setTxnNoSync(true);
+////
+//// envConfig.setTxnWriteNoSync(true);
+//
+// Environment env1 = new Environment(file, envConfig);
+//
+// DatabaseConfig dbConfig1 = new DatabaseConfig();
+//
+// dbConfig1.setTransactional(true);
+//
+// dbConfig1.setAllowCreate(true);
+//
+// Database msgDB = env1.openDatabase(null, "message", dbConfig1);
+//
+// DatabaseConfig dbConfig2 = new DatabaseConfig();
+//
+// dbConfig2.setTransactional(true);
+//
+// dbConfig2.setAllowCreate(true);
+//
+// Database refDB = env1.openDatabase(null, "ref", dbConfig2);
+//
+//
+// DatabaseConfig dbConfig3 = new DatabaseConfig();
+//
+// dbConfig3.setTransactional(true);
+//
+// dbConfig3.setAllowCreate(true);
+//
+// Database txDB = env1.openDatabase(null, "tx", dbConfig3);
+//
+//
+//
+// final int numMessages = 5000;
+//
+// byte[] msgBytes = new byte[256];
+//
+// final int numRefs = 10;
+//
+// byte[] refBytes = new byte[numRefs * 20];
+//
+// byte[] txBytes = new byte[numRefs * 50];
+//
+//
+// long start = System.currentTimeMillis();
+//
+// for (int i = 0; i < numMessages; i++)
+// {
+// byte[] keyBytes = new byte[8];
+//
+// ByteBuffer buffer = ByteBuffer.wrap(keyBytes);
+//
+// buffer.putLong(i);
+//
+// DatabaseEntry key = new DatabaseEntry(keyBytes);
+//
+//
+// //Prepare refs
+//
+// DatabaseEntry refVal = new DatabaseEntry(refBytes);
+//
+// Transaction tx = env1.beginTransaction(null, null);
+//
+// refDB.put(tx, key, refVal);
+//
+// // Message
+//
+// DatabaseEntry msgVal = new DatabaseEntry(msgBytes);
+//
+// msgDB.put(tx, key, msgVal);
+//
+// //Write a tx record
+//
+// DatabaseEntry txVal = new DatabaseEntry(txBytes);
+//
+// txDB.put(tx, key, txVal);
+//
+// //Commit the prepare
+//
+// tx.commit();
+//
+// tx = env1.beginTransaction(null, null);
+//
+// //Now commit the refs
+//
+// refVal = new DatabaseEntry(refBytes);
+//
+// refDB.put(tx, key, refVal);
+//
+// //And delete the tx record
+//
+// txDB.delete(tx, key);
+//
+// tx.commit();
+// }
+//
+// long end = System.currentTimeMillis();
+//
+// double rate = 1000 * (double)numMessages / (end - start);
+//
+// System.out.println("Insert Rate: " + rate);
+//
+//
+// }
+//
+// public void testMsgandRefsDifferentDBSXA() throws Exception
+// {
+// File file = new File("/home/tim/test-env");
+//
+// deleteDirectory(file);
+//
+// file.mkdir();
+//
+// EnvironmentConfig envConfig = new EnvironmentConfig();
+//
+// envConfig.setAllowCreate(true);
+//
+// envConfig.setTransactional(true);
+//
+// XAEnvironment env1 = new XAEnvironment(file, envConfig);
+//
+// DatabaseConfig dbConfig1 = new DatabaseConfig();
+//
+// dbConfig1.setTransactional(true);
+//
+// dbConfig1.setAllowCreate(true);
+//
+// Database msgDB = env1.openDatabase(null, "message", dbConfig1);
+//
+// DatabaseConfig dbConfig2 = new DatabaseConfig();
+//
+// dbConfig2.setTransactional(true);
+//
+// dbConfig2.setAllowCreate(true);
+//
+// Database refDB = env1.openDatabase(null, "ref", dbConfig2);
+//
+//
+//
+// final int numMessages = 5000;
+//
+// byte[] msgBytes = new byte[256];
+//
+// final int numRefs = 10;
+//
+// byte[] refBytes = new byte[numRefs * 20];
+//
+// long start = System.currentTimeMillis();
+//
+// for (int i = 0; i < numMessages; i++)
+// {
+// Xid xid = generateXid();
+//
+// env1.start(xid, XAResource.TMNOFLAGS);
+//
+// byte[] keyBytes = new byte[8];
+//
+// ByteBuffer buffer = ByteBuffer.wrap(keyBytes);
+//
+// buffer.putLong(i);
+//
+// DatabaseEntry key = new DatabaseEntry(keyBytes);
+//
+// DatabaseEntry msgVal = new DatabaseEntry(msgBytes);
+//
+// Transaction tx = env1.beginTransaction(null, null);
+//
+// msgDB.put(null, key, msgVal);
+//
+// //Now the refs
+//
+// DatabaseEntry refVal = new DatabaseEntry(refBytes);
+//
+// refDB.put(null, key, refVal);
+//
+// env1.end(xid, XAResource.TMSUCCESS);
+//
+// env1.prepare(xid);
+//
+// env1.commit(xid, false);
+//
+// // tx.commit();
+// }
+//
+// long end = System.currentTimeMillis();
+//
+// double rate = 1000 * (double)numMessages / (end - start);
+//
+// System.out.println("Insert Rate: " + rate);
+//
+// start = System.currentTimeMillis();
+// }
+//
+// private Xid generateXid()
+// {
+// String id = java.util.UUID.randomUUID().toString();
+//
+// Xid xid = new MessagingXid("blah".getBytes(), 123, id.getBytes());
+//
+// return xid;
+// }
+//
+//
+//// This is very slow
+//// public void testMsgandRefsSameDBSNoXA() throws Exception
+//// {
+//// File file = new File("/home/tim/test-env");
+////
+//// deleteDirectory(file);
+////
+//// file.mkdir();
+////
+//// EnvironmentConfig envConfig = new EnvironmentConfig();
+////
+//// envConfig.setAllowCreate(true);
+////
+//// envConfig.setTransactional(true);
+////
+//// Environment env1 = new Environment(file, envConfig);
+////
+//// DatabaseConfig dbConfig1 = new DatabaseConfig();
+////
+//// dbConfig1.setTransactional(true);
+////
+//// dbConfig1.setAllowCreate(true);
+////
+//// Database msgDB = env1.openDatabase(null, "message", dbConfig1);
+////
+//// final int numMessages = 5000;
+////
+//// byte[] msgBytes = new byte[10 * 1024];
+////
+//// byte[] refBytes = new byte[24];
+////
+//// long start = System.currentTimeMillis();
+////
+//// for (int i = 0; i < numMessages; i++)
+//// {
+//// byte[] keyBytes = new byte[8];
+////
+//// ByteBuffer buffer = ByteBuffer.wrap(keyBytes);
+////
+//// buffer.putLong(i);
+////
+//// DatabaseEntry key = new DatabaseEntry(keyBytes);
+////
+//// DatabaseEntry msgVal = new DatabaseEntry(msgBytes);
+////
+//// Transaction tx = env1.beginTransaction(null, null);
+////
+//// msgDB.put(tx, key, msgVal);
+////
+//// //Now the refs
+////
+//// final int numRefs = 50;
+////
+//// for (int j = 0; j < numRefs; j++)
+//// {
+//// keyBytes = new byte[8];
+////
+//// buffer = ByteBuffer.wrap(keyBytes);
+////
+//// buffer.putLong(j);
+////
+//// key = new DatabaseEntry(keyBytes);
+////
+//// DatabaseEntry value = new DatabaseEntry();
+////
+//// value.setPartial(0, refBytes.length, true);
+////
+//// value.setData(refBytes);
+////
+//// msgDB.put(tx, key, value);
+//// }
+////
+//// tx.commit();
+////
+//// }
+////
+//// long end = System.currentTimeMillis();
+////
+//// double rate = 1000 * (double)numMessages / (end - start);
+////
+//// System.out.println("Rate: " + rate);
+//// }
+}
Copied: branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/unit/FakeBDBJEEnvironmentTest.java (from rev 3462, branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/FakeBDBJEEnvironmentTest.java)
===================================================================
--- branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/unit/FakeBDBJEEnvironmentTest.java (rev 0)
+++ branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/unit/FakeBDBJEEnvironmentTest.java 2007-12-10 17:26:06 UTC (rev 3464)
@@ -0,0 +1,25 @@
+package org.jboss.messaging.newcore.impl.bdbje.test.unit;
+
+import org.jboss.messaging.newcore.impl.bdbje.BDBJEEnvironment;
+import org.jboss.messaging.newcore.impl.bdbje.test.unit.fakes.FakeBDBJEEnvironment;
+
+/**
+ *
+ * A FakeBDBJEEnvironmentTest
+ *
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ *
+ */
+public class FakeBDBJEEnvironmentTest extends BDBJEEnvironmentTestBase
+{
+ protected BDBJEEnvironment createEnvironment() throws Exception
+ {
+ BDBJEEnvironment env = new FakeBDBJEEnvironment();
+
+ return env;
+ }
+
+ protected void createDir(String path)
+ {
+ }
+}
Copied: branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/unit/fakes/FakeBDBJEEnvironment.java (from rev 3462, branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/fakes/FakeBDBJEEnvironment.java)
===================================================================
--- branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/unit/fakes/FakeBDBJEEnvironment.java (rev 0)
+++ branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/bdbje/test/unit/fakes/FakeBDBJEEnvironment.java 2007-12-10 17:26:06 UTC (rev 3464)
@@ -0,0 +1,501 @@
+package org.jboss.messaging.newcore.impl.bdbje.test.unit.fakes;
+
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import javax.transaction.xa.Xid;
+
+import org.jboss.messaging.newcore.impl.bdbje.BDBJECursor;
+import org.jboss.messaging.newcore.impl.bdbje.BDBJEDatabase;
+import org.jboss.messaging.newcore.impl.bdbje.BDBJEEnvironment;
+import org.jboss.messaging.newcore.impl.bdbje.BDBJETransaction;
+import org.jboss.messaging.util.Pair;
+
+/**
+ *
+ * A FakeBDBJEEnvironment
+ *
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ *
+ */
+public class FakeBDBJEEnvironment implements BDBJEEnvironment
+{
+ private String environmentPath;
+
+ private Map<String, FakeBDBJETransaction> transactions = new HashMap<String, FakeBDBJETransaction>();
+
+ private boolean transacted;
+
+ private boolean syncOS;
+
+ private boolean syncVM;
+
+ private long memoryCacheSize = -1;
+
+ private boolean started;
+
+ private Map<Thread, FakeBDBJETransaction> implicitTXs = new ConcurrentHashMap<Thread, FakeBDBJETransaction>();
+
+ private Map<String, BDBJEDatabase> databases = new HashMap<String, BDBJEDatabase>();
+
+ public boolean isSyncOS()
+ {
+ return syncOS;
+ }
+
+ public void setSyncOS(boolean sync)
+ {
+ if (started)
+ {
+ throw new IllegalStateException("Cannot set SyncOS when started");
+ }
+ syncOS = sync;
+ }
+
+ public boolean isSyncVM()
+ {
+ return syncVM;
+ }
+
+ public void setSyncVM(boolean sync)
+ {
+ if (started)
+ {
+ throw new IllegalStateException("Cannot set SyncVM when started");
+ }
+ syncVM = sync;
+ }
+
+ public boolean isTransacted()
+ {
+ return transacted;
+ }
+
+ public void setTransacted(boolean transacted)
+ {
+ if (started)
+ {
+ throw new IllegalStateException("Cannot set transacted when started");
+ }
+ this.transacted = transacted;
+ }
+
+ public long getMemoryCacheSize()
+ {
+ return memoryCacheSize;
+ }
+
+ public void setMemoryCacheSize(long size)
+ {
+ if (started)
+ {
+ throw new IllegalStateException("Cannot set memory cache size when started");
+ }
+ this.memoryCacheSize = size;
+ }
+
+ public void setEnvironmentPath(String environmentPath)
+ {
+ if (started)
+ {
+ throw new IllegalStateException("Cannot set environmentPath when started");
+ }
+ this.environmentPath = environmentPath;
+ }
+
+ public String getEnvironmentPath()
+ {
+ return environmentPath;
+ }
+
+ public void start() throws Exception
+ {
+ if (environmentPath == null)
+ {
+ throw new IllegalStateException("Must set environmentPath before starting");
+ }
+ if (started)
+ {
+ throw new IllegalStateException("Already started");
+ }
+ started = true;
+ }
+
+ public void stop() throws Exception
+ {
+ if (!started)
+ {
+ throw new IllegalStateException("Not started");
+ }
+ started = false;
+ }
+
+ public BDBJETransaction createTransaction() throws Exception
+ {
+ return createTransactionInternal(null);
+ }
+
+ public synchronized BDBJEDatabase getDatabase(String databaseName) throws Exception
+ {
+ BDBJEDatabase database = databases.get(databaseName);
+
+ if (database == null)
+ {
+ database = new FakeBDBJEDatabase();
+
+ databases.put(databaseName, database);
+ }
+
+ return database;
+ }
+
+ public List<Xid> getInDoubtXids() throws Exception
+ {
+ List<Xid> xids = new ArrayList<Xid>();
+
+ for (FakeBDBJETransaction tx: transactions.values())
+ {
+ if (tx.isPrepared())
+ {
+ xids.add(tx.getXid());
+ }
+ }
+
+ return xids;
+ }
+
+ public void startWork(Xid xid) throws Exception
+ {
+ if (implicitTXs.get(Thread.currentThread()) != null)
+ {
+ throw new IllegalStateException("Already implicit transaction");
+ }
+
+ implicitTXs.put(Thread.currentThread(), createTransactionInternal(xid));
+ }
+
+ public void endWork(Xid xid, boolean fail) throws Exception
+ {
+ FakeBDBJETransaction removed = implicitTXs.remove(Thread.currentThread());
+
+ if (removed == null)
+ {
+ throw new IllegalStateException("No implicit tx");
+ }
+ }
+
+ private FakeBDBJETransaction findTXForXid(Xid xid)
+ {
+ for (FakeBDBJETransaction tx: transactions.values())
+ {
+ if (xid.equals(tx.xid))
+ {
+ return tx;
+ }
+ }
+ return null;
+ }
+
+ public void prepare(Xid xid) throws Exception
+ {
+ FakeBDBJETransaction tx = findTXForXid(xid);
+
+ if (tx == null)
+ {
+ throw new IllegalStateException("Cannot find tx for xid " + xid);
+ }
+
+ if (implicitTXs.containsKey(Thread.currentThread()))
+ {
+ throw new IllegalStateException("Work not ended");
+ }
+
+ tx.prepare();
+ }
+
+ public void commit(Xid xid) throws Exception
+ {
+ FakeBDBJETransaction tx = findTXForXid(xid);
+
+ if (tx == null)
+ {
+ throw new IllegalStateException("Cannot find tx for xid " + xid);
+ }
+
+ if (implicitTXs.containsKey(Thread.currentThread()))
+ {
+ throw new IllegalStateException("Work not ended");
+ }
+
+ tx.commit();
+ }
+
+ public void rollback(Xid xid) throws Exception
+ {
+ FakeBDBJETransaction tx = findTXForXid(xid);
+
+ if (tx == null)
+ {
+ throw new IllegalStateException("Cannot find tx for xid " + xid);
+ }
+
+ if (implicitTXs.containsKey(Thread.currentThread()))
+ {
+ throw new IllegalStateException("Work not ended");
+ }
+
+ tx.rollback();
+ }
+
+ // Private -----------------------------------------------------------------------
+
+ private FakeBDBJETransaction createTransactionInternal(Xid xid) throws Exception
+ {
+ FakeBDBJETransaction tx = new FakeBDBJETransaction(xid);
+
+ transactions.put(tx.id, tx);
+
+ return tx;
+ }
+
+ // Inner classes ------------------------------------------------------------------
+
+ private class FakeBDBJETransaction implements BDBJETransaction
+ {
+ private String id = java.util.UUID.randomUUID().toString();
+
+ private Xid xid;
+
+ private List<Action> actions = new ArrayList<Action>();
+
+ private boolean prepared;
+
+ FakeBDBJETransaction(Xid xid)
+ {
+ this.xid = xid;
+ }
+
+ public void commit() throws Exception
+ {
+ for (Action action : actions)
+ {
+ if (action.put)
+ {
+ action.database.put(null, action.id, action.bytes, action.offset, action.length);
+ }
+ else
+ {
+ action.database.remove(null, action.id);
+ }
+ }
+
+ actions.clear();
+
+ transactions.remove(id);
+ }
+
+ public void prepare() throws Exception
+ {
+ prepared = true;
+ }
+
+ public void rollback() throws Exception
+ {
+ actions.clear();
+
+ transactions.remove(id);
+ }
+
+ public boolean isPrepared()
+ {
+ return prepared;
+ }
+
+ public Xid getXid()
+ {
+ return xid;
+ }
+
+ }
+
+ private class Action
+ {
+ public BDBJEDatabase database;
+
+ public boolean put;
+
+ public long id;
+
+ public byte[] bytes;
+
+ public int offset;
+
+ public int length;
+
+ public Action(BDBJEDatabase database, boolean put, long id, byte[] bytes, int offset, int length)
+ {
+ this.database = database;
+
+ this.put = put;
+
+ this.id = id;
+
+ this.bytes = bytes;
+
+ this.offset = offset;
+
+ this.length = length;
+ }
+ }
+
+ private class FakeBDBJEDatabase implements BDBJEDatabase
+ {
+ private Map<Long, byte[]> store = new LinkedHashMap<Long, byte[]>();
+
+ private AtomicInteger cursorCount = new AtomicInteger(0);
+
+ public void close() throws Exception
+ {
+ if (cursorCount.get() != 0)
+ {
+ throw new IllegalStateException("Cannot close. There are cursors open");
+ }
+ }
+
+ public BDBJECursor cursor() throws Exception
+ {
+ return new FakeBDBJECursor();
+ }
+
+ private BDBJETransaction getTx(BDBJETransaction tx)
+ {
+ if (tx == null)
+ {
+ tx = implicitTXs.get(Thread.currentThread());
+ }
+ return tx;
+ }
+
+ public void put(BDBJETransaction tx, long id, byte[] bytes, int offset,
+ int length) throws Exception
+ {
+ tx = getTx(tx);
+
+ if (tx == null)
+ {
+ if (offset == 0 && bytes.length == length)
+ {
+ store.put(id, bytes);
+ }
+ else
+ {
+ byte[] currentBytes = store.get(id);
+
+ if (offset == currentBytes.length)
+ {
+ byte[] newBytes = new byte[currentBytes.length + bytes.length];
+
+ ByteBuffer buffer = ByteBuffer.wrap(newBytes);
+
+ buffer.put(currentBytes);
+
+ buffer.put(bytes);
+
+ store.put(id, newBytes);
+ }
+ else if (offset < currentBytes.length)
+ {
+ if (offset + length > currentBytes.length)
+ {
+ throw new IllegalStateException("Invalid offset/length");
+ }
+
+ byte[] newBytes = new byte[currentBytes.length - (length - bytes.length)];
+
+ System.arraycopy(currentBytes, 0, newBytes, 0, offset);
+
+ System.arraycopy(bytes, 0, newBytes, offset, bytes.length);
+
+ System.arraycopy(currentBytes, offset + length, newBytes,
+ offset + bytes.length, currentBytes.length - (offset + length));
+
+ store.put(id, newBytes);
+ }
+ else
+ {
+ throw new IllegalStateException("Invalid offset " + offset);
+ }
+ }
+ }
+ else
+ {
+ FakeBDBJETransaction ftx = (FakeBDBJETransaction)tx;
+
+ ftx.actions.add(new Action(this, true, id, bytes, offset, length));
+ }
+
+ }
+
+ public void remove(BDBJETransaction tx, long id) throws Exception
+ {
+ tx = getTx(tx);
+
+ if (tx == null)
+ {
+ store.remove(id);
+ }
+ else
+ {
+ FakeBDBJETransaction ftx = (FakeBDBJETransaction)tx;
+
+ ftx.actions.add(new Action(this, false, id, null, -1, -1));
+ }
+ }
+
+ public byte[] get(long id) throws Exception
+ {
+ return store.get(id);
+ }
+
+ public long size() throws Exception
+ {
+ return store.size();
+ }
+
+ private class FakeBDBJECursor implements BDBJECursor
+ {
+ private Iterator<Map.Entry<Long,byte[]>> iterator = store.entrySet().iterator();
+
+ FakeBDBJECursor()
+ {
+ cursorCount.incrementAndGet();
+ }
+
+ public void close() throws Exception
+ {
+ cursorCount.decrementAndGet();
+ }
+
+ public Pair<Long, byte[]> getNext() throws Exception
+ {
+ if (iterator.hasNext())
+ {
+ Map.Entry<Long,byte[]> entry = iterator.next();
+
+ return new Pair<Long, byte[]>(entry.getKey(), entry.getValue());
+ }
+ else
+ {
+ return null;
+ }
+ }
+ }
+ }
+}
Added: branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/test/unit/RoundRobinDistributionPolicyTest.java
===================================================================
--- branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/test/unit/RoundRobinDistributionPolicyTest.java (rev 0)
+++ branches/Branch_New_Persistence/src/main/org/jboss/messaging/newcore/impl/test/unit/RoundRobinDistributionPolicyTest.java 2007-12-10 17:26:06 UTC (rev 3464)
@@ -0,0 +1,74 @@
+package org.jboss.messaging.newcore.impl.test.unit;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.jboss.messaging.newcore.impl.RoundRobinDistributionPolicy;
+import org.jboss.messaging.newcore.impl.test.unit.fakes.FakeConsumer;
+import org.jboss.messaging.newcore.intf.Consumer;
+import org.jboss.messaging.newcore.intf.DistributionPolicy;
+import org.jboss.messaging.test.unit.UnitTestCase;
+
+/**
+ *
+ * A RoundRobinDistributionPolicyTest
+ *
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ *
+ */
+public class RoundRobinDistributionPolicyTest extends UnitTestCase
+{
+
+ public void testNoConsumers()
+ {
+ List<Consumer> consumers = new ArrayList<Consumer>();
+
+ DistributionPolicy dp = new RoundRobinDistributionPolicy();
+
+ int pos = dp.select(consumers, -1);
+
+ assertEquals(0, pos);
+ }
+
+ public void testConsumers()
+ {
+ List<Consumer> consumers = new ArrayList<Consumer>();
+
+ consumers.add(new FakeConsumer());
+ consumers.add(new FakeConsumer());
+ consumers.add(new FakeConsumer());
+
+ DistributionPolicy dp = new RoundRobinDistributionPolicy();
+
+ int pos = -1;
+
+ pos = dp.select(consumers, pos);
+
+ assertEquals(0, pos);
+
+ pos = dp.select(consumers, pos);
+
+ assertEquals(1, pos);
+
+ pos = dp.select(consumers, pos);
+
+ assertEquals(2, pos);
+
+ pos = dp.select(consumers, pos);
+
+ assertEquals(0, pos);
+
+ pos = dp.select(consumers, pos);
+
+ assertEquals(1, pos);
+
+ pos = dp.select(consumers, pos);
+
+ assertEquals(2, pos);
+
+ pos = dp.select(consumers, pos);
+
+ assertEquals(0, pos);
+ }
+
+}
More information about the jboss-cvs-commits
mailing list