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.*;
22 import java.util.concurrent.atomic.AtomicBoolean;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.fail;
27 public class DynamicWritableWrapperTest extends AbstractDynamicWrapperTest {
28 private final int newThreadCount = 10;
29 private final AtomicBoolean atomicBoolean = new AtomicBoolean();
30 private final ReadOnlyAtomicBoolean readOnlyAtomicBoolean = new ReadOnlyAtomicBooleanImpl(
34 protected AbstractDynamicWrapper getDynamicWrapper(Module module,
35 ModuleIdentifier moduleIdentifier) {
36 return new DynamicWritableWrapper(module, moduleIdentifier,
37 new TransactionIdentifier("transaction-1"),
38 readOnlyAtomicBoolean, MBeanServerFactory.createMBeanServer(),
43 public void testSetAttribute() throws Exception {
44 DynamicMBean proxy = JMX.newMBeanProxy(platformMBeanServer,
45 threadPoolDynamicWrapperON, DynamicMBean.class);
47 proxy.setAttribute(new Attribute(THREAD_COUNT, newThreadCount));
49 assertEquals(newThreadCount, proxy.getAttribute(THREAD_COUNT));
50 assertEquals(newThreadCount, threadPoolConfigBean.getThreadCount());
52 AttributeList attributeList = new AttributeList();
53 attributeList.add(new Attribute(THREAD_COUNT, threadCount));
55 attributeList.add(new Attribute(TRIGGER_NEW_INSTANCE_CREATION, bool));
56 proxy.setAttributes(attributeList);
58 assertEquals(threadCount, threadPoolConfigBean.getThreadCount());
59 assertEquals(bool, threadPoolConfigBean.isTriggerNewInstanceCreation());
63 public void testSettersWithMXBeanProxy() {
64 TestingFixedThreadPoolConfigMXBean proxy = JMX.newMXBeanProxy(
65 platformMBeanServer, threadPoolDynamicWrapperON,
66 TestingFixedThreadPoolConfigMXBean.class);
67 proxy.setThreadCount(newThreadCount);
68 assertEquals(newThreadCount, threadPoolConfigBean.getThreadCount());
72 * Try to call setter with ObjectName containing transaction name. Verify
73 * that ObjectName without transaction name was actually passed on the
77 public void testObjectNameSetterWithONContainingTransaction_shouldBeTranslatedToReadOnlyON()
79 TestingParallelAPSPModuleFactory testingParallelAPSPConfigBeanFactory = new TestingParallelAPSPModuleFactory();
80 TestingParallelAPSPModule apspConfigBean = testingParallelAPSPConfigBeanFactory
81 .createModule("", null, null);
82 ModuleIdentifier moduleIdentifier2 = new ModuleIdentifier("apsp",
84 ObjectName dynON2 = ObjectNameUtil
85 .createReadOnlyModuleON(moduleIdentifier2);
86 AbstractDynamicWrapper dyn = getDynamicWrapper(apspConfigBean,
88 platformMBeanServer.registerMBean(dyn, dynON2);
90 TestingParallelAPSPConfigMXBean proxy = JMX.newMBeanProxy(
91 platformMBeanServer, dynON2,
92 TestingParallelAPSPConfigMXBean.class);
93 ObjectName withTransactionName = ObjectNameUtil
94 .createTransactionModuleON("transaction1", "moduleName", "instanceName");
95 proxy.setThreadPool(withTransactionName);
96 ObjectName withoutTransactionName = ObjectNameUtil
97 .withoutTransactionName(withTransactionName);
98 assertEquals(withoutTransactionName, proxy.getThreadPool());
100 platformMBeanServer.unregisterMBean(dynON2);
104 private void setNumberOfThreads(int numberOfThreads) throws Exception {
105 DynamicMBean proxy = JMX.newMBeanProxy(platformMBeanServer,
106 threadPoolDynamicWrapperON, DynamicMBean.class);
108 proxy.setAttribute(new Attribute(THREAD_COUNT, numberOfThreads));
113 public void testDisablingOfWriteOperations() throws Exception {
114 setNumberOfThreads(newThreadCount);
115 atomicBoolean.set(true);
117 setNumberOfThreads(newThreadCount);
119 } catch (IllegalStateException e) {
120 assertEquals("Operation is not allowed now", e.getMessage());
122 atomicBoolean.set(false);