From a934400705ab24abe0dae08c50072a00c0f9a488 Mon Sep 17 00:00:00 2001 From: Robert Godfrey Date: Wed, 26 Nov 2014 19:57:18 +0000 Subject: QPID-6246 : @ManagedAnnotation should only be applied to interfaces which extend ManagedInterface and should not be inherited git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@1641910 13f79535-47bb-0310-9956-ffa450edef68 --- .../validation/ManagedAnnotationValidator.java | 98 +++++++ .../services/javax.annotation.processing.Processor | 1 + .../server/model/ConfiguredObjectTypeRegistry.java | 296 ++++++++++----------- .../qpid/server/model/ManagedAnnotation.java | 2 - .../model/ConfigureObjectTypeRegistryTest.java | 204 -------------- .../model/ConfiguredObjectTypeRegistryTest.java | 204 ++++++++++++++ .../server/model/testmodel/TestManagedClass2.java | 4 +- .../server/model/testmodel/TestManagedClass3.java | 8 +- .../model/testmodel/TestManagedInterface3.java | 29 ++ 9 files changed, 477 insertions(+), 369 deletions(-) create mode 100644 qpid/java/broker-codegen/src/main/java/org/apache/qpid/server/model/validation/ManagedAnnotationValidator.java delete mode 100644 qpid/java/broker-core/src/test/java/org/apache/qpid/server/model/ConfigureObjectTypeRegistryTest.java create mode 100644 qpid/java/broker-core/src/test/java/org/apache/qpid/server/model/ConfiguredObjectTypeRegistryTest.java create mode 100644 qpid/java/broker-core/src/test/java/org/apache/qpid/server/model/testmodel/TestManagedInterface3.java (limited to 'qpid/java') diff --git a/qpid/java/broker-codegen/src/main/java/org/apache/qpid/server/model/validation/ManagedAnnotationValidator.java b/qpid/java/broker-codegen/src/main/java/org/apache/qpid/server/model/validation/ManagedAnnotationValidator.java new file mode 100644 index 0000000000..b38866147b --- /dev/null +++ b/qpid/java/broker-codegen/src/main/java/org/apache/qpid/server/model/validation/ManagedAnnotationValidator.java @@ -0,0 +1,98 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.server.model.validation; + +import java.util.Set; + +import javax.annotation.processing.AbstractProcessor; +import javax.annotation.processing.RoundEnvironment; +import javax.annotation.processing.SupportedAnnotationTypes; +import javax.lang.model.SourceVersion; +import javax.lang.model.element.Element; +import javax.lang.model.element.ElementKind; +import javax.lang.model.element.TypeElement; +import javax.lang.model.type.TypeMirror; +import javax.lang.model.util.Elements; +import javax.lang.model.util.Types; +import javax.tools.Diagnostic; + +@SupportedAnnotationTypes(ManagedAnnotationValidator.MANAGED_ANNOTATION_CLASS_NAME) +public class ManagedAnnotationValidator extends AbstractProcessor +{ + public static final String MANAGED_ANNOTATION_CLASS_NAME = "org.apache.qpid.server.model.ManagedAnnotation"; + + + @Override + public SourceVersion getSupportedSourceVersion() + { + return SourceVersion.latest(); + } + + + @Override + public boolean process(final Set annotations, final RoundEnvironment roundEnv) + { + Elements elementUtils = processingEnv.getElementUtils(); + Types typeUtils = processingEnv.getTypeUtils(); + + TypeElement annotationElement = elementUtils.getTypeElement(MANAGED_ANNOTATION_CLASS_NAME); + + String className = "org.apache.qpid.server.model.ManagedInterface"; + TypeMirror configuredObjectType = getErasure(className); + + for (Element e : roundEnv.getElementsAnnotatedWith(annotationElement)) + { + if (e.getKind() != ElementKind.INTERFACE) + { + processingEnv.getMessager() + .printMessage(Diagnostic.Kind.ERROR, + "@" + + annotationElement.getSimpleName() + + " can only be applied to an interface", + e + ); + } + + + if(!typeUtils.isAssignable(typeUtils.erasure(e.asType()), configuredObjectType)) + { + + processingEnv.getMessager() + .printMessage(Diagnostic.Kind.ERROR, + "@" + + annotationElement.getSimpleName() + + " can only be applied to an interface which extends " + className, + e + ); + } + } + + return false; + } + + + private TypeMirror getErasure(final String className) + { + final Types typeUtils = processingEnv.getTypeUtils(); + final Elements elementUtils = processingEnv.getElementUtils(); + return typeUtils.erasure(elementUtils.getTypeElement(className).asType()); + } +} diff --git a/qpid/java/broker-codegen/src/main/resources/META-INF/services/javax.annotation.processing.Processor b/qpid/java/broker-codegen/src/main/resources/META-INF/services/javax.annotation.processing.Processor index 64440bb999..7f88042433 100644 --- a/qpid/java/broker-codegen/src/main/resources/META-INF/services/javax.annotation.processing.Processor +++ b/qpid/java/broker-codegen/src/main/resources/META-INF/services/javax.annotation.processing.Processor @@ -22,3 +22,4 @@ org.apache.qpid.server.plugin.PluggableProcessor org.apache.qpid.server.model.ConfiguredObjectRegistrationGenerator org.apache.qpid.server.model.validation.AttributeAnnotationValidator org.apache.qpid.server.model.validation.AttributeFieldValidation +org.apache.qpid.server.model.validation.ManagedAnnotationValidator diff --git a/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/ConfiguredObjectTypeRegistry.java b/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/ConfiguredObjectTypeRegistry.java index 70a64e8130..8ca5ff3d6a 100644 --- a/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/ConfiguredObjectTypeRegistry.java +++ b/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/ConfiguredObjectTypeRegistry.java @@ -20,7 +20,6 @@ */ package org.apache.qpid.server.model; -import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; @@ -41,6 +40,7 @@ import java.util.TreeSet; import org.apache.log4j.Logger; import org.apache.qpid.server.plugin.ConfiguredObjectRegistration; +import org.apache.qpid.server.util.Action; import org.apache.qpid.server.util.ServerScopedRuntimeException; import org.apache.qpid.util.Strings; @@ -385,6 +385,7 @@ public class ConfiguredObjectTypeRegistry } } + for(Class iface : clazz.getInterfaces() ) { if(ConfiguredObject.class.isAssignableFrom(iface)) @@ -455,94 +456,37 @@ public class ConfiguredObjectTypeRegistry return; } - - for(Class parent : clazz.getInterfaces()) + doWithAllParents(clazz, new Action>() { - if(ConfiguredObject.class.isAssignableFrom(parent)) + @Override + public void performAction(final Class parent) { - process((Class) parent); + process(parent); } - } - final Class superclass = clazz.getSuperclass(); - if(superclass != null && ConfiguredObject.class.isAssignableFrom(superclass)) - { - process((Class) superclass); - } + }); final SortedSet> attributeSet = new TreeSet<>(OBJECT_NAME_COMPARATOR); final SortedSet> statisticSet = new TreeSet<>(OBJECT_NAME_COMPARATOR); + final Set> managedInterfaces = new HashSet<>(); _allAttributes.put(clazz, attributeSet); _allStatistics.put(clazz, statisticSet); + _allManagedInterfaces.put(clazz, managedInterfaces); - for(Class parent : clazz.getInterfaces()) + doWithAllParents(clazz, new Action>() { - if(ConfiguredObject.class.isAssignableFrom(parent)) + @Override + public void performAction(final Class parent) { initialiseWithParentAttributes(attributeSet, statisticSet, - (Class) parent); - } - } - if(superclass != null && ConfiguredObject.class.isAssignableFrom(superclass)) - { - initialiseWithParentAttributes(attributeSet, - statisticSet, - (Class) superclass); - } - - - for(Method m : clazz.getDeclaredMethods()) - { - - if(m.isAnnotationPresent(ManagedAttribute.class)) - { - ManagedAttribute annotation = m.getAnnotation(ManagedAttribute.class); - - if(!clazz.isInterface() || !ConfiguredObject.class.isAssignableFrom(clazz)) - { - throw new ServerScopedRuntimeException("Can only define ManagedAttributes on interfaces which extend " + ConfiguredObject.class.getSimpleName() + ". " + clazz.getSimpleName() + " does not meet these criteria."); - } + managedInterfaces, + parent); - ConfiguredObjectAttribute attribute = new ConfiguredAutomatedAttribute<>(clazz, m, annotation); - if(attributeSet.contains(attribute)) - { - attributeSet.remove(attribute); - } - attributeSet.add(attribute); } - else if(m.isAnnotationPresent(DerivedAttribute.class)) - { - DerivedAttribute annotation = m.getAnnotation(DerivedAttribute.class); - - if(!clazz.isInterface() || !ConfiguredObject.class.isAssignableFrom(clazz)) - { - throw new ServerScopedRuntimeException("Can only define DerivedAttributes on interfaces which extend " + ConfiguredObject.class.getSimpleName() + ". " + clazz.getSimpleName() + " does not meet these criteria."); - } - - ConfiguredObjectAttribute attribute = new ConfiguredDerivedAttribute<>(clazz, m, annotation); - if(attributeSet.contains(attribute)) - { - attributeSet.remove(attribute); - } - attributeSet.add(attribute); + }); - } - else if(m.isAnnotationPresent(ManagedStatistic.class)) - { - ManagedStatistic statAnnotation = m.getAnnotation(ManagedStatistic.class); - if(!clazz.isInterface() || !ConfiguredObject.class.isAssignableFrom(clazz)) - { - throw new ServerScopedRuntimeException("Can only define ManagedStatistics on interfaces which extend " + ConfiguredObject.class.getSimpleName() + ". " + clazz.getSimpleName() + " does not meet these criteria."); - } - ConfiguredObjectStatistic statistic = new ConfiguredObjectStatistic(clazz, m); - if(statisticSet.contains(statistic)) - { - statisticSet.remove(statistic); - } - statisticSet.add(statistic); - } - } + processMethods(clazz, attributeSet, statisticSet); processAttributesTypesAndFields(clazz); @@ -554,28 +498,117 @@ public class ConfiguredObjectTypeRegistry } } - private void initialiseWithParentAttributes(final SortedSet> attributeSet, - final SortedSet> statisticSet, - final Class parent) + private static void doWithAllParents(Class clazz, Action> action) { - Collection> attrs = _allAttributes.get(parent); - for(ConfiguredObjectAttribute attr : attrs) + for(Class parent : clazz.getInterfaces()) { - if(!attributeSet.contains(attr)) + if(ConfiguredObject.class.isAssignableFrom(parent)) { - attributeSet.add(attr); + action.performAction((Class) parent); } } - Collection> stats = _allStatistics.get(parent); - for(ConfiguredObjectStatistic stat : stats) + final Class superclass = clazz.getSuperclass(); + if(superclass != null && ConfiguredObject.class.isAssignableFrom(superclass)) { - if(!statisticSet.contains(stat)) - { - statisticSet.add(stat); - } + action.performAction((Class) superclass); + } + } + + private void processMethods(final Class clazz, + final SortedSet> attributeSet, + final SortedSet> statisticSet) + { + for(Method method : clazz.getDeclaredMethods()) + { + processMethod(clazz, attributeSet, statisticSet, method); + } + } + + private void processMethod(final Class clazz, + final SortedSet> attributeSet, + final SortedSet> statisticSet, + final Method m) + { + if(m.isAnnotationPresent(ManagedAttribute.class)) + { + processManagedAttribute(clazz, attributeSet, m); + } + else if(m.isAnnotationPresent(DerivedAttribute.class)) + { + processDerivedAttribute(clazz, attributeSet, m); + + } + else if(m.isAnnotationPresent(ManagedStatistic.class)) + { + processManagedStatistic(clazz, statisticSet, m); } } + private void processManagedStatistic(final Class clazz, + final SortedSet> statisticSet, + final Method m) + { + ManagedStatistic statAnnotation = m.getAnnotation(ManagedStatistic.class); + if(!clazz.isInterface() || !ConfiguredObject.class.isAssignableFrom(clazz)) + { + throw new ServerScopedRuntimeException("Can only define ManagedStatistics on interfaces which extend " + ConfiguredObject.class.getSimpleName() + ". " + clazz.getSimpleName() + " does not meet these criteria."); + } + ConfiguredObjectStatistic statistic = new ConfiguredObjectStatistic(clazz, m); + if(statisticSet.contains(statistic)) + { + statisticSet.remove(statistic); + } + statisticSet.add(statistic); + } + + private void processDerivedAttribute(final Class clazz, + final SortedSet> attributeSet, + final Method m) + { + DerivedAttribute annotation = m.getAnnotation(DerivedAttribute.class); + + if(!clazz.isInterface() || !ConfiguredObject.class.isAssignableFrom(clazz)) + { + throw new ServerScopedRuntimeException("Can only define DerivedAttributes on interfaces which extend " + ConfiguredObject.class.getSimpleName() + ". " + clazz.getSimpleName() + " does not meet these criteria."); + } + + ConfiguredObjectAttribute attribute = new ConfiguredDerivedAttribute<>(clazz, m, annotation); + if(attributeSet.contains(attribute)) + { + attributeSet.remove(attribute); + } + attributeSet.add(attribute); + } + + private void processManagedAttribute(final Class clazz, + final SortedSet> attributeSet, + final Method m) + { + ManagedAttribute annotation = m.getAnnotation(ManagedAttribute.class); + + if(!clazz.isInterface() || !ConfiguredObject.class.isAssignableFrom(clazz)) + { + throw new ServerScopedRuntimeException("Can only define ManagedAttributes on interfaces which extend " + ConfiguredObject.class.getSimpleName() + ". " + clazz.getSimpleName() + " does not meet these criteria."); + } + + ConfiguredObjectAttribute attribute = new ConfiguredAutomatedAttribute<>(clazz, m, annotation); + if(attributeSet.contains(attribute)) + { + attributeSet.remove(attribute); + } + attributeSet.add(attribute); + } + + private void initialiseWithParentAttributes(final SortedSet> attributeSet, + final SortedSet> statisticSet, + final Set> managedInterfaces, + final Class parent) + { + attributeSet.addAll(_allAttributes.get(parent)); + statisticSet.addAll(_allStatistics.get(parent)); + managedInterfaces.addAll(_allManagedInterfaces.get(parent)); + } + private void processAttributesTypesAndFields(final Class clazz) { Map> attrMap = new TreeMap<>(NAME_COMPARATOR); @@ -625,25 +658,20 @@ public class ConfiguredObjectTypeRegistry private void processStateChangeMethods(Class clazz) { - Map> map = new HashMap<>(); + final Map> map = new HashMap<>(); _stateChangeMethods.put(clazz, map); addStateTransitions(clazz, map); - for(Class parent : clazz.getInterfaces()) + + doWithAllParents(clazz, new Action>() { - if(ConfiguredObject.class.isAssignableFrom(parent)) + @Override + public void performAction(final Class parent) { - inheritTransitions((Class) parent, map); + inheritTransitions(parent, map); } - } - - Class superclass = clazz.getSuperclass(); - - if(superclass != null && ConfiguredObject.class.isAssignableFrom(superclass)) - { - inheritTransitions((Class) superclass, map); - } + }); } private void inheritTransitions(final Class parent, @@ -811,21 +839,23 @@ public class ConfiguredObjectTypeRegistry protected Collection> getAttributes(final Class clazz) { - if(!_allAttributes.containsKey(clazz)) - { - process(clazz); - } + processClassIfNecessary(clazz); final Collection> attributes = (Collection) _allAttributes.get(clazz); return attributes; } - - protected Collection getStatistics(final Class clazz) + private void processClassIfNecessary(final Class clazz) { if(!_allAttributes.containsKey(clazz)) { process(clazz); } + } + + + protected Collection getStatistics(final Class clazz) + { + processClassIfNecessary(clazz); final Collection statistics = (Collection) _allStatistics.get(clazz); return statistics; } @@ -833,28 +863,19 @@ public class ConfiguredObjectTypeRegistry public Map> getAttributeTypes(final Class clazz) { - if(!_allAttributes.containsKey(clazz)) - { - process(clazz); - } + processClassIfNecessary(clazz); return _allAttributeTypes.get(clazz); } Map getAutomatedFields(Class clazz) { - if(!_allAttributes.containsKey(clazz)) - { - process(clazz); - } + processClassIfNecessary(clazz); return _allAutomatedFields.get(clazz); } Map> getStateChangeMethods(final Class objectClass) { - if(!_allAttributes.containsKey(objectClass)) - { - process(objectClass); - } + processClassIfNecessary(objectClass); Map> map = _stateChangeMethods.get(objectClass); return map != null ? Collections.unmodifiableMap(map) : Collections.>emptyMap(); @@ -868,10 +889,7 @@ public class ConfiguredObjectTypeRegistry public Set> getManagedInterfaces(final Class classObject) { - if (!_allManagedInterfaces.containsKey(classObject)) - { - process(classObject); - } + processClassIfNecessary(classObject); Set> interfaces = _allManagedInterfaces.get(classObject); return interfaces == null ? Collections.>emptySet() : interfaces; } @@ -879,46 +897,14 @@ public class ConfiguredObjectTypeRegistry private void processManagedInterfaces(Class clazz) { - Set> managedInterfaces = new HashSet<>(); - if (checkManagedAnnotationAndFindManagedInterfaces(clazz, managedInterfaces, false)) + final Set> managedInterfaces = _allManagedInterfaces.get(clazz); + for(Class iface : clazz.getInterfaces()) { - _allManagedInterfaces.put(clazz, Collections.unmodifiableSet(managedInterfaces)); - } - else - { - _allManagedInterfaces.put(clazz, Collections.>emptySet()); - } - } - - private boolean checkManagedAnnotationAndFindManagedInterfaces(Class type, Set> managedInterfaces, boolean hasManagedAnnotation) - { - while(type != null && ManagedInterface.class.isAssignableFrom(type)) - { - Annotation[] annotations = type.getAnnotations(); - for (Annotation annotation : annotations) - { - if (annotation instanceof ManagedAnnotation) - { - hasManagedAnnotation = true; - } - } - - if (hasManagedAnnotation && type.isInterface() && ManagedInterface.class.isAssignableFrom(type) && type != ManagedInterface.class) - { - managedInterfaces.add((Class)type); - } - - Class[] interfaceClasses = type.getInterfaces(); - for (Class interfaceClass : interfaceClasses) + if (iface.isAnnotationPresent(ManagedAnnotation.class) && ManagedInterface.class.isAssignableFrom(iface)) { - if (checkManagedAnnotationAndFindManagedInterfaces(interfaceClass, managedInterfaces, hasManagedAnnotation)) - { - hasManagedAnnotation = true; - } + managedInterfaces.add((Class) iface); } - type = type.getSuperclass(); } - return hasManagedAnnotation; } } diff --git a/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/ManagedAnnotation.java b/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/ManagedAnnotation.java index 80b25fd7f8..ddd2396e26 100644 --- a/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/ManagedAnnotation.java +++ b/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/ManagedAnnotation.java @@ -21,14 +21,12 @@ package org.apache.qpid.server.model; import java.lang.annotation.ElementType; -import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) -@Inherited public @interface ManagedAnnotation { } diff --git a/qpid/java/broker-core/src/test/java/org/apache/qpid/server/model/ConfigureObjectTypeRegistryTest.java b/qpid/java/broker-core/src/test/java/org/apache/qpid/server/model/ConfigureObjectTypeRegistryTest.java deleted file mode 100644 index 68186cc534..0000000000 --- a/qpid/java/broker-core/src/test/java/org/apache/qpid/server/model/ConfigureObjectTypeRegistryTest.java +++ /dev/null @@ -1,204 +0,0 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.server.model; - -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.HashSet; -import java.util.Set; - -import junit.framework.TestCase; - -import org.apache.qpid.server.model.testmodel.TestManagedClass0; -import org.apache.qpid.server.model.testmodel.TestManagedClass1; -import org.apache.qpid.server.model.testmodel.TestManagedClass2; -import org.apache.qpid.server.model.testmodel.TestManagedClass3; -import org.apache.qpid.server.model.testmodel.TestManagedClass4; -import org.apache.qpid.server.model.testmodel.TestManagedClass5; -import org.apache.qpid.server.model.testmodel.TestManagedInterface1; -import org.apache.qpid.server.model.testmodel.TestManagedInterface2; -import org.apache.qpid.server.model.testmodel.Test2RootCategory; -import org.apache.qpid.server.model.testmodel.Test2RootCategoryImpl; -import org.apache.qpid.server.model.testmodel.TestChildCategory; -import org.apache.qpid.server.model.testmodel.TestModel; -import org.apache.qpid.server.model.testmodel.TestRootCategory; -import org.apache.qpid.server.model.testmodel.TestRootCategoryImpl; -import org.apache.qpid.server.plugin.ConfiguredObjectRegistration; - -public class ConfigureObjectTypeRegistryTest extends TestCase -{ - private ConfiguredObjectTypeRegistry _typeRegistry; - - @Override - public void setUp() throws Exception - { - super.setUp(); - Model model = TestModel.getInstance(); - _typeRegistry = model.getTypeRegistry(); - } - - public void testAllTypesRegistered() - { - Collection> types = - _typeRegistry.getTypeSpecialisations(TestRootCategory.class); - - assertEquals(2, types.size()); - assertTrue(types.contains(TestRootCategoryImpl.class)); - - assertTrue(types.contains(Test2RootCategoryImpl.class)); - } - - public void testTypeSpecificAttributes() - { - Collection> special = - _typeRegistry.getTypeSpecificAttributes(Test2RootCategoryImpl.class); - assertEquals(1, special.size()); - ConfiguredObjectAttribute attr = special.iterator().next(); - assertEquals("derivedAttribute",attr.getName()); - assertTrue(attr.isDerived()); - - special = _typeRegistry.getTypeSpecificAttributes(TestRootCategoryImpl.class); - assertEquals(0, special.size()); - - } - - public void testDefaultedValues() - { - checkDefaultedValue(_typeRegistry.getAttributes((Class) TestRootCategoryImpl.class), - TestRootCategory.DEFAULTED_VALUE_DEFAULT); - - checkDefaultedValue(_typeRegistry.getAttributes((Class) Test2RootCategoryImpl.class), - Test2RootCategory.DEFAULTED_VALUE_DEFAULT); - } - - public void testValidValues() - { - checkValidValues("validValue",_typeRegistry.getAttributes((Class) TestRootCategoryImpl.class), - Arrays.asList( TestRootCategory.VALID_VALUE1, TestRootCategory.VALID_VALUE2 ) ); - - checkValidValues("validValue", _typeRegistry.getAttributes((Class) Test2RootCategoryImpl.class), - Test2RootCategoryImpl.functionGeneratedValidValues()); - - - checkValidValues("validValueNotInterpolated", _typeRegistry.getAttributes((Class) TestChildCategory.class), - Arrays.asList(TestChildCategory.NON_INTERPOLATED_VALID_VALUE)); - - - } - - public void testGetManagedInterfacesForTypeNotImplementingManagedInterfaceAndNotHavingManagedAnnotation() - { - ConfiguredObjectTypeRegistry typeRegistry = createConfiguredObjectTypeRegistry(TestRootCategoryImpl.class); - assertEquals("Unexpected interfaces from object not implementing Managed interfaces", - Collections.emptySet(), typeRegistry.getManagedInterfaces(TestRootCategory.class)); - } - - public void testGetManagedInterfacesForTypeImplementingManagedInterfaceButNotHavingManagedAnnotation() - { - ConfiguredObjectTypeRegistry typeRegistry = createConfiguredObjectTypeRegistry(TestRootCategoryImpl.class, TestManagedClass5.class); - assertEquals("Unexpected interfaces from object not implementing Managed interfaces", - Collections.emptySet(), typeRegistry.getManagedInterfaces(TestManagedClass5.class)); - } - - public void testGetManagedInterfacesForTypesImplementingManagedInterfacesWithManagedAnnotation() - { - ConfiguredObjectTypeRegistry typeRegistry = createConfiguredObjectTypeRegistry(TestRootCategoryImpl.class, TestManagedClass0.class, TestManagedClass1.class, TestManagedClass4.class); - Set> expected = Collections.>singleton(TestManagedInterface1.class); - assertEquals("Unexpected interfaces on child class", expected, typeRegistry.getManagedInterfaces(TestManagedClass1.class)); - assertEquals("Unexpected interfaces on super class", expected, typeRegistry.getManagedInterfaces(TestManagedClass0.class)); - assertEquals("Unexpected interfaces on class implementing interface with annotation twice", - expected, typeRegistry.getManagedInterfaces(TestManagedClass4.class)); - } - - public void testGetManagedInterfacesForTypeHavingDirectManagedAnnotation() - { - ConfiguredObjectTypeRegistry typeRegistry = createConfiguredObjectTypeRegistry(TestRootCategoryImpl.class, TestManagedClass2.class, TestManagedClass3.class); - - assertEquals("Unexpected interfaces on class implementing 2 interfaces with annotation", - new HashSet<>(Arrays.asList(TestManagedInterface2.class, TestManagedInterface1.class)), typeRegistry.getManagedInterfaces(TestManagedClass2.class)); - assertEquals("Unexpected interfaces on class implementing 2 direct interfaces with annotation", - new HashSet<>(Arrays.asList(TestManagedInterface2.class, TestManagedInterface1.class)), typeRegistry.getManagedInterfaces(TestManagedClass3.class)); - - } - - private ConfiguredObjectTypeRegistry createConfiguredObjectTypeRegistry(Class... supportedTypes) - { - ConfiguredObjectRegistration configuredObjectRegistration = createConfiguredObjectRegistration(supportedTypes); - - return new ConfiguredObjectTypeRegistry(Arrays.asList(configuredObjectRegistration), Arrays.asList(TestRootCategory.class, TestChildCategory.class)); - } - - private ConfiguredObjectRegistration createConfiguredObjectRegistration(final Class... supportedTypes) - { - return new ConfiguredObjectRegistration() - { - @Override - public Collection> getConfiguredObjectClasses() - { - return Arrays.asList(supportedTypes); - } - - @Override - public String getType() - { - return "test"; - } - }; - } - - private void checkDefaultedValue(final Collection> attrs, - final String defaultedValueDefault) - { - boolean found = false; - for(ConfiguredObjectAttribute attr : attrs) - { - if(attr.getName().equals("defaultedValue")) - { - assertEquals(defaultedValueDefault, ((ConfiguredAutomatedAttribute)attr).defaultValue()); - found = true; - break; - } - - } - assertTrue("Could not find attribute defaultedValue", found); - } - - private void checkValidValues(final String attrName, final Collection> attrs, - final Collection validValues) - { - boolean found = false; - for(ConfiguredObjectAttribute attr : attrs) - { - if(attr.getName().equals(attrName)) - { - Collection foundValues = ((ConfiguredAutomatedAttribute) attr).validValues(); - assertEquals("Valid values not as expected, counts differ", validValues.size(), foundValues.size()); - assertTrue("Valid values do not include all expected values", foundValues.containsAll(validValues)); - assertTrue("Valid values contain unexpected addtional values", validValues.containsAll(foundValues)); - found = true; - break; - } - - } - assertTrue("Could not find attribute " + attrName, found); - } -} diff --git a/qpid/java/broker-core/src/test/java/org/apache/qpid/server/model/ConfiguredObjectTypeRegistryTest.java b/qpid/java/broker-core/src/test/java/org/apache/qpid/server/model/ConfiguredObjectTypeRegistryTest.java new file mode 100644 index 0000000000..426526dbea --- /dev/null +++ b/qpid/java/broker-core/src/test/java/org/apache/qpid/server/model/ConfiguredObjectTypeRegistryTest.java @@ -0,0 +1,204 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.server.model; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +import junit.framework.TestCase; + +import org.apache.qpid.server.model.testmodel.Test2RootCategory; +import org.apache.qpid.server.model.testmodel.Test2RootCategoryImpl; +import org.apache.qpid.server.model.testmodel.TestChildCategory; +import org.apache.qpid.server.model.testmodel.TestManagedClass0; +import org.apache.qpid.server.model.testmodel.TestManagedClass1; +import org.apache.qpid.server.model.testmodel.TestManagedClass2; +import org.apache.qpid.server.model.testmodel.TestManagedClass3; +import org.apache.qpid.server.model.testmodel.TestManagedClass4; +import org.apache.qpid.server.model.testmodel.TestManagedClass5; +import org.apache.qpid.server.model.testmodel.TestManagedInterface1; +import org.apache.qpid.server.model.testmodel.TestManagedInterface3; +import org.apache.qpid.server.model.testmodel.TestModel; +import org.apache.qpid.server.model.testmodel.TestRootCategory; +import org.apache.qpid.server.model.testmodel.TestRootCategoryImpl; +import org.apache.qpid.server.plugin.ConfiguredObjectRegistration; + +public class ConfiguredObjectTypeRegistryTest extends TestCase +{ + private ConfiguredObjectTypeRegistry _typeRegistry; + + @Override + public void setUp() throws Exception + { + super.setUp(); + Model model = TestModel.getInstance(); + _typeRegistry = model.getTypeRegistry(); + } + + public void testAllTypesRegistered() + { + Collection> types = + _typeRegistry.getTypeSpecialisations(TestRootCategory.class); + + assertEquals(2, types.size()); + assertTrue(types.contains(TestRootCategoryImpl.class)); + + assertTrue(types.contains(Test2RootCategoryImpl.class)); + } + + public void testTypeSpecificAttributes() + { + Collection> special = + _typeRegistry.getTypeSpecificAttributes(Test2RootCategoryImpl.class); + assertEquals(1, special.size()); + ConfiguredObjectAttribute attr = special.iterator().next(); + assertEquals("derivedAttribute",attr.getName()); + assertTrue(attr.isDerived()); + + special = _typeRegistry.getTypeSpecificAttributes(TestRootCategoryImpl.class); + assertEquals(0, special.size()); + + } + + public void testDefaultedValues() + { + checkDefaultedValue(_typeRegistry.getAttributes((Class) TestRootCategoryImpl.class), + TestRootCategory.DEFAULTED_VALUE_DEFAULT); + + checkDefaultedValue(_typeRegistry.getAttributes((Class) Test2RootCategoryImpl.class), + Test2RootCategory.DEFAULTED_VALUE_DEFAULT); + } + + public void testValidValues() + { + checkValidValues("validValue",_typeRegistry.getAttributes((Class) TestRootCategoryImpl.class), + Arrays.asList( TestRootCategory.VALID_VALUE1, TestRootCategory.VALID_VALUE2 ) ); + + checkValidValues("validValue", _typeRegistry.getAttributes((Class) Test2RootCategoryImpl.class), + Test2RootCategoryImpl.functionGeneratedValidValues()); + + + checkValidValues("validValueNotInterpolated", _typeRegistry.getAttributes((Class) TestChildCategory.class), + Arrays.asList(TestChildCategory.NON_INTERPOLATED_VALID_VALUE)); + + + } + + public void testGetManagedInterfacesForTypeNotImplementingManagedInterfaceAndNotHavingManagedAnnotation() + { + ConfiguredObjectTypeRegistry typeRegistry = createConfiguredObjectTypeRegistry(TestRootCategoryImpl.class); + assertEquals("Unexpected interfaces from object not implementing Managed interfaces", + Collections.emptySet(), typeRegistry.getManagedInterfaces(TestRootCategory.class)); + } + + public void testGetManagedInterfacesForTypeImplementingManagedInterfaceButNotHavingManagedAnnotation() + { + ConfiguredObjectTypeRegistry typeRegistry = createConfiguredObjectTypeRegistry(TestRootCategoryImpl.class, TestManagedClass5.class); + assertEquals("Unexpected interfaces from object not implementing Managed interfaces", + Collections.emptySet(), typeRegistry.getManagedInterfaces(TestManagedClass5.class)); + } + + public void testGetManagedInterfacesForTypesImplementingManagedInterfacesWithManagedAnnotation() + { + ConfiguredObjectTypeRegistry typeRegistry = createConfiguredObjectTypeRegistry(TestRootCategoryImpl.class, TestManagedClass0.class, TestManagedClass1.class, TestManagedClass4.class); + Set> expected = Collections.>singleton(TestManagedInterface1.class); + assertEquals("Unexpected interfaces on child class", expected, typeRegistry.getManagedInterfaces(TestManagedClass1.class)); + assertEquals("Unexpected interfaces on super class", expected, typeRegistry.getManagedInterfaces(TestManagedClass0.class)); + assertEquals("Unexpected interfaces on class implementing interface with annotation twice", + expected, typeRegistry.getManagedInterfaces(TestManagedClass4.class)); + } + + public void testGetManagedInterfacesForTypeHavingDirectManagedAnnotation() + { + ConfiguredObjectTypeRegistry typeRegistry = createConfiguredObjectTypeRegistry(TestRootCategoryImpl.class, TestManagedClass2.class, TestManagedClass3.class); + + assertEquals("Unexpected interfaces on class implementing 1 interface with annotation", + new HashSet<>(Arrays.asList(TestManagedInterface1.class)), typeRegistry.getManagedInterfaces(TestManagedClass2.class)); + assertEquals("Unexpected interfaces on class implementing 2 interfaces with annotation", + new HashSet<>(Arrays.asList(TestManagedInterface3.class, TestManagedInterface1.class)), typeRegistry.getManagedInterfaces(TestManagedClass3.class)); + + } + + private ConfiguredObjectTypeRegistry createConfiguredObjectTypeRegistry(Class... supportedTypes) + { + ConfiguredObjectRegistration configuredObjectRegistration = createConfiguredObjectRegistration(supportedTypes); + + return new ConfiguredObjectTypeRegistry(Arrays.asList(configuredObjectRegistration), Arrays.asList(TestRootCategory.class, TestChildCategory.class)); + } + + private ConfiguredObjectRegistration createConfiguredObjectRegistration(final Class... supportedTypes) + { + return new ConfiguredObjectRegistration() + { + @Override + public Collection> getConfiguredObjectClasses() + { + return Arrays.asList(supportedTypes); + } + + @Override + public String getType() + { + return "test"; + } + }; + } + + private void checkDefaultedValue(final Collection> attrs, + final String defaultedValueDefault) + { + boolean found = false; + for(ConfiguredObjectAttribute attr : attrs) + { + if(attr.getName().equals("defaultedValue")) + { + assertEquals(defaultedValueDefault, ((ConfiguredAutomatedAttribute)attr).defaultValue()); + found = true; + break; + } + + } + assertTrue("Could not find attribute defaultedValue", found); + } + + private void checkValidValues(final String attrName, final Collection> attrs, + final Collection validValues) + { + boolean found = false; + for(ConfiguredObjectAttribute attr : attrs) + { + if(attr.getName().equals(attrName)) + { + Collection foundValues = ((ConfiguredAutomatedAttribute) attr).validValues(); + assertEquals("Valid values not as expected, counts differ", validValues.size(), foundValues.size()); + assertTrue("Valid values do not include all expected values", foundValues.containsAll(validValues)); + assertTrue("Valid values contain unexpected addtional values", validValues.containsAll(foundValues)); + found = true; + break; + } + + } + assertTrue("Could not find attribute " + attrName, found); + } +} diff --git a/qpid/java/broker-core/src/test/java/org/apache/qpid/server/model/testmodel/TestManagedClass2.java b/qpid/java/broker-core/src/test/java/org/apache/qpid/server/model/testmodel/TestManagedClass2.java index bd1bc6b24c..62cc0c0c01 100644 --- a/qpid/java/broker-core/src/test/java/org/apache/qpid/server/model/testmodel/TestManagedClass2.java +++ b/qpid/java/broker-core/src/test/java/org/apache/qpid/server/model/testmodel/TestManagedClass2.java @@ -22,15 +22,13 @@ package org.apache.qpid.server.model.testmodel; import java.util.Map; -import org.apache.qpid.server.model.ManagedAnnotation; import org.apache.qpid.server.model.ManagedObject; /** * This is a test managed type implementing managed interface TestManagedInterface2 and having ManagedAnnotation set. - * The instances of this class will be managed entities of types TestManagedInterface1 and TestManagedInterface2 + * The instances of this class will be managed entities of type TestManagedInterface1 */ @ManagedObject( category = false , type = "ChildClass2" ) -@ManagedAnnotation public class TestManagedClass2 extends TestManagedClass0 implements TestManagedInterface2 { public TestManagedClass2(final Map attributes, TestRootCategory parent) diff --git a/qpid/java/broker-core/src/test/java/org/apache/qpid/server/model/testmodel/TestManagedClass3.java b/qpid/java/broker-core/src/test/java/org/apache/qpid/server/model/testmodel/TestManagedClass3.java index fbd3c60045..c78f7404a6 100644 --- a/qpid/java/broker-core/src/test/java/org/apache/qpid/server/model/testmodel/TestManagedClass3.java +++ b/qpid/java/broker-core/src/test/java/org/apache/qpid/server/model/testmodel/TestManagedClass3.java @@ -22,16 +22,14 @@ package org.apache.qpid.server.model.testmodel; import java.util.Map; -import org.apache.qpid.server.model.ManagedAnnotation; import org.apache.qpid.server.model.ManagedObject; /** - * This is a test managed type implementing managed interface TestManagedInterface1 and TestManagedInterface2. - * The instances of this class will be managed entities of types TestManagedInterface1 and TestManagedInterface2. + * This is a test managed type implementing managed interface TestManagedInterface1 and TestManagedInterface3. + * The instances of this class will be managed entities of types TestManagedInterface1 and TestManagedInterface3. */ @ManagedObject( category = false , type = "ChildClass3" ) -@ManagedAnnotation -public class TestManagedClass3 extends TestChildCategoryImpl implements TestManagedInterface1,TestManagedInterface2 +public class TestManagedClass3 extends TestChildCategoryImpl implements TestManagedInterface1,TestManagedInterface3 { public TestManagedClass3(final Map attributes, TestRootCategory parent) { diff --git a/qpid/java/broker-core/src/test/java/org/apache/qpid/server/model/testmodel/TestManagedInterface3.java b/qpid/java/broker-core/src/test/java/org/apache/qpid/server/model/testmodel/TestManagedInterface3.java new file mode 100644 index 0000000000..d1d0a1b820 --- /dev/null +++ b/qpid/java/broker-core/src/test/java/org/apache/qpid/server/model/testmodel/TestManagedInterface3.java @@ -0,0 +1,29 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.server.model.testmodel; + +import org.apache.qpid.server.model.ManagedAnnotation; +import org.apache.qpid.server.model.ManagedInterface; + +@ManagedAnnotation +public interface TestManagedInterface3 extends ManagedInterface +{ +} -- cgit v1.2.1