001: /*
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */
008:
009: package test.javax.management;
010:
011: import javax.management.Attribute;
012: import javax.management.AttributeList;
013: import javax.management.JMRuntimeException;
014: import javax.management.MBeanServer;
015: import javax.management.MBeanServerFactory;
016: import javax.management.NotCompliantMBeanException;
017: import javax.management.ObjectName;
018: import javax.management.RuntimeMBeanException;
019:
020: import test.MX4JTestCase;
021: import test.javax.management.support.MBeanDynamic;
022:
023: /**
024: * @version $Revision: 1.10 $
025: */
026: public class DynamicMBeanFunctionalityTest extends MX4JTestCase {
027: private MBeanServer m_server;
028: private ObjectName m_name;
029:
030: public DynamicMBeanFunctionalityTest(String s) {
031: super (s);
032: }
033:
034: protected void setUp() throws Exception {
035: m_server = MBeanServerFactory.createMBeanServer("dynamic");
036: m_name = new ObjectName(":type=dynamic");
037: Object dynamic = new MBeanDynamic();
038: m_server.registerMBean(dynamic, m_name);
039: }
040:
041: protected void tearDown() throws Exception {
042: m_server.unregisterMBean(m_name);
043: MBeanServerFactory.releaseMBeanServer(m_server);
044: }
045:
046: public void testGetAttributes() throws Exception {
047: AttributeList list = null;
048:
049: list = m_server.getAttributes(m_name, new String[0]);
050: assertEquals(list.size(), 0);
051:
052: list = m_server.getAttributes(m_name,
053: new String[] { "doesNotExist" });
054: if (list.size() != 0) {
055: fail("Attribute does not exist");
056: }
057:
058: String attributeName = "DynamicAttribute1";
059:
060: list = m_server.getAttributes(m_name,
061: new String[] { attributeName });
062: if (list.size() != 1
063: && ((Attribute) list.get(0)).getName().equals(
064: attributeName)) {
065: fail("Attribute exists");
066: }
067: }
068:
069: public void testGetSetAttribute() throws Exception {
070: String attributeName = "DynamicAttribute1";
071:
072: Object valueBefore = m_server.getAttribute(m_name,
073: attributeName);
074:
075: Object newValue = "newValue";
076: Attribute attribute = new Attribute(attributeName, newValue);
077: m_server.setAttribute(m_name, attribute);
078:
079: Object valueAfter = m_server
080: .getAttribute(m_name, attributeName);
081: if (valueAfter.equals(valueBefore)
082: || !valueAfter.equals(newValue)) {
083: fail("setAttribute does not work");
084: }
085: }
086:
087: public void testSetAttributes() throws Exception {
088: String attributeName1 = "DynamicAttribute1";
089: Object value1Before = m_server.getAttribute(m_name,
090: attributeName1);
091: String attributeName2 = "DynamicAttribute2";
092: Object value2Before = m_server.getAttribute(m_name,
093: attributeName2);
094: AttributeList changeThese = new AttributeList();
095: AttributeList list = m_server
096: .setAttributes(m_name, changeThese);
097: if (list.size() != 0) {
098: fail("No Attributes were changed");
099: }
100: if (!value1Before.equals(m_server.getAttribute(m_name,
101: attributeName1))
102: || !value2Before.equals(m_server.getAttribute(m_name,
103: attributeName2))) {
104: fail("Attribute was not changed");
105: }
106: Attribute attr = new Attribute(attributeName2, "Value2");
107: changeThese.add(attr);
108: list = m_server.setAttributes(m_name, changeThese);
109: if (list.size() != 1) {
110: fail("One attribute was changed");
111: }
112: if (!list.get(0).equals(attr)) {
113: fail("Wrong return value");
114: }
115: if (!value1Before.equals(m_server.getAttribute(m_name,
116: attributeName1))
117: || value2Before.equals(m_server.getAttribute(m_name,
118: attributeName2))
119: || !attr.getValue().equals(
120: m_server.getAttribute(m_name, attributeName2))) {
121: fail("Attribute was not changed");
122: }
123: }
124:
125: public void testInvoke() throws Exception {
126: String attributeName1 = "DynamicAttribute1";
127: Object value1 = m_server.getAttribute(m_name, attributeName1);
128:
129: Boolean result = (Boolean) m_server.invoke(m_name,
130: "dynamicOperation", new Object[] { "dummy" },
131: new String[] { "java.lang.String" });
132: if (result.booleanValue()) {
133: fail("Operation does not work");
134: }
135: result = (Boolean) m_server.invoke(m_name, "dynamicOperation",
136: new Object[] { value1 },
137: new String[] { "java.lang.String" });
138: if (!result.booleanValue()) {
139: fail("Operation does not work");
140: }
141: }
142:
143: public void testGetMBeanInfoRegistrationException()
144: throws Exception {
145: try {
146: m_server
147: .createMBean(
148: "test.javax.management.support.ExceptionGeneratingDMB",
149: new ObjectName(":register=no"),
150: new Object[] { new Boolean(false) },
151: new String[] { "boolean" });
152: fail("Expecting NotCompliantMBeanException");
153: } catch (NotCompliantMBeanException x) {
154: }
155: }
156:
157: public void testGetMBeanInfoInvocationException() throws Exception {
158: try {
159: ObjectName objname = new ObjectName(":register=yes");
160: m_server
161: .createMBean(
162: "test.javax.management.support.ExceptionGeneratingDMB",
163: objname,
164: new Object[] { new Boolean(true) },
165: new String[] { "boolean" });
166: m_server.getMBeanInfo(objname);
167: fail("Expecting RuntimeMBeanException");
168: } catch (RuntimeMBeanException x) {
169: }
170: }
171:
172: public void testNullMBeanInfo() throws Exception {
173: try {
174: ObjectName objname = new ObjectName(":id=testNullMBeanInfo");
175: m_server.createMBean(
176: "test.javax.management.support.NullMBeanInfoDMB",
177: objname);
178: m_server.getMBeanInfo(objname);
179: fail("Expecting JMRuntimeException");
180: } catch (JMRuntimeException x) {
181: }
182: }
183: }
|