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.fail;
12 import java.util.concurrent.atomic.AtomicBoolean;
13 import javax.management.Attribute;
14 import javax.management.AttributeList;
15 import javax.management.DynamicMBean;
16 import javax.management.JMX;
17 import javax.management.MBeanServerFactory;
18 import javax.management.ObjectName;
19 import org.junit.Test;
20 import org.opendaylight.controller.config.api.ModuleIdentifier;
21 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
22 import org.opendaylight.controller.config.manager.impl.dynamicmbean.ReadOnlyAtomicBoolean.ReadOnlyAtomicBooleanImpl;
23 import org.opendaylight.controller.config.manager.testingservices.parallelapsp.TestingParallelAPSPConfigMXBean;
24 import org.opendaylight.controller.config.manager.testingservices.parallelapsp.TestingParallelAPSPModule;
25 import org.opendaylight.controller.config.manager.testingservices.parallelapsp.TestingParallelAPSPModuleFactory;
26 import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingFixedThreadPoolConfigMXBean;
27 import org.opendaylight.controller.config.spi.Module;
29 public class DynamicWritableWrapperTest extends AbstractDynamicWrapperTest {
30 private final int newThreadCount = 10;
31 private final AtomicBoolean atomicBoolean = new AtomicBoolean();
32 private final ReadOnlyAtomicBoolean readOnlyAtomicBoolean = new ReadOnlyAtomicBooleanImpl(
36 protected AbstractDynamicWrapper getDynamicWrapper(Module module,
37 ModuleIdentifier moduleIdentifier) {
38 return new DynamicWritableWrapper(module, moduleIdentifier,
40 readOnlyAtomicBoolean, MBeanServerFactory.createMBeanServer(),
45 public void testSetAttribute() throws Exception {
46 DynamicMBean proxy = JMX.newMBeanProxy(platformMBeanServer,
47 threadPoolDynamicWrapperON, DynamicMBean.class);
49 proxy.setAttribute(new Attribute(THREAD_COUNT, newThreadCount));
51 assertEquals(newThreadCount, proxy.getAttribute(THREAD_COUNT));
52 assertEquals(newThreadCount, threadPoolConfigBean.getThreadCount());
54 AttributeList attributeList = new AttributeList();
55 attributeList.add(new Attribute(THREAD_COUNT, threadCount));
57 attributeList.add(new Attribute(TRIGGER_NEW_INSTANCE_CREATION, bool));
58 proxy.setAttributes(attributeList);
60 assertEquals(threadCount, threadPoolConfigBean.getThreadCount());
61 assertEquals(bool, threadPoolConfigBean.isTriggerNewInstanceCreation());
65 public void testSettersWithMXBeanProxy() {
66 TestingFixedThreadPoolConfigMXBean proxy = JMX.newMXBeanProxy(
67 platformMBeanServer, threadPoolDynamicWrapperON,
68 TestingFixedThreadPoolConfigMXBean.class);
69 proxy.setThreadCount(newThreadCount);
70 assertEquals(newThreadCount, threadPoolConfigBean.getThreadCount());
74 * Try to call setter with ObjectName containing transaction name. Verify
75 * that ObjectName without transaction name was actually passed on the
79 public void testObjectNameSetterWithONContainingTransaction_shouldBeTranslatedToReadOnlyON()
81 TestingParallelAPSPModuleFactory testingParallelAPSPConfigBeanFactory = new TestingParallelAPSPModuleFactory();
82 TestingParallelAPSPModule apspConfigBean = testingParallelAPSPConfigBeanFactory
83 .createModule("", null, null);
84 ModuleIdentifier moduleIdentifier2 = new ModuleIdentifier("apsp",
86 ObjectName dynON2 = ObjectNameUtil
87 .createReadOnlyModuleON(moduleIdentifier2);
88 AbstractDynamicWrapper dyn = getDynamicWrapper(apspConfigBean,
90 platformMBeanServer.registerMBean(dyn, dynON2);
92 TestingParallelAPSPConfigMXBean proxy = JMX.newMBeanProxy(
93 platformMBeanServer, dynON2,
94 TestingParallelAPSPConfigMXBean.class);
95 ObjectName withTransactionName = ObjectNameUtil
96 .createTransactionModuleON("transaction1", "moduleName", "instanceName");
97 proxy.setThreadPool(withTransactionName);
98 ObjectName withoutTransactionName = ObjectNameUtil
99 .withoutTransactionName(withTransactionName);
100 assertEquals(withoutTransactionName, proxy.getThreadPool());
102 platformMBeanServer.unregisterMBean(dynON2);
106 private void setNumberOfThreads(int numberOfThreads) throws Exception {
107 DynamicMBean proxy = JMX.newMBeanProxy(platformMBeanServer,
108 threadPoolDynamicWrapperON, DynamicMBean.class);
110 proxy.setAttribute(new Attribute(THREAD_COUNT, numberOfThreads));
115 public void testDisablingOfWriteOperations() throws Exception {
116 setNumberOfThreads(newThreadCount);
117 atomicBoolean.set(true);
119 setNumberOfThreads(newThreadCount);
121 } catch (IllegalStateException e) {
122 assertEquals("Operation is not allowed now", e.getMessage());
124 atomicBoolean.set(false);