[jboss-cvs] JBoss Messaging SVN: r3518 - in trunk: src/main/org/jboss/messaging and 20 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Tue Dec 18 12:45:47 EST 2007
Author: timfox
Date: 2007-12-18 12:45:46 -0500 (Tue, 18 Dec 2007)
New Revision: 3518
Added:
trunk/tests/src/org/jboss/messaging/newcore/
trunk/tests/src/org/jboss/messaging/newcore/impl/
trunk/tests/src/org/jboss/messaging/newcore/impl/bdbje/
trunk/tests/src/org/jboss/messaging/newcore/impl/bdbje/integration/
trunk/tests/src/org/jboss/messaging/newcore/impl/bdbje/integration/test/
trunk/tests/src/org/jboss/messaging/newcore/impl/bdbje/integration/test/unit/
trunk/tests/src/org/jboss/messaging/newcore/impl/bdbje/integration/test/unit/BDBSpeedTest.java
trunk/tests/src/org/jboss/messaging/newcore/impl/bdbje/integration/test/unit/RealBDBJEEnvironmentTest.java
trunk/tests/src/org/jboss/messaging/newcore/impl/bdbje/test/
trunk/tests/src/org/jboss/messaging/newcore/impl/bdbje/test/unit/
trunk/tests/src/org/jboss/messaging/newcore/impl/bdbje/test/unit/BDBJEEnvironmentTestBase.java
trunk/tests/src/org/jboss/messaging/newcore/impl/bdbje/test/unit/BDBJEPersistenceManagerTest.java
trunk/tests/src/org/jboss/messaging/newcore/impl/bdbje/test/unit/FakeBDBJEEnvironmentTest.java
trunk/tests/src/org/jboss/messaging/newcore/impl/bdbje/test/unit/fakes/
trunk/tests/src/org/jboss/messaging/newcore/impl/bdbje/test/unit/fakes/FakeBDBJEEnvironment.java
trunk/tests/src/org/jboss/messaging/newcore/impl/test/
trunk/tests/src/org/jboss/messaging/newcore/impl/test/concurrent/
trunk/tests/src/org/jboss/messaging/newcore/impl/test/concurrent/QueueTest.java
trunk/tests/src/org/jboss/messaging/newcore/impl/test/timing/
trunk/tests/src/org/jboss/messaging/newcore/impl/test/timing/QueueTest.java
trunk/tests/src/org/jboss/messaging/newcore/impl/test/unit/
trunk/tests/src/org/jboss/messaging/newcore/impl/test/unit/MessageTest.java
trunk/tests/src/org/jboss/messaging/newcore/impl/test/unit/QueueTest.java
trunk/tests/src/org/jboss/messaging/newcore/impl/test/unit/RoundRobinDistributionPolicyTest.java
trunk/tests/src/org/jboss/messaging/newcore/impl/test/unit/TransactionTest.java
trunk/tests/src/org/jboss/messaging/newcore/impl/test/unit/fakes/
trunk/tests/src/org/jboss/messaging/newcore/impl/test/unit/fakes/FakeConsumer.java
trunk/tests/src/org/jboss/messaging/newcore/impl/test/unit/fakes/FakeFilter.java
trunk/tests/src/org/jboss/messaging/test/
trunk/tests/src/org/jboss/messaging/test/unit/
trunk/tests/src/org/jboss/messaging/test/unit/UnitTestCase.java
Removed:
trunk/src/main/org/jboss/messaging/newcore/impl/bdbje/integration/test/unit/BDBSpeedTest.java
trunk/src/main/org/jboss/messaging/newcore/impl/bdbje/integration/test/unit/RealBDBJEEnvironmentTest.java
trunk/src/main/org/jboss/messaging/newcore/impl/bdbje/test/
trunk/src/main/org/jboss/messaging/newcore/impl/test/
trunk/src/main/org/jboss/messaging/test/
Modified:
trunk/build-messaging.xml
Log:
Moved new core tests into tests src tree
Modified: trunk/build-messaging.xml
===================================================================
--- trunk/build-messaging.xml 2007-12-18 16:56:43 UTC (rev 3517)
+++ trunk/build-messaging.xml 2007-12-18 17:45:46 UTC (rev 3518)
@@ -184,6 +184,7 @@
<path id="compilation.classpath">
<path refid="external.dependencies.classpath"/>
<path refid="jboss.dependencies.classpath"/>
+ <pathelement location="${project.root}/lib/je-3.2.44.jar"/>
</path>
<!--
@@ -269,8 +270,6 @@
failonerror="${javac.fail.onerror}">
<src path="${source.java}"/>
<include name="**/*.java"/>
- <exclude name="**/test/**"/>
- <exclude name="**/newcore/**"/>
<classpath refid="compilation.classpath"/>
</javac>
Deleted: trunk/src/main/org/jboss/messaging/newcore/impl/bdbje/integration/test/unit/BDBSpeedTest.java
===================================================================
--- trunk/src/main/org/jboss/messaging/newcore/impl/bdbje/integration/test/unit/BDBSpeedTest.java 2007-12-18 16:56:43 UTC (rev 3517)
+++ trunk/src/main/org/jboss/messaging/newcore/impl/bdbje/integration/test/unit/BDBSpeedTest.java 2007-12-18 17:45:46 UTC (rev 3518)
@@ -1,660 +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.integration.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.Message;
-import org.jboss.messaging.newcore.MessageReference;
-import org.jboss.messaging.newcore.Queue;
-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.test.unit.UnitTestCase;
-
-/**
- *
- * A BDBSpeedTest
- *
- * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
- *
- */
-public class BDBSpeedTest extends UnitTestCase
-{
-
- public void test1()
- {
- }
-
-// 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: trunk/src/main/org/jboss/messaging/newcore/impl/bdbje/integration/test/unit/RealBDBJEEnvironmentTest.java
===================================================================
--- trunk/src/main/org/jboss/messaging/newcore/impl/bdbje/integration/test/unit/RealBDBJEEnvironmentTest.java 2007-12-18 16:56:43 UTC (rev 3517)
+++ trunk/src/main/org/jboss/messaging/newcore/impl/bdbje/integration/test/unit/RealBDBJEEnvironmentTest.java 2007-12-18 17:45:46 UTC (rev 3518)
@@ -1,70 +0,0 @@
-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();
- }
-
- protected void copyEnvironment() throws Exception
- {
- File envCopyDir = new File(ENV_COPY_DIR);
-
- deleteDirectory(envCopyDir);
-
- copyRecursive(new File(ENV_DIR), envCopyDir);
- }
-
- protected void copyBackEnvironment() throws Exception
- {
- File envDir = new File(ENV_DIR);
-
- File envCopyDir = new File(ENV_COPY_DIR);
-
- deleteDirectory(envDir);
-
- copyRecursive(envCopyDir, envDir);
-
- deleteDirectory(envCopyDir);
- }
-}
Copied: trunk/tests/src/org/jboss/messaging/newcore/impl/bdbje/integration/test/unit/BDBSpeedTest.java (from rev 3516, trunk/src/main/org/jboss/messaging/newcore/impl/bdbje/integration/test/unit/BDBSpeedTest.java)
===================================================================
--- trunk/tests/src/org/jboss/messaging/newcore/impl/bdbje/integration/test/unit/BDBSpeedTest.java (rev 0)
+++ trunk/tests/src/org/jboss/messaging/newcore/impl/bdbje/integration/test/unit/BDBSpeedTest.java 2007-12-18 17:45:46 UTC (rev 3518)
@@ -0,0 +1,660 @@
+/*
+ * 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.integration.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.Message;
+import org.jboss.messaging.newcore.MessageReference;
+import org.jboss.messaging.newcore.Queue;
+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.test.unit.UnitTestCase;
+
+/**
+ *
+ * A BDBSpeedTest
+ *
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ *
+ */
+public class BDBSpeedTest extends UnitTestCase
+{
+
+ public void test1()
+ {
+ }
+
+// 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: trunk/tests/src/org/jboss/messaging/newcore/impl/bdbje/integration/test/unit/RealBDBJEEnvironmentTest.java (from rev 3516, trunk/src/main/org/jboss/messaging/newcore/impl/bdbje/integration/test/unit/RealBDBJEEnvironmentTest.java)
===================================================================
--- trunk/tests/src/org/jboss/messaging/newcore/impl/bdbje/integration/test/unit/RealBDBJEEnvironmentTest.java (rev 0)
+++ trunk/tests/src/org/jboss/messaging/newcore/impl/bdbje/integration/test/unit/RealBDBJEEnvironmentTest.java 2007-12-18 17:45:46 UTC (rev 3518)
@@ -0,0 +1,87 @@
+package org.jboss.messaging.newcore.impl.bdbje.integration.test.unit;
+
+import java.io.File;
+import java.io.FileOutputStream;
+
+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();
+ }
+
+ protected void copyEnvironment() throws Exception
+ {
+ File envDir = new File(ENV_DIR);
+
+ File envCopyDir = new File(ENV_COPY_DIR);
+
+ deleteDirectory(envCopyDir);
+
+ copyRecursive(envDir, envCopyDir);
+
+ deleteDirectory(envDir);
+
+ //Need to sync
+
+ FileOutputStream fos = new FileOutputStream(ENV_COPY_DIR + "/je.lck");
+
+ fos.getFD().sync();
+ }
+
+ protected void copyBackEnvironment() throws Exception
+ {
+ File envDir = new File(ENV_DIR);
+
+ File envCopyDir = new File(ENV_COPY_DIR);
+
+ deleteDirectory(envDir);
+
+ copyRecursive(envCopyDir, envDir);
+
+ deleteDirectory(envCopyDir);
+
+ //Need to sync
+
+ FileOutputStream fos = new FileOutputStream(ENV_DIR + "/je.lck");
+
+ fos.getFD().sync();
+ }
+}
Added: trunk/tests/src/org/jboss/messaging/newcore/impl/bdbje/test/unit/BDBJEEnvironmentTestBase.java
===================================================================
--- trunk/tests/src/org/jboss/messaging/newcore/impl/bdbje/test/unit/BDBJEEnvironmentTestBase.java (rev 0)
+++ trunk/tests/src/org/jboss/messaging/newcore/impl/bdbje/test/unit/BDBJEEnvironmentTestBase.java 2007-12-18 17:45:46 UTC (rev 3518)
@@ -0,0 +1,1754 @@
+/*
+ * 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.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 ENV_COPY_DIR = "./test-bdb-environment-copy";
+
+ protected static final String DB_NAME = "test-db";
+
+ @Override
+ protected void setUp() throws Exception
+ {
+ super.setUp();
+
+ env = createEnvironment();
+
+ env.setEnvironmentPath(ENV_DIR);
+
+ env.start();
+
+ database = env.getDatabase(DB_NAME);
+ }
+
+ protected abstract void createDir(String path);
+
+ protected void tearDown() throws Exception
+ {
+ super.tearDown();
+
+ database.close();
+
+ env.stop();
+ }
+
+ protected abstract BDBJEEnvironment createEnvironment() throws Exception;
+
+ protected abstract void copyEnvironment() throws Exception;
+
+ protected abstract void copyBackEnvironment() 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, 2, 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, 3, 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));
+
+ env.commit(xid1);
+
+ env.commit(xid2);
+
+ env.commit(xid3);
+
+ }
+
+ // Commented out until http://jira.jboss.org/jira/browse/JBMESSAGING-1192 is complete
+// 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, 2, 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, 3, 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));
+//
+// //Now to simulate a crash we copy the environment directory from under BDB, then close
+// //BDB. Then start it again with the copied config.
+// //We can't just stop the environment and restart it since on stopping cleanly BDB will abort any open
+// //transactions
+//
+// copyEnvironment();
+//
+// database.close();
+//
+// env.stop();
+//
+// copyBackEnvironment();
+//
+// 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();
+
+ //This will abort the transaction
+ 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());
+ }
+}
Added: trunk/tests/src/org/jboss/messaging/newcore/impl/bdbje/test/unit/BDBJEPersistenceManagerTest.java
===================================================================
--- trunk/tests/src/org/jboss/messaging/newcore/impl/bdbje/test/unit/BDBJEPersistenceManagerTest.java (rev 0)
+++ trunk/tests/src/org/jboss/messaging/newcore/impl/bdbje/test/unit/BDBJEPersistenceManagerTest.java 2007-12-18 17:45:46 UTC (rev 3518)
@@ -0,0 +1,808 @@
+/*
+ * 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.Message;
+import org.jboss.messaging.newcore.MessageReference;
+import org.jboss.messaging.newcore.Queue;
+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.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);
+
+ try
+ {
+ pm.addMessage(null);
+
+ fail("Should throw exception");
+ }
+ catch (NullPointerException ok)
+ {
+ //ok
+ }
+ }
+
+ 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();
+
+ try
+ {
+ pm.deleteReference(null);
+
+ fail("Should throw exception");
+ }
+ catch (NullPointerException ok)
+ {
+ //ok
+ }
+ }
+
+ 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 ---------------------------------------------------------------
+
+}
Added: trunk/tests/src/org/jboss/messaging/newcore/impl/bdbje/test/unit/FakeBDBJEEnvironmentTest.java
===================================================================
--- trunk/tests/src/org/jboss/messaging/newcore/impl/bdbje/test/unit/FakeBDBJEEnvironmentTest.java (rev 0)
+++ trunk/tests/src/org/jboss/messaging/newcore/impl/bdbje/test/unit/FakeBDBJEEnvironmentTest.java 2007-12-18 17:45:46 UTC (rev 3518)
@@ -0,0 +1,33 @@
+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)
+ {
+ }
+
+ protected void copyEnvironment()
+ {
+ }
+
+ protected void copyBackEnvironment()
+ {
+ }
+}
Added: trunk/tests/src/org/jboss/messaging/newcore/impl/bdbje/test/unit/fakes/FakeBDBJEEnvironment.java
===================================================================
--- trunk/tests/src/org/jboss/messaging/newcore/impl/bdbje/test/unit/fakes/FakeBDBJEEnvironment.java (rev 0)
+++ trunk/tests/src/org/jboss/messaging/newcore/impl/bdbje/test/unit/fakes/FakeBDBJEEnvironment.java 2007-12-18 17:45:46 UTC (rev 3518)
@@ -0,0 +1,533 @@
+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
+ {
+ checkStartWork(xid);
+
+ implicitTXs.put(Thread.currentThread(), createTransactionInternal(xid));
+ }
+
+ public void endWork(Xid xid, boolean fail) throws Exception
+ {
+ checkEndWork(xid);
+
+ implicitTXs.remove(Thread.currentThread());
+ }
+
+ public void prepare(Xid xid) throws Exception
+ {
+ FakeBDBJETransaction tx = findTXForXid(xid);
+
+ checkPrepare(tx);
+
+ tx.prepare();
+ }
+
+ public void commit(Xid xid) throws Exception
+ {
+ FakeBDBJETransaction tx = findTXForXid(xid);
+
+ checkCommitRollback(tx);
+
+ tx.commit();
+ }
+
+ public void rollback(Xid xid) throws Exception
+ {
+ FakeBDBJETransaction tx = findTXForXid(xid);
+
+ checkCommitRollback(tx);
+
+ tx.rollback();
+ }
+
+ // Private -----------------------------------------------------------------------
+
+ private FakeBDBJETransaction createTransactionInternal(Xid xid) throws Exception
+ {
+ FakeBDBJETransaction tx = new FakeBDBJETransaction(xid);
+
+ transactions.put(tx.id, tx);
+
+ return tx;
+ }
+
+ private FakeBDBJETransaction findTXForXid(Xid xid)
+ {
+ for (FakeBDBJETransaction tx: transactions.values())
+ {
+ if (xid.equals(tx.xid))
+ {
+ return tx;
+ }
+ }
+ return null;
+ }
+
+ private void checkStartWork(Xid xid)
+ {
+ if (implicitTXs.get(Thread.currentThread()) != null)
+ {
+ throw new IllegalStateException("Already implicit transaction");
+ }
+
+ if (findTXForXid(xid) != null)
+ {
+ throw new IllegalStateException("Already tx for xid");
+ }
+ }
+
+ private void checkEndWork(Xid xid)
+ {
+ FakeBDBJETransaction tx = implicitTXs.get(Thread.currentThread());
+
+ if (tx == null)
+ {
+ throw new IllegalStateException("No implicit tx");
+ }
+
+ if (!tx.xid.equals(xid))
+ {
+ throw new IllegalStateException("Wrong xid");
+ }
+ }
+
+
+ private void checkPrepare(FakeBDBJETransaction tx)
+ {
+ if (tx == null)
+ {
+ throw new IllegalStateException("Cannot find tx for xid");
+ }
+
+ if (implicitTXs.containsKey(Thread.currentThread()))
+ {
+ throw new IllegalStateException("Work not ended");
+ }
+ }
+
+
+ private void checkCommitRollback(FakeBDBJETransaction tx)
+ {
+ if (tx == null)
+ {
+ throw new IllegalStateException("Cannot find tx for xid");
+ }
+
+ if (!tx.prepared)
+ {
+ throw new IllegalStateException("Tx not prepared");
+ }
+
+ if (implicitTXs.containsKey(Thread.currentThread()))
+ {
+ throw new IllegalStateException("Work not ended");
+ }
+ }
+
+
+ // 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: trunk/tests/src/org/jboss/messaging/newcore/impl/test/concurrent/QueueTest.java
===================================================================
--- trunk/tests/src/org/jboss/messaging/newcore/impl/test/concurrent/QueueTest.java (rev 0)
+++ trunk/tests/src/org/jboss/messaging/newcore/impl/test/concurrent/QueueTest.java 2007-12-18 17:45:46 UTC (rev 3518)
@@ -0,0 +1,204 @@
+/*
+ * 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.test.concurrent;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.jboss.messaging.newcore.HandleStatus;
+import org.jboss.messaging.newcore.Message;
+import org.jboss.messaging.newcore.MessageReference;
+import org.jboss.messaging.newcore.Queue;
+import org.jboss.messaging.newcore.impl.QueueImpl;
+import org.jboss.messaging.newcore.impl.test.unit.fakes.FakeConsumer;
+import org.jboss.messaging.test.unit.UnitTestCase;
+
+/**
+ *
+ * A concurrent QueueTest
+ *
+ * All the concurrent queue tests go in here
+ *
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ *
+ */
+public class QueueTest extends UnitTestCase
+{
+ // The tests ----------------------------------------------------------------
+
+ /*
+ * Concurrent set consumer not busy, busy then, call deliver while messages are being added and consumed
+ */
+ public void testConcurrentAddsDeliver() throws Exception
+ {
+ Queue queue = new QueueImpl(1);
+
+ FakeConsumer consumer = new FakeConsumer();
+
+ queue.addConsumer(consumer);
+
+ final long testTime = 5000;
+
+ Sender sender = new Sender(queue, testTime);
+
+ Toggler toggler = new Toggler(queue, consumer, testTime);
+
+ sender.start();
+
+ toggler.start();
+
+ sender.join();
+
+ toggler.join();
+
+ consumer.setStatusImmediate(HandleStatus.HANDLED);
+
+ queue.deliver();
+
+ if (sender.getException() != null)
+ {
+ throw sender.getException();
+ }
+
+ if (toggler.getException() != null)
+ {
+ throw toggler.getException();
+ }
+
+ assertRefListsIdenticalRefs(sender.getReferences(), consumer.getReferences());
+
+ System.out.println("num refs: " + sender.getReferences().size());
+
+ System.out.println("num toggles: " + toggler.getNumToggles());
+
+ }
+
+ // Inner classes ---------------------------------------------------------------
+
+ class Sender extends Thread
+ {
+ private volatile Exception e;
+
+ private Queue queue;
+
+ private long testTime;
+
+ private volatile int i;
+
+ public Exception getException()
+ {
+ return e;
+ }
+
+ private List<MessageReference> refs = new ArrayList<MessageReference>();
+
+ public List<MessageReference> getReferences()
+ {
+ return refs;
+ }
+
+ Sender(Queue queue, long testTime)
+ {
+ this.testTime = testTime;
+
+ this.queue = queue;
+ }
+
+ public void run()
+ {
+ long start = System.currentTimeMillis();
+
+ while (System.currentTimeMillis() - start < testTime)
+ {
+ Message message = generateMessage(i);
+
+ MessageReference ref = message.createReference(queue);
+
+ queue.addLast(ref);
+
+ refs.add(ref);
+
+ i++;
+ }
+ }
+ }
+
+ class Toggler extends Thread
+ {
+ private volatile Exception e;
+
+ private Queue queue;
+
+ private FakeConsumer consumer;
+
+ private long testTime;
+
+ private boolean toggle;
+
+ private volatile int numToggles;
+
+ public int getNumToggles()
+ {
+ return numToggles;
+ }
+
+ public Exception getException()
+ {
+ return e;
+ }
+
+ Toggler(Queue queue, FakeConsumer consumer, long testTime)
+ {
+ this.testTime = testTime;
+
+ this.queue = queue;
+
+ this.consumer = consumer;
+ }
+
+ public void run()
+ {
+ long start = System.currentTimeMillis();
+
+ while (System.currentTimeMillis() - start < testTime)
+ {
+ if (toggle)
+ {
+ consumer.setStatusImmediate(HandleStatus.BUSY);
+ }
+ else
+ {
+ consumer.setStatusImmediate(HandleStatus.HANDLED);
+
+ queue.deliver();
+ }
+ toggle = !toggle;
+
+ numToggles++;
+ }
+ }
+ }
+
+}
+
+
+
Added: trunk/tests/src/org/jboss/messaging/newcore/impl/test/timing/QueueTest.java
===================================================================
--- trunk/tests/src/org/jboss/messaging/newcore/impl/test/timing/QueueTest.java (rev 0)
+++ trunk/tests/src/org/jboss/messaging/newcore/impl/test/timing/QueueTest.java 2007-12-18 17:45:46 UTC (rev 3518)
@@ -0,0 +1,225 @@
+/*
+ * 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.test.timing;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.jboss.messaging.newcore.MessageReference;
+import org.jboss.messaging.newcore.Queue;
+import org.jboss.messaging.newcore.impl.QueueImpl;
+import org.jboss.messaging.newcore.impl.test.unit.fakes.FakeConsumer;
+import org.jboss.messaging.test.unit.UnitTestCase;
+
+/**
+ *
+ * A timing-sensitive QueueTest
+ *
+ * All the queue tests which are timing sensitive - go in here
+ *
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ *
+ */
+public class QueueTest extends UnitTestCase
+{
+ private static final long TIMEOUT = 10000;
+
+ // The tests ----------------------------------------------------------------
+
+ public void testScheduledDirect()
+ {
+ testScheduled(true);
+ }
+
+ public void testScheduledQueueing()
+ {
+ testScheduled(false);
+ }
+
+ public void testScheduledNoConsumer() throws Exception
+ {
+ Queue queue = new QueueImpl(1);
+
+ //Send one scheduled
+
+ long now = System.currentTimeMillis();
+
+ MessageReference ref1 = generateReference(queue, 1);
+ ref1.setScheduledDeliveryTime(now + 7000);
+ queue.addLast(ref1);
+
+ //Send some non scheduled messages
+
+ MessageReference ref2 = generateReference(queue, 2);
+ queue.addLast(ref2);
+ MessageReference ref3 = generateReference(queue, 3);
+ queue.addLast(ref3);
+ MessageReference ref4 = generateReference(queue, 4);
+ queue.addLast(ref4);
+
+
+ //Now send some more scheduled messages
+
+ MessageReference ref5 = generateReference(queue, 5);
+ ref5.setScheduledDeliveryTime(now + 5000);
+ queue.addLast(ref5);
+
+ MessageReference ref6 = generateReference(queue, 6);
+ ref6.setScheduledDeliveryTime(now + 4000);
+ queue.addLast(ref6);
+
+ MessageReference ref7 = generateReference(queue, 7);
+ ref7.setScheduledDeliveryTime(now + 3000);
+ queue.addLast(ref7);
+
+ MessageReference ref8 = generateReference(queue, 8);
+ ref8.setScheduledDeliveryTime(now + 6000);
+ queue.addLast(ref8);
+
+ List<MessageReference> refs = new ArrayList<MessageReference>();
+
+ //Scheduled refs are added back to *FRONT* of queue - otherwise if there were many messages in the queue
+ //They may get stranded behind a big backlog
+
+ refs.add(ref1);
+ refs.add(ref8);
+ refs.add(ref5);
+ refs.add(ref6);
+ refs.add(ref7);
+
+ refs.add(ref2);
+ refs.add(ref3);
+ refs.add(ref4);
+
+ Thread.sleep(7500);
+
+ FakeConsumer consumer = new FakeConsumer();
+
+ queue.addConsumer(consumer);
+
+ queue.deliver();
+
+ assertRefListsIdenticalRefs(refs, consumer.getReferences());
+ }
+
+ private void testScheduled(boolean direct)
+ {
+ Queue queue = new QueueImpl(1);
+
+ FakeConsumer consumer = null;
+
+ if (direct)
+ {
+ consumer = new FakeConsumer();
+
+ queue.addConsumer(consumer);
+ }
+
+ //Send one scheduled
+
+ long now = System.currentTimeMillis();
+
+ MessageReference ref1 = generateReference(queue, 1);
+ ref1.setScheduledDeliveryTime(now + 7000);
+ queue.addLast(ref1);
+
+ //Send some non scheduled messages
+
+ MessageReference ref2 = generateReference(queue, 2);
+ queue.addLast(ref2);
+ MessageReference ref3 = generateReference(queue, 3);
+ queue.addLast(ref3);
+ MessageReference ref4 = generateReference(queue, 4);
+ queue.addLast(ref4);
+
+
+ //Now send some more scheduled messages
+
+ MessageReference ref5 = generateReference(queue, 5);
+ ref5.setScheduledDeliveryTime(now + 5000);
+ queue.addLast(ref5);
+
+ MessageReference ref6 = generateReference(queue, 6);
+ ref6.setScheduledDeliveryTime(now + 4000);
+ queue.addLast(ref6);
+
+ MessageReference ref7 = generateReference(queue, 7);
+ ref7.setScheduledDeliveryTime(now + 3000);
+ queue.addLast(ref7);
+
+ MessageReference ref8 = generateReference(queue, 8);
+ ref8.setScheduledDeliveryTime(now + 6000);
+ queue.addLast(ref8);
+
+ if (!direct)
+ {
+ consumer = new FakeConsumer();
+
+ queue.addConsumer(consumer);
+
+ queue.deliver();
+ }
+
+ List<MessageReference> refs = new ArrayList<MessageReference>();
+
+ refs.add(ref2);
+ refs.add(ref3);
+ refs.add(ref4);
+
+ assertRefListsIdenticalRefs(refs, consumer.getReferences());
+
+ refs.clear();
+ consumer.getReferences().clear();
+
+
+ MessageReference ref = consumer.waitForNextReference(TIMEOUT);
+ assertEquals(ref7, ref);
+ long now2 = System.currentTimeMillis();
+ assertTrue(now2 - now >= 3000);
+
+ ref = consumer.waitForNextReference(TIMEOUT);
+ assertEquals(ref6, ref);
+ now2 = System.currentTimeMillis();
+ assertTrue(now2 - now >= 4000);
+
+ ref = consumer.waitForNextReference(TIMEOUT);
+ assertEquals(ref5, ref);
+ now2 = System.currentTimeMillis();
+ assertTrue(now2 - now >= 5000);
+
+ ref = consumer.waitForNextReference(TIMEOUT);
+ assertEquals(ref8, ref);
+ now2 = System.currentTimeMillis();
+ assertTrue(now2 - now >= 6000);
+
+ ref = consumer.waitForNextReference(TIMEOUT);
+ assertEquals(ref1, ref);
+ now2 = System.currentTimeMillis();
+ assertTrue(now2 - now >= 7000);
+
+ assertTrue(consumer.getReferences().isEmpty());
+ }
+
+ // Inner classes ---------------------------------------------------------------
+
+}
+
Added: trunk/tests/src/org/jboss/messaging/newcore/impl/test/unit/MessageTest.java
===================================================================
--- trunk/tests/src/org/jboss/messaging/newcore/impl/test/unit/MessageTest.java (rev 0)
+++ trunk/tests/src/org/jboss/messaging/newcore/impl/test/unit/MessageTest.java 2007-12-18 17:45:46 UTC (rev 3518)
@@ -0,0 +1,313 @@
+/*
+ * 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.test.unit;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.jboss.messaging.newcore.Message;
+import org.jboss.messaging.newcore.MessageReference;
+import org.jboss.messaging.newcore.Queue;
+import org.jboss.messaging.newcore.impl.MessageImpl;
+import org.jboss.messaging.newcore.impl.QueueImpl;
+import org.jboss.messaging.test.unit.UnitTestCase;
+
+/**
+ *
+ * Tests for Message and MessageReference
+ *
+ * TODO - Test streaming and destreaming
+ *
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ *
+ */
+public class MessageTest extends UnitTestCase
+{
+ public void testCreateMessage()
+ {
+ long id = 56465;
+ int type = 655;
+ boolean reliable = true;
+ long expiration = 6712671;
+ long timestamp = 82798172;
+ byte priority = 32;
+
+ Message message = new MessageImpl(id, type, reliable, expiration, timestamp, priority);
+
+ assertEquals(id, message.getMessageID());
+ assertEquals(type, message.getType());
+ assertEquals(reliable, message.isReliable());
+ assertEquals(timestamp, message.getTimestamp());
+ assertEquals(priority, message.getPriority());
+
+ reliable = false;
+
+ message = new MessageImpl(id, type, reliable, expiration, timestamp, priority);
+
+ assertEquals(id, message.getMessageID());
+ assertEquals(type, message.getType());
+ assertEquals(reliable, message.isReliable());
+ assertEquals(timestamp, message.getTimestamp());
+ assertEquals(priority, message.getPriority());
+ }
+
+ public void testCreateMessageFromStorage() throws Exception
+ {
+ long id = 56465;
+ int type = 655;
+ boolean reliable = true;
+ long expiration = 6712671;
+ long timestamp = 82798172;
+ byte priority = 32;
+
+ byte[] bytes = "blah blah blah".getBytes();
+
+ Message message = new MessageImpl(id, type, reliable, expiration, timestamp, priority,
+ null, bytes);
+
+ assertEquals(id, message.getMessageID());
+ assertEquals(type, message.getType());
+ assertEquals(reliable, message.isReliable());
+ assertEquals(timestamp, message.getTimestamp());
+ assertEquals(priority, message.getPriority());
+
+ assertByteArraysEquivalent(bytes, message.getPayload());
+
+ //TODO - headers - they should really be combined into single blob
+ }
+
+ public void testCopy()
+ {
+ long id = 56465;
+ int type = 655;
+ boolean reliable = true;
+ long expiration = 6712671;
+ long timestamp = 82798172;
+ byte priority = 32;
+
+ Message message = new MessageImpl(id, type, reliable, expiration, timestamp, priority);
+
+ Message message2 = message.copy();
+
+ assertEquivalent(message, message2);
+ }
+
+ public void testSetAndGetMessageID()
+ {
+ Message message = new MessageImpl();
+
+ long id = 765432;
+ message.setMessageID(id);
+ assertEquals(id, message.getMessageID());
+ }
+
+ public void testSetAndGetReliable()
+ {
+ Message message = new MessageImpl();
+
+ boolean reliable = true;
+ message.setReliable(reliable);
+ assertEquals(reliable, message.isReliable());
+
+ reliable = false;
+ message.setReliable(reliable);
+ assertEquals(reliable, message.isReliable());
+ }
+
+ public void testSetAndGetExpiration()
+ {
+ Message message = new MessageImpl();
+
+ long expiration = System.currentTimeMillis() + 10000;
+ message.setExpiration(expiration);
+ assertEquals(expiration, message.getExpiration());
+ assertFalse(message.isExpired());
+ message.setExpiration(System.currentTimeMillis() - 1);
+ assertTrue(message.isExpired());
+
+ expiration = 0; //O means never expire
+ message.setExpiration(expiration);
+ assertEquals(expiration, message.getExpiration());
+ assertFalse(message.isExpired());
+ }
+
+ public void testSetAndGetTimestamp()
+ {
+ Message message = new MessageImpl();
+
+ long timestamp = System.currentTimeMillis();
+ message.setTimestamp(timestamp);
+ assertEquals(timestamp, message.getTimestamp());
+ }
+
+ public void testSetAndGetPriority()
+ {
+ Message message = new MessageImpl();
+ byte priority = 7;
+ message.setPriority(priority);
+ assertEquals(priority, message.getPriority());
+ }
+
+ public void testSetAndGetConnectionID()
+ {
+ Message message = new MessageImpl();
+
+ assertNull(message.getConnectionID());
+ String connectionID = "conn123";
+ message.setConnectionID(connectionID);
+ assertEquals(connectionID, message.getConnectionID());
+ }
+
+ public void testSetAndGetPayload()
+ {
+ Message message = new MessageImpl();
+
+ assertNull(message.getPayload());
+
+ byte[] bytes = "blah blah blah".getBytes();
+ message.setPayload(bytes);
+
+ assertByteArraysEquivalent(bytes, message.getPayload());
+ }
+
+ public void testHeaders()
+ {
+ Message message = new MessageImpl();
+
+ assertNotNull(message.getHeaders());
+ assertTrue(message.getHeaders().isEmpty());
+
+ String key1 = "key1";
+ String val1 = "wibble";
+ String key2 = "key2";
+ Object val2 = new Object();
+ String key3 = "key3";
+ Double val3 = new Double(123.456);
+ Long val4 = new Long(77777);
+ message.putHeader(key1, val1);
+ assertEquals(val1, message.getHeaders().get(key1));
+ assertEquals(1, message.getHeaders().size());
+ assertTrue(message.containsHeader(key1));
+ assertFalse(message.containsHeader("does not exist"));
+ message.putHeader(key2, val2);
+ assertEquals(val2, message.getHeaders().get(key2));
+ assertEquals(2, message.getHeaders().size());
+ assertTrue(message.containsHeader(key2));
+ message.putHeader(key3, val3);
+ assertEquals(val3, message.getHeaders().get(key3));
+ assertEquals(3, message.getHeaders().size());
+ assertTrue(message.containsHeader(key3));
+ message.putHeader(key3, val4);
+ assertEquals(val4, message.getHeaders().get(key3));
+ assertEquals(3, message.getHeaders().size());
+ assertEquals(val2, message.removeHeader(key2));
+ assertEquals(2, message.getHeaders().size());
+ assertFalse(message.containsHeader(key2));
+ assertNull(message.removeHeader("does not exist"));
+ assertEquals(val1, message.removeHeader(key1));
+ assertFalse(message.containsHeader(key2));
+ assertEquals(1, message.getHeaders().size());
+ assertEquals(val4, message.removeHeader(key3));
+ assertFalse(message.containsHeader(key3));
+ assertTrue(message.getHeaders().isEmpty());
+ }
+
+ public void testEquals()
+ {
+ Message message1 = new MessageImpl();
+ message1.setMessageID(1);
+
+ Message message2 = new MessageImpl();
+ message2.setMessageID(2);
+
+ Message message3 = new MessageImpl();
+ message3.setMessageID(1);
+
+ assertTrue(message1.equals(message1));
+ assertTrue(message2.equals(message2));
+ assertTrue(message3.equals(message3));
+
+ assertFalse(message1.equals(message2));
+ assertFalse(message2.equals(message1));
+
+ assertFalse(message2.equals(message3));
+ assertFalse(message3.equals(message2));
+
+ assertTrue(message1.equals(message3));
+ assertTrue(message3.equals(message1));
+
+ }
+
+ public void testHashcode()
+ {
+ long id1 = 6567575;
+ Message message1 = new MessageImpl();
+ message1.setMessageID(id1);
+
+ assertEquals((int) ((id1 >>> 32) ^ id1), message1.hashCode());
+ }
+
+ public void testMessageReference()
+ {
+ Message message = new MessageImpl();
+
+ assertTrue(message.getReferences().isEmpty());
+
+ Queue queue1 = new QueueImpl(1);
+ Queue queue2 = new QueueImpl(2);
+
+ List<MessageReference> refs = new ArrayList<MessageReference>();
+
+ MessageReference ref1 = message.createReference(queue1);
+ refs.add(ref1);
+ MessageReference ref2 = message.createReference(queue2);
+ refs.add(ref2);
+ MessageReference ref3 = message.createReference(queue1);
+ refs.add(ref3);
+ MessageReference ref4 = message.createReference(queue2);
+ refs.add(ref4);
+
+ assertRefListsIdenticalRefs(refs, message.getReferences());
+
+ assertEquals(queue1, ref1.getQueue());
+ assertEquals(queue2, ref2.getQueue());
+ assertEquals(queue1, ref3.getQueue());
+ assertEquals(queue2, ref4.getQueue());
+
+ int deliveryCount = 65235;
+ ref1.setDeliveryCount(deliveryCount);
+ assertEquals(deliveryCount, ref1.getDeliveryCount());
+
+ long scheduledDeliveryTime = 908298123;
+ ref1.setScheduledDeliveryTime(scheduledDeliveryTime);
+ assertEquals(scheduledDeliveryTime, ref1.getScheduledDeliveryTime());
+
+ Queue queue3 = new QueueImpl(3);
+ MessageReference ref5 = ref1.copy(queue3);
+
+ assertEquals(deliveryCount, ref5.getDeliveryCount());
+ assertEquals(scheduledDeliveryTime, ref5.getScheduledDeliveryTime());
+ assertEquals(queue3, ref5.getQueue());
+ }
+
+
+}
Added: trunk/tests/src/org/jboss/messaging/newcore/impl/test/unit/QueueTest.java
===================================================================
--- trunk/tests/src/org/jboss/messaging/newcore/impl/test/unit/QueueTest.java (rev 0)
+++ trunk/tests/src/org/jboss/messaging/newcore/impl/test/unit/QueueTest.java 2007-12-18 17:45:46 UTC (rev 3518)
@@ -0,0 +1,1080 @@
+/*
+ * 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.test.unit;
+
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.jboss.messaging.newcore.Consumer;
+import org.jboss.messaging.newcore.DistributionPolicy;
+import org.jboss.messaging.newcore.Filter;
+import org.jboss.messaging.newcore.HandleStatus;
+import org.jboss.messaging.newcore.MessageReference;
+import org.jboss.messaging.newcore.Queue;
+import org.jboss.messaging.newcore.impl.QueueImpl;
+import org.jboss.messaging.newcore.impl.RoundRobinDistributionPolicy;
+import org.jboss.messaging.newcore.impl.test.unit.fakes.FakeConsumer;
+import org.jboss.messaging.newcore.impl.test.unit.fakes.FakeFilter;
+import org.jboss.messaging.test.unit.UnitTestCase;
+
+/**
+ *
+ * A QueueTest
+ *
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ *
+ */
+public class QueueTest extends UnitTestCase
+{
+ // The tests ----------------------------------------------------------------
+
+ public void testID()
+ {
+ final long id = 123;
+
+ Queue queue = new QueueImpl(id);
+
+ assertEquals(id, queue.getID());
+ }
+
+ public void testGetSetMaxSize()
+ {
+ final int maxSize = 123456;
+
+ final int id = 123;
+
+ Queue queue = new QueueImpl(id, null, maxSize);
+
+ assertEquals(id, queue.getID());
+
+ assertEquals(maxSize, queue.getMaxSize());
+
+ final int maxSize2 = 654321;
+
+ queue.setMaxSize(maxSize2);
+
+ assertEquals(maxSize2, queue.getMaxSize());
+ }
+
+ public void testAddRemoveConsumer()
+ {
+ Consumer cons1 = new FakeConsumer();
+
+ Consumer cons2 = new FakeConsumer();
+
+ Consumer cons3 = new FakeConsumer();
+
+ Queue queue = new QueueImpl(1);
+
+ assertEquals(0, queue.getConsumerCount());
+
+ queue.addConsumer(cons1);
+
+ assertEquals(1, queue.getConsumerCount());
+
+ assertTrue(queue.removeConsumer(cons1));
+
+ assertEquals(0, queue.getConsumerCount());
+
+ queue.addConsumer(cons1);
+
+ queue.addConsumer(cons2);
+
+ queue.addConsumer(cons3);
+
+ assertEquals(3, queue.getConsumerCount());
+
+ assertFalse(queue.removeConsumer(new FakeConsumer()));
+
+ assertEquals(3, queue.getConsumerCount());
+
+ assertTrue(queue.removeConsumer(cons1));
+
+ assertEquals(2, queue.getConsumerCount());
+
+ assertTrue(queue.removeConsumer(cons2));
+
+ assertEquals(1, queue.getConsumerCount());
+
+ assertTrue(queue.removeConsumer(cons3));
+
+ assertEquals(0, queue.getConsumerCount());
+
+ assertFalse(queue.removeConsumer(cons3));
+ }
+
+ public void testGetSetDistributionPolicy()
+ {
+ Queue queue = new QueueImpl(1);
+
+ assertNotNull(queue.getDistributionPolicy());
+
+ assertTrue(queue.getDistributionPolicy() instanceof RoundRobinDistributionPolicy);
+
+ DistributionPolicy policy = new DummyDistributionPolicy();
+
+ queue.setDistributionPolicy(policy);
+
+ assertEquals(policy, queue.getDistributionPolicy());
+ }
+
+ public void testGetSetFilter()
+ {
+ Queue queue = new QueueImpl(1);
+
+ assertNull(queue.getFilter());
+
+ Filter filter = new FakeFilter();
+
+ queue.setFilter(filter);
+
+ assertEquals(filter, queue.getFilter());
+
+ queue = new QueueImpl(1, filter);
+
+ assertEquals(filter, queue.getFilter());
+ }
+
+ public void testDefaultMaxSize()
+ {
+ Queue queue = new QueueImpl(1);
+
+ assertEquals(-1, queue.getMaxSize());
+
+ queue = new QueueImpl(1, new FakeFilter());
+
+ assertEquals(-1, queue.getMaxSize());
+ }
+
+ public void testSimpleAddLast()
+ {
+ Queue queue = new QueueImpl(1);
+
+ final int numMessages = 10;
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ queue.addLast(ref);
+ }
+
+ assertEquals(numMessages, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+
+ }
+
+ public void testSimpleDirectDelivery()
+ {
+ Queue queue = new QueueImpl(1);
+
+ FakeConsumer consumer = new FakeConsumer();
+
+ queue.addConsumer(consumer);
+
+ final int numMessages = 10;
+
+ List<MessageReference> refs = new ArrayList<MessageReference>();
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ refs.add(ref);
+
+ queue.addLast(ref);
+ }
+
+ assertEquals(0, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+
+ assertRefListsIdenticalRefs(refs, consumer.getReferences());
+ }
+
+ public void testSimpleNonDirectDelivery()
+ {
+ Queue queue = new QueueImpl(1);
+
+ final int numMessages = 10;
+
+ List<MessageReference> refs = new ArrayList<MessageReference>();
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ refs.add(ref);
+
+ queue.addLast(ref);
+ }
+
+ assertEquals(10, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+
+ //Now add a consumer
+ FakeConsumer consumer = new FakeConsumer();
+
+ queue.addConsumer(consumer);
+
+ assertTrue(consumer.getReferences().isEmpty());
+ assertEquals(10, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+
+ queue.deliver();
+
+ assertRefListsIdenticalRefs(refs, consumer.getReferences());
+ assertEquals(0, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+ }
+
+ public void testBusyConsumer()
+ {
+ Queue queue = new QueueImpl(1);
+
+ FakeConsumer consumer = new FakeConsumer();
+
+ consumer.setStatusImmediate(HandleStatus.BUSY);
+
+ queue.addConsumer(consumer);
+
+ final int numMessages = 10;
+
+ List<MessageReference> refs = new ArrayList<MessageReference>();
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ refs.add(ref);
+
+ queue.addLast(ref);
+ }
+
+ assertEquals(10, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+
+ queue.deliver();
+
+ assertEquals(10, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+ assertTrue(consumer.getReferences().isEmpty());
+
+ consumer.setStatusImmediate(HandleStatus.HANDLED);
+
+ queue.deliver();
+
+ assertRefListsIdenticalRefs(refs, consumer.getReferences());
+ assertEquals(0, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+ }
+
+ public void testBusyConsumerThenAddMoreMessages()
+ {
+ Queue queue = new QueueImpl(1);
+
+ FakeConsumer consumer = new FakeConsumer();
+
+ consumer.setStatusImmediate(HandleStatus.BUSY);
+
+ queue.addConsumer(consumer);
+
+ final int numMessages = 10;
+
+ List<MessageReference> refs = new ArrayList<MessageReference>();
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ refs.add(ref);
+
+ queue.addLast(ref);
+ }
+
+ assertEquals(10, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+
+ queue.deliver();
+
+ assertEquals(10, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+ assertTrue(consumer.getReferences().isEmpty());
+
+ for (int i = numMessages; i < numMessages * 2; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ refs.add(ref);
+
+ queue.addLast(ref);
+ }
+
+ assertEquals(20, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+ assertTrue(consumer.getReferences().isEmpty());
+
+ consumer.setStatusImmediate(HandleStatus.HANDLED);
+
+ for (int i = numMessages * 2; i < numMessages * 3; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ refs.add(ref);
+
+ queue.addLast(ref);
+ }
+
+ queue.deliver();
+
+ assertRefListsIdenticalRefs(refs, consumer.getReferences());
+ assertEquals(0, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+ }
+
+ public void testAddFirstAddLast()
+ {
+ Queue queue = new QueueImpl(1);
+
+ final int numMessages = 10;
+
+ List<MessageReference> refs1 = new ArrayList<MessageReference>();
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ refs1.add(ref);
+
+ queue.addLast(ref);
+ }
+
+ LinkedList<MessageReference> refs2 = new LinkedList<MessageReference>();
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ MessageReference ref = generateReference(queue, i + numMessages);
+
+ refs2.addFirst(ref);
+
+ queue.addFirst(ref);
+ }
+
+ List<MessageReference> refs3 = new ArrayList<MessageReference>();
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ MessageReference ref = generateReference(queue, i + 2 * numMessages);
+
+ refs3.add(ref);
+
+ queue.addLast(ref);
+ }
+
+ FakeConsumer consumer = new FakeConsumer();
+
+ queue.addConsumer(consumer);
+
+ queue.deliver();
+
+ List<MessageReference> allRefs = new ArrayList<MessageReference>();
+
+ allRefs.addAll(refs2);
+ allRefs.addAll(refs1);
+ allRefs.addAll(refs3);
+
+ assertRefListsIdenticalRefs(allRefs, consumer.getReferences());
+ }
+
+
+ public void testChangeConsumersAndDeliver()
+ {
+ Queue queue = new QueueImpl(1);
+
+ final int numMessages = 10;
+
+ List<MessageReference> refs = new ArrayList<MessageReference>();
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ refs.add(ref);
+
+ queue.addLast(ref);
+ }
+
+ assertEquals(numMessages, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+
+ FakeConsumer cons1 = new FakeConsumer();
+
+ queue.addConsumer(cons1);
+
+ queue.deliver();
+
+ assertEquals(0, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+
+ assertRefListsIdenticalRefs(refs, cons1.getReferences());
+
+ FakeConsumer cons2 = new FakeConsumer();
+
+ queue.addConsumer(cons2);
+
+ assertEquals(2, queue.getConsumerCount());
+
+ cons1.getReferences().clear();
+
+ refs.clear();
+
+ for (int i = 0; i < 2 * numMessages; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ refs.add(ref);
+
+ queue.addLast(ref);
+ }
+
+ assertEquals(0, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+
+ assertEquals(numMessages, cons1.getReferences().size());
+
+ assertEquals(numMessages, cons2.getReferences().size());
+
+ cons1.getReferences().clear();
+ cons2.getReferences().clear();
+ refs.clear();
+
+ FakeConsumer cons3 = new FakeConsumer();
+
+ queue.addConsumer(cons3);
+
+ assertEquals(3, queue.getConsumerCount());
+
+ for (int i = 0; i < 3 * numMessages; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ refs.add(ref);
+
+ queue.addLast(ref);
+ }
+
+ assertEquals(0, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+
+ assertEquals(numMessages, cons1.getReferences().size());
+
+ assertEquals(numMessages, cons2.getReferences().size());
+
+ assertEquals(numMessages, cons3.getReferences().size());
+
+ queue.removeConsumer(cons1);
+
+ cons3.getReferences().clear();
+ cons2.getReferences().clear();
+ refs.clear();
+
+ for (int i = 0; i < 2 * numMessages; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ refs.add(ref);
+
+ queue.addLast(ref);
+ }
+
+ assertEquals(0, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+
+ assertEquals(numMessages, cons2.getReferences().size());
+
+ assertEquals(numMessages, cons3.getReferences().size());
+
+ queue.removeConsumer(cons3);
+
+ cons2.getReferences().clear();
+ refs.clear();
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ refs.add(ref);
+
+ queue.addLast(ref);
+ }
+
+ assertEquals(0, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+
+ assertEquals(numMessages, cons2.getReferences().size());
+
+ }
+
+ public void testRoundRobinWithQueueing()
+ {
+ Queue queue = new QueueImpl(1);
+
+ assertTrue(queue.getDistributionPolicy() instanceof RoundRobinDistributionPolicy);
+
+ final int numMessages = 10;
+
+ List<MessageReference> refs = new ArrayList<MessageReference>();
+
+ //Test first with queueing
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ refs.add(ref);
+
+ queue.addLast(ref);
+ }
+
+ FakeConsumer cons1 = new FakeConsumer();
+
+ FakeConsumer cons2 = new FakeConsumer();
+
+ queue.addConsumer(cons1);
+
+ queue.addConsumer(cons2);
+
+ queue.deliver();
+
+ assertEquals(numMessages / 2, cons1.getReferences().size());
+
+ assertEquals(numMessages / 2, cons2.getReferences().size());
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ MessageReference ref;
+
+ ref = (i % 2 == 0) ? cons1.getReferences().get(i / 2) : cons2.getReferences().get(i / 2);
+
+ assertEquals(refs.get(i), ref);
+ }
+ }
+
+ public void testRoundRobinDirect()
+ {
+ Queue queue = new QueueImpl(1);
+
+ assertTrue(queue.getDistributionPolicy() instanceof RoundRobinDistributionPolicy);
+
+ final int numMessages = 10;
+
+ List<MessageReference> refs = new ArrayList<MessageReference>();
+
+ FakeConsumer cons1 = new FakeConsumer();
+
+ FakeConsumer cons2 = new FakeConsumer();
+
+ queue.addConsumer(cons1);
+
+ queue.addConsumer(cons2);
+
+ queue.deliver();
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ refs.add(ref);
+
+ queue.addLast(ref);
+ }
+
+ assertEquals(numMessages / 2, cons1.getReferences().size());
+
+ assertEquals(numMessages / 2, cons2.getReferences().size());
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ MessageReference ref;
+
+ ref = (i % 2 == 0) ? cons1.getReferences().get(i / 2) : cons2.getReferences().get(i / 2);
+
+ assertEquals(refs.get(i), ref);
+ }
+ }
+
+ public void testRemoveAllReferences()
+ {
+ Queue queue = new QueueImpl(1);
+
+ final int numMessages = 10;
+
+ List<MessageReference> refs = new ArrayList<MessageReference>();
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ refs.add(ref);
+
+ queue.addLast(ref);
+ }
+
+ assertEquals(numMessages, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+
+ queue.removeAllReferences();
+
+ assertEquals(0, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+
+ FakeConsumer consumer = new FakeConsumer();
+
+ queue.addConsumer(consumer);
+
+ queue.deliver();
+
+ assertTrue(consumer.getReferences().isEmpty());
+ }
+
+ public void testMaxSize()
+ {
+ final int maxSize = 20;
+
+ Queue queue = new QueueImpl(1, null, maxSize);
+
+ List<MessageReference> refs = new ArrayList<MessageReference>();
+
+ for (int i = 0; i < maxSize; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ refs.add(ref);
+
+ assertEquals(HandleStatus.HANDLED, queue.addLast(ref));
+ }
+
+ assertEquals(maxSize, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+
+ //Try to add more
+
+ for (int i = 0; i < 10; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ assertEquals(HandleStatus.BUSY, queue.addLast(ref));
+ }
+
+ assertEquals(maxSize, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+
+ // Try to add at front too
+
+ for (int i = 0; i < 10; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ assertEquals(HandleStatus.BUSY, queue.addLast(ref));
+ }
+
+ //Increase the max size
+
+ queue.setMaxSize(2 * queue.getMaxSize());
+
+ for (int i = 0; i < maxSize; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ refs.add(ref);
+
+ assertEquals(HandleStatus.HANDLED, queue.addLast(ref));
+ }
+
+ assertEquals(maxSize * 2, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+
+ //Now try and decrease maxSize
+
+ try
+ {
+ queue.setMaxSize(maxSize);
+
+ fail("Should throw exception");
+ }
+ catch (IllegalArgumentException e)
+ {
+ //Ok
+ }
+
+ assertEquals(2 * maxSize, queue.getMaxSize());
+ }
+
+ public void testQueueWithFilter()
+ {
+ Filter filter = new FakeFilter("fruit", "orange");
+
+ Queue queue = new QueueImpl(1, filter);
+
+ FakeConsumer consumer = new FakeConsumer();
+
+ List<MessageReference> refs = new ArrayList<MessageReference>();
+
+ MessageReference ref1 = generateReference(queue, 1);
+
+ ref1.getMessage().putHeader("fruit", "banana");
+
+ assertEquals(HandleStatus.NO_MATCH, queue.addLast(ref1));
+
+ MessageReference ref2 = generateReference(queue, 2);
+
+ ref2.getMessage().putHeader("cheese", "stilton");
+
+ assertEquals(HandleStatus.NO_MATCH, queue.addLast(ref2));
+
+ MessageReference ref3 = generateReference(queue, 3);
+
+ ref3.getMessage().putHeader("cake", "sponge");
+
+ assertEquals(HandleStatus.NO_MATCH, queue.addLast(ref3));
+
+ MessageReference ref4 = generateReference(queue, 4);
+
+ ref4.getMessage().putHeader("fruit", "orange");
+
+ refs.add(ref4);
+
+ assertEquals(HandleStatus.HANDLED, queue.addLast(ref4));
+
+ MessageReference ref5 = generateReference(queue, 5);
+
+ ref5.getMessage().putHeader("fruit", "apple");
+
+ assertEquals(HandleStatus.NO_MATCH, queue.addLast(ref5));
+
+ MessageReference ref6 = generateReference(queue, 6);
+
+ ref6.getMessage().putHeader("fruit", "orange");
+
+ refs.add(ref6);
+
+ assertEquals(HandleStatus.HANDLED, queue.addLast(ref6));
+
+ //Add a few first
+
+ MessageReference ref7 = generateReference(queue, 7);
+
+ ref7.getMessage().putHeader("fruit", "banana");
+
+ assertEquals(HandleStatus.NO_MATCH, queue.addFirst(ref7));
+
+ MessageReference ref8 = generateReference(queue, 8);
+
+ ref8.getMessage().putHeader("fruit", "nectarine");
+
+ assertEquals(HandleStatus.NO_MATCH, queue.addFirst(ref8));
+
+ MessageReference ref9 = generateReference(queue, 9);
+
+ ref9.getMessage().putHeader("fruit", "orange");
+
+ assertEquals(HandleStatus.HANDLED, queue.addFirst(ref9));
+
+ List<MessageReference> newRefs = new ArrayList<MessageReference>();
+
+ newRefs.add(ref9);
+ newRefs.addAll(refs);
+
+ queue.setFilter(null);
+
+ MessageReference ref10 = generateReference(queue, 10);
+
+ ref10.getMessage().putHeader("sport", "skiing");
+
+ assertEquals(HandleStatus.HANDLED, queue.addLast(ref10));
+
+ newRefs.add(ref10);
+
+ assertEquals(4, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+
+ queue.addConsumer(consumer);
+
+ queue.deliver();
+
+ assertEquals(0, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+
+ assertRefListsIdenticalRefs(newRefs, consumer.getReferences());
+ }
+
+ public void testWithPriorities()
+ {
+ Queue queue = new QueueImpl(1);
+
+ final int numMessages = 10;
+
+ List<MessageReference> refs = new ArrayList<MessageReference>();
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ ref.getMessage().setPriority((byte)i);
+
+ refs.add(ref);
+
+ assertEquals(HandleStatus.HANDLED, queue.addLast(ref));
+ }
+
+ FakeConsumer consumer = new FakeConsumer();
+
+ queue.addConsumer(consumer);
+
+ queue.deliver();
+
+ List<MessageReference> receivedRefs = consumer.getReferences();
+
+ //Should be in reverse order
+
+ assertEquals(refs.size(), receivedRefs.size());
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ assertEquals(refs.get(i), receivedRefs.get(9 - i));
+ }
+
+ //But if we send more - since we are now in direct mode - the order will be the send order
+ //since the refs don't get queued
+
+ consumer.clearReferences();
+
+ refs.clear();
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ ref.getMessage().setPriority((byte)i);
+
+ refs.add(ref);
+
+ assertEquals(HandleStatus.HANDLED, queue.addLast(ref));
+ }
+
+ assertRefListsIdenticalRefs(refs, consumer.getReferences());
+ }
+
+ public void testConsumerWithFiltersDirect()
+ {
+ testConsumerWithFilters(true);
+ }
+
+ public void testConsumerWithFiltersQueueing()
+ {
+ testConsumerWithFilters(false);
+ }
+
+ public void testList()
+ {
+ Queue queue = new QueueImpl(1);
+
+ final int numMessages = 20;
+
+ List<MessageReference> refs = new ArrayList<MessageReference>();
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ queue.addLast(ref);
+
+ refs.add(ref);
+ }
+
+ assertEquals(numMessages, queue.getMessageCount());
+
+ List<MessageReference> list = queue.list(null);
+
+ assertRefListsIdenticalRefs(refs, list);
+ }
+
+ public void testListWithFilter()
+ {
+ Queue queue = new QueueImpl(1);
+
+ final int numMessages = 20;
+
+ List<MessageReference> refs = new ArrayList<MessageReference>();
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ if (i % 2 == 0)
+ {
+ ref.getMessage().putHeader("god", "dog");
+ }
+
+ queue.addLast(ref);
+
+ refs.add(ref);
+ }
+
+ assertEquals(numMessages, queue.getMessageCount());
+
+ Filter filter = new FakeFilter("god", "dog");
+
+ List<MessageReference> list = queue.list(filter);
+
+ assertEquals(numMessages / 2, list.size());
+
+ for (int i = 0; i < numMessages; i += 2)
+ {
+ assertEquals(refs.get(i), list.get(i / 2));
+ }
+ }
+
+ /*
+ public void testQuickSpeedTest()
+ {
+ Queue queue = new QueueImpl(1);
+
+ final int numMessages = 1000000;
+
+ FakeConsumer cons = new FakeConsumer();
+
+ queue.addConsumer(cons);
+
+ long start = System.currentTimeMillis();
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ MessageReference ref = this.generateReference(1);
+
+ queue.addLast(ref);
+ }
+
+ long end = System.currentTimeMillis();
+
+ double rate = 1000 * (double)numMessages / (end - start);
+
+ System.out.println("Rate: " + rate);
+
+ assertEquals(numMessages, cons.getReferences().size());
+ }
+ */
+
+ // Private ------------------------------------------------------------------------------
+
+ private void testConsumerWithFilters(boolean direct)
+ {
+ Queue queue = new QueueImpl(1);
+
+ Filter filter = new FakeFilter("fruit", "orange");
+
+ FakeConsumer consumer = new FakeConsumer(filter);
+
+ if (direct)
+ {
+ queue.addConsumer(consumer);
+ }
+
+ List<MessageReference> refs = new ArrayList<MessageReference>();
+
+ MessageReference ref1 = generateReference(queue, 1);
+
+ ref1.getMessage().putHeader("fruit", "banana");
+
+ assertEquals(HandleStatus.HANDLED, queue.addLast(ref1));
+
+ MessageReference ref2 = generateReference(queue, 2);
+
+ ref2.getMessage().putHeader("cheese", "stilton");
+
+ assertEquals(HandleStatus.HANDLED, queue.addLast(ref2));
+
+ MessageReference ref3 = generateReference(queue, 3);
+
+ ref3.getMessage().putHeader("cake", "sponge");
+
+ assertEquals(HandleStatus.HANDLED, queue.addLast(ref3));
+
+ MessageReference ref4 = generateReference(queue, 4);
+
+ ref4.getMessage().putHeader("fruit", "orange");
+
+ refs.add(ref4);
+
+ assertEquals(HandleStatus.HANDLED, queue.addLast(ref4));
+
+ MessageReference ref5 = generateReference(queue, 5);
+
+ ref5.getMessage().putHeader("fruit", "apple");
+
+ assertEquals(HandleStatus.HANDLED, queue.addLast(ref5));
+
+ MessageReference ref6 = generateReference(queue, 6);
+
+ ref6.getMessage().putHeader("fruit", "orange");
+
+ refs.add(ref6);
+
+ assertEquals(HandleStatus.HANDLED, queue.addLast(ref6));
+
+ if (!direct)
+ {
+ queue.addConsumer(consumer);
+
+ queue.deliver();
+ }
+
+ assertEquals(4, queue.getMessageCount());
+
+ assertEquals(2, consumer.getReferences().size());
+
+ assertRefListsIdenticalRefs(refs, consumer.getReferences());
+
+ queue.removeConsumer(consumer);
+
+ consumer = new FakeConsumer();
+
+ queue.addConsumer(consumer);
+
+ queue.deliver();
+
+ assertEquals(0, queue.getMessageCount());
+
+ assertEquals(4, consumer.getReferences().size());
+ }
+
+ // Inner classes ---------------------------------------------------------------
+
+ class DummyDistributionPolicy implements DistributionPolicy
+ {
+ public int select(List<Consumer> consumers, int lastPos)
+ {
+ return 0;
+ }
+ }
+
+}
Added: trunk/tests/src/org/jboss/messaging/newcore/impl/test/unit/RoundRobinDistributionPolicyTest.java
===================================================================
--- trunk/tests/src/org/jboss/messaging/newcore/impl/test/unit/RoundRobinDistributionPolicyTest.java (rev 0)
+++ trunk/tests/src/org/jboss/messaging/newcore/impl/test/unit/RoundRobinDistributionPolicyTest.java 2007-12-18 17:45:46 UTC (rev 3518)
@@ -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.Consumer;
+import org.jboss.messaging.newcore.DistributionPolicy;
+import org.jboss.messaging.newcore.impl.RoundRobinDistributionPolicy;
+import org.jboss.messaging.newcore.impl.test.unit.fakes.FakeConsumer;
+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);
+ }
+
+}
Added: trunk/tests/src/org/jboss/messaging/newcore/impl/test/unit/TransactionTest.java
===================================================================
--- trunk/tests/src/org/jboss/messaging/newcore/impl/test/unit/TransactionTest.java (rev 0)
+++ trunk/tests/src/org/jboss/messaging/newcore/impl/test/unit/TransactionTest.java 2007-12-18 17:45:46 UTC (rev 3518)
@@ -0,0 +1,326 @@
+package org.jboss.messaging.newcore.impl.test.unit;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.transaction.xa.Xid;
+
+import org.easymock.EasyMock;
+import org.jboss.messaging.newcore.Message;
+import org.jboss.messaging.newcore.MessageReference;
+import org.jboss.messaging.newcore.PersistenceManager;
+import org.jboss.messaging.newcore.Queue;
+import org.jboss.messaging.newcore.Transaction;
+import org.jboss.messaging.newcore.TransactionSynchronization;
+import org.jboss.messaging.newcore.impl.QueueImpl;
+import org.jboss.messaging.newcore.impl.TransactionImpl;
+import org.jboss.messaging.test.unit.UnitTestCase;
+
+/**
+ *
+ * A TransactionTest
+ *
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ *
+ */
+public class TransactionTest extends UnitTestCase
+{
+
+ public void test1PCCommit() throws Exception
+ {
+ List<Message> msgsToAdd = new ArrayList<Message>();
+
+ List<MessageReference> refsToRemove = new ArrayList<MessageReference>();
+
+ Queue queue = new QueueImpl(1);
+
+ MessageReference ref1 = this.generateReference(queue, 1);
+ msgsToAdd.add(ref1.getMessage());
+
+ MessageReference ref2 = this.generateReference(queue, 2);
+ refsToRemove.add(ref2);
+
+ Transaction tx = new TransactionImpl(msgsToAdd, refsToRemove, true);
+
+ PersistenceManager pm = EasyMock.createStrictMock(PersistenceManager.class);
+
+ pm.commitTransaction(msgsToAdd, refsToRemove);
+
+ EasyMock.replay(pm);
+
+ tx.commit(pm);
+
+ EasyMock.verify(pm);
+
+ assertEquals(ref1, queue.list(null).get(0));
+ }
+
+ public void test1PCRollback() throws Exception
+ {
+ List<Message> msgsToAdd = new ArrayList<Message>();
+
+ List<MessageReference> refsToRemove = new ArrayList<MessageReference>();
+
+ Queue queue = new QueueImpl(1);
+
+ MessageReference ref1 = this.generateReference(queue, 1);
+ msgsToAdd.add(ref1.getMessage());
+
+ MessageReference ref2 = this.generateReference(queue, 2);
+ refsToRemove.add(ref2);
+
+ Transaction tx = new TransactionImpl(msgsToAdd, refsToRemove, true);
+
+ PersistenceManager pm = EasyMock.createStrictMock(PersistenceManager.class);
+
+ EasyMock.replay(pm);
+
+ tx.rollback(pm);
+
+ EasyMock.verify(pm);
+
+ assertTrue(queue.list(null).isEmpty());
+ }
+
+ public void test1PCPrepare() throws Exception
+ {
+ List<Message> msgsToAdd = new ArrayList<Message>();
+
+ List<MessageReference> refsToRemove = new ArrayList<MessageReference>();
+
+ Queue queue = new QueueImpl(1);
+
+ MessageReference ref1 = this.generateReference(queue, 1);
+ msgsToAdd.add(ref1.getMessage());
+
+ MessageReference ref2 = this.generateReference(queue, 2);
+ refsToRemove.add(ref2);
+
+ Transaction tx = new TransactionImpl(msgsToAdd, refsToRemove, true);
+
+ PersistenceManager pm = EasyMock.createStrictMock(PersistenceManager.class);
+
+ try
+ {
+ tx.prepare(pm);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //OK
+ }
+
+ assertTrue(queue.list(null).isEmpty());
+ }
+
+ public void test2PCPrepareCommit() throws Exception
+ {
+ List<Message> msgsToAdd = new ArrayList<Message>();
+
+ List<MessageReference> refsToRemove = new ArrayList<MessageReference>();
+
+ Queue queue = new QueueImpl(1);
+
+ MessageReference ref1 = this.generateReference(queue, 1);
+ msgsToAdd.add(ref1.getMessage());
+
+ MessageReference ref2 = this.generateReference(queue, 2);
+ refsToRemove.add(ref2);
+
+ Xid xid = generateXid();
+
+ Transaction tx = new TransactionImpl(xid, msgsToAdd, refsToRemove, true);
+
+ PersistenceManager pm = EasyMock.createStrictMock(PersistenceManager.class);
+
+ pm.prepareTransaction(xid, msgsToAdd, refsToRemove);
+
+ EasyMock.replay(pm);
+
+ tx.prepare(pm);
+
+ EasyMock.verify(pm);
+
+ EasyMock.reset(pm);
+
+ pm.commitPreparedTransaction(xid);
+
+ EasyMock.replay(pm);
+
+ tx.commit(pm);
+
+ EasyMock.verify(pm);
+ }
+
+ public void test2PCCommitBeforePrepare() throws Exception
+ {
+ List<Message> msgsToAdd = new ArrayList<Message>();
+
+ List<MessageReference> refsToRemove = new ArrayList<MessageReference>();
+
+ Queue queue = new QueueImpl(1);
+
+ MessageReference ref1 = this.generateReference(queue, 1);
+ msgsToAdd.add(ref1.getMessage());
+
+ MessageReference ref2 = this.generateReference(queue, 2);
+ refsToRemove.add(ref2);
+
+ Xid xid = generateXid();
+
+ Transaction tx = new TransactionImpl(xid, msgsToAdd, refsToRemove, true);
+
+ PersistenceManager pm = EasyMock.createStrictMock(PersistenceManager.class);
+
+ try
+ {
+ tx.commit(pm);
+
+ fail ("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+ }
+
+ public void test2PCPrepareRollback() throws Exception
+ {
+ List<Message> msgsToAdd = new ArrayList<Message>();
+
+ List<MessageReference> refsToRemove = new ArrayList<MessageReference>();
+
+ Queue queue = new QueueImpl(1);
+
+ MessageReference ref1 = this.generateReference(queue, 1);
+ msgsToAdd.add(ref1.getMessage());
+
+ MessageReference ref2 = this.generateReference(queue, 2);
+ refsToRemove.add(ref2);
+
+ Xid xid = generateXid();
+
+ Transaction tx = new TransactionImpl(xid, msgsToAdd, refsToRemove, true);
+
+ PersistenceManager pm = EasyMock.createStrictMock(PersistenceManager.class);
+
+ pm.prepareTransaction(xid, msgsToAdd, refsToRemove);
+
+ EasyMock.replay(pm);
+
+ tx.prepare(pm);
+
+ EasyMock.verify(pm);
+
+ EasyMock.reset(pm);
+
+ pm.unprepareTransaction(xid, msgsToAdd, refsToRemove);
+
+ EasyMock.replay(pm);
+
+ tx.rollback(pm);
+
+ EasyMock.verify(pm);
+ }
+
+ public void testSynchronizations() throws Exception
+ {
+ List<Message> msgsToAdd = new ArrayList<Message>();
+
+ List<MessageReference> refsToRemove = new ArrayList<MessageReference>();
+
+ Queue queue = new QueueImpl(1);
+
+ MessageReference ref1 = this.generateReference(queue, 1);
+ msgsToAdd.add(ref1.getMessage());
+
+ MessageReference ref2 = this.generateReference(queue, 2);
+ refsToRemove.add(ref2);
+
+ Transaction tx = new TransactionImpl(msgsToAdd, refsToRemove, true);
+
+ TransactionSynchronization sync = EasyMock.createStrictMock(TransactionSynchronization.class);
+
+ PersistenceManager pm = EasyMock.createStrictMock(PersistenceManager.class);
+
+ tx.addSynchronization(sync);
+
+ sync.beforeCommit();
+ sync.afterCommit();
+
+ EasyMock.replay(sync);
+
+ tx.commit(pm);
+
+ EasyMock.verify(sync);
+
+ EasyMock.reset(sync);
+
+ tx = new TransactionImpl(msgsToAdd, refsToRemove, true);
+
+ tx.addSynchronization(sync);
+
+ sync.beforeRollback();
+ sync.afterRollback();
+
+ EasyMock.replay(sync);
+
+ tx.rollback(pm);
+
+ EasyMock.verify(sync);
+ }
+
+ public void testSynchronizations2PC() throws Exception
+ {
+ List<Message> msgsToAdd = new ArrayList<Message>();
+
+ List<MessageReference> refsToRemove = new ArrayList<MessageReference>();
+
+ Queue queue = new QueueImpl(1);
+
+ MessageReference ref1 = this.generateReference(queue, 1);
+ msgsToAdd.add(ref1.getMessage());
+
+ MessageReference ref2 = this.generateReference(queue, 2);
+ refsToRemove.add(ref2);
+
+ Xid xid = generateXid();
+
+ Transaction tx = new TransactionImpl(xid, msgsToAdd, refsToRemove, true);
+
+ TransactionSynchronization sync = EasyMock.createStrictMock(TransactionSynchronization.class);
+
+ PersistenceManager pm = EasyMock.createStrictMock(PersistenceManager.class);
+
+ tx.addSynchronization(sync);
+
+ sync.beforeCommit();
+ sync.afterCommit();
+
+ EasyMock.replay(sync);
+
+ tx.prepare(pm);
+ tx.commit(pm);
+
+ EasyMock.verify(sync);
+
+ EasyMock.reset(sync);
+
+ xid = generateXid();
+
+ tx = new TransactionImpl(xid, msgsToAdd, refsToRemove, true);
+
+ tx.addSynchronization(sync);
+
+ sync.beforeRollback();
+ sync.afterRollback();
+
+ EasyMock.replay(sync);
+
+ tx.prepare(pm);
+ tx.rollback(pm);
+
+ EasyMock.verify(sync);
+ }
+
+}
Added: trunk/tests/src/org/jboss/messaging/newcore/impl/test/unit/fakes/FakeConsumer.java
===================================================================
--- trunk/tests/src/org/jboss/messaging/newcore/impl/test/unit/fakes/FakeConsumer.java (rev 0)
+++ trunk/tests/src/org/jboss/messaging/newcore/impl/test/unit/fakes/FakeConsumer.java 2007-12-18 17:45:46 UTC (rev 3518)
@@ -0,0 +1,144 @@
+/*
+ * 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.test.unit.fakes;
+
+import java.util.LinkedList;
+import java.util.List;
+
+import org.jboss.messaging.newcore.Consumer;
+import org.jboss.messaging.newcore.Filter;
+import org.jboss.messaging.newcore.HandleStatus;
+import org.jboss.messaging.newcore.MessageReference;
+
+/**
+ *
+ * A FakeConsumer
+ *
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ *
+ */
+public class FakeConsumer implements Consumer
+{
+ private HandleStatus statusToReturn = HandleStatus.HANDLED;
+
+ private HandleStatus newStatus;
+
+ private int delayCountdown = 0;
+
+ private LinkedList<MessageReference> references = new LinkedList<MessageReference>();
+
+ private Filter filter;
+
+ public FakeConsumer()
+ {
+ }
+
+ public FakeConsumer(Filter filter)
+ {
+ this.filter = filter;
+ }
+
+ public synchronized MessageReference waitForNextReference(long timeout)
+ {
+ while (references.isEmpty() && timeout > 0)
+ {
+ long start = System.currentTimeMillis();
+ try
+ {
+ wait();
+ }
+ catch (InterruptedException e)
+ {
+ }
+ timeout -= (System.currentTimeMillis() - start);
+ }
+
+ if (timeout <= 0)
+ {
+ throw new IllegalStateException("Timed out waiting for reference");
+ }
+
+ return references.removeFirst();
+ }
+
+ public synchronized void setStatusImmediate(HandleStatus newStatus)
+ {
+ this.statusToReturn = newStatus;
+ }
+
+ public synchronized void setStatusDelayed(HandleStatus newStatus, int numReferences)
+ {
+ this.newStatus = newStatus;
+
+ this.delayCountdown = numReferences;
+ }
+
+ public synchronized List<MessageReference> getReferences()
+ {
+ return references;
+ }
+
+ public synchronized void clearReferences()
+ {
+ this.references.clear();
+ }
+
+ public synchronized HandleStatus handle(MessageReference reference)
+ {
+ if (filter != null)
+ {
+ if (filter.match(reference.getMessage()))
+ {
+ references.addLast(reference);
+ notify();
+
+ return HandleStatus.HANDLED;
+ }
+ else
+ {
+ return HandleStatus.NO_MATCH;
+ }
+ }
+
+ if (newStatus != null)
+ {
+ if (delayCountdown == 0)
+ {
+ statusToReturn = newStatus;
+
+ newStatus = null;
+ }
+ else
+ {
+ delayCountdown--;
+ }
+ }
+
+ if (statusToReturn == HandleStatus.HANDLED)
+ {
+ references.addLast(reference);
+ notify();
+ }
+
+ return statusToReturn;
+ }
+}
Added: trunk/tests/src/org/jboss/messaging/newcore/impl/test/unit/fakes/FakeFilter.java
===================================================================
--- trunk/tests/src/org/jboss/messaging/newcore/impl/test/unit/fakes/FakeFilter.java (rev 0)
+++ trunk/tests/src/org/jboss/messaging/newcore/impl/test/unit/fakes/FakeFilter.java 2007-12-18 17:45:46 UTC (rev 3518)
@@ -0,0 +1,72 @@
+/*
+ * 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.test.unit.fakes;
+
+import org.jboss.messaging.newcore.Filter;
+import org.jboss.messaging.newcore.Message;
+
+/**
+ *
+ * A FakeFilter
+ *
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ *
+ */
+public class FakeFilter implements Filter
+{
+ private String headerName;
+
+ private Object headerValue;
+
+ public FakeFilter(String headerName, Object headerValue)
+ {
+ this.headerName = headerName;
+
+ this.headerValue = headerValue;
+ }
+
+ public FakeFilter()
+ {
+ }
+
+ public boolean match(Message message)
+ {
+ if (headerName != null)
+ {
+ Object value = message.getHeader(headerName);
+
+ if (value != null && headerValue.equals(value))
+ {
+ return true;
+ }
+
+ return false;
+ }
+
+ return true;
+ }
+
+ public String getFilterString()
+ {
+ return null;
+ }
+}
Added: trunk/tests/src/org/jboss/messaging/test/unit/UnitTestCase.java
===================================================================
--- trunk/tests/src/org/jboss/messaging/test/unit/UnitTestCase.java (rev 0)
+++ trunk/tests/src/org/jboss/messaging/test/unit/UnitTestCase.java 2007-12-18 17:45:46 UTC (rev 3518)
@@ -0,0 +1,288 @@
+/*
+ * 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.test.unit;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import javax.transaction.xa.Xid;
+
+import junit.framework.TestCase;
+
+import org.jboss.jms.tx.MessagingXid;
+import org.jboss.messaging.newcore.Message;
+import org.jboss.messaging.newcore.MessageReference;
+import org.jboss.messaging.newcore.Queue;
+import org.jboss.messaging.newcore.impl.MessageImpl;
+
+/**
+ *
+ * Helper base class for our unit tests
+ *
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ *
+ */
+public class UnitTestCase extends TestCase
+{
+ protected void assertByteArraysEquivalent(byte[] bytes1, byte[] bytes2)
+ {
+ if (bytes1.length != bytes2.length)
+ {
+ fail("Byte arrays different sizes bytes1: " + dumpBytes(bytes1) + " bytes2: " + dumpBytes(bytes2));
+ }
+
+ for (int i = 0; i < bytes1.length; i++)
+ {
+ if (bytes1[i] != bytes2[i])
+ {
+ fail("Byte arrays not equivalent: " + dumpBytes(bytes1) + " bytes2: " + dumpBytes(bytes2));
+ }
+ }
+ }
+
+ protected String dumpBytes(byte[] bytes)
+ {
+ StringBuffer buff = new StringBuffer();
+
+ buff.append(System.identityHashCode(bytes) + ", size: " + bytes.length + " [");
+
+ for (int i = 0; i < bytes.length; i++)
+ {
+ buff.append(bytes[i]);
+
+ if (i != bytes.length - 1)
+ {
+ buff.append(", ");
+ }
+ }
+
+ buff.append("]");
+
+ return buff.toString();
+ }
+
+ protected boolean deleteDirectory(File directory)
+ {
+ if (directory.isDirectory())
+ {
+ String[] files = directory.list();
+
+ for (int j = 0; j < files.length; j++)
+ {
+ if (!deleteDirectory(new File(directory, files[j])))
+ {
+ return false;
+ }
+ }
+ }
+
+ return directory.delete();
+ }
+
+ protected void copyRecursive(File from , File to) throws Exception
+ {
+ if (from.isDirectory())
+ {
+ if (!to.exists())
+ {
+ to.mkdir();
+ }
+
+ String[] subs = from.list();
+
+ for (int i = 0; i < subs.length; i++)
+ {
+ copyRecursive(new File(from, subs[i]),
+ new File(to, subs[i]));
+ }
+ }
+ else
+ {
+ InputStream in = null;
+
+ OutputStream out = null;
+
+ try
+ {
+ in = new BufferedInputStream(new FileInputStream(from));
+
+ out = new BufferedOutputStream(new FileOutputStream(to));
+
+ int b;
+
+ while ((b = in.read()) != -1)
+ {
+ out.write(b);
+ }
+ }
+ finally
+ {
+ if (in != null)
+ {
+ in.close();
+ }
+
+ if (out != null)
+ {
+ out.close();
+ }
+ }
+ }
+ }
+
+ protected void assertRefListsIdenticalRefs(List<MessageReference> l1, List<MessageReference> l2)
+ {
+ if (l1.size() != l2.size())
+ {
+ fail("Lists different sizes: " + l1.size() + ", " + l2.size());
+ }
+
+ Iterator<MessageReference> iter1 = l1.iterator();
+ Iterator<MessageReference> iter2 = l2.iterator();
+
+ while (iter1.hasNext())
+ {
+ MessageReference o1 = iter1.next();
+ MessageReference o2 = iter2.next();
+
+ assertTrue(o1 == o2);
+ }
+ }
+
+ protected void assertRefListsEquivalent(List<MessageReference> l1, List<MessageReference> l2)
+ {
+ if (l1.size() != l2.size())
+ {
+ fail("Lists different sizes: " + l1.size() + ", " + l2.size());
+ }
+
+ Iterator<MessageReference> iter1 = l1.iterator();
+ Iterator<MessageReference> iter2 = l2.iterator();
+
+ while (iter1.hasNext())
+ {
+ MessageReference o1 = iter1.next();
+ MessageReference o2 = iter2.next();
+
+ assertEquals(o1.getMessage().getMessageID(), o2.getMessage().getMessageID());
+
+ assertEquals(o1.getScheduledDeliveryTime(), o2.getScheduledDeliveryTime());
+
+ assertEquals(o1.getDeliveryCount(), o2.getDeliveryCount());
+ }
+ }
+
+ protected Message generateMessage(long id)
+ {
+ Message message = new MessageImpl(id, 0, true, 0, System.currentTimeMillis(), (byte)4);
+
+ byte[] bytes = new byte[1024];
+
+ for (int i = 0; i < 1024; i++)
+ {
+ bytes[i] = (byte)i;
+ }
+
+ message.setPayload(bytes);
+
+ return message;
+ }
+
+ protected MessageReference generateReference(Queue queue, long id)
+ {
+ Message message = generateMessage(id);
+
+ return message.createReference(queue);
+ }
+
+ protected void assertEquivalent(Message msg1, Message msg2)
+ {
+ assertEquals(msg1.getMessageID(), msg2.getMessageID());
+
+ assertEquals(msg1.isReliable(), msg2.isReliable());
+
+ assertEquals(msg1.getExpiration(), msg2.getExpiration());
+
+ assertEquals(msg1.getTimestamp(), msg2.getTimestamp());
+
+ assertEquals(msg1.getPriority(), msg2.getPriority());
+
+ assertEquals(msg1.getType(), msg2.getType());
+
+ if (msg1.getPayload() == null)
+ {
+ assertNull(msg2.getPayload());
+ }
+ else
+ {
+ assertByteArraysEquivalent(msg1.getPayload(), msg2.getPayload());
+ }
+
+ assertMapsEquivalent(msg1.getHeaders(), msg2.getHeaders());
+
+ assertEquals(msg1.getReferences().size(), msg2.getReferences().size());
+
+ for (int i = 0; i < msg1.getReferences().size(); i++)
+ {
+ MessageReference ref1 = msg1.getReferences().get(i);
+
+ MessageReference ref2 = msg2.getReferences().get(i);
+
+ assertEquals(ref1.getScheduledDeliveryTime(), ref2.getScheduledDeliveryTime());
+
+ assertEquals(ref1.getDeliveryCount(), ref2.getDeliveryCount());
+
+ assertEquals(ref1.getQueue(), ref2.getQueue());
+ }
+ }
+
+ protected void assertMapsEquivalent(Map<String, Object> headers1, Map<String, Object> headers2)
+ {
+ assertEquals(headers1.size(), headers2.size());
+
+ for (Map.Entry<String, Object> entry : headers1.entrySet())
+ {
+ assertEquals(entry.getValue(), headers2.get(entry.getKey()));
+ }
+ }
+
+ protected Xid generateXid()
+ {
+ String id1 = java.util.UUID.randomUUID().toString();
+
+ String id2 = java.util.UUID.randomUUID().toString();
+
+ Xid xid = new MessagingXid(id1.getBytes(), 123, id2.getBytes());
+
+ return xid;
+ }
+
+
+}
More information about the jboss-cvs-commits
mailing list