001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *
019: */
020:
021: package org.apache.harmony.lang.management;
022:
023: import java.lang.management.ManagementFactory;
024: import java.util.ArrayList;
025: import java.util.Enumeration;
026: import java.util.HashMap;
027: import java.util.Hashtable;
028: import java.util.Iterator;
029: import java.util.Properties;
030:
031: import javax.management.Attribute;
032: import javax.management.AttributeList;
033: import javax.management.AttributeNotFoundException;
034: import javax.management.MBeanAttributeInfo;
035: import javax.management.MBeanConstructorInfo;
036: import javax.management.MBeanException;
037: import javax.management.MBeanInfo;
038: import javax.management.MBeanNotificationInfo;
039: import javax.management.MBeanOperationInfo;
040: import javax.management.openmbean.TabularData;
041:
042: import org.apache.harmony.lang.management.RuntimeMXBeanImpl;
043:
044: public class RuntimeMXBeanImplTest extends
045: SingleInstanceDynamicMXBeanImplTestBase {
046:
047: protected void setUp() throws Exception {
048: super .setUp();
049: mb = (RuntimeMXBeanImpl) ManagementFactory.getRuntimeMXBean();
050: }
051:
052: protected void tearDown() throws Exception {
053: super .tearDown();
054: }
055:
056: // -----------------------------------------------------------------
057: // DynamicMBean behaviour tests follow ....
058: // -----------------------------------------------------------------
059:
060: public final void testGetAttribute() throws Exception {
061: // The good attributes...
062: if (((Boolean) mb.getAttribute("BootClassPathSupported"))) {
063: assertNotNull(mb.getAttribute("BootClassPath"));
064: assertTrue(mb.getAttribute("BootClassPath") instanceof String);
065: } else {
066: try {
067: String bcp = (String) mb.getAttribute("BootClassPath");
068: fail("Should have thrown exception!");
069: } catch (MBeanException ignore) {
070: }
071: }
072:
073: assertNotNull(mb.getAttribute("ClassPath"));
074: assertTrue(mb.getAttribute("ClassPath") instanceof String);
075:
076: assertNotNull(mb.getAttribute("InputArguments"));
077: assertTrue(mb.getAttribute("InputArguments") instanceof String[]);
078:
079: assertNotNull(mb.getAttribute("LibraryPath"));
080: assertTrue(mb.getAttribute("LibraryPath") instanceof String);
081:
082: assertNotNull(mb.getAttribute("ManagementSpecVersion"));
083: assertTrue(mb.getAttribute("ManagementSpecVersion") instanceof String);
084:
085: assertNotNull(mb.getAttribute("Name"));
086: assertTrue(mb.getAttribute("Name") instanceof String);
087:
088: assertNotNull(mb.getAttribute("SpecName"));
089: assertTrue(mb.getAttribute("SpecName") instanceof String);
090:
091: assertNotNull(mb.getAttribute("SpecVendor"));
092: assertTrue(mb.getAttribute("SpecVendor") instanceof String);
093:
094: assertNotNull(mb.getAttribute("SpecVersion"));
095: assertTrue(mb.getAttribute("SpecVersion") instanceof String);
096:
097: assertTrue(mb.getAttribute("StartTime") instanceof Long);
098: assertTrue(((Long) mb.getAttribute("StartTime")) > -1);
099:
100: assertNotNull(mb.getAttribute("SystemProperties"));
101: assertTrue(mb.getAttribute("SystemProperties") instanceof TabularData);
102: assertTrue(((TabularData) (mb.getAttribute("SystemProperties")))
103: .size() > 0);
104: if (System.getSecurityManager() == null) {
105: assertTrue(((TabularData) (mb
106: .getAttribute("SystemProperties"))).size() == System
107: .getProperties().size());
108: }// end if no security manager
109:
110: assertNotNull(mb.getAttribute("Uptime"));
111: assertTrue(mb.getAttribute("Uptime") instanceof Long);
112: assertTrue((Long) mb.getAttribute("Uptime") > -1);
113:
114: assertNotNull(mb.getAttribute("VmName"));
115: assertTrue(mb.getAttribute("VmName") instanceof String);
116:
117: assertNotNull(mb.getAttribute("VmVendor"));
118: assertTrue(mb.getAttribute("VmVendor") instanceof String);
119:
120: assertNotNull(mb.getAttribute("VmVersion"));
121: assertTrue(mb.getAttribute("VmVersion") instanceof String);
122:
123: // A nonexistent attribute should throw an AttributeNotFoundException
124: try {
125: long rpm = ((Long) (mb.getAttribute("RPM")));
126: fail("Should have thrown an AttributeNotFoundException.");
127: } catch (AttributeNotFoundException ignore) {
128: }
129:
130: // Type mismatch should result in a casting exception
131: try {
132: String bad = (String) (mb
133: .getAttribute("TotalLoadedClassCount"));
134: fail("Should have thrown an AttributeNotFoundException");
135: } catch (AttributeNotFoundException ignore) {
136: }
137: }
138:
139: public final void testSetAttribute() throws Exception {
140: // Nothing is writable for this type
141: Attribute attr = new Attribute("BootClassPath", "Boris");
142: try {
143: mb.setAttribute(attr);
144: fail("Should have thrown an AttributeNotFoundException");
145: } catch (AttributeNotFoundException ignore) {
146: }
147:
148: attr = new Attribute("BootClassPath", "Pasternak");
149: try {
150: mb.setAttribute(attr);
151: fail("Should have thrown an AttributeNotFoundException");
152: } catch (AttributeNotFoundException ignore) {
153: }
154:
155: attr = new Attribute("InputArguments", new ArrayList<String>());
156: try {
157: mb.setAttribute(attr);
158: fail("Should have thrown an AttributeNotFoundException");
159: } catch (AttributeNotFoundException ignore) {
160: }
161:
162: attr = new Attribute("LibraryPath", "Sterling Morrison");
163: try {
164: mb.setAttribute(attr);
165: fail("Should have thrown an AttributeNotFoundException");
166: } catch (AttributeNotFoundException ignore) {
167: }
168:
169: attr = new Attribute("ManagementSpecVersion", "Moe Tucker");
170: try {
171: mb.setAttribute(attr);
172: fail("Should have thrown an AttributeNotFoundException");
173: } catch (AttributeNotFoundException ignore) {
174: }
175:
176: attr = new Attribute("Name", "Julian Cope");
177: try {
178: mb.setAttribute(attr);
179: fail("Should have thrown an AttributeNotFoundException");
180: } catch (AttributeNotFoundException ignore) {
181: }
182:
183: attr = new Attribute("SpecName", "Andy Partridge");
184: try {
185: mb.setAttribute(attr);
186: fail("Should have thrown an AttributeNotFoundException");
187: } catch (AttributeNotFoundException ignore) {
188: }
189:
190: attr = new Attribute("SpecVendor", "Siouxie Sioux");
191: try {
192: mb.setAttribute(attr);
193: fail("Should have thrown an AttributeNotFoundException");
194: } catch (AttributeNotFoundException ignore) {
195: }
196:
197: attr = new Attribute("SpecVersion", "Ari Up");
198: try {
199: mb.setAttribute(attr);
200: fail("Should have thrown an AttributeNotFoundException");
201: } catch (AttributeNotFoundException ignore) {
202: }
203:
204: attr = new Attribute("StartTime", new Long(2333));
205: try {
206: mb.setAttribute(attr);
207: fail("Should have thrown an AttributeNotFoundException");
208: } catch (AttributeNotFoundException ignore) {
209: }
210:
211: attr = new Attribute("SystemProperties",
212: new HashMap<String, String>());
213: try {
214: mb.setAttribute(attr);
215: fail("Should have thrown an AttributeNotFoundException");
216: } catch (AttributeNotFoundException ignore) {
217: }
218:
219: attr = new Attribute("Uptime", new Long(1979));
220: try {
221: mb.setAttribute(attr);
222: fail("Should have thrown an AttributeNotFoundException");
223: } catch (AttributeNotFoundException ignore) {
224: }
225:
226: attr = new Attribute("VmName", "Joe Strummer");
227: try {
228: mb.setAttribute(attr);
229: fail("Should have thrown an AttributeNotFoundException");
230: } catch (AttributeNotFoundException ignore) {
231: }
232:
233: attr = new Attribute("VmVendor", "Paul Haig");
234: try {
235: mb.setAttribute(attr);
236: fail("Should have thrown an AttributeNotFoundException");
237: } catch (AttributeNotFoundException ignore) {
238: }
239:
240: attr = new Attribute("VmVersion", "Jerry Dammers");
241: try {
242: mb.setAttribute(attr);
243: fail("Should have thrown an AttributeNotFoundException");
244: } catch (AttributeNotFoundException ignore) {
245: }
246:
247: attr = new Attribute("BootClassPathSupported", new Boolean(
248: false));
249: try {
250: mb.setAttribute(attr);
251: fail("Should have thrown an AttributeNotFoundException");
252: } catch (AttributeNotFoundException ignore) {
253: }
254:
255: // Try and set the Name attribute with an incorrect type.
256: attr = new Attribute("Name", new Long(42));
257: try {
258: mb.setAttribute(attr);
259: fail("Should have thrown an AttributeNotFoundException");
260: } catch (AttributeNotFoundException ignore) {
261: }
262: }
263:
264: public final void testGetAttributes() {
265: AttributeList attributes = mb.getAttributes(attribs.keySet()
266: .toArray(new String[] {}));
267: assertNotNull(attributes);
268: assertTrue(attributes.size() <= attribs.size());
269:
270: // Check through the returned values
271: Iterator<?> it = attributes.iterator();
272: while (it.hasNext()) {
273: Attribute element = (Attribute) it.next();
274: assertNotNull(element);
275: String name = element.getName();
276: Object value = element.getValue();
277: if (attribs.containsKey(name)) {
278: if (attribs.get(name).type.equals(String.class
279: .getName())) {
280: assertNotNull(value);
281: assertTrue(value instanceof String);
282: }// end if a String value expected
283: else if (attribs.get(name).type.equals(Long.TYPE
284: .getName())) {
285: assertTrue(((Long) (value)) > -1);
286: }// end else a long expected
287: else if (attribs.get(name).type.equals(Boolean.TYPE
288: .getName())) {
289: boolean tmp = ((Boolean) value).booleanValue();
290: }// end else a boolean expected
291: else if (attribs.get(name).type
292: .equals("[Ljava.lang.String;")) {
293: String[] tmp = (String[]) value;
294: assertNotNull(tmp);
295: }// end else a String array expected
296: else if (attribs.get(name).type
297: .equals(TabularData.class.getName())) {
298: assertNotNull(value);
299: assertTrue(value instanceof TabularData);
300: // Sanity check on the contents of the returned
301: // TabularData instance. Only one attribute of the
302: // RuntimeMXBean returns a TabularDataType -
303: // the SystemProperties.
304: TabularData td = (TabularData) value;
305: assertTrue(td.size() > 0);
306: if (System.getSecurityManager() == null) {
307: Properties props = System.getProperties();
308: assertTrue(td.size() == props.size());
309: Enumeration<?> propNames = props
310: .propertyNames();
311: while (propNames.hasMoreElements()) {
312: String property = (String) propNames
313: .nextElement();
314: String propVal = props
315: .getProperty(property);
316: assertEquals(propVal, td.get(
317: new String[] { property }).get(
318: "value"));
319: }// end while
320: }// end if no security manager
321: }// end else a String array expected
322: }// end if a known attribute
323: else {
324: fail("Unexpected attribute name returned!");
325: }// end else an unknown attribute
326: }// end while
327: }
328:
329: public final void testSetAttributes() {
330: // No writable attributes for this type - should get a failure...
331: AttributeList badList = new AttributeList();
332: Attribute garbage = new Attribute("Name", "City Sickness");
333: Attribute trash = new Attribute("SpecVendor", "Marbles");
334: badList.add(garbage);
335: badList.add(trash);
336: AttributeList setAttrs = mb.setAttributes(badList);
337: assertNotNull(setAttrs);
338: assertTrue(setAttrs.size() == 0);
339: }
340:
341: public final void testGetMBeanInfo() {
342: MBeanInfo mbi = mb.getMBeanInfo();
343: assertNotNull(mbi);
344:
345: // Now make sure that what we got back is what we expected.
346:
347: // Class name
348: assertTrue(mbi.getClassName().equals(mb.getClass().getName()));
349:
350: // No public constructors
351: MBeanConstructorInfo[] constructors = mbi.getConstructors();
352: assertNotNull(constructors);
353: assertTrue(constructors.length == 0);
354:
355: // No public operations
356: MBeanOperationInfo[] operations = mbi.getOperations();
357: assertNotNull(operations);
358: assertTrue(operations.length == 0);
359:
360: // No notifications
361: MBeanNotificationInfo[] notifications = mbi.getNotifications();
362: assertNotNull(notifications);
363: assertTrue(notifications.length == 0);
364:
365: // Description is just the class name (until I hear it should be
366: // different)
367: assertTrue(mbi.getDescription().equals(mb.getClass().getName()));
368:
369: // Sixteen attributes - none writable.
370: MBeanAttributeInfo[] attributes = mbi.getAttributes();
371: assertNotNull(attributes);
372: assertTrue(attributes.length == 16);
373: for (int i = 0; i < attributes.length; i++) {
374: MBeanAttributeInfo info = attributes[i];
375: assertNotNull(info);
376: validateAttributeInfo(info);
377: }// end for
378: }
379:
380: @Override
381: protected void populateTestAttributes() {
382: attribs = new Hashtable<String, AttributeData>();
383: attribs.put("BootClassPath", new AttributeData(String.class
384: .getName(), true, false, false));
385: attribs.put("ClassPath", new AttributeData(String.class
386: .getName(), true, false, false));
387: attribs.put("InputArguments", new AttributeData(
388: "[Ljava.lang.String;", true, false, false));
389: attribs.put("LibraryPath", new AttributeData(String.class
390: .getName(), true, false, false));
391: attribs.put("ManagementSpecVersion", new AttributeData(
392: String.class.getName(), true, false, false));
393: attribs.put("Name", new AttributeData(String.class.getName(),
394: true, false, false));
395: attribs.put("SpecName", new AttributeData(String.class
396: .getName(), true, false, false));
397: attribs.put("SpecVendor", new AttributeData(String.class
398: .getName(), true, false, false));
399: attribs.put("SpecVersion", new AttributeData(String.class
400: .getName(), true, false, false));
401: attribs.put("StartTime", new AttributeData(Long.TYPE.getName(),
402: true, false, false));
403: attribs.put("SystemProperties", new AttributeData(
404: TabularData.class.getName(), true, false, false));
405: attribs.put("Uptime", new AttributeData(Long.TYPE.getName(),
406: true, false, false));
407: attribs.put("VmName", new AttributeData(String.class.getName(),
408: true, false, false));
409: attribs.put("VmVendor", new AttributeData(String.class
410: .getName(), true, false, false));
411: attribs.put("VmVersion", new AttributeData(String.class
412: .getName(), true, false, false));
413: attribs.put("BootClassPathSupported", new AttributeData(
414: Boolean.TYPE.getName(), true, false, true));
415: }
416: }
|