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 org.junit.After;
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.opendaylight.controller.config.api.ModuleIdentifier;
14 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
15 import org.opendaylight.controller.config.manager.impl.AbstractLockedPlatformMBeanServerTest;
16 import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingFixedThreadPool;
17 import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingFixedThreadPoolConfigMXBean;
18 import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingFixedThreadPoolModule;
19 import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingFixedThreadPoolModuleFactory;
20 import org.opendaylight.controller.config.spi.Module;
22 import javax.management.*;
23 import java.lang.management.ManagementFactory;
25 import static org.junit.Assert.assertEquals;
27 public abstract class AbstractDynamicWrapperTest extends
28 AbstractLockedPlatformMBeanServerTest {
29 protected final MBeanServer platformMBeanServer = ManagementFactory
30 .getPlatformMBeanServer();
31 private static final String moduleName = "impl";
32 protected final ObjectName threadPoolDynamicWrapperON = ObjectNameUtil
33 .createReadOnlyModuleON(moduleName, "fixed1");
34 protected static final String THREAD_COUNT = "ThreadCount";
35 protected static final String TRIGGER_NEW_INSTANCE_CREATION = "TriggerNewInstanceCreation";
37 protected final int threadCount = 5;
38 protected TestingFixedThreadPoolModule threadPoolConfigBean;
39 private static final ModuleIdentifier moduleIdentifier = new ModuleIdentifier(
40 moduleName, "clientname2");
42 protected MBeanServer internalServer;
45 public void registerToJMX() throws Exception {
46 internalServer = MBeanServerFactory.createMBeanServer();
47 TestingFixedThreadPoolModuleFactory testingFixedThreadPoolConfigBeanFactory = new TestingFixedThreadPoolModuleFactory();
48 threadPoolConfigBean = testingFixedThreadPoolConfigBeanFactory
49 .createModule("", null, null);
51 threadPoolConfigBean.setThreadCount(threadCount);
52 AbstractDynamicWrapper dynamicWrapper = getDynamicWrapper(
53 threadPoolConfigBean, moduleIdentifier);
54 platformMBeanServer.registerMBean(dynamicWrapper,
55 threadPoolDynamicWrapperON);
59 public void unregisterFromJMX() throws Exception {
60 TestingFixedThreadPool.cleanUp();
61 platformMBeanServer.unregisterMBean(threadPoolDynamicWrapperON);
62 MBeanServerFactory.releaseMBeanServer(internalServer);
65 protected abstract AbstractDynamicWrapper getDynamicWrapper(Module module,
66 ModuleIdentifier moduleIdentifier);
69 public void testReadAttributes() throws Exception {
71 DynamicMBean proxy = JMX.newMBeanProxy(platformMBeanServer,
72 threadPoolDynamicWrapperON, DynamicMBean.class);
74 assertEquals(threadCount, proxy.getAttribute(THREAD_COUNT));
76 assertEquals(threadPoolConfigBean.isTriggerNewInstanceCreation(),
77 proxy.getAttribute(TRIGGER_NEW_INSTANCE_CREATION));
79 AttributeList attributes = proxy.getAttributes(new String[] {
80 THREAD_COUNT, TRIGGER_NEW_INSTANCE_CREATION });
81 assertEquals(2, attributes.size());
82 Attribute threadCountAttr = (Attribute) attributes.get(0);
83 assertEquals(THREAD_COUNT, threadCountAttr.getName());
84 assertEquals(threadCount, threadCountAttr.getValue());
85 Attribute boolTestAttr = (Attribute) attributes.get(1);
86 assertEquals(TRIGGER_NEW_INSTANCE_CREATION, boolTestAttr.getName());
87 assertEquals(threadPoolConfigBean.isTriggerNewInstanceCreation(),
88 boolTestAttr.getValue());
90 MBeanInfo mBeanInfo = proxy.getMBeanInfo();
91 assertEquals(2, mBeanInfo.getAttributes().length);
96 public void testGettersWithMXBeanProxy() {
97 TestingFixedThreadPoolConfigMXBean proxy = JMX.newMXBeanProxy(
98 platformMBeanServer, threadPoolDynamicWrapperON,
99 TestingFixedThreadPoolConfigMXBean.class);
100 assertEquals(threadCount, proxy.getThreadCount());