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.runtimembean;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertThat;
12 import static org.junit.Assert.fail;
13 import static org.junit.internal.matchers.StringContains.containsString;
15 import java.lang.management.ManagementFactory;
18 import javax.management.InstanceNotFoundException;
19 import javax.management.ObjectName;
21 import org.junit.After;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.controller.config.api.ModuleIdentifier;
25 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
26 import org.opendaylight.controller.config.api.runtime.HierarchicalRuntimeBeanRegistration;
27 import org.opendaylight.controller.config.manager.impl.AbstractLockedPlatformMBeanServerTest;
28 import org.opendaylight.controller.config.manager.impl.jmx.BaseJMXRegistrator;
29 import org.opendaylight.controller.config.manager.impl.jmx.HierarchicalRuntimeBeanRegistrationImpl;
30 import org.opendaylight.controller.config.manager.impl.jmx.RootRuntimeBeanRegistratorImpl;
32 import com.google.common.collect.ImmutableMap;
33 import com.google.common.collect.Maps;
35 public class RuntimeBeanRegistratorImplTest extends
36 AbstractLockedPlatformMBeanServerTest {
37 static final String module1 = "module1";
38 static final String INSTANCE_NAME = "instanceName";
39 String additionalKey = "key";
40 String additionalValue = "value";
41 Map<String, String> additionalProperties = ImmutableMap.of(additionalKey,
44 private BaseJMXRegistrator baseJMXRegistrator;
45 private RootRuntimeBeanRegistratorImpl tested;
46 private final ModuleIdentifier moduleIdentifier = new ModuleIdentifier(
47 module1, INSTANCE_NAME);
51 baseJMXRegistrator = new BaseJMXRegistrator(
52 ManagementFactory.getPlatformMBeanServer());
53 tested = baseJMXRegistrator
54 .createRuntimeBeanRegistrator(moduleIdentifier);
58 public void tearDown() {
60 assertEquals(0, baseJMXRegistrator.getRegisteredObjectNames().size());
63 protected void checkExists(ObjectName on) throws Exception {
64 platformMBeanServer.getMBeanInfo(on);
67 protected void checkNotExists(ObjectName on) throws Exception {
69 platformMBeanServer.getMBeanInfo(on);
71 } catch (InstanceNotFoundException e) {
77 public void testRegisterMBeanWithoutAdditionalProperties() throws Exception {
81 private HierarchicalRuntimeBeanRegistrationImpl createRoot()
83 HierarchicalRuntimeBeanRegistrationImpl rootRegistration = tested
84 .registerRoot(new TestingRuntimeBean());
86 ObjectName expectedON1 = ObjectNameUtil.createRuntimeBeanName(module1,
87 INSTANCE_NAME, Maps.<String, String> newHashMap());
89 assertEquals(expectedON1, rootRegistration.getObjectName());
90 checkExists(rootRegistration.getObjectName());
91 return rootRegistration;
95 public void testRegisterMBeanWithAdditionalProperties() throws Exception {
96 HierarchicalRuntimeBeanRegistrationImpl rootRegistration = createRoot();
97 createAdditional(rootRegistration);
100 private HierarchicalRuntimeBeanRegistration createAdditional(
101 HierarchicalRuntimeBeanRegistrationImpl rootRegistration)
104 HierarchicalRuntimeBeanRegistrationImpl registration = rootRegistration
105 .register(additionalKey, additionalValue, new TestingRuntimeBean());
107 ObjectName expectedON1 = ObjectNameUtil.createRuntimeBeanName(module1,
108 INSTANCE_NAME, additionalProperties);
110 assertEquals(expectedON1, registration.getObjectName());
111 checkExists(registration.getObjectName());
116 public void testCloseRegistration() throws Exception {
117 HierarchicalRuntimeBeanRegistrationImpl rootRegistration = createRoot();
118 rootRegistration.close();
119 checkNotExists(rootRegistration.getObjectName());
123 public void testCloseRegistrator() throws Exception {
124 HierarchicalRuntimeBeanRegistrationImpl rootRegistration = createRoot();
125 HierarchicalRuntimeBeanRegistration childRegistration = createAdditional(rootRegistration);
127 checkNotExists(rootRegistration.getObjectName());
128 checkNotExists(childRegistration.getObjectName());
131 @Test(expected = IllegalArgumentException.class)
132 public void testRegistration_overrideType() throws Exception {
133 HierarchicalRuntimeBeanRegistrationImpl rootRegistration = createRoot();
134 rootRegistration.register("type", "xxx", new TestingRuntimeBean());
138 public void testRegistrationException() throws Exception {
139 HierarchicalRuntimeBeanRegistrationImpl rootRegistration = createRoot();
143 } catch (IllegalStateException e) {
144 assertThat(e.getMessage(), containsString(rootRegistration
145 .getObjectName().toString()));
146 assertThat(e.getMessage(),
147 containsString("Could not register runtime bean"));
148 assertThat(e.getMessage(),
149 containsString(moduleIdentifier.toString()));
154 public void testIgnoringExceptionInClose() throws Exception {
155 HierarchicalRuntimeBeanRegistrationImpl rootRegistration = createRoot();
156 platformMBeanServer.unregisterMBean(rootRegistration.getObjectName());
157 rootRegistration.close();