qpid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kw...@apache.org
Subject svn commit: r1605011 - in /qpid/trunk/qpid/java/broker-core/src: main/java/org/apache/qpid/server/model/ main/java/org/apache/qpid/server/virtualhost/ test/java/org/apache/qpid/server/model/ test/java/org/apache/qpid/server/util/ test/java/org/apache/q...
Date Tue, 24 Jun 2014 06:13:47 GMT
Author: kwall
Date: Tue Jun 24 06:13:46 2014
New Revision: 1605011

URL: http://svn.apache.org/r1605011
Log:
QPID-5834: [Java Broker] Move validation that prevents to deletion of default virtualhost
to validate, in order that it can veto the proposed state change

* Added a unit test, improve other VH unit tests
* Removed MockVirtualHost

Removed:
    qpid/trunk/qpid/java/broker-core/src/test/java/org/apache/qpid/server/virtualhost/MockVirtualHost.java
Modified:
    qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/VirtualHost.java
    qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/virtualhost/AbstractVirtualHost.java
    qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/virtualhost/HouseKeepingTask.java
    qpid/trunk/qpid/java/broker-core/src/test/java/org/apache/qpid/server/model/VirtualHostTest.java
    qpid/trunk/qpid/java/broker-core/src/test/java/org/apache/qpid/server/util/BrokerTestHelper.java
    qpid/trunk/qpid/java/broker-core/src/test/java/org/apache/qpid/server/virtualhost/HouseKeepingTaskTest.java

Modified: qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/VirtualHost.java
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/VirtualHost.java?rev=1605011&r1=1605010&r2=1605011&view=diff
==============================================================================
--- qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/VirtualHost.java
(original)
+++ qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/VirtualHost.java
Tue Jun 24 06:13:46 2014
@@ -145,10 +145,12 @@ public interface VirtualHost<X extends V
 
     Collection<String> getExchangeTypeNames();
 
-    void delete();
-
     void start();
 
