You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by rg...@apache.org on 2014/08/21 11:20:42 UTC

svn commit: r1619322 - in /qpid/trunk/qpid/java/broker-core/src: main/java/org/apache/qpid/server/store/JsonFileConfigStore.java test/java/org/apache/qpid/server/store/JsonFileConfigStoreTest.java

Author: rgodfrey
Date: Thu Aug 21 09:20:42 2014
New Revision: 1619322

URL: http://svn.apache.org/r1619322
Log:
QPID-6027 : [Java Broker] Enforce that all records being added into the Json store have a name which is a String

Modified:
    qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/store/JsonFileConfigStore.java
    qpid/trunk/qpid/java/broker-core/src/test/java/org/apache/qpid/server/store/JsonFileConfigStoreTest.java

Modified: qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/store/JsonFileConfigStore.java
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/store/JsonFileConfigStore.java?rev=1619322&r1=1619321&r2=1619322&view=diff
==============================================================================
--- qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/store/JsonFileConfigStore.java (original)
+++ qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/store/JsonFileConfigStore.java Thu Aug 21 09:20:42 2014
@@ -37,8 +37,6 @@ import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.SortedSet;
-import java.util.TreeSet;
 import java.util.UUID;
 
 import org.apache.log4j.Logger;
@@ -60,6 +58,28 @@ public class JsonFileConfigStore impleme
 {
     private static final Logger _logger = Logger.getLogger(JsonFileConfigStore.class);
 
+    private static final Comparator<Class<? extends ConfiguredObject>> CATEGORY_CLASS_COMPARATOR =
+            new Comparator<Class<? extends ConfiguredObject>>()
+            {
+                @Override
+                public int compare(final Class<? extends ConfiguredObject> left,
+                                   final Class<? extends ConfiguredObject> right)
+                {
+                    return left.getSimpleName().compareTo(right.getSimpleName());
+                }
+            };
+    private static final Comparator<ConfiguredObjectRecord> CONFIGURED_OBJECT_RECORD_COMPARATOR =
+            new Comparator<ConfiguredObjectRecord>()
+            {
+                @Override
+                public int compare(final ConfiguredObjectRecord left, final ConfiguredObjectRecord right)
+                {
+                    String leftName = (String) left.getAttributes().get(ConfiguredObject.NAME);
+                    String rightName = (String) right.getAttributes().get(ConfiguredObject.NAME);
+                    return leftName.compareTo(rightName);
+                }
+            };
+
     private final Map<UUID, ConfiguredObjectRecord> _objectsById = new HashMap<UUID, ConfiguredObjectRecord>();
     private final Map<String, List<UUID>> _idsByType = new HashMap<String, List<UUID>>();
     private final ObjectMapper _objectMapper = new ObjectMapper();
@@ -316,6 +336,14 @@ public class JsonFileConfigStore impleme
         {
             throw new StoreException("Cannot create object of unknown type " + record.getType());
         }
+        else if(record.getAttributes() == null || !(record.getAttributes().get(ConfiguredObject.NAME) instanceof String))
+        {
+            throw new StoreException("The record " + record.getId()
+                                     + " of type " + record.getType()
+                                     + " does not have an attribute '"
+                                     + ConfiguredObject.NAME
+                                     + "' of type String");
+        }
         else
         {
             record = new ConfiguredObjectRecordImpl(record);
@@ -410,14 +438,7 @@ public class JsonFileConfigStore impleme
         List<Class<? extends ConfiguredObject>> childClasses =
                 new ArrayList<Class<? extends ConfiguredObject>>(_parent.getModel().getChildTypes(type));
 
-        Collections.sort(childClasses, new Comparator<Class<? extends ConfiguredObject>>()
-        {
-            @Override
-            public int compare(final Class<? extends ConfiguredObject> o1, final Class<? extends ConfiguredObject> o2)
-            {
-                return o1.getSimpleName().compareTo(o2.getSimpleName());
-            }
-        });
+        Collections.sort(childClasses, CATEGORY_CLASS_COMPARATOR);
 
         for(Class<? extends ConfiguredObject> childClass : childClasses)
         {
@@ -429,18 +450,7 @@ public class JsonFileConfigStore impleme
                 if(childIds != null)
                 {
                     List<Map<String,Object>> entities = new ArrayList<Map<String, Object>>();
-                    SortedSet<ConfiguredObjectRecord> sortedChildren = new TreeSet<>(new Comparator<ConfiguredObjectRecord>()
-                    {
-                        @Override
-                        public int compare(final ConfiguredObjectRecord left, final ConfiguredObjectRecord right)
-                        {
-                            String leftName = (String) left.getAttributes().get(ConfiguredObject.NAME);
-                            String rightName = (String) right.getAttributes().get(ConfiguredObject.NAME);
-                            return leftName == null
-                                    ?  -1
-                                    : rightName == null ? 1 : leftName.compareTo(rightName);
-                        }
-                    });
+                    List<ConfiguredObjectRecord> sortedChildren = new ArrayList<>();
                     for(UUID childId : childIds)
                     {
                         ConfiguredObjectRecord childRecord = _objectsById.get(childId);
@@ -452,6 +462,9 @@ public class JsonFileConfigStore impleme
                             sortedChildren.add(childRecord);
                         }
                     }
+
+                    Collections.sort(sortedChildren, CONFIGURED_OBJECT_RECORD_COMPARATOR);
+
                     for(ConfiguredObjectRecord childRecord : sortedChildren)
                     {
                         entities.add(build(childClass, childRecord.getId()));
@@ -505,6 +518,13 @@ public class JsonFileConfigStore impleme
             final UUID id = record.getId();
             final String type = record.getType();
 
+            if(record.getAttributes() == null || !(record.getAttributes().get(ConfiguredObject.NAME) instanceof String))
+            {
+                throw new StoreException("The record " + id + " of type " + type + " does not have an attribute '"
+                                         + ConfiguredObject.NAME
+                                         + "' of type String");
+            }
+
             if(_objectsById.containsKey(id))
             {
                 final ConfiguredObjectRecord existingRecord = _objectsById.get(id);

Modified: qpid/trunk/qpid/java/broker-core/src/test/java/org/apache/qpid/server/store/JsonFileConfigStoreTest.java
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/java/broker-core/src/test/java/org/apache/qpid/server/store/JsonFileConfigStoreTest.java?rev=1619322&r1=1619321&r2=1619322&view=diff
==============================================================================
--- qpid/trunk/qpid/java/broker-core/src/test/java/org/apache/qpid/server/store/JsonFileConfigStoreTest.java (original)
+++ qpid/trunk/qpid/java/broker-core/src/test/java/org/apache/qpid/server/store/JsonFileConfigStoreTest.java Thu Aug 21 09:20:42 2014
@@ -38,6 +38,7 @@ import org.mockito.ArgumentMatcher;
 import org.mockito.InOrder;
 
 import org.apache.qpid.server.model.BrokerModel;
+import org.apache.qpid.server.model.ConfiguredObject;
 import org.apache.qpid.server.model.ConfiguredObjectFactory;
 import org.apache.qpid.server.model.ConfiguredObjectFactoryImpl;
 import org.apache.qpid.server.model.Queue;
@@ -248,10 +249,14 @@ public class JsonFileConfigStoreTest ext
         createRootRecord();
 
         final UUID id = UUID.randomUUID();
-        _store.create(new ConfiguredObjectRecordImpl(id, "Queue", Collections.<String, Object>emptyMap(), getRootAsParentMap()));
+        _store.create(new ConfiguredObjectRecordImpl(id, "Queue",
+                                                     Collections.<String, Object>singletonMap(ConfiguredObject.NAME, "queue"),
+                                                     getRootAsParentMap()));
         try
         {
-            _store.create(new ConfiguredObjectRecordImpl(id, "Exchange", Collections.<String, Object>emptyMap(), getRootAsParentMap()));
+            _store.create(new ConfiguredObjectRecordImpl(id, "Exchange",
+                                                         Collections.<String, Object>singletonMap(ConfiguredObject.NAME, "exchange"),
+                                                         getRootAsParentMap()));
             fail("Should not be able to create two objects with same id");
         }
         catch (StoreException e)
@@ -261,19 +266,61 @@ public class JsonFileConfigStoreTest ext
     }
 
 
+    public void testObjectWithoutName() throws Exception
+    {
+        _store.openConfigurationStore(_parent, false);
+        createRootRecord();
+
+        final UUID id = UUID.randomUUID();
+        try
+        {
+            _store.create(new ConfiguredObjectRecordImpl(id, "Exchange",
+                                                         Collections.<String, Object>emptyMap(),
+                                                         getRootAsParentMap()));
+            fail("Should not be able to create an object without a name");
+        }
+        catch (StoreException e)
+        {
+            // pass
+        }
+    }
+
+    public void testObjectWithNonStringName() throws Exception
+    {
+        _store.openConfigurationStore(_parent, false);
+        createRootRecord();
+
+        final UUID id = UUID.randomUUID();
+        try
+        {
+            _store.update(true, new ConfiguredObjectRecordImpl(id, "Exchange",
+                                                         Collections.<String, Object>singletonMap(ConfiguredObject.NAME, 3),
+                                                         getRootAsParentMap()));
+            fail("Should not be able to create an object without a name");
+        }
+        catch (StoreException e)
+        {
+            // pass
+        }
+    }
+
     public void testChangeTypeOfObject() throws Exception
     {
         _store.openConfigurationStore(_parent, false);
         createRootRecord();
 
         final UUID id = UUID.randomUUID();
-        _store.create(new ConfiguredObjectRecordImpl(id, "Queue", Collections.<String, Object>emptyMap(), getRootAsParentMap()));
+        _store.create(new ConfiguredObjectRecordImpl(id, "Queue",
+                                                     Collections.<String, Object>singletonMap(ConfiguredObject.NAME, "queue"),
+                                                     getRootAsParentMap()));
         _store.closeConfigurationStore();
         _store.openConfigurationStore(_parent, false);
 
         try
         {
-            _store.update(false, new ConfiguredObjectRecordImpl(id, "Exchange", Collections.<String, Object>emptyMap(), getRootAsParentMap()));
+            _store.update(false, new ConfiguredObjectRecordImpl(id, "Exchange",
+                                                                Collections.<String, Object>singletonMap(ConfiguredObject.NAME, "exchange"),
+                                                                getRootAsParentMap()));
             fail("Should not be able to update object to different type");
         }
         catch (StoreException e)
@@ -329,40 +376,57 @@ public class JsonFileConfigStoreTest ext
         final UUID queueId = new UUID(0, 1);
         final UUID queue2Id = new UUID(1, 1);
 
-        final Map<String, Object> EMPTY_ATTR = Collections.emptyMap();
         final UUID exchangeId = new UUID(0, 2);
 
         final UUID bindingId = new UUID(0, 3);
         final UUID binding2Id = new UUID(1, 3);
 
         Map<String, UUID> parents = getRootAsParentMap();
-        final ConfiguredObjectRecordImpl queueRecord = new ConfiguredObjectRecordImpl(queueId, "Queue", EMPTY_ATTR, parents);
+        Map<String, Object> queueAttr = Collections.<String, Object>singletonMap(ConfiguredObject.NAME, "queue");
+        final ConfiguredObjectRecordImpl queueRecord =
+                new ConfiguredObjectRecordImpl(queueId, "Queue",
+                                               queueAttr,
+                                               parents);
         _store.create(queueRecord);
-        final ConfiguredObjectRecordImpl queue2Record = new ConfiguredObjectRecordImpl(queue2Id, "Queue", EMPTY_ATTR, parents);
+        Map<String, Object> queue2Attr = Collections.<String, Object>singletonMap(ConfiguredObject.NAME, "queue2");
+        final ConfiguredObjectRecordImpl queue2Record =
+                new ConfiguredObjectRecordImpl(queue2Id, "Queue",
+                                               queue2Attr,
+                                               parents);
         _store.create(queue2Record);
-        final ConfiguredObjectRecordImpl exchangeRecord = new ConfiguredObjectRecordImpl(exchangeId, "Exchange", EMPTY_ATTR, parents);
+        Map<String, Object> exchangeAttr = Collections.<String, Object>singletonMap(ConfiguredObject.NAME, "exchange");
+        final ConfiguredObjectRecordImpl exchangeRecord =
+                new ConfiguredObjectRecordImpl(exchangeId, "Exchange",
+                                               exchangeAttr,
+                                               parents);
         _store.create(exchangeRecord);
         Map<String,UUID> bindingParents = new HashMap();
         bindingParents.put("Exchange", exchangeRecord.getId());
         bindingParents.put("Queue", queueRecord.getId());
+        Map<String, Object> bindingAttr = Collections.<String, Object>singletonMap(ConfiguredObject.NAME, "binding");
         final ConfiguredObjectRecordImpl bindingRecord =
-                new ConfiguredObjectRecordImpl(bindingId, "Binding", EMPTY_ATTR, bindingParents);
+                new ConfiguredObjectRecordImpl(bindingId, "Binding",
+                                               bindingAttr,
+                                               bindingParents);
 
 
         Map<String,UUID> binding2Parents = new HashMap();
         binding2Parents.put("Exchange", exchangeRecord.getId());
         binding2Parents.put("Queue", queue2Record.getId());
+        Map<String, Object> binding2Attr = Collections.<String, Object>singletonMap(ConfiguredObject.NAME, "binding2");
         final ConfiguredObjectRecordImpl binding2Record =
-                new ConfiguredObjectRecordImpl(binding2Id, "Binding", EMPTY_ATTR, binding2Parents);
+                new ConfiguredObjectRecordImpl(binding2Id, "Binding",
+                                               binding2Attr,
+                                               binding2Parents);
         _store.update(true, bindingRecord, binding2Record);
         _store.closeConfigurationStore();
         _store.openConfigurationStore(_parent, false);
         _store.visitConfiguredObjectRecords(_handler);
-        verify(_handler).handle(matchesRecord(queueId, "Queue", EMPTY_ATTR));
-        verify(_handler).handle(matchesRecord(queue2Id, "Queue", EMPTY_ATTR));
-        verify(_handler).handle(matchesRecord(exchangeId, "Exchange", EMPTY_ATTR));
-        verify(_handler).handle(matchesRecord(bindingId, "Binding", EMPTY_ATTR));
-        verify(_handler).handle(matchesRecord(binding2Id, "Binding", EMPTY_ATTR));
+        verify(_handler).handle(matchesRecord(queueId, "Queue", queueAttr));
+        verify(_handler).handle(matchesRecord(queue2Id, "Queue", queue2Attr));
+        verify(_handler).handle(matchesRecord(exchangeId, "Exchange", exchangeAttr));
+        verify(_handler).handle(matchesRecord(bindingId, "Binding", bindingAttr));
+        verify(_handler).handle(matchesRecord(binding2Id, "Binding", binding2Attr));
         _store.closeConfigurationStore();
 
     }
@@ -371,7 +435,10 @@ public class JsonFileConfigStoreTest ext
     private void createRootRecord()
     {
         UUID rootRecordId = UUID.randomUUID();
-        _rootRecord = new ConfiguredObjectRecordImpl(rootRecordId, VIRTUAL_HOST_TYPE, Collections.<String, Object>emptyMap());
+        _rootRecord =
+                new ConfiguredObjectRecordImpl(rootRecordId,
+                                               VIRTUAL_HOST_TYPE,
+                                               Collections.<String, Object>singletonMap(ConfiguredObject.NAME, "root"));
         _store.create(_rootRecord);
     }
 



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