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 java.lang.reflect.Method;
17 import java.util.Arrays;
18 import java.util.Collections;
19 import java.util.HashSet;
21 import java.util.concurrent.Executor;
22 import java.util.concurrent.ExecutorService;
24 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 import com.google.common.base.Throwables;
33 import com.google.common.collect.Sets;
35 public class AnnotationsTest {
37 private final String setSomethingString = "setSomething";
39 private void assertRequireInterfaceAnnotationHasCorrectValue(
40 Class<?> clazz, String methodName,
41 Set<Class<?>> inspectedInterfaces,
42 Class<? extends AbstractServiceInterface> expectedValue) {
43 Method setter = findMethod(clazz, methodName);
44 RequireInterface found = AttributeHolder
45 .findRequireInterfaceAnnotation(setter, inspectedInterfaces);
46 if (expectedValue == null) {
50 assertEquals(expectedValue, found.value());
54 private Method findMethod(Class<?> clazz, String methodName) {
57 setter = clazz.getMethod(methodName,
58 new Class[] { ObjectName.class });
59 } catch (Exception e) {
60 throw Throwables.propagate(e);
65 private void assertDescription(Class<?> clazz, String methodName,
66 Set<Class<?>> exportedInterfaces, String expectedValue) {
67 Method setter = findMethod(clazz, methodName);
68 String found = AttributeHolder.findDescription(setter,
70 if (expectedValue == null) {
74 assertEquals(expectedValue, found);
78 private void assertDescriptionOnClass(Class<?> clazz,
79 Set<Class<?>> jmxInterfaces, String expectedValue) {
80 String found = AbstractDynamicWrapper.findDescription(clazz,
82 if (expectedValue == null) {
86 assertEquals(expectedValue, found);
90 private void assertNoDescriptionOnClass(Class<?> clazz,
91 Set<Class<?>> jmxInterfaces) {
92 String found = AbstractDynamicWrapper.findDescription(clazz,
94 assertTrue(found.isEmpty());
97 static final String SIMPLE = "simple";
98 static final String SUBCLASS2 = "subclass2";
100 @ServiceInterfaceAnnotation(value = SIMPLE, osgiRegistrationType = Executor.class,
101 namespace = "ns", revision = "rev", localName = SIMPLE)
102 static interface SimpleSI extends AbstractServiceInterface {
106 @Description("class")
107 public static class SuperClass {
108 @RequireInterface(SimpleSI.class)
109 @Description("descr")
110 public void setSomething(ObjectName objectName) {
115 private static Set<Class<?>> emptySetOfInterfaces() {
116 return Collections.emptySet();
120 public void testFindAnnotation_directly() throws Exception {
121 assertRequireInterfaceAnnotationHasCorrectValue(SuperClass.class,
122 setSomethingString, emptySetOfInterfaces(), SimpleSI.class);
123 assertDescription(SuperClass.class, setSomethingString,
124 emptySetOfInterfaces(), "descr");
125 assertDescriptionOnClass(SuperClass.class, emptySetOfInterfaces(),
129 public static class SubClassWithout extends SuperClass {
134 public void testFindAnnotation_subclassWithout() throws Exception {
135 assertRequireInterfaceAnnotationHasCorrectValue(SubClassWithout.class,
136 setSomethingString, emptySetOfInterfaces(), SimpleSI.class);
137 assertDescription(SubClassWithout.class, setSomethingString,
138 emptySetOfInterfaces(), "descr");
139 assertDescriptionOnClass(SuperClass.class, emptySetOfInterfaces(),
143 public static class SubClassWithEmptyMethod extends SuperClass {
145 public void setSomething(ObjectName objectName) {
151 public void testOverridingWithoutAnnotation() throws Exception {
152 assertRequireInterfaceAnnotationHasCorrectValue(
153 SubClassWithEmptyMethod.class, setSomethingString,
154 emptySetOfInterfaces(), SimpleSI.class);
155 assertDescription(SubClassWithEmptyMethod.class, setSomethingString,
156 emptySetOfInterfaces(), "descr");
157 assertDescriptionOnClass(SubClassWithEmptyMethod.class,
158 emptySetOfInterfaces(), "class");
161 static interface SubSI extends SimpleSI {
165 @ServiceInterfaceAnnotation(value = SUBCLASS2, osgiRegistrationType = ExecutorService.class,
166 namespace = "ns", revision = "rev", localName = SUBCLASS2)
168 static interface SubSI2 extends SubSI {
172 public static class SubClassWithAnnotation extends SuperClass {
174 @RequireInterface(SubSI2.class)
175 @Description("descr2")
176 public void setSomething(ObjectName objectName) {
182 public void testFindAnnotation_SubClassWithAnnotation() throws Exception {
183 assertDescription(SubClassWithAnnotation.class, setSomethingString,
184 emptySetOfInterfaces(), "descr2\ndescr");
186 assertRequireInterfaceAnnotationHasCorrectValue(
187 SubClassWithAnnotation.class, setSomethingString,
188 emptySetOfInterfaces(), SubSI2.class);
190 } catch (IllegalStateException e) {
194 .startsWith("Error finding @RequireInterface. More than one value specified"));
198 public static interface HasSomeMethod {
199 void setSomething(ObjectName objectName);
202 public static class SubClassWithoutMethodWithInterface extends SuperClass
203 implements HasSomeMethod {
208 public void testFindAnnotation_SubClassWithoutMethodWithInterface()
210 assertRequireInterfaceAnnotationHasCorrectValue(
211 SubClassWithoutMethodWithInterface.class, setSomethingString,
212 emptySetOfInterfaces(), SimpleSI.class);
213 assertDescription(SubClassWithoutMethodWithInterface.class,
214 setSomethingString, emptySetOfInterfaces(), "descr");
217 static abstract class SuperClassWithInterface implements HasSomeMethod {
219 @RequireInterface(SubSI2.class)
220 @Description("descr")
221 public void setSomething(ObjectName objectName) {
226 @Description("class")
227 public static class SubClassOfSuperClassWithInterface extends
228 SuperClassWithInterface {
233 public void testFindAnnotation_SubClassOfSuperClassWithInterface()
235 assertRequireInterfaceAnnotationHasCorrectValue(
236 SubClassOfSuperClassWithInterface.class, setSomethingString,
237 emptySetOfInterfaces(), SubSI2.class);
238 assertDescription(SubClassOfSuperClassWithInterface.class,
239 setSomethingString, emptySetOfInterfaces(), "descr");
240 assertDescriptionOnClass(SubClassOfSuperClassWithInterface.class,
241 emptySetOfInterfaces(), "class");
245 public void testFindAnnotation2() throws Exception {
246 assertNoDescriptionOnClass(SuperClassWithInterface.class,
247 emptySetOfInterfaces());
250 @Description("class")
251 static interface HasSomeMethodWithAnnotations {
253 @RequireInterface(SubSI2.class)
254 @Description("descr")
255 void setSomething(ObjectName objectName);
258 static class HasSomeMethodWithAnnotationsImpl implements
259 HasSomeMethodWithAnnotations {
261 public void setSomething(ObjectName objectName) {
267 public void testHasSomeMethodWithAnnotationsImpl() {
268 HashSet<Class<?>> exportedInterfaces = Sets
269 .<Class<?>> newHashSet(HasSomeMethodWithAnnotations.class);
270 assertRequireInterfaceAnnotationHasCorrectValue(
271 HasSomeMethodWithAnnotationsImpl.class, setSomethingString,
272 exportedInterfaces, SubSI2.class);
274 assertDescription(HasSomeMethodWithAnnotationsImpl.class,
275 setSomethingString, exportedInterfaces, "descr");
277 assertDescriptionOnClass(
278 HasSomeMethodWithAnnotationsImpl.class,
279 new HashSet<Class<?>>(Arrays
280 .asList(HasSomeMethodWithAnnotations.class)), "class");