+    void stop();
+
+    void delete();
+
     public static interface Transaction
     {
         void dequeue(MessageInstance entry);

Modified: qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/virtualhost/AbstractVirtualHost.java
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/virtualhost/AbstractVirtualHost.java?rev=1605011&r1=1605010&r2=1605011&view=diff
==============================================================================
--- qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/virtualhost/AbstractVirtualHost.java
(original)
+++ qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/virtualhost/AbstractVirtualHost.java
Tue Jun 24 06:13:46 2014
@@ -207,6 +207,14 @@ public abstract class AbstractVirtualHos
         {
             throw new IllegalArgumentException(getClass().getSimpleName() + " must be durable");
         }
+        if (changedAttributes.contains(DESIRED_STATE))
+        {
+            if (State.DELETED == proxyForValidation.getDesiredState()
+                && getName().equals(_broker.getDefaultVirtualHost()))
+            {
+                throw new IntegrityViolationException("Cannot delete default virtual host
'" + getName() + "'");
+            }
+        }
     }
 
     @Override
@@ -1144,10 +1152,6 @@ public abstract class AbstractVirtualHos
         {
             String hostName = getName();
 
-            if (hostName.equals(_broker.getAttribute(Broker.DEFAULT_VIRTUAL_HOST)))
-            {
-                throw new IntegrityViolationException("Cannot delete default virtual host
'" + hostName + "'");
-            }
             close();
 
             MessageStore ms = getMessageStore();

Modified: qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/virtualhost/HouseKeepingTask.java
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/virtualhost/HouseKeepingTask.java?rev=1605011&r1=1605010&r2=1605011&view=diff
==============================================================================
--- qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/virtualhost/HouseKeepingTask.java
(original)
+++ qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/virtualhost/HouseKeepingTask.java
Tue Jun 24 06:13:46 2014
@@ -22,6 +22,7 @@ package org.apache.qpid.server.virtualho
 
 import org.apache.log4j.Logger;
 
+import org.apache.qpid.server.model.VirtualHost;
 import org.apache.qpid.server.security.SecurityManager;
 
 import javax.security.auth.Subject;
@@ -31,16 +32,13 @@ public abstract class HouseKeepingTask i
 {
     private Logger _logger = Logger.getLogger(this.getClass());
 
-    private VirtualHostImpl _virtualHost;
-
     private String _name;
 
     private final Subject _subject;
 
-    public HouseKeepingTask(VirtualHostImpl vhost)
+    public HouseKeepingTask(VirtualHost vhost)
     {
-        _virtualHost = vhost;
-        _name = _virtualHost.getName() + ":" + this.getClass().getSimpleName();
+        _name = vhost.getName() + ":" + this.getClass().getSimpleName();
         _subject = SecurityManager.getSystemTaskSubject(_name);
     }
 
@@ -75,11 +73,6 @@ public abstract class HouseKeepingTask i
         }
     }
 
-    public VirtualHostImpl getVirtualHost()
-    {
-        return _virtualHost;
-    }
-
     /** Execute the plugin. */
     public abstract void execute();
 

Modified: qpid/trunk/qpid/java/broker-core/src/test/java/org/apache/qpid/server/model/VirtualHostTest.java
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/java/broker-core/src/test/java/org/apache/qpid/server/model/VirtualHostTest.java?rev=1605011&r1=1605010&r2=1605011&view=diff
==============================================================================
--- qpid/trunk/qpid/java/broker-core/src/test/java/org/apache/qpid/server/model/VirtualHostTest.java
(original)
+++ qpid/trunk/qpid/java/broker-core/src/test/java/org/apache/qpid/server/model/VirtualHostTest.java
Tue Jun 24 06:13:46 2014
@@ -20,15 +20,23 @@
  */
 package org.apache.qpid.server.model;
 
+import static org.mockito.Matchers.argThat;
+import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
 import java.util.HashMap;
 import java.util.Map;
 import java.util.UUID;
 
+import org.mockito.ArgumentMatcher;
+
 import org.apache.qpid.server.configuration.updater.CurrentThreadTaskExecutor;
 import org.apache.qpid.server.configuration.updater.TaskExecutor;
+import org.apache.qpid.server.store.ConfiguredObjectRecord;
 import org.apache.qpid.server.store.DurableConfigurationStore;
 import org.apache.qpid.server.util.BrokerTestHelper;
 import org.apache.qpid.server.virtualhost.TestMemoryVirtualHost;
@@ -39,6 +47,7 @@ public class VirtualHostTest extends Qpi
     private Broker _broker;
     private TaskExecutor _taskExecutor;
     private VirtualHostNode<?> _virtualHostNode;
+    private DurableConfigurationStore _configStore;
 
     @Override
     protected void setUp() throws Exception
@@ -46,86 +55,163 @@ public class VirtualHostTest extends Qpi
         super.setUp();
 
         _broker = BrokerTestHelper.createBrokerMock();
+
         _taskExecutor = new CurrentThreadTaskExecutor();
         _taskExecutor.start();
         when(_broker.getTaskExecutor()).thenReturn(_taskExecutor);
 
         _virtualHostNode = mock(VirtualHostNode.class);
-        when(_virtualHostNode.getConfigurationStore()).thenReturn(mock(DurableConfigurationStore.class));
+        _configStore = mock(DurableConfigurationStore.class);
+        when(_virtualHostNode.getConfigurationStore()).thenReturn(_configStore);
+
+        // Virtualhost needs the EventLogger from the SystemContext.
         when(_virtualHostNode.getParent(Broker.class)).thenReturn(_broker);
+
         ConfiguredObjectFactory objectFactory = _broker.getObjectFactory();
         when(_virtualHostNode.getModel()).thenReturn(objectFactory.getModel());
-        when(_virtualHostNode.getObjectFactory()).thenReturn(objectFactory);
         when(_virtualHostNode.getTaskExecutor()).thenReturn(_taskExecutor);
-
-        when(((VirtualHostNode)_virtualHostNode).getCategoryClass()).thenReturn(VirtualHostNode.class);
     }
 
-
     @Override
     public void tearDown() throws Exception
     {
-        _taskExecutor.stopImmediately();
-        super.tearDown();
+        try
+        {
+            _taskExecutor.stopImmediately();
+        }
+        finally
+        {
+            super.tearDown();
+        }
+    }
+
+    public void testNewVirtualHost()
+    {
+        String virtualHostName = getName();
+        VirtualHost<?,?,?> virtualHost = createVirtualHost(virtualHostName);
+
+        assertNotNull("Unexpected id", virtualHost.getId());
+        assertEquals("Unexpected name", virtualHostName, virtualHost.getName());
+        assertEquals("Unexpected state", State.ACTIVE, virtualHost.getState());
+
+        verify(_configStore).create(matchesRecord(virtualHost.getId(), virtualHost.getType()));
     }
 
-    public void testActiveState()
+    public void testDeleteVirtualHost()
     {
-        VirtualHost<?,?,?> host = createHost();
+        VirtualHost<?,?,?> virtualHost = createVirtualHost(getName());
+        assertEquals("Unexpected state", State.ACTIVE, virtualHost.getState());
 
+        virtualHost.delete();
 
-        host.start();
-        assertEquals("Unexpected state", State.ACTIVE, host.getAttribute(VirtualHost.STATE));
+        assertEquals("Unexpected state", State.DELETED, virtualHost.getState());
+        verify(_configStore).remove(matchesRecord(virtualHost.getId(), virtualHost.getType()));
     }
 
-    public void testDeletedState()
+    public void testDeleteDefaultVirtualHostIsDisallowed()
     {
-        VirtualHost<?,?,?> host = createHost();
+        String virtualHostName = getName();
+        when(_broker.getDefaultVirtualHost()).thenReturn(virtualHostName);
+
+        VirtualHost<?,?,?> virtualHost = createVirtualHost(virtualHostName);
 
-        host.delete();
-        assertEquals("Unexpected state", State.DELETED, host.getAttribute(VirtualHost.STATE));
+        try
+        {
+            virtualHost.delete();
+            fail("Exception not thrown");
+        }
+        catch(IntegrityViolationException ive)
+        {
+            // PASS
+        }
+
+        assertEquals("Unexpected state", State.ACTIVE, virtualHost.getState());
+        verify(_configStore, never()).remove(matchesRecord(virtualHost.getId(), virtualHost.getType()));
     }
 
-    public void testCreateQueueChildHavingMessageGroupingAttributes()
+    public void testStopAndStartVirtualHost()
     {
-        VirtualHost<?,?,?> host = createHost();
+        String virtualHostName = getName();
+
+        VirtualHost<?,?,?> virtualHost = createVirtualHost(virtualHostName);
+        assertEquals("Unexpected state", State.ACTIVE, virtualHost.getState());
 
-        host.start();
+        virtualHost.stop();
+        assertEquals("Unexpected state", State.STOPPED, virtualHost.getState());
 
-        String queueName = getTestName();
-        Map<String, Object> arguments = new HashMap<String, Object>();
-        arguments.put(Queue.MESSAGE_GROUP_KEY, "mykey");
-        arguments.put(Queue.MESSAGE_GROUP_SHARED_GROUPS, true);
+        virtualHost.start();
+        assertEquals("Unexpected state", State.ACTIVE, virtualHost.getState());
+
+        verify(_configStore, times(1)).create(matchesRecord(virtualHost.getId(), virtualHost.getType()));
+        verify(_configStore, times(2)).update(eq(false), matchesRecord(virtualHost.getId(),
virtualHost.getType()));
+    }
+
+    public void testCreateDurableQueue()
+    {
+        String virtualHostName = getName();
+        VirtualHost<?,?,?> virtualHost = createVirtualHost(virtualHostName);
+
+        String queueName = "myQueue";
+        Map<String, Object> arguments = new HashMap<>();
         arguments.put(Queue.NAME, queueName);
+        arguments.put(Queue.DURABLE, Boolean.TRUE);
+
+        Queue queue = virtualHost.createChild(Queue.class, arguments);
+        assertNotNull(queue.getId());
+        assertEquals(queueName, queue.getName());
 
-        host.createChild(Queue.class, arguments);
+        verify(_configStore).create(matchesRecord(queue.getId(), queue.getType()));
+    }
+
+    public void testCreateNonDurableQueue()
+    {
+        String virtualHostName = getName();
+        VirtualHost<?,?,?> virtualHost = createVirtualHost(virtualHostName);
 
-        Queue<?> queue = host.getChildByName(Queue.class, queueName);
-        Object messageGroupKey = queue.getAttribute(Queue.MESSAGE_GROUP_KEY);
-        assertEquals("Unexpected message group key attribute", "mykey", messageGroupKey);
+        String queueName = "myQueue";
+        Map<String, Object> arguments = new HashMap<>();
+        arguments.put(Queue.NAME, queueName);
+        arguments.put(Queue.DURABLE, Boolean.FALSE);
 
-        Object sharedGroups = queue.getAttribute(Queue.MESSAGE_GROUP_SHARED_GROUPS);
-        assertEquals("Unexpected shared groups attribute", true, sharedGroups);
+        Queue queue = virtualHost.createChild(Queue.class, arguments);
+        assertNotNull(queue.getId());
+        assertEquals(queueName, queue.getName());
 
+        verify(_configStore, never()).create(matchesRecord(queue.getId(), queue.getType()));
     }
 
-    private VirtualHost<?,?,?> createHost()
+    private VirtualHost<?,?,?> createVirtualHost(final String virtualHostName)
     {
-        Map<String, Object> attributes = new HashMap<String, Object>();
-        attributes.put(VirtualHost.NAME, getName());
+        Map<String, Object> attributes = new HashMap<>();
+        attributes.put(VirtualHost.NAME, virtualHostName);
         attributes.put(VirtualHost.TYPE, TestMemoryVirtualHost.VIRTUAL_HOST_TYPE);
 
-        VirtualHost<?,?,?> host = createHost(attributes);
+        TestMemoryVirtualHost host = new TestMemoryVirtualHost(attributes, _virtualHostNode);
+        host.create();
         return host;
     }
 
-    private VirtualHost<?,?,?> createHost(Map<String, Object> attributes)
+    private static ConfiguredObjectRecord matchesRecord(UUID id, String type)
     {
-        attributes = new HashMap<String, Object>(attributes);
-        attributes.put(VirtualHost.ID, UUID.randomUUID());
-        TestMemoryVirtualHost host= new TestMemoryVirtualHost(attributes, _virtualHostNode);
-        host.create();
-        return host;
+        return argThat(new MinimalConfiguredObjectRecordMatcher(id, type));
     }
 
+    private static class MinimalConfiguredObjectRecordMatcher extends ArgumentMatcher<ConfiguredObjectRecord>
+    {
+        private final UUID _id;
+        private final String _type;
+
+        private MinimalConfiguredObjectRecordMatcher(UUID id, String type)
+        {
+            _id = id;
+            _type = type;
+        }
+
+        @Override
+        public boolean matches(Object argument)
+        {
+            ConfiguredObjectRecord rhs = (ConfiguredObjectRecord) argument;
+            return (_id.equals(rhs.getId()) || _type.equals(rhs.getType()));
+        }
+    }
 }

