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 void assertRequireInterfaceAnnotationHasCorrectValue(
37 Class<?> clazz, String methodName,
38 Set<Class<?>> inspectedInterfaces,
39 Class<? extends AbstractServiceInterface> expectedValue) {
40 Method setter = findMethod(clazz, methodName);
41 RequireInterface found = AttributeHolder
42 .findRequireInterfaceAnnotation(setter, inspectedInterfaces);
43 if (expectedValue == null) {
47 assertEquals(expectedValue, found.value());
51 private Method findMethod(Class<?> clazz, String methodName) {
54 setter = clazz.getMethod(methodName,
55 new Class[] { ObjectName.class });
56 } catch (Exception e) {
57 throw Throwables.propagate(e);
62 private void assertDescription(Class<?> clazz, String methodName,
63 Set<Class<?>> exportedInterfaces, String expectedValue) {
64 Method setter = findMethod(clazz, methodName);
65 String found = AttributeHolder.findDescription(setter,
67 if (expectedValue == null) {
71 assertEquals(expectedValue, found);
75 private void assertDescriptionOnClass(Class<?> clazz,
76 Set<Class<?>> jmxInterfaces, String expectedValue) {
77 String found = AbstractDynamicWrapper.findDescription(clazz,
79 if (expectedValue == null) {
83 assertEquals(expectedValue, found);
87 private void assertNoDescriptionOnClass(Class<?> clazz,
88 Set<Class<?>> jmxInterfaces) {
89 String found = AbstractDynamicWrapper.findDescription(clazz,
91 assertTrue(found.isEmpty());
94 static final String SIMPLE = "simple";
95 static final String SUBCLASS2 = "subclass2";
97 @ServiceInterfaceAnnotation(value = SIMPLE, osgiRegistrationType = Executor.class,
98 namespace = "ns", revision = "rev", localName = SIMPLE)
99 static interface SimpleSI extends AbstractServiceInterface {
103 @Description("class")
104 public static class SuperClass {
105 @RequireInterface(SimpleSI.class)
106 @Description("descr")
107 public void setSomething(ObjectName objectName) {
112 private static Set<Class<?>> emptySetOfInterfaces() {
113 return Collections.emptySet();
117 public void testFindAnnotation_directly() throws Exception {
118 assertRequireInterfaceAnnotationHasCorrectValue(SuperClass.class,
119 setSomethingString, emptySetOfInterfaces(), SimpleSI.class);
120 assertDescription(SuperClass.class, setSomethingString,
121 emptySetOfInterfaces(), "descr");
122 assertDescriptionOnClass(SuperClass.class, emptySetOfInterfaces(),
126 public static class SubClassWithout extends SuperClass {
131 public void testFindAnnotation_subclassWithout() throws Exception {
132 assertRequireInterfaceAnnotationHasCorrectValue(SubClassWithout.class,
133 setSomethingString, emptySetOfInterfaces(), SimpleSI.class);
134 assertDescription(SubClassWithout.class, setSomethingString,
135 emptySetOfInterfaces(), "descr");
136 assertDescriptionOnClass(SuperClass.class, emptySetOfInterfaces(),
140 public static class SubClassWithEmptyMethod extends SuperClass {
142 public void setSomething(ObjectName objectName) {
148 public void testOverridingWithoutAnnotation() throws Exception {
149 assertRequireInterfaceAnnotationHasCorrectValue(
150 SubClassWithEmptyMethod.class, setSomethingString,
151 emptySetOfInterfaces(), SimpleSI.class);
152 assertDescription(SubClassWithEmptyMethod.class, setSomethingString,
153 emptySetOfInterfaces(), "descr");
154 assertDescriptionOnClass(SubClassWithEmptyMethod.class,
155 emptySetOfInterfaces(), "class");
158 static interface SubSI extends SimpleSI {
162 @ServiceInterfaceAnnotation(value = SUBCLASS2, osgiRegistrationType = ExecutorService.class,
163 namespace = "ns", revision = "rev", localName = SUBCLASS2)
165 static interface SubSI2 extends SubSI {
169 public static class SubClassWithAnnotation extends SuperClass {
171 @RequireInterface(SubSI2.class)
172 @Description("descr2")
173 public void setSomething(ObjectName objectName) {
179 public void testFindAnnotation_SubClassWithAnnotation() throws Exception {
180 assertDescription(SubClassWithAnnotation.class, setSomethingString,
181 emptySetOfInterfaces(), "descr2\ndescr");
183 assertRequireInterfaceAnnotationHasCorrectValue(
184 SubClassWithAnnotation.class, setSomethingString,
185 emptySetOfInterfaces(), SubSI2.class);
187 } catch (IllegalStateException e) {
191 .startsWith("Error finding @RequireInterface. More than one value specified"));
195 public static interface HasSomeMethod {
196 void setSomething(ObjectName objectName);
199 public static class SubClassWithoutMethodWithInterface extends SuperClass
200 implements HasSomeMethod {
205 public void testFindAnnotation_SubClassWithoutMethodWithInterface()
207 assertRequireInterfaceAnnotationHasCorrectValue(
208 SubClassWithoutMethodWithInterface.class, setSomethingString,
209 emptySetOfInterfaces(), SimpleSI.class);
210 assertDescription(SubClassWithoutMethodWithInterface.class,
211 setSomethingString, emptySetOfInterfaces(), "descr");
214 static abstract class SuperClassWithInterface implements HasSomeMethod {
216 @RequireInterface(SubSI2.class)
217 @Description("descr")
218 public void setSomething(ObjectName objectName) {
223 @Description("class")
224 public static class SubClassOfSuperClassWithInterface extends
225 SuperClassWithInterface {
230 public void testFindAnnotation_SubClassOfSuperClassWithInterface()
232 assertRequireInterfaceAnnotationHasCorrectValue(
233 SubClassOfSuperClassWithInterface.class, setSomethingString,
234 emptySetOfInterfaces(), SubSI2.class);
235 assertDescription(SubClassOfSuperClassWithInterface.class,
236 setSomethingString, emptySetOfInterfaces(), "descr");
237 assertDescriptionOnClass(SubClassOfSuperClassWithInterface.class,
238 emptySetOfInterfaces(), "class");
242 public void testFindAnnotation2() throws Exception {
243 assertNoDescriptionOnClass(SuperClassWithInterface.class,
244 emptySetOfInterfaces());
247 @Description("class")
248 static interface HasSomeMethodWithAnnotations {
250 @RequireInterface(SubSI2.class)
251 @Description("descr")
252 void setSomething(ObjectName objectName);
255 static class HasSomeMethodWithAnnotationsImpl implements
256 HasSomeMethodWithAnnotations {
258 public void setSomething(ObjectName objectName) {
264 public void testHasSomeMethodWithAnnotationsImpl() {
265 HashSet<Class<?>> exportedInterfaces = Sets
266 .<Class<?>> newHashSet(HasSomeMethodWithAnnotations.class);
267 assertRequireInterfaceAnnotationHasCorrectValue(
268 HasSomeMethodWithAnnotationsImpl.class, setSomethingString,
269 exportedInterfaces, SubSI2.class);
271 assertDescription(HasSomeMethodWithAnnotationsImpl.class,
272 setSomethingString, exportedInterfaces, "descr");
274 assertDescriptionOnClass(
275 HasSomeMethodWithAnnotationsImpl.class,
276 new HashSet<Class<?>>(Arrays
277 .asList(HasSomeMethodWithAnnotations.class)), "class");