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;
13 import java.util.concurrent.atomic.AtomicBoolean;
15 import javax.management.Attribute;
16 import javax.management.AttributeList;
17 import javax.management.DynamicMBean;
18 import javax.management.JMX;
19 import javax.management.MBeanServerFactory;
20 import javax.management.ObjectName;
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.TransactionIdentifier;
26 import org.opendaylight.controller.config.manager.impl.dynamicmbean.ReadOnlyAtomicBoolean.ReadOnlyAtomicBooleanImpl;
27 import org.opendaylight.controller.config.manager.testingservices.parallelapsp.TestingParallelAPSPConfigMXBean;
28 import org.opendaylight.controller.config.manager.testingservices.parallelapsp.TestingParallelAPSPModule;
29 import org.opendaylight.controller.config.manager.testingservices.parallelapsp.TestingParallelAPSPModuleFactory;
30 import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingFixedThreadPoolConfigMXBean;
31 import org.opendaylight.controller.config.spi.Module;
33 public class DynamicWritableWrapperTest extends AbstractDynamicWrapperTest {
34 private final int newThreadCount = 10;
35 private final AtomicBoolean atomicBoolean = new AtomicBoolean();
36 private final ReadOnlyAtomicBoolean readOnlyAtomicBoolean = new ReadOnlyAtomicBooleanImpl(
40 protected AbstractDynamicWrapper getDynamicWrapper(Module module,
41 ModuleIdentifier moduleIdentifier) {
42 return new DynamicWritableWrapper(module, moduleIdentifier,
43 new TransactionIdentifier("transaction-1"),
44 readOnlyAtomicBoolean, MBeanServerFactory.createMBeanServer(),
49 public void testSetAttribute() throws Exception {
50 DynamicMBean proxy = JMX.newMBeanProxy(platformMBeanServer,
51 threadPoolDynamicWrapperON, DynamicMBean.class);
53 proxy.setAttribute(new Attribute(THREAD_COUNT, newThreadCount));
55 assertEquals(newThreadCount, proxy.getAttribute(THREAD_COUNT));
56 assertEquals(newThreadCount, threadPoolConfigBean.getThreadCount());
58 AttributeList attributeList = new AttributeList();
59 attributeList.add(new Attribute(THREAD_COUNT, threadCount));
61 attributeList.add(new Attribute(TRIGGER_NEW_INSTANCE_CREATION, bool));
62 proxy.setAttributes(attributeList);
64 assertEquals(threadCount, threadPoolConfigBean.getThreadCount());
65 assertEquals(bool, threadPoolConfigBean.isTriggerNewInstanceCreation());
69 public void testSettersWithMXBeanProxy() {
70 TestingFixedThreadPoolConfigMXBean proxy = JMX.newMXBeanProxy(
71 platformMBeanServer, threadPoolDynamicWrapperON,
72 TestingFixedThreadPoolConfigMXBean.class);
73 proxy.setThreadCount(newThreadCount);
74 assertEquals(newThreadCount, threadPoolConfigBean.getThreadCount());
78 * Try to call setter with ObjectName containing transaction name. Verify
79 * that ObjectName without transaction name was actually passed on the
83 public void testObjectNameSetterWithONContainingTransaction_shouldBeTranslatedToReadOnlyON()
85 TestingParallelAPSPModuleFactory testingParallelAPSPConfigBeanFactory = new TestingParallelAPSPModuleFactory();
86 TestingParallelAPSPModule apspConfigBean = testingParallelAPSPConfigBeanFactory
87 .createModule("", null);
88 ModuleIdentifier moduleIdentifier2 = new ModuleIdentifier("apsp",
90 ObjectName dynON2 = ObjectNameUtil
91 .createReadOnlyModuleON(moduleIdentifier2);
92 AbstractDynamicWrapper dyn = getDynamicWrapper(apspConfigBean,
94 platformMBeanServer.registerMBean(dyn, dynON2);
96 TestingParallelAPSPConfigMXBean proxy = JMX.newMBeanProxy(
97 platformMBeanServer, dynON2,
98 TestingParallelAPSPConfigMXBean.class);
99 ObjectName withTransactionName = ObjectNameUtil
100 .createTransactionModuleON("transaction1", "moduleName", "instanceName");
101 proxy.setThreadPool(withTransactionName);
102 ObjectName withoutTransactionName = ObjectNameUtil
103 .withoutTransactionName(withTransactionName);
104 assertEquals(withoutTransactionName, proxy.getThreadPool());
106 platformMBeanServer.unregisterMBean(dynON2);
110 private void setNumberOfThreads(int numberOfThreads) throws Exception {
111 DynamicMBean proxy = JMX.newMBeanProxy(platformMBeanServer,
112 threadPoolDynamicWrapperON, DynamicMBean.class);
114 proxy.setAttribute(new Attribute(THREAD_COUNT, numberOfThreads));
119 public void testDisablingOfWriteOperations() throws Exception {
120 setNumberOfThreads(newThreadCount);
121 atomicBoolean.set(true);
123 setNumberOfThreads(newThreadCount);
125 } catch (IllegalStateException e) {
126 assertEquals("Operation is not allowed now", e.getMessage());
128 atomicBoolean.set(false);