Modified: qpid/trunk/qpid/java/broker-core/src/test/java/org/apache/qpid/server/util/BrokerTestHelper.java
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/java/broker-core/src/test/java/org/apache/qpid/server/util/BrokerTestHelper.java?rev=1605011&r1=1605010&r2=1605011&view=diff
==============================================================================
--- qpid/trunk/qpid/java/broker-core/src/test/java/org/apache/qpid/server/util/BrokerTestHelper.java
(original)
+++ qpid/trunk/qpid/java/broker-core/src/test/java/org/apache/qpid/server/util/BrokerTestHelper.java
Tue Jun 24 06:13:46 2014
@@ -32,7 +32,6 @@ import java.util.UUID;
 
 import javax.security.auth.Subject;
 
-import org.apache.qpid.server.configuration.store.JsonConfigurationEntryStore;
 import org.apache.qpid.server.configuration.updater.CurrentThreadTaskExecutor;
 import org.apache.qpid.server.configuration.updater.TaskExecutor;
 import org.apache.qpid.server.exchange.ExchangeImpl;
@@ -63,9 +62,6 @@ import org.apache.qpid.server.virtualhos
 public class BrokerTestHelper
 {
 
-    protected static final String BROKER_STORE_CLASS_NAME_KEY = "brokerstore.class.name";
-    protected static final String JSON_BROKER_STORE_CLASS_NAME = JsonConfigurationEntryStore.class.getName();
-
     private static final TaskExecutor TASK_EXECUTOR = new CurrentThreadTaskExecutor();
     static
     {

Modified: qpid/trunk/qpid/java/broker-core/src/test/java/org/apache/qpid/server/virtualhost/HouseKeepingTaskTest.java
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/java/broker-core/src/test/java/org/apache/qpid/server/virtualhost/HouseKeepingTaskTest.java?rev=1605011&r1=1605010&r2=1605011&view=diff
==============================================================================
--- qpid/trunk/qpid/java/broker-core/src/test/java/org/apache/qpid/server/virtualhost/HouseKeepingTaskTest.java
(original)
+++ qpid/trunk/qpid/java/broker-core/src/test/java/org/apache/qpid/server/virtualhost/HouseKeepingTaskTest.java
Tue Jun 24 06:13:46 2014
@@ -20,6 +20,10 @@
  */
 package org.apache.qpid.server.virtualhost;
 
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import org.apache.qpid.server.model.VirtualHost;
 import org.apache.qpid.test.utils.QpidTestCase;
 
 public class HouseKeepingTaskTest extends QpidTestCase
@@ -27,14 +31,15 @@ public class HouseKeepingTaskTest extend
 
     public void testThreadNameIsSetForDurationOfTask() throws Exception
     {
-
         String originalThreadName = Thread.currentThread().getName();
 
         String vhostName = "HouseKeepingTaskTestVhost";
 
         String expectedThreadNameDuringExecution = vhostName + ":" + "ThreadNameRememberingTask";
 
-        ThreadNameRememberingTask testTask = new ThreadNameRememberingTask(new MockVirtualHost(vhostName));
+        VirtualHost virtualHost = mock(VirtualHost.class);
+        when(virtualHost.getName()).thenReturn(vhostName);
+        ThreadNameRememberingTask testTask = new ThreadNameRememberingTask(virtualHost);
 
         testTask.run();
 
@@ -48,7 +53,7 @@ public class HouseKeepingTaskTest extend
     {
         private String _threadNameDuringExecution;
 
-        private ThreadNameRememberingTask(VirtualHostImpl vhost)
+        private ThreadNameRememberingTask(VirtualHost vhost)
         {
             super(vhost);
         }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org


Mime
View raw message