Fix star import and enable checkstyle rule to prevent it.
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / impl / dynamicmbean / AbstractDynamicWrapperTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.config.manager.impl.dynamicmbean;
9
10 import javax.management.Attribute;
11 import javax.management.AttributeList;
12 import javax.management.DynamicMBean;
13 import javax.management.JMX;
14 import javax.management.MBeanInfo;
15 import javax.management.MBeanServer;
16 import javax.management.MBeanServerFactory;
17 import javax.management.ObjectName;
18 import java.lang.management.ManagementFactory;
19
20 import org.junit.After;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.opendaylight.controller.config.api.ModuleIdentifier;
24 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
25 import org.opendaylight.controller.config.manager.impl.AbstractLockedPlatformMBeanServerTest;
26 import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingFixedThreadPool;
27 import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingFixedThreadPoolConfigMXBean;
28 import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingFixedThreadPoolModule;
29 import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingFixedThreadPoolModuleFactory;
30 import org.opendaylight.controller.config.spi.Module;
31
32 import static org.junit.Assert.assertEquals;
33
34 public abstract class AbstractDynamicWrapperTest extends
35         AbstractLockedPlatformMBeanServerTest {
36     protected final MBeanServer platformMBeanServer = ManagementFactory
37             .getPlatformMBeanServer();
38     private static final String moduleName = "impl";
39     protected final ObjectName threadPoolDynamicWrapperON = ObjectNameUtil
40             .createReadOnlyModuleON(moduleName, "fixed1");
41     protected static final String THREAD_COUNT = "ThreadCount";
42     protected static final String TRIGGER_NEW_INSTANCE_CREATION = "TriggerNewInstanceCreation";
43
44     protected final int threadCount = 5;
45     protected TestingFixedThreadPoolModule threadPoolConfigBean;
46     private static final ModuleIdentifier moduleIdentifier = new ModuleIdentifier(
47             moduleName, "clientname2");
48
49     protected MBeanServer internalServer;
50
51     @Before
52     public void registerToJMX() throws Exception {
53         internalServer = MBeanServerFactory.createMBeanServer();
54         TestingFixedThreadPoolModuleFactory testingFixedThreadPoolConfigBeanFactory = new TestingFixedThreadPoolModuleFactory();
55         threadPoolConfigBean = testingFixedThreadPoolConfigBeanFactory
56                 .createModule("", null, null);
57
58         threadPoolConfigBean.setThreadCount(threadCount);
59         AbstractDynamicWrapper dynamicWrapper = getDynamicWrapper(
60                 threadPoolConfigBean, moduleIdentifier);
61         platformMBeanServer.registerMBean(dynamicWrapper,
62                 threadPoolDynamicWrapperON);
63     }
64
65     @After
66     public void unregisterFromJMX() throws Exception {
67         TestingFixedThreadPool.cleanUp();
68         platformMBeanServer.unregisterMBean(threadPoolDynamicWrapperON);
69         MBeanServerFactory.releaseMBeanServer(internalServer);
70     }
71
72     protected abstract AbstractDynamicWrapper getDynamicWrapper(Module module,
73             ModuleIdentifier moduleIdentifier);
74
75     @Test
76     public void testReadAttributes() throws Exception {
77
78         DynamicMBean proxy = JMX.newMBeanProxy(platformMBeanServer,
79                 threadPoolDynamicWrapperON, DynamicMBean.class);
80
81         assertEquals(threadCount, proxy.getAttribute(THREAD_COUNT));
82
83         assertEquals(threadPoolConfigBean.isTriggerNewInstanceCreation(),
84                 proxy.getAttribute(TRIGGER_NEW_INSTANCE_CREATION));
85
86         AttributeList attributes = proxy.getAttributes(new String[] {
87                 THREAD_COUNT, TRIGGER_NEW_INSTANCE_CREATION });
88         assertEquals(2, attributes.size());
89         Attribute threadCountAttr = (Attribute) attributes.get(0);
90         assertEquals(THREAD_COUNT, threadCountAttr.getName());
91         assertEquals(threadCount, threadCountAttr.getValue());
92         Attribute boolTestAttr = (Attribute) attributes.get(1);
93         assertEquals(TRIGGER_NEW_INSTANCE_CREATION, boolTestAttr.getName());
94         assertEquals(threadPoolConfigBean.isTriggerNewInstanceCreation(),
95                 boolTestAttr.getValue());
96
97         MBeanInfo mBeanInfo = proxy.getMBeanInfo();
98         assertEquals(2, mBeanInfo.getAttributes().length);
99
100     }
101
102     @Test
103     public void testGettersWithMXBeanProxy() {
104         TestingFixedThreadPoolConfigMXBean proxy = JMX.newMXBeanProxy(
105                 platformMBeanServer, threadPoolDynamicWrapperON,
106                 TestingFixedThreadPoolConfigMXBean.class);
107         assertEquals(threadCount, proxy.getThreadCount());
108     }
109
110 }