2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.config.manager.impl.dynamicmbean;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
16 import com.google.common.base.Throwables;
17 import com.google.common.collect.Sets;
18 import java.lang.reflect.Method;
19 import java.util.Arrays;
20 import java.util.Collections;
21 import java.util.HashSet;
23 import java.util.concurrent.Executor;
24 import java.util.concurrent.ExecutorService;
25 import javax.management.ObjectName;
26 import org.junit.Test;
27 import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface;
28 import org.opendaylight.controller.config.api.annotations.Description;
29 import org.opendaylight.controller.config.api.annotations.RequireInterface;
30 import org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation;
32 public class AnnotationsTest {
34 private final String setSomethingString = "setSomething";
36 private static void assertRequireInterfaceAnnotationHasCorrectValue(final Class<?> clazz, final String methodName,
37 final Set<Class<?>> inspectedInterfaces, final Class<? extends AbstractServiceInterface> expectedValue) {
38 Method setter = findMethod(clazz, methodName);
39 RequireInterface found = AttributeHolder
40 .findRequireInterfaceAnnotation(setter, inspectedInterfaces);
41 if (expectedValue == null) {
45 assertEquals(expectedValue, found.value());
49 private static Method findMethod(final Class<?> clazz, final String methodName) {
52 setter = clazz.getMethod(methodName, new Class[] { ObjectName.class });
53 } catch (Exception e) {
54 throw Throwables.propagate(e);
59 private static void assertDescription(final Class<?> clazz, final String methodName,
60 final Set<Class<?>> exportedInterfaces, final String expectedValue) {
61 Method setter = findMethod(clazz, methodName);
62 String found = AttributeHolder.findDescription(setter,
64 if (expectedValue == null) {
68 assertEquals(expectedValue, found);
72 private static void assertDescriptionOnClass(final Class<?> clazz, final Set<Class<?>> jmxInterfaces,
73 final String expectedValue) {
74 String found = AbstractDynamicWrapper.findDescription(clazz, jmxInterfaces);
75 if (expectedValue == null) {
79 assertEquals(expectedValue, found);
83 private static void assertNoDescriptionOnClass(final Class<?> clazz, final Set<Class<?>> jmxInterfaces) {
84 String found = AbstractDynamicWrapper.findDescription(clazz, jmxInterfaces);
85 assertTrue(found.isEmpty());
88 static final String SIMPLE = "simple";
89 static final String SUBCLASS2 = "subclass2";
91 @ServiceInterfaceAnnotation(value = SIMPLE, osgiRegistrationType = Executor.class,
92 namespace = "ns", revision = "rev", localName = SIMPLE)
93 interface SimpleSI extends AbstractServiceInterface {
98 public static class SuperClass {
99 @RequireInterface(SimpleSI.class)
100 @Description("descr")
101 public void setSomething(final ObjectName objectName) {
106 private static Set<Class<?>> emptySetOfInterfaces() {
107 return Collections.emptySet();
111 public void testFindAnnotation_directly() throws Exception {
112 assertRequireInterfaceAnnotationHasCorrectValue(SuperClass.class,
113 setSomethingString, emptySetOfInterfaces(), SimpleSI.class);
114 assertDescription(SuperClass.class, setSomethingString,
115 emptySetOfInterfaces(), "descr");
116 assertDescriptionOnClass(SuperClass.class, emptySetOfInterfaces(),
120 public static class SubClassWithout extends SuperClass {
125 public void testFindAnnotation_subclassWithout() throws Exception {
126 assertRequireInterfaceAnnotationHasCorrectValue(SubClassWithout.class,
127 setSomethingString, emptySetOfInterfaces(), SimpleSI.class);
128 assertDescription(SubClassWithout.class, setSomethingString,
129 emptySetOfInterfaces(), "descr");
130 assertDescriptionOnClass(SuperClass.class, emptySetOfInterfaces(),
134 public static class SubClassWithEmptyMethod extends SuperClass {
136 public void setSomething(final ObjectName objectName) {
142 public void testOverridingWithoutAnnotation() throws Exception {
143 assertRequireInterfaceAnnotationHasCorrectValue(
144 SubClassWithEmptyMethod.class, setSomethingString,
145 emptySetOfInterfaces(), SimpleSI.class);
146 assertDescription(SubClassWithEmptyMethod.class, setSomethingString,
147 emptySetOfInterfaces(), "descr");
148 assertDescriptionOnClass(SubClassWithEmptyMethod.class,
149 emptySetOfInterfaces(), "class");
152 interface SubSI extends SimpleSI {
156 @ServiceInterfaceAnnotation(value = SUBCLASS2, osgiRegistrationType = ExecutorService.class,
157 namespace = "ns", revision = "rev", localName = SUBCLASS2)
158 interface SubSI2 extends SubSI {
162 public static class SubClassWithAnnotation extends SuperClass {
164 @RequireInterface(SubSI2.class)
165 @Description("descr2")
166 public void setSomething(final ObjectName objectName) {
172 public void testFindAnnotation_SubClassWithAnnotation() throws Exception {
173 assertDescription(SubClassWithAnnotation.class, setSomethingString, emptySetOfInterfaces(), "descr2\ndescr");
175 assertRequireInterfaceAnnotationHasCorrectValue(SubClassWithAnnotation.class, setSomethingString,
176 emptySetOfInterfaces(), SubSI2.class);
178 } catch (IllegalStateException e) {
179 assertTrue(e.getMessage(),
180 e.getMessage().startsWith("Error finding @RequireInterface. More than one value specified"));
184 public interface HasSomeMethod {
185 void setSomething(ObjectName objectName);
188 public static class SubClassWithoutMethodWithInterface extends SuperClass implements HasSomeMethod {
193 public void testFindAnnotation_SubClassWithoutMethodWithInterface()
195 assertRequireInterfaceAnnotationHasCorrectValue(
196 SubClassWithoutMethodWithInterface.class, setSomethingString,
197 emptySetOfInterfaces(), SimpleSI.class);
198 assertDescription(SubClassWithoutMethodWithInterface.class,
199 setSomethingString, emptySetOfInterfaces(), "descr");
202 static abstract class SuperClassWithInterface implements HasSomeMethod {
204 @RequireInterface(SubSI2.class)
205 @Description("descr")
206 public void setSomething(final ObjectName objectName) {
211 @Description("class")
212 public static class SubClassOfSuperClassWithInterface extends SuperClassWithInterface {
217 public void testFindAnnotation_SubClassOfSuperClassWithInterface()
219 assertRequireInterfaceAnnotationHasCorrectValue(
220 SubClassOfSuperClassWithInterface.class, setSomethingString,
221 emptySetOfInterfaces(), SubSI2.class);
222 assertDescription(SubClassOfSuperClassWithInterface.class,
223 setSomethingString, emptySetOfInterfaces(), "descr");
224 assertDescriptionOnClass(SubClassOfSuperClassWithInterface.class,
225 emptySetOfInterfaces(), "class");
229 public void testFindAnnotation2() throws Exception {
230 assertNoDescriptionOnClass(SuperClassWithInterface.class, emptySetOfInterfaces());
233 @Description("class")
234 interface HasSomeMethodWithAnnotations {
235 @RequireInterface(SubSI2.class)
236 @Description("descr")
237 void setSomething(ObjectName objectName);
240 static class HasSomeMethodWithAnnotationsImpl implements HasSomeMethodWithAnnotations {
242 public void setSomething(final ObjectName objectName) {
247 public void testHasSomeMethodWithAnnotationsImpl() {
248 HashSet<Class<?>> exportedInterfaces = Sets
249 .<Class<?>> newHashSet(HasSomeMethodWithAnnotations.class);
250 assertRequireInterfaceAnnotationHasCorrectValue(HasSomeMethodWithAnnotationsImpl.class, setSomethingString,
251 exportedInterfaces, SubSI2.class);
253 assertDescription(HasSomeMethodWithAnnotationsImpl.class, setSomethingString, exportedInterfaces, "descr");
255 assertDescriptionOnClass(HasSomeMethodWithAnnotationsImpl.class,
256 new HashSet<>(Arrays.asList(HasSomeMethodWithAnnotations.class)), "class");