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.Test;
11 import org.opendaylight.controller.config.api.ModuleIdentifier;
12 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
13 import org.opendaylight.controller.config.manager.impl.TransactionIdentifier;
14 import org.opendaylight.controller.config.manager.impl.dynamicmbean.ReadOnlyAtomicBoolean.ReadOnlyAtomicBooleanImpl;
15 import org.opendaylight.controller.config.manager.testingservices.parallelapsp.TestingParallelAPSPConfigMXBean;
16 import org.opendaylight.controller.config.manager.testingservices.parallelapsp.TestingParallelAPSPModule;
17 import org.opendaylight.controller.config.manager.testingservices.parallelapsp.TestingParallelAPSPModuleFactory;
18 import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingFixedThreadPoolConfigMXBean;
19 import org.opendaylight.controller.config.spi.Module;
21 import javax.management.Attribute;
22 import javax.management.AttributeList;
23 import javax.management.DynamicMBean;
24 import javax.management.JMX;
25 import javax.management.MBeanServerFactory;
26 import javax.management.ObjectName;
27 import java.util.concurrent.atomic.AtomicBoolean;
29 import static org.junit.Assert.assertEquals;
30 import static org.junit.Assert.fail;
32 public class DynamicWritableWrapperTest extends AbstractDynamicWrapperTest {
33 private final int newThreadCount = 10;
34 private final AtomicBoolean atomicBoolean = new AtomicBoolean();
35 private final ReadOnlyAtomicBoolean readOnlyAtomicBoolean = new ReadOnlyAtomicBooleanImpl(
39 protected AbstractDynamicWrapper getDynamicWrapper(Module module,
40 ModuleIdentifier moduleIdentifier) {
41 return new DynamicWritableWrapper(module, moduleIdentifier,
42 new TransactionIdentifier("transaction-1"),
43 readOnlyAtomicBoolean, MBeanServerFactory.createMBeanServer(),
48 public void testSetAttribute() throws Exception {
49 DynamicMBean proxy = JMX.newMBeanProxy(platformMBeanServer,
50 threadPoolDynamicWrapperON, DynamicMBean.class);
52 proxy.setAttribute(new Attribute(THREAD_COUNT, newThreadCount));
54 assertEquals(newThreadCount, proxy.getAttribute(THREAD_COUNT));
55 assertEquals(newThreadCount, threadPoolConfigBean.getThreadCount());
57 AttributeList attributeList = new AttributeList();
58 attributeList.add(new Attribute(THREAD_COUNT, threadCount));
60 attributeList.add(new Attribute(TRIGGER_NEW_INSTANCE_CREATION, bool));
61 proxy.setAttributes(attributeList);
63 assertEquals(threadCount, threadPoolConfigBean.getThreadCount());
64 assertEquals(bool, threadPoolConfigBean.isTriggerNewInstanceCreation());
68 public void testSettersWithMXBeanProxy() {
69 TestingFixedThreadPoolConfigMXBean proxy = JMX.newMXBeanProxy(
70 platformMBeanServer, threadPoolDynamicWrapperON,
71 TestingFixedThreadPoolConfigMXBean.class);
72 proxy.setThreadCount(newThreadCount);
73 assertEquals(newThreadCount, threadPoolConfigBean.getThreadCount());
77 * Try to call setter with ObjectName containing transaction name. Verify
78 * that ObjectName without transaction name was actually passed on the
82 public void testObjectNameSetterWithONContainingTransaction_shouldBeTranslatedToReadOnlyON()
84 TestingParallelAPSPModuleFactory testingParallelAPSPConfigBeanFactory = new TestingParallelAPSPModuleFactory();
85 TestingParallelAPSPModule apspConfigBean = testingParallelAPSPConfigBeanFactory
86 .createModule("", null, null);
87 ModuleIdentifier moduleIdentifier2 = new ModuleIdentifier("apsp",
89 ObjectName dynON2 = ObjectNameUtil
90 .createReadOnlyModuleON(moduleIdentifier2);
91 AbstractDynamicWrapper dyn = getDynamicWrapper(apspConfigBean,
93 platformMBeanServer.registerMBean(dyn, dynON2);
95 TestingParallelAPSPConfigMXBean proxy = JMX.newMBeanProxy(
96 platformMBeanServer, dynON2,
97 TestingParallelAPSPConfigMXBean.class);
98 ObjectName withTransactionName = ObjectNameUtil
99 .createTransactionModuleON("transaction1", "moduleName", "instanceName");
100 proxy.setThreadPool(withTransactionName);
101 ObjectName withoutTransactionName = ObjectNameUtil
102 .withoutTransactionName(withTransactionName);
103 assertEquals(withoutTransactionName, proxy.getThreadPool());
105 platformMBeanServer.unregisterMBean(dynON2);
109 private void setNumberOfThreads(int numberOfThreads) throws Exception {
110 DynamicMBean proxy = JMX.newMBeanProxy(platformMBeanServer,
111 threadPoolDynamicWrapperON, DynamicMBean.class);
113 proxy.setAttribute(new Attribute(THREAD_COUNT, numberOfThreads));
118 public void testDisablingOfWriteOperations() throws Exception {
119 setNumberOfThreads(newThreadCount);
120 atomicBoolean.set(true);
122 setNumberOfThreads(newThreadCount);
124 } catch (IllegalStateException e) {
125 assertEquals("Operation is not allowed now", e.getMessage());
127 atomicBoolean.set(false);