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;
14 import javax.management.Attribute;
15 import javax.management.AttributeList;
16 import javax.management.DynamicMBean;
17 import javax.management.JMX;
18 import javax.management.MBeanServerFactory;
19 import javax.management.ObjectName;
20 import org.junit.Test;
21 import org.opendaylight.controller.config.api.ModuleIdentifier;
22 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
23 import org.opendaylight.controller.config.manager.impl.TransactionIdentifier;
24 import org.opendaylight.controller.config.manager.impl.dynamicmbean.ReadOnlyAtomicBoolean.ReadOnlyAtomicBooleanImpl;
25 import org.opendaylight.controller.config.manager.testingservices.parallelapsp.TestingParallelAPSPConfigMXBean;
26 import org.opendaylight.controller.config.manager.testingservices.parallelapsp.TestingParallelAPSPModule;
27 import org.opendaylight.controller.config.manager.testingservices.parallelapsp.TestingParallelAPSPModuleFactory;
28 import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingFixedThreadPoolConfigMXBean;
29 import org.opendaylight.controller.config.spi.Module;
31 public class DynamicWritableWrapperTest extends AbstractDynamicWrapperTest {
32 private final int newThreadCount = 10;
33 private final AtomicBoolean atomicBoolean = new AtomicBoolean();
34 private final ReadOnlyAtomicBoolean readOnlyAtomicBoolean = new ReadOnlyAtomicBooleanImpl(
38 protected AbstractDynamicWrapper getDynamicWrapper(Module module,
39 ModuleIdentifier moduleIdentifier) {
40 return new DynamicWritableWrapper(module, moduleIdentifier,
41 new TransactionIdentifier("transaction-1"),
42 readOnlyAtomicBoolean, MBeanServerFactory.createMBeanServer(),
47 public void testSetAttribute() throws Exception {
48 DynamicMBean proxy = JMX.newMBeanProxy(platformMBeanServer,
49 threadPoolDynamicWrapperON, DynamicMBean.class);
51 proxy.setAttribute(new Attribute(THREAD_COUNT, newThreadCount));
53 assertEquals(newThreadCount, proxy.getAttribute(THREAD_COUNT));
54 assertEquals(newThreadCount, threadPoolConfigBean.getThreadCount());
56 AttributeList attributeList = new AttributeList();
57 attributeList.add(new Attribute(THREAD_COUNT, threadCount));
59 attributeList.add(new Attribute(TRIGGER_NEW_INSTANCE_CREATION, bool));
60 proxy.setAttributes(attributeList);
62 assertEquals(threadCount, threadPoolConfigBean.getThreadCount());
63 assertEquals(bool, threadPoolConfigBean.isTriggerNewInstanceCreation());
67 public void testSettersWithMXBeanProxy() {
68 TestingFixedThreadPoolConfigMXBean proxy = JMX.newMXBeanProxy(
69 platformMBeanServer, threadPoolDynamicWrapperON,
70 TestingFixedThreadPoolConfigMXBean.class);
71 proxy.setThreadCount(newThreadCount);
72 assertEquals(newThreadCount, threadPoolConfigBean.getThreadCount());
76 * Try to call setter with ObjectName containing transaction name. Verify
77 * that ObjectName without transaction name was actually passed on the
81 public void testObjectNameSetterWithONContainingTransaction_shouldBeTranslatedToReadOnlyON()
83 TestingParallelAPSPModuleFactory testingParallelAPSPConfigBeanFactory = new TestingParallelAPSPModuleFactory();
84 TestingParallelAPSPModule apspConfigBean = testingParallelAPSPConfigBeanFactory
85 .createModule("", null, null);
86 ModuleIdentifier moduleIdentifier2 = new ModuleIdentifier("apsp",
88 ObjectName dynON2 = ObjectNameUtil
89 .createReadOnlyModuleON(moduleIdentifier2);
90 AbstractDynamicWrapper dyn = getDynamicWrapper(apspConfigBean,
92 platformMBeanServer.registerMBean(dyn, dynON2);
94 TestingParallelAPSPConfigMXBean proxy = JMX.newMBeanProxy(
95 platformMBeanServer, dynON2,
96 TestingParallelAPSPConfigMXBean.class);
97 ObjectName withTransactionName = ObjectNameUtil
98 .createTransactionModuleON("transaction1", "moduleName", "instanceName");
99 proxy.setThreadPool(withTransactionName);
100 ObjectName withoutTransactionName = ObjectNameUtil
101 .withoutTransactionName(withTransactionName);
102 assertEquals(withoutTransactionName, proxy.getThreadPool());
104 platformMBeanServer.unregisterMBean(dynON2);
108 private void setNumberOfThreads(int numberOfThreads) throws Exception {
109 DynamicMBean proxy = JMX.newMBeanProxy(platformMBeanServer,
110 threadPoolDynamicWrapperON, DynamicMBean.class);
112 proxy.setAttribute(new Attribute(THREAD_COUNT, numberOfThreads));
117 public void testDisablingOfWriteOperations() throws Exception {
118 setNumberOfThreads(newThreadCount);
119 atomicBoolean.set(true);
121 setNumberOfThreads(newThreadCount);
123 } catch (IllegalStateException e) {
124 assertEquals("Operation is not allowed now", e.getMessage());
126 atomicBoolean.set(false);