[rhmessaging-commits] rhmessaging commits: r3801 - store/branches/java/0.5-release/src/test/java/org/apache/qpid/server/store/berkeleydb.

rhmessaging-commits at lists.jboss.org rhmessaging-commits at lists.jboss.org
Thu Jan 14 12:21:38 EST 2010


Author: rgemmell
Date: 2010-01-14 12:21:38 -0500 (Thu, 14 Jan 2010)
New Revision: 3801

Added:
   store/branches/java/0.5-release/src/test/java/org/apache/qpid/server/store/berkeleydb/BDBStoreBindingsWorkaroundTest.java
Log:
Add unit testing for the bindings workaround tool


Added: store/branches/java/0.5-release/src/test/java/org/apache/qpid/server/store/berkeleydb/BDBStoreBindingsWorkaroundTest.java
===================================================================
--- store/branches/java/0.5-release/src/test/java/org/apache/qpid/server/store/berkeleydb/BDBStoreBindingsWorkaroundTest.java	                        (rev 0)
+++ store/branches/java/0.5-release/src/test/java/org/apache/qpid/server/store/berkeleydb/BDBStoreBindingsWorkaroundTest.java	2010-01-14 17:21:38 UTC (rev 3801)
@@ -0,0 +1,335 @@
+package org.apache.qpid.server.store.berkeleydb;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.configuration.PropertiesConfiguration;
+import org.apache.qpid.exchange.ExchangeDefaults;
+import org.apache.qpid.framing.AMQShortString;
+import org.apache.qpid.server.configuration.VirtualHostConfiguration;
+import org.apache.qpid.server.exchange.DirectExchange;
+import org.apache.qpid.server.exchange.Exchange;
+import org.apache.qpid.server.exchange.ExchangeFactory;
+import org.apache.qpid.server.exchange.ExchangeRegistry;
+import org.apache.qpid.server.queue.AMQQueue;
+import org.apache.qpid.server.queue.AMQQueueFactory;
+import org.apache.qpid.server.registry.ApplicationRegistry;
+import org.apache.qpid.server.util.NullApplicationRegistry;
+import org.apache.qpid.server.virtualhost.VirtualHost;
+
+public class BDBStoreBindingsWorkaroundTest extends TestCase
+{
+    private final static String STORE_PARENT_LOC = System.getProperty("QPID_WORK") + File.separator + "BindingsWorkaroundTest";
+    
+    private static BDBMessageStore _store;
+    private VirtualHost _virtualHost;
+    private File BDB_DIR;
+    
+    public void setUp() throws Exception
+    {
+        if(System.getProperty("QPID_HOME") == null)
+        {
+            fail("QPID_HOME is not set");
+        }
+        
+        if(System.getProperty("QPID_WORK") == null)
+        {
+            fail("QPID_WORK is not set");
+        }
+        
+        BDB_DIR = new File(STORE_PARENT_LOC + getName());
+        
+        if (BDB_DIR.exists())
+        {
+            if(!deleteDirectory(BDB_DIR))
+            {
+                fail("Unable to clean existing BDB directory");
+            }
+        }
+        
+        BDB_DIR.mkdirs();
+        
+        ApplicationRegistry.initialise(new NullApplicationRegistry());
+
+        _store = new BDBMessageStore();
+        _store.configure(BDB_DIR);
+
+        PropertiesConfiguration config = new PropertiesConfiguration();
+
+        VirtualHostConfiguration vhostConfig = new VirtualHostConfiguration("test", config);
+
+        _virtualHost = new VirtualHost(vhostConfig, _store);
+        _store.setVirtualHost(_virtualHost);
+    }
+    
+    public void tearDown() throws Exception
+    {
+        ApplicationRegistry.removeAll();
+        
+        if (BDB_DIR.exists())
+        {
+            deleteDirectory(BDB_DIR);
+        }
+    }
+
+    private void reload() throws Exception
+    {
+        _virtualHost.close();
+        
+        PropertiesConfiguration env = new PropertiesConfiguration();
+
+        env.addProperty("store.environment-path", BDB_DIR.getAbsolutePath());
+        env.addProperty("store.class", "org.apache.qpid.server.store.berkeleydb.BDBMessageStore");
+
+        _virtualHost = new VirtualHost(new VirtualHostConfiguration("test", env), null);
+
+        _store = (BDBMessageStore) _virtualHost.getMessageStore();
+    }
+    
+    /**
+     * This tests that the queue bindings are NOT recovered. If this test fails, then the
+     * bindings were recovered and the default exchanges are already in the store provided.
+    */
+    public void testBindingsAreActuallyBroken() throws Exception
+    {
+        AMQShortString queueName = new AMQShortString("testBindingsAreBroken");
+        AMQQueue queue = AMQQueueFactory.createAMQQueueImpl(queueName, true, queueName, false, _virtualHost, null);
+
+        _store.createQueue(queue, null);
+
+        AMQShortString routingKey = new AMQShortString("testBindingsAreBroken-Key");
+
+        _store.bindQueue(_virtualHost.getExchangeRegistry().getDefaultExchange(), routingKey, queue, null);
+
+        reload();
+        
+        AMQQueue returnedQueue = _virtualHost.getQueueRegistry().getQueue(queueName);
+
+        assertNotNull("Exchange binding were null.", returnedQueue.getExchangeBindings());
+        assertEquals("Unexpected binding count for queue. Expected 0 bindings due to be " +
+        		     "recovered due to default exchanges not being entered in the store.",
+                     0, returnedQueue.getExchangeBindings().size());
+    }
+    
+    /**
+     * This tests that the start method throws a FNFE when an invalid config location is specified.
+     * This is caught within the tools main() method normally.
+    */
+    public void testNonExistantConfig() throws Exception
+    {
+        BDBStoreBindingsWorkaround tool = new BDBStoreBindingsWorkaround(
+                                            new String[]{"-c ", String.valueOf(System.currentTimeMillis())});
+        try
+        {
+            tool.start();
+            fail("Should have thrown FileNotFoundException");
+        }
+        catch(FileNotFoundException e)
+        {
+            //expected exception
+        }
+    }
+
+    /**
+     * This tests checks that the expected Exchange names are recovered from the store.
+    */
+    public void testGetBDBExchangeNames() throws Exception
+    {       
+        //verify current exchange count is 0
+        List<AMQShortString> bdbExchanges = BDBStoreBindingsWorkaround.getBDBStoreExchangeNames(_virtualHost, _store);
+        assertEquals("Unexpected existing Exchange names in the store", 0, bdbExchanges.size());
+        
+        ExchangeFactory exchangeFactory = _virtualHost.getExchangeFactory();
+        ExchangeRegistry exchangeRegistry = _virtualHost.getExchangeRegistry();
+        
+        //Add a new exchange to the store
+        AMQShortString exchangeName1 = new AMQShortString("testGetBDBExchangeNames1");
+        Exchange exchange1 = exchangeFactory.createExchange(exchangeName1, DirectExchange.TYPE.getName(), true, false, 0);
+        assertNotNull("Exchange is null", exchange1);
+        
+        //registering currently creates the store entry as a side-effect
+        exchangeRegistry.registerExchange(exchange1);
+        
+        bdbExchanges = BDBStoreBindingsWorkaround.getBDBStoreExchangeNames(_virtualHost, _store);
+        assertEquals("Expected 1 exchanges names to be returned in the name list", 1, bdbExchanges.size());
+        assertTrue("The expected exchange name was not present", bdbExchanges.contains(exchangeName1));
+        
+        //Add another new exchange to the store
+        AMQShortString exchangeName2 = new AMQShortString("testGetBDBExchangeNames2");
+        Exchange exchange2 = exchangeFactory.createExchange(exchangeName2, DirectExchange.TYPE.getName(), true, false, 0);
+        assertNotNull("Exchange is null", exchange2);
+        
+        //registering currently creates the store entry as a side-effect
+        exchangeRegistry.registerExchange(exchange2);
+        
+        bdbExchanges = BDBStoreBindingsWorkaround.getBDBStoreExchangeNames(_virtualHost, _store);
+        assertEquals("Expected 2 exchanges names to be returned in the name list", 2, bdbExchanges.size());
+        assertTrue("The expected exchange names were not present", bdbExchanges.contains(exchangeName1));
+        assertTrue("The expected exchange names were not present", bdbExchanges.contains(exchangeName2));
+    }
+    
+    /**
+     * This tests checks that given a vhost, any durable exchanges in the registry that are not 
+     * already in the store are then added to it.
+    */
+    public void testAddDurableExchangesToStoreIfRequired() throws Exception
+    {      
+        ExchangeFactory exchangeFactory = _virtualHost.getExchangeFactory();
+        ExchangeRegistry exchangeRegistry = _virtualHost.getExchangeRegistry();
+      
+        //Create a new durable exchange and but dont register it(a side effect will add it to store)
+        AMQShortString durExchangeName = new AMQShortString("testAddDurableExchangesToStoreIfRequired.Durable");
+        Exchange durExchange = exchangeFactory.createExchange(durExchangeName, DirectExchange.TYPE.getName(), true, false, 0);
+        assertNotNull("Exchange is null", durExchange);
+        
+        //Create a new non-durable exchange and but dont register it
+        AMQShortString transExchangeName = new AMQShortString("testAddDurableExchangesToStoreIfRequired.Transient");
+        Exchange transExchange = exchangeFactory.createExchange(transExchangeName, DirectExchange.TYPE.getName(), false, false, 0);
+        assertNotNull("Exchange is null", transExchange);
+
+        //verify current exchange count in the registry is 5 and verify the names (<<default>>, amq.[direct|topic|match|fanout])
+        Collection<AMQShortString> exNames = exchangeRegistry.getExchangeNames();
+        assertEquals("Unexpected number of exchanges in the registry", 5, exNames.size());
+        assertTrue(exNames.contains(ExchangeDefaults.DEFAULT_EXCHANGE_NAME));
+        assertTrue(exNames.contains(ExchangeDefaults.DIRECT_EXCHANGE_NAME));
+        assertTrue(exNames.contains(ExchangeDefaults.TOPIC_EXCHANGE_NAME));
+        assertTrue(exNames.contains(ExchangeDefaults.FANOUT_EXCHANGE_NAME));
+        assertTrue(exNames.contains(ExchangeDefaults.HEADERS_EXCHANGE_NAME));
+        
+        //create a list of the Exchanges from the registry, plus the 2 created above
+        final List<Exchange> exList = new ArrayList<Exchange>();
+        
+        for(AMQShortString name : exNames)
+        {
+            Exchange ex = exchangeRegistry.getExchange(name);
+            exList.add(ex);
+        }
+        exList.add(transExchange);
+        exList.add(durExchange);
+        
+        Collection<VirtualHost> vhosts = new ArrayList<VirtualHost>();
+        vhosts.add(_virtualHost);
+        
+        /**
+         * Override the getVhostExchanges to allow defining the list of Exchanges that the
+         * registry supposedly holds. This is required to allow returning a new non-default/amq.
+         * durable exchange that isnt already in the store, as exchange registration will
+         * currently also add it to the store.
+        */
+        BDBStoreBindingsWorkaround tool = new BDBStoreBindingsWorkaround(new String[]{})
+        {
+            protected List<Exchange> getVhostExchanges(VirtualHost vhost)
+            {
+                return exList;
+            }
+        };
+        
+        //use the tool to add the records to the store if required
+        tool.addDurableExchangesToStoreIfRequired(vhosts);
+        
+        //reload the vhost and recover the store.
+        reload();
+        
+        exchangeFactory = _virtualHost.getExchangeFactory();
+        exchangeRegistry = _virtualHost.getExchangeRegistry();
+      
+        //verify current exchange count in the registry is now 6 and verify the
+        //names (<<default>>, amq.[direct|topic|match|fanout]) + durable one created above.
+        exNames = exchangeRegistry.getExchangeNames();
+        assertEquals("Unexpected number of exchanges in the registry", 6, exNames.size());
+        assertTrue(exNames.contains(ExchangeDefaults.DEFAULT_EXCHANGE_NAME));
+        assertTrue(exNames.contains(ExchangeDefaults.DIRECT_EXCHANGE_NAME));
+        assertTrue(exNames.contains(ExchangeDefaults.TOPIC_EXCHANGE_NAME));
+        assertTrue(exNames.contains(ExchangeDefaults.FANOUT_EXCHANGE_NAME));
+        assertTrue(exNames.contains(ExchangeDefaults.HEADERS_EXCHANGE_NAME));
+        assertTrue(exNames.contains(durExchangeName));
+        
+        //also confirm the names in the bdbstore
+        List<AMQShortString> bdbExchanges = BDBStoreBindingsWorkaround.getBDBStoreExchangeNames(_virtualHost, _store);
+        assertEquals("Unexpected number of exchanges in the registry", 6, bdbExchanges.size());
+        assertTrue(bdbExchanges.contains(ExchangeDefaults.DEFAULT_EXCHANGE_NAME));
+        assertTrue(bdbExchanges.contains(ExchangeDefaults.DIRECT_EXCHANGE_NAME));
+        assertTrue(bdbExchanges.contains(ExchangeDefaults.TOPIC_EXCHANGE_NAME));
+        assertTrue(bdbExchanges.contains(ExchangeDefaults.FANOUT_EXCHANGE_NAME));
+        assertTrue(bdbExchanges.contains(ExchangeDefaults.HEADERS_EXCHANGE_NAME));
+        assertTrue(bdbExchanges.contains(durExchangeName));
+    }
+    
+    /**
+     * This tests checks that given a vhost, any durable exchanges in the registry that are not 
+     * already in the store are then added to it.
+    */
+    public void testGetVhostExchanges() throws Exception
+    {      
+        ExchangeFactory exchangeFactory = _virtualHost.getExchangeFactory();
+        ExchangeRegistry exchangeRegistry = _virtualHost.getExchangeRegistry();
+      
+        //verify current exchange count in the registry is 5 and verify the names (<<default>>, amq.[direct|topic|match|fanout])
+        Collection<AMQShortString> exNames = exchangeRegistry.getExchangeNames();
+        assertEquals("Unexpected number of exchanges in the registry", 5, exNames.size());
+        assertTrue(exNames.contains(ExchangeDefaults.DEFAULT_EXCHANGE_NAME));
+        assertTrue(exNames.contains(ExchangeDefaults.DIRECT_EXCHANGE_NAME));
+        assertTrue(exNames.contains(ExchangeDefaults.TOPIC_EXCHANGE_NAME));
+        assertTrue(exNames.contains(ExchangeDefaults.FANOUT_EXCHANGE_NAME));
+        assertTrue(exNames.contains(ExchangeDefaults.HEADERS_EXCHANGE_NAME));
+        
+        BDBStoreBindingsWorkaround tool = new BDBStoreBindingsWorkaround(new String[]{});
+        List<Exchange> vhostExchanges = tool.getVhostExchanges(_virtualHost);
+        
+        //verify the exchanges returned from the tool are those from the vhost registry
+        assertEquals("Unexpected number of exchanges returned", 5, vhostExchanges.size());
+        assertTrue(vhostExchanges.contains(exchangeRegistry.getExchange(ExchangeDefaults.DEFAULT_EXCHANGE_NAME)));
+        assertTrue(vhostExchanges.contains(exchangeRegistry.getExchange(ExchangeDefaults.DIRECT_EXCHANGE_NAME)));
+        assertTrue(vhostExchanges.contains(exchangeRegistry.getExchange(ExchangeDefaults.TOPIC_EXCHANGE_NAME)));
+        assertTrue(vhostExchanges.contains(exchangeRegistry.getExchange(ExchangeDefaults.FANOUT_EXCHANGE_NAME)));
+        assertTrue(vhostExchanges.contains(exchangeRegistry.getExchange(ExchangeDefaults.HEADERS_EXCHANGE_NAME)));
+        
+        //Create a new durable exchange and register it(a side effect will add it to store)
+        AMQShortString durExchangeName = new AMQShortString("testGetVhostExchanges.Durable");
+        Exchange durExchange = exchangeFactory.createExchange(durExchangeName, DirectExchange.TYPE.getName(), true, false, 0);
+        assertNotNull("Exchange is null", durExchange);
+        exchangeRegistry.registerExchange(durExchange);
+        
+        //Create a new non-durable exchange and register it
+        AMQShortString transExchangeName = new AMQShortString("testGetVhostExchanges.Transient");
+        Exchange transExchange = exchangeFactory.createExchange(transExchangeName, DirectExchange.TYPE.getName(), false, false, 0);
+        assertNotNull("Exchange is null", transExchange);
+        exchangeRegistry.registerExchange(transExchange);
+         
+        vhostExchanges = tool.getVhostExchanges(_virtualHost);
+        
+        //verify the exchanges returned from the tool are those from the vhost registry
+        assertEquals("Unexpected number of exchanges returned", 7, vhostExchanges.size());
+        assertTrue(vhostExchanges.contains(exchangeRegistry.getExchange(ExchangeDefaults.DEFAULT_EXCHANGE_NAME)));
+        assertTrue(vhostExchanges.contains(exchangeRegistry.getExchange(ExchangeDefaults.DIRECT_EXCHANGE_NAME)));
+        assertTrue(vhostExchanges.contains(exchangeRegistry.getExchange(ExchangeDefaults.TOPIC_EXCHANGE_NAME)));
+        assertTrue(vhostExchanges.contains(exchangeRegistry.getExchange(ExchangeDefaults.FANOUT_EXCHANGE_NAME)));
+        assertTrue(vhostExchanges.contains(exchangeRegistry.getExchange(ExchangeDefaults.HEADERS_EXCHANGE_NAME)));
+        assertTrue(vhostExchanges.contains(exchangeRegistry.getExchange(durExchangeName)));
+        assertTrue(vhostExchanges.contains(exchangeRegistry.getExchange(transExchangeName)));
+    }
+    
+    
+    public static boolean deleteDirectory(File dir)
+    {
+        if (dir.isDirectory())
+        {
+            String[] children = dir.list();
+            for (int i = 0; i < children.length; i++)
+            {
+                if (!deleteDirectory(new File(dir, children[i])))
+                {
+                    return false;
+                }
+            }
+        }
+
+        return (dir.delete());
+    }
+
+}



More information about the rhmessaging-commits mailing list