Return-Path: Delivered-To: apmail-harmony-commits-archive@www.apache.org Received: (qmail 24107 invoked from network); 13 Jan 2007 05:10:29 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 13 Jan 2007 05:10:29 -0000 Received: (qmail 38747 invoked by uid 500); 13 Jan 2007 05:10:35 -0000 Delivered-To: apmail-harmony-commits-archive@harmony.apache.org Received: (qmail 38723 invoked by uid 500); 13 Jan 2007 05:10:35 -0000 Mailing-List: contact commits-help@harmony.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@harmony.apache.org Delivered-To: mailing list commits@harmony.apache.org Received: (qmail 38714 invoked by uid 99); 13 Jan 2007 05:10:35 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 12 Jan 2007 21:10:35 -0800 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 12 Jan 2007 21:10:26 -0800 Received: by eris.apache.org (Postfix, from userid 65534) id 662891A9829; Fri, 12 Jan 2007 21:08:58 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r495839 [7/7] - in /harmony/enhanced/classlib/trunk/modules/lang-management: ./ META-INF/ make/ src/main/java/com/ src/main/java/com/ibm/ src/main/java/com/ibm/lang/ src/main/java/com/ibm/lang/management/ src/main/java/java/lang/management/... Date: Sat, 13 Jan 2007 05:08:52 -0000 To: commits@harmony.apache.org From: ndbeyer@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20070113050858.662891A9829@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Added: harmony/enhanced/classlib/trunk/modules/lang-management/src/test/impl/java/com/ibm/lang/management/OperatingSystemMXBeanImplTest.java URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/lang-management/src/test/impl/java/com/ibm/lang/management/OperatingSystemMXBeanImplTest.java?view=auto&rev=495839 ============================================================================== --- harmony/enhanced/classlib/trunk/modules/lang-management/src/test/impl/java/com/ibm/lang/management/OperatingSystemMXBeanImplTest.java (added) +++ harmony/enhanced/classlib/trunk/modules/lang-management/src/test/impl/java/com/ibm/lang/management/OperatingSystemMXBeanImplTest.java Fri Jan 12 21:08:48 2007 @@ -0,0 +1,220 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package com.ibm.lang.management; + +import java.lang.management.ManagementFactory; +import java.util.Hashtable; +import java.util.Iterator; + +import javax.management.Attribute; +import javax.management.AttributeList; +import javax.management.AttributeNotFoundException; +import javax.management.MBeanAttributeInfo; +import javax.management.MBeanConstructorInfo; +import javax.management.MBeanInfo; +import javax.management.MBeanNotificationInfo; +import javax.management.MBeanOperationInfo; +import javax.management.NotificationEmitter; + +import com.ibm.lang.management.OperatingSystemMXBeanImpl; + +public class OperatingSystemMXBeanImplTest extends + SingleInstanceDynamicMXBeanImplTest { + + private OperatingSystemMXBeanImpl notifierBean; + + protected void setUp() throws Exception { + super.setUp(); + mb = (OperatingSystemMXBeanImpl) ManagementFactory + .getOperatingSystemMXBean(); + notifierBean = (OperatingSystemMXBeanImpl) mb; + } + + protected void tearDown() throws Exception { + super.tearDown(); + } + + // ----------------------------------------------------------------- + // DynamicMBean behaviour tests follow .... + // ----------------------------------------------------------------- + + public final void testGetAttribute() throws Exception { + // The good attributes... + assertTrue(mb.getAttribute("Arch") != null); + assertTrue(mb.getAttribute("Arch") instanceof String); + assertTrue(((Integer) (mb.getAttribute("AvailableProcessors"))) > -1); + assertTrue(mb.getAttribute("Name") != null); + assertTrue(mb.getAttribute("Name") instanceof String); + assertTrue(mb.getAttribute("Version") != null); + assertTrue(mb.getAttribute("Version") instanceof String); + + // A nonexistent attribute should throw an AttributeNotFoundException + try { + mb.getAttribute("RPM"); + fail("Should have thrown an AttributeNotFoundException."); + } catch (AttributeNotFoundException ignore) { + } + + // Type mismatch should result in a casting exception + try { + String bad = (String) (mb.getAttribute("AvailableProcessors")); + fail("Should have thrown a ClassCastException"); + } catch (ClassCastException ignore) { + } + } + + public final void testSetAttribute() throws Exception { + // Nothing is writable for this type + Attribute attr = new Attribute("Name", "Boris"); + try { + mb.setAttribute(attr); + fail("Should have thrown an AttributeNotFoundException."); + } catch (AttributeNotFoundException ignore) { + } + + attr = new Attribute("Arch", "ie Bunker"); + try { + mb.setAttribute(attr); + fail("Should have thrown an AttributeNotFoundException."); + } catch (AttributeNotFoundException ignore) { + } + + attr = new Attribute("Version", "27 and a half"); + try { + mb.setAttribute(attr); + fail("Should have thrown an AttributeNotFoundException."); + } catch (AttributeNotFoundException ignore) { + } + + attr = new Attribute("AvailableProcessors", new Integer(2)); + try { + mb.setAttribute(attr); + fail("Should have thrown an AttributeNotFoundException."); + } catch (AttributeNotFoundException ignore) { + } + + // Try and set the Name attribute with an incorrect type. + attr = new Attribute("Name", new Long(42)); + try { + mb.setAttribute(attr); + fail("Should have thrown an AttributeNotFoundException."); + } catch (AttributeNotFoundException ignore) { + } + } + + public final void testGetAttributes() { + AttributeList attributes = mb.getAttributes(attribs.keySet().toArray( + new String[] {})); + assertNotNull(attributes); + assertEquals(attribs.size(), attributes.size()); + + // Check through the returned values + Iterator it = attributes.iterator(); + while (it.hasNext()) { + Attribute element = (Attribute) it.next(); + assertNotNull(element); + String name = element.getName(); + Object value = element.getValue(); + if (name.equals("Arch")) { + assertTrue(value instanceof String); + assertEquals(System.getProperty("os.arch"), (String) value); + } else if (name.equals("AvailableProcessors")) { + assertTrue(((Integer) (value)) > -1); + } else if (name.equals("Name")) { + assertTrue(value instanceof String); + assertEquals(System.getProperty("os.name"), (String) value); + } else if (name.equals("Version")) { + assertTrue(value instanceof String); + assertEquals(System.getProperty("os.version"), (String) value); + } else if (name.equals("TotalPhysicalMemory")) { + assertTrue(value instanceof Long); + assertTrue(((Long) (value)) > -1); + } else if (name.equals("ProcessingCapacity")) { + assertTrue(value instanceof Integer); + assertTrue(((Integer) (value)) > -1); + } else { + fail("Unexpected attribute name returned!"); + } + }// end while + } + + public final void testSetAttributes() { + // No writable attributes for this type - should get a failure... + AttributeList badList = new AttributeList(); + Attribute garbage = new Attribute("Name", "Waiting for the moon"); + badList.add(garbage); + AttributeList setAttrs = mb.setAttributes(badList); + assertNotNull(setAttrs); + assertTrue(setAttrs.size() == 0); + } + + public final void testGetMBeanInfo() { + MBeanInfo mbi = mb.getMBeanInfo(); + assertNotNull(mbi); + + // Now make sure that what we got back is what we expected. + + // Class name + assertTrue(mbi.getClassName().equals(mb.getClass().getName())); + + // No public constructors + MBeanConstructorInfo[] constructors = mbi.getConstructors(); + assertNotNull(constructors); + assertTrue(constructors.length == 0); + + // No public operations + MBeanOperationInfo[] operations = mbi.getOperations(); + assertNotNull(operations); + assertTrue(operations.length == 0); + + // No public notifications + MBeanNotificationInfo[] notifications = mbi.getNotifications(); + assertNotNull(notifications); + assertTrue(notifications.length == 0); + + // Description is just the class name (until I hear it should be + // different) + assertTrue(mbi.getDescription().equals(mb.getClass().getName())); + + // 4 standard attributes + MBeanAttributeInfo[] attributes = mbi.getAttributes(); + assertNotNull(attributes); + assertTrue(attributes.length == 4); + for (int i = 0; i < attributes.length; i++) { + MBeanAttributeInfo info = attributes[i]; + assertNotNull(info); + validateAttributeInfo(info); + }// end for + } + + @Override + protected void populateTestAttributes() { + attribs = new Hashtable(); + attribs.put("Arch", new AttributeData(String.class.getName(), true, + false, false)); + attribs.put("AvailableProcessors", new AttributeData(Integer.TYPE + .getName(), true, false, false)); + attribs.put("Name", new AttributeData(String.class.getName(), true, + false, false)); + attribs.put("Version", new AttributeData(String.class.getName(), true, + false, false)); + } +} Propchange: harmony/enhanced/classlib/trunk/modules/lang-management/src/test/impl/java/com/ibm/lang/management/OperatingSystemMXBeanImplTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: harmony/enhanced/classlib/trunk/modules/lang-management/src/test/impl/java/com/ibm/lang/management/RuntimeMXBeanImplTest.java URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/lang-management/src/test/impl/java/com/ibm/lang/management/RuntimeMXBeanImplTest.java?view=auto&rev=495839 ============================================================================== --- harmony/enhanced/classlib/trunk/modules/lang-management/src/test/impl/java/com/ibm/lang/management/RuntimeMXBeanImplTest.java (added) +++ harmony/enhanced/classlib/trunk/modules/lang-management/src/test/impl/java/com/ibm/lang/management/RuntimeMXBeanImplTest.java Fri Jan 12 21:08:48 2007 @@ -0,0 +1,402 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package com.ibm.lang.management; + +import java.lang.management.ManagementFactory; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Hashtable; +import java.util.Iterator; +import java.util.Properties; + +import javax.management.Attribute; +import javax.management.AttributeList; +import javax.management.AttributeNotFoundException; +import javax.management.MBeanAttributeInfo; +import javax.management.MBeanConstructorInfo; +import javax.management.MBeanException; +import javax.management.MBeanInfo; +import javax.management.MBeanNotificationInfo; +import javax.management.MBeanOperationInfo; +import javax.management.openmbean.TabularData; + +import com.ibm.lang.management.RuntimeMXBeanImpl; + +public class RuntimeMXBeanImplTest extends SingleInstanceDynamicMXBeanImplTest { + + protected void setUp() throws Exception { + super.setUp(); + mb = (RuntimeMXBeanImpl) ManagementFactory.getRuntimeMXBean(); + } + + protected void tearDown() throws Exception { + super.tearDown(); + } + + // ----------------------------------------------------------------- + // DynamicMBean behaviour tests follow .... + // ----------------------------------------------------------------- + + public final void testGetAttribute() throws Exception { + // The good attributes... + if (((Boolean) mb.getAttribute("BootClassPathSupported"))) { + assertNotNull(mb.getAttribute("BootClassPath")); + assertTrue(mb.getAttribute("BootClassPath") instanceof String); + } else { + try { + String bcp = (String) mb.getAttribute("BootClassPath"); + fail("Should have thrown exception!"); + } catch (MBeanException ignore) { + } + } + + assertNotNull(mb.getAttribute("ClassPath")); + assertTrue(mb.getAttribute("ClassPath") instanceof String); + + assertNotNull(mb.getAttribute("InputArguments")); + assertTrue(mb.getAttribute("InputArguments") instanceof String[]); + + assertNotNull(mb.getAttribute("LibraryPath")); + assertTrue(mb.getAttribute("LibraryPath") instanceof String); + + assertNotNull(mb.getAttribute("ManagementSpecVersion")); + assertTrue(mb.getAttribute("ManagementSpecVersion") instanceof String); + + assertNotNull(mb.getAttribute("Name")); + assertTrue(mb.getAttribute("Name") instanceof String); + + assertNotNull(mb.getAttribute("SpecName")); + assertTrue(mb.getAttribute("SpecName") instanceof String); + + assertNotNull(mb.getAttribute("SpecVendor")); + assertTrue(mb.getAttribute("SpecVendor") instanceof String); + + assertNotNull(mb.getAttribute("SpecVersion")); + assertTrue(mb.getAttribute("SpecVersion") instanceof String); + + assertTrue(mb.getAttribute("StartTime") instanceof Long); + assertTrue(((Long) mb.getAttribute("StartTime")) > -1); + + assertNotNull(mb.getAttribute("SystemProperties")); + assertTrue(mb.getAttribute("SystemProperties") instanceof TabularData); + assertTrue(((TabularData) (mb.getAttribute("SystemProperties"))).size() > 0); + if (System.getSecurityManager() == null) { + assertTrue(((TabularData) (mb.getAttribute("SystemProperties"))) + .size() == System.getProperties().size()); + }// end if no security manager + + assertNotNull(mb.getAttribute("Uptime")); + assertTrue(mb.getAttribute("Uptime") instanceof Long); + assertTrue((Long) mb.getAttribute("Uptime") > -1); + + assertNotNull(mb.getAttribute("VmName")); + assertTrue(mb.getAttribute("VmName") instanceof String); + + assertNotNull(mb.getAttribute("VmVendor")); + assertTrue(mb.getAttribute("VmVendor") instanceof String); + + assertNotNull(mb.getAttribute("VmVersion")); + assertTrue(mb.getAttribute("VmVersion") instanceof String); + + // A nonexistent attribute should throw an AttributeNotFoundException + try { + long rpm = ((Long) (mb.getAttribute("RPM"))); + fail("Should have thrown an AttributeNotFoundException."); + } catch (AttributeNotFoundException ignore) { + } + + // Type mismatch should result in a casting exception + try { + String bad = (String) (mb.getAttribute("TotalLoadedClassCount")); + fail("Should have thrown an AttributeNotFoundException"); + } catch (AttributeNotFoundException ignore) { + } + } + + public final void testSetAttribute() throws Exception { + // Nothing is writable for this type + Attribute attr = new Attribute("BootClassPath", "Boris"); + try { + mb.setAttribute(attr); + fail("Should have thrown an AttributeNotFoundException"); + } catch (AttributeNotFoundException ignore) { + } + + attr = new Attribute("BootClassPath", "Pasternak"); + try { + mb.setAttribute(attr); + fail("Should have thrown an AttributeNotFoundException"); + } catch (AttributeNotFoundException ignore) { + } + + attr = new Attribute("InputArguments", new ArrayList()); + try { + mb.setAttribute(attr); + fail("Should have thrown an AttributeNotFoundException"); + } catch (AttributeNotFoundException ignore) { + } + + attr = new Attribute("LibraryPath", "Sterling Morrison"); + try { + mb.setAttribute(attr); + fail("Should have thrown an AttributeNotFoundException"); + } catch (AttributeNotFoundException ignore) { + } + + attr = new Attribute("ManagementSpecVersion", "Moe Tucker"); + try { + mb.setAttribute(attr); + fail("Should have thrown an AttributeNotFoundException"); + } catch (AttributeNotFoundException ignore) { + } + + attr = new Attribute("Name", "Julian Cope"); + try { + mb.setAttribute(attr); + fail("Should have thrown an AttributeNotFoundException"); + } catch (AttributeNotFoundException ignore) { + } + + attr = new Attribute("SpecName", "Andy Partridge"); + try { + mb.setAttribute(attr); + fail("Should have thrown an AttributeNotFoundException"); + } catch (AttributeNotFoundException ignore) { + } + + attr = new Attribute("SpecVendor", "Siouxie Sioux"); + try { + mb.setAttribute(attr); + fail("Should have thrown an AttributeNotFoundException"); + } catch (AttributeNotFoundException ignore) { + } + + attr = new Attribute("SpecVersion", "Ari Up"); + try { + mb.setAttribute(attr); + fail("Should have thrown an AttributeNotFoundException"); + } catch (AttributeNotFoundException ignore) { + } + + attr = new Attribute("StartTime", new Long(2333)); + try { + mb.setAttribute(attr); + fail("Should have thrown an AttributeNotFoundException"); + } catch (AttributeNotFoundException ignore) { + } + + attr = new Attribute("SystemProperties", new HashMap()); + try { + mb.setAttribute(attr); + fail("Should have thrown an AttributeNotFoundException"); + } catch (AttributeNotFoundException ignore) { + } + + attr = new Attribute("Uptime", new Long(1979)); + try { + mb.setAttribute(attr); + fail("Should have thrown an AttributeNotFoundException"); + } catch (AttributeNotFoundException ignore) { + } + + attr = new Attribute("VmName", "Joe Strummer"); + try { + mb.setAttribute(attr); + fail("Should have thrown an AttributeNotFoundException"); + } catch (AttributeNotFoundException ignore) { + } + + attr = new Attribute("VmVendor", "Paul Haig"); + try { + mb.setAttribute(attr); + fail("Should have thrown an AttributeNotFoundException"); + } catch (AttributeNotFoundException ignore) { + } + + attr = new Attribute("VmVersion", "Jerry Dammers"); + try { + mb.setAttribute(attr); + fail("Should have thrown an AttributeNotFoundException"); + } catch (AttributeNotFoundException ignore) { + } + + attr = new Attribute("BootClassPathSupported", new Boolean(false)); + try { + mb.setAttribute(attr); + fail("Should have thrown an AttributeNotFoundException"); + } catch (AttributeNotFoundException ignore) { + } + + // Try and set the Name attribute with an incorrect type. + attr = new Attribute("Name", new Long(42)); + try { + mb.setAttribute(attr); + fail("Should have thrown an AttributeNotFoundException"); + } catch (AttributeNotFoundException ignore) { + } + } + + public final void testGetAttributes() { + AttributeList attributes = mb.getAttributes(attribs.keySet().toArray( + new String[] {})); + assertNotNull(attributes); + assertTrue(attributes.size() <= attribs.size()); + + // Check through the returned values + Iterator it = attributes.iterator(); + while (it.hasNext()) { + Attribute element = (Attribute) it.next(); + assertNotNull(element); + String name = element.getName(); + Object value = element.getValue(); + if (attribs.containsKey(name)) { + if (attribs.get(name).type.equals(String.class.getName())) { + assertNotNull(value); + assertTrue(value instanceof String); + }// end if a String value expected + else if (attribs.get(name).type.equals(Long.TYPE.getName())) { + assertTrue(((Long) (value)) > -1); + }// end else a long expected + else if (attribs.get(name).type.equals(Boolean.TYPE.getName())) { + boolean tmp = ((Boolean) value).booleanValue(); + }// end else a boolean expected + else if (attribs.get(name).type.equals("[Ljava.lang.String;")) { + String[] tmp = (String[]) value; + assertNotNull(tmp); + }// end else a String array expected + else if (attribs.get(name).type.equals(TabularData.class + .getName())) { + assertNotNull(value); + assertTrue(value instanceof TabularData); + // Sanity check on the contents of the returned + // TabularData instance. Only one attribute of the + // RuntimeMXBean returns a TabularDataType - + // the SystemProperties. + TabularData td = (TabularData) value; + assertTrue(td.size() > 0); + if (System.getSecurityManager() == null) { + Properties props = System.getProperties(); + assertTrue(td.size() == props.size()); + Enumeration propNames = props.propertyNames(); + while (propNames.hasMoreElements()) { + String property = (String) propNames.nextElement(); + String propVal = props.getProperty(property); + assertEquals(propVal, td.get( + new String[] { property }).get("value")); + }// end while + }// end if no security manager + }// end else a String array expected + }// end if a known attribute + else { + fail("Unexpected attribute name returned!"); + }// end else an unknown attribute + }// end while + } + + public final void testSetAttributes() { + // No writable attributes for this type - should get a failure... + AttributeList badList = new AttributeList(); + Attribute garbage = new Attribute("Name", "City Sickness"); + Attribute trash = new Attribute("SpecVendor", "Marbles"); + badList.add(garbage); + badList.add(trash); + AttributeList setAttrs = mb.setAttributes(badList); + assertNotNull(setAttrs); + assertTrue(setAttrs.size() == 0); + } + + public final void testGetMBeanInfo() { + MBeanInfo mbi = mb.getMBeanInfo(); + assertNotNull(mbi); + + // Now make sure that what we got back is what we expected. + + // Class name + assertTrue(mbi.getClassName().equals(mb.getClass().getName())); + + // No public constructors + MBeanConstructorInfo[] constructors = mbi.getConstructors(); + assertNotNull(constructors); + assertTrue(constructors.length == 0); + + // No public operations + MBeanOperationInfo[] operations = mbi.getOperations(); + assertNotNull(operations); + assertTrue(operations.length == 0); + + // No notifications + MBeanNotificationInfo[] notifications = mbi.getNotifications(); + assertNotNull(notifications); + assertTrue(notifications.length == 0); + + // Description is just the class name (until I hear it should be + // different) + assertTrue(mbi.getDescription().equals(mb.getClass().getName())); + + // Sixteen attributes - none writable. + MBeanAttributeInfo[] attributes = mbi.getAttributes(); + assertNotNull(attributes); + assertTrue(attributes.length == 16); + for (int i = 0; i < attributes.length; i++) { + MBeanAttributeInfo info = attributes[i]; + assertNotNull(info); + validateAttributeInfo(info); + }// end for + } + + @Override + protected void populateTestAttributes() { + attribs = new Hashtable(); + attribs.put("BootClassPath", new AttributeData(String.class.getName(), + true, false, false)); + attribs.put("ClassPath", new AttributeData(String.class.getName(), + true, false, false)); + attribs.put("InputArguments", new AttributeData("[Ljava.lang.String;", + true, false, false)); + attribs.put("LibraryPath", new AttributeData(String.class.getName(), + true, false, false)); + attribs.put("ManagementSpecVersion", new AttributeData(String.class + .getName(), true, false, false)); + attribs.put("Name", new AttributeData(String.class.getName(), true, + false, false)); + attribs.put("SpecName", new AttributeData(String.class.getName(), true, + false, false)); + attribs.put("SpecVendor", new AttributeData(String.class.getName(), + true, false, false)); + attribs.put("SpecVersion", new AttributeData(String.class.getName(), + true, false, false)); + attribs.put("StartTime", new AttributeData(Long.TYPE.getName(), true, + false, false)); + attribs.put("SystemProperties", new AttributeData(TabularData.class + .getName(), true, false, false)); + attribs.put("Uptime", new AttributeData(Long.TYPE.getName(), true, + false, false)); + attribs.put("VmName", new AttributeData(String.class.getName(), true, + false, false)); + attribs.put("VmVendor", new AttributeData(String.class.getName(), true, + false, false)); + attribs.put("VmVersion", new AttributeData(String.class.getName(), + true, false, false)); + attribs.put("BootClassPathSupported", new AttributeData(Boolean.TYPE + .getName(), true, false, true)); + } +} Propchange: harmony/enhanced/classlib/trunk/modules/lang-management/src/test/impl/java/com/ibm/lang/management/RuntimeMXBeanImplTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: harmony/enhanced/classlib/trunk/modules/lang-management/src/test/impl/java/com/ibm/lang/management/SimpleTestListener.java URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/lang-management/src/test/impl/java/com/ibm/lang/management/SimpleTestListener.java?view=auto&rev=495839 ============================================================================== --- harmony/enhanced/classlib/trunk/modules/lang-management/src/test/impl/java/com/ibm/lang/management/SimpleTestListener.java (added) +++ harmony/enhanced/classlib/trunk/modules/lang-management/src/test/impl/java/com/ibm/lang/management/SimpleTestListener.java Fri Jan 12 21:08:48 2007 @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package com.ibm.lang.management; + +import javax.management.Notification; +import javax.management.NotificationListener; + +public class SimpleTestListener implements NotificationListener { + private int count = 0; + + private Object handback = null; + + private Notification notification = null; + + public Object getHandback() { + return this.handback; + } + + public int getNotificationsReceivedCount() { + return count; + } + + public Notification getNotification() { + return this.notification; + } + + public void resetNotificationsReceivedCount() { + count = 0; + } + + public void handleNotification(Notification notification, Object handback) { + // Could also verify the contents of the notification here. + this.count++; + this.handback = handback; + this.notification = notification; + } + +} Propchange: harmony/enhanced/classlib/trunk/modules/lang-management/src/test/impl/java/com/ibm/lang/management/SimpleTestListener.java ------------------------------------------------------------------------------ svn:eol-style = native Added: harmony/enhanced/classlib/trunk/modules/lang-management/src/test/impl/java/com/ibm/lang/management/SingleInstanceDynamicMXBeanImplTest.java URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/lang-management/src/test/impl/java/com/ibm/lang/management/SingleInstanceDynamicMXBeanImplTest.java?view=auto&rev=495839 ============================================================================== --- harmony/enhanced/classlib/trunk/modules/lang-management/src/test/impl/java/com/ibm/lang/management/SingleInstanceDynamicMXBeanImplTest.java (added) +++ harmony/enhanced/classlib/trunk/modules/lang-management/src/test/impl/java/com/ibm/lang/management/SingleInstanceDynamicMXBeanImplTest.java Fri Jan 12 21:08:48 2007 @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package com.ibm.lang.management; + +import javax.management.AttributeList; +import javax.management.ReflectionException; + +import com.ibm.lang.management.DynamicMXBeanImpl; + +public abstract class SingleInstanceDynamicMXBeanImplTest extends + DynamicMXBeanImplTest { + + protected DynamicMXBeanImpl mb; + + public void testInvoke() throws Exception { + // Default for DynamicMXBeanImpl that has no operations to invoke... + try { + Object retVal = mb.invoke("KissTheBlarney", new Object[] { + new Long(7446), new Long(54) }, new String[] { + "java.lang.Long", "java.lang.Long" }); + fail("Should have thrown a ReflectionException."); + } catch (ReflectionException ignore) { + } + } + + public void testGetAttributesBad() throws Exception { + // A failing scenario - pass in an attribute that is not part of + // the management interface. + String[] badNames = { "Cork", "Galway" }; + AttributeList attributes = mb.getAttributes(badNames); + assertNotNull(attributes); + // No attributes will have been returned. + assertTrue(attributes.size() == 0); + } +} Propchange: harmony/enhanced/classlib/trunk/modules/lang-management/src/test/impl/java/com/ibm/lang/management/SingleInstanceDynamicMXBeanImplTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: harmony/enhanced/classlib/trunk/modules/lang-management/src/test/impl/java/com/ibm/lang/management/ThreadInfoTest.java URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/lang-management/src/test/impl/java/com/ibm/lang/management/ThreadInfoTest.java?view=auto&rev=495839 ============================================================================== --- harmony/enhanced/classlib/trunk/modules/lang-management/src/test/impl/java/com/ibm/lang/management/ThreadInfoTest.java (added) +++ harmony/enhanced/classlib/trunk/modules/lang-management/src/test/impl/java/com/ibm/lang/management/ThreadInfoTest.java Fri Jan 12 21:08:48 2007 @@ -0,0 +1,325 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package com.ibm.lang.management; + +import java.lang.management.ThreadInfo; + +import javax.management.openmbean.ArrayType; +import javax.management.openmbean.CompositeData; +import javax.management.openmbean.CompositeDataSupport; +import javax.management.openmbean.CompositeType; +import javax.management.openmbean.OpenDataException; +import javax.management.openmbean.OpenType; +import javax.management.openmbean.SimpleType; + +import junit.framework.TestCase; + +/** + * ThreadInfo objects can only be obtained from the ThreadMXBean or else by + * calling the static from() method with an existing CompositeData object that + * maps to a ThreadInfo. Trying to unit test using the former approach only gets + * us so far as we have no idea what the expected values should be. We only know + * their types, ranges etc etc. This testcase goes the long way round by + * creating a CompositeData representing a ThreadInfo with well understood + * values that we can test for once we have passed the CompositeData into the + * from() method to get a ThreadInfo. + *

+ * The "problem" with this approach is that the hand-rolled CompositeData + * created in this testcase is not liked by the RI which has its own internal + * way of validating CompositeData and CompositeType instances. So, while this + * test case is creating CompositeData and CompositeType objects which adhere to + * the spec, the below tests will all fail when run against the RI. For that + * reason, this testcase cannot be considered as implementation independent. + * + */ +public class ThreadInfoTest extends TestCase { + + private static final boolean GOOD_SUSPENDED = false; + + private static final boolean GOOD_IN_NATIVE = false; + + private static final int GOOD_STACK_SIZE = 3; + + private static final boolean GOOD_STACK_NATIVEMETHOD = false; + + private static final int GOOD_STACK_LINENUMBER = 2100; + + private static final String GOOD_STACK_FILENAME = "Blobby.java"; + + private static final String GOOD_STACK_METHODNAME = "takeOverWorld"; + + private static final String GOOD_STACK_CLASSNAME = "foo.bar.Blobby"; + + private static final Thread.State GOOD_THREAD_STATE = Thread.State.RUNNABLE; + + private static final String GOOD_THREAD_NAME = "Marty"; + + private static final int GOOD_THREAD_ID = 46664; + + private static final String GOOD_LOCK_OWNER_NAME = "Noam Chomsky"; + + private static final int GOOD_LOCK_OWNER_ID = 24141; + + private static final int GOOD_WAITED_TIME = 3779; + + private static final int GOOD_WAITED_COUNT = 21; + + private static final int GOOD_BLOCKED_TIME = 3309; + + private static final int GOOD_BLOCKED_COUNT = 250; + + private static final String GOOD_LOCK_NAME = "foo.Bar@1234567"; + + private CompositeData tiCD; + + private ThreadInfo ti; + + protected void setUp() throws Exception { + super.setUp(); + tiCD = createGoodCompositeData(); + ti = ThreadInfo.from(tiCD); + assertNotNull(ti); + } + + public CompositeData createGoodCompositeData() { + CompositeData result = null; + String[] names = { "threadId", "threadName", "threadState", + "suspended", "inNative", "blockedCount", "blockedTime", + "waitedCount", "waitedTime", "lockName", "lockOwnerId", + "lockOwnerName", "stackTrace" }; + Object[] values = { + /* threadId */new Long(GOOD_THREAD_ID), + /* threadName */new String(GOOD_THREAD_NAME), + /* threadState */new String(GOOD_THREAD_STATE.toString()), + /* suspended */new Boolean(GOOD_SUSPENDED), + /* inNative */new Boolean(GOOD_IN_NATIVE), + /* blockedCount */new Long(GOOD_BLOCKED_COUNT), + /* blockedTime */new Long(GOOD_BLOCKED_TIME), + /* waitedCount */new Long(GOOD_WAITED_COUNT), + /* waitedTime */new Long(GOOD_WAITED_TIME), + /* lockName */new String(GOOD_LOCK_NAME), + /* lockOwnerId */new Long(GOOD_LOCK_OWNER_ID), + /* lockOwnerName */new String(GOOD_LOCK_OWNER_NAME), + /* stackTrace */createGoodStackTraceCompositeData() }; + CompositeType cType = createGoodThreadInfoCompositeType(); + try { + result = new CompositeDataSupport(cType, names, values); + } catch (OpenDataException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return result; + } + + /** + * @return new array of CompositeData representing an array + * of StackTraceElement. + */ + public static CompositeData[] createGoodStackTraceCompositeData() { + // Let's make the array have three elements. Doesn't matter that + // they are all identical. + CompositeData[] result = new CompositeData[GOOD_STACK_SIZE]; + CompositeType cType = createGoodStackTraceElementCompositeType(); + String[] names = { "className", "methodName", "fileName", "lineNumber", + "nativeMethod" }; + Object[] values = { GOOD_STACK_CLASSNAME, GOOD_STACK_METHODNAME, + GOOD_STACK_FILENAME, new Integer(GOOD_STACK_LINENUMBER), + new Boolean(GOOD_STACK_NATIVEMETHOD) }; + + for (int i = 0; i < result.length; i++) { + try { + result[i] = new CompositeDataSupport(cType, names, values); + } catch (OpenDataException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + }// end for + return result; + } + + /** + * @return CompositeType for use when wrapping up + * ThreadInfo objects in CompositeData + * s. + */ + private static CompositeType createGoodThreadInfoCompositeType() { + CompositeType result = null; + try { + String[] typeNames = { "threadId", "threadName", "threadState", + "suspended", "inNative", "blockedCount", "blockedTime", + "waitedCount", "waitedTime", "lockName", "lockOwnerId", + "lockOwnerName", "stackTrace" }; + String[] typeDescs = { "threadId", "threadName", "threadState", + "suspended", "inNative", "blockedCount", "blockedTime", + "waitedCount", "waitedTime", "lockName", "lockOwnerId", + "lockOwnerName", "stackTrace", }; + OpenType[] typeTypes = { + SimpleType.LONG, + SimpleType.STRING, + SimpleType.STRING, + SimpleType.BOOLEAN, + SimpleType.BOOLEAN, + SimpleType.LONG, + SimpleType.LONG, + SimpleType.LONG, + SimpleType.LONG, + SimpleType.STRING, + SimpleType.LONG, + SimpleType.STRING, + new ArrayType(1, createGoodStackTraceElementCompositeType()) }; + result = new CompositeType(ThreadInfo.class.getName(), + ThreadInfo.class.getName(), typeNames, typeDescs, typeTypes); + } catch (OpenDataException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return result; + } + + private static CompositeType createGoodStackTraceElementCompositeType() { + CompositeType result = null; + String[] typeNames = { "className", "methodName", "fileName", + "lineNumber", "nativeMethod" }; + String[] typeDescs = { "className", "methodName", "fileName", + "lineNumber", "nativeMethod" }; + OpenType[] typeTypes = { SimpleType.STRING, SimpleType.STRING, + SimpleType.STRING, SimpleType.INTEGER, SimpleType.BOOLEAN }; + try { + result = new CompositeType(StackTraceElement.class.getName(), + StackTraceElement.class.getName(), typeNames, typeDescs, + typeTypes); + } catch (OpenDataException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return result; + } + + protected void tearDown() throws Exception { + super.tearDown(); + } + + /* + * Test method for 'java.lang.management.ThreadInfo.getBlockedCount()' + */ + public void testGetBlockedCount() { + assertEquals(GOOD_BLOCKED_COUNT, ti.getBlockedCount()); + } + + /* + * Test method for 'java.lang.management.ThreadInfo.getBlockedTime()' + */ + public void testGetBlockedTime() { + assertEquals(GOOD_BLOCKED_TIME, ti.getBlockedTime()); + } + + /* + * Test method for 'java.lang.management.ThreadInfo.getLockOwnerId()' + */ + public void testGetLockOwnerId() { + assertEquals(GOOD_LOCK_OWNER_ID, ti.getLockOwnerId()); + } + + /* + * Test method for 'java.lang.management.ThreadInfo.getLockOwnerName()' + */ + public void testGetLockOwnerName() { + assertEquals(GOOD_LOCK_OWNER_NAME, ti.getLockOwnerName()); + } + + /* + * Test method for 'java.lang.management.ThreadInfo.getStackTrace()' + */ + public void testGetStackTrace() { + StackTraceElement[] stack = ti.getStackTrace(); + assertEquals(GOOD_STACK_SIZE, stack.length); + for (StackTraceElement element : stack) { + assertEquals(GOOD_STACK_CLASSNAME, element.getClassName()); + assertEquals(GOOD_STACK_NATIVEMETHOD, element.isNativeMethod()); + assertEquals(GOOD_STACK_FILENAME, element.getFileName()); + assertEquals(GOOD_STACK_LINENUMBER, element.getLineNumber()); + assertEquals(GOOD_STACK_METHODNAME, element.getMethodName()); + } + } + + /* + * Test method for 'java.lang.management.ThreadInfo.getThreadId()' + */ + public void testGetThreadId() { + assertEquals(GOOD_THREAD_ID, ti.getThreadId()); + } + + /* + * Test method for 'java.lang.management.ThreadInfo.getThreadName()' + */ + public void testGetThreadName() { + assertEquals(GOOD_THREAD_NAME, ti.getThreadName()); + } + + /* + * Test method for 'java.lang.management.ThreadInfo.getThreadState()' + */ + public void testGetThreadState() { + assertEquals(GOOD_THREAD_STATE, (ti.getThreadState())); + } + + /* + * Test method for 'java.lang.management.ThreadInfo.getWaitedCount()' + */ + public void testGetWaitedCount() { + assertEquals(GOOD_WAITED_COUNT, ti.getWaitedCount()); + } + + /* + * Test method for 'java.lang.management.ThreadInfo.getWaitedTime()' + */ + public void testGetWaitedTime() { + assertEquals(GOOD_WAITED_TIME, ti.getWaitedTime()); + } + + /* + * Test method for 'java.lang.management.ThreadInfo.isInNative()' + */ + public void testIsInNative() { + assertEquals(GOOD_IN_NATIVE, ti.isInNative()); + } + + /* + * Test method for 'java.lang.management.ThreadInfo.isSuspended()' + */ + public void testIsSuspended() { + assertEquals(GOOD_SUSPENDED, ti.isSuspended()); + } + + /* + * Test method for 'java.lang.management.ThreadInfo.toString()' + */ + public void testToString() { + assertEquals(getGoodToStringVal(), ti.toString()); + } + + String getGoodToStringVal() { + StringBuilder result = new StringBuilder(); + result.append("Thread " + GOOD_THREAD_NAME + " (Id = " + GOOD_THREAD_ID + + ") " + GOOD_THREAD_STATE + " " + GOOD_LOCK_NAME); + return result.toString(); + } +} Propchange: harmony/enhanced/classlib/trunk/modules/lang-management/src/test/impl/java/com/ibm/lang/management/ThreadInfoTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: harmony/enhanced/classlib/trunk/modules/lang-management/src/test/impl/java/com/ibm/lang/management/ThreadMXBeanImplTest.java URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/lang-management/src/test/impl/java/com/ibm/lang/management/ThreadMXBeanImplTest.java?view=auto&rev=495839 ============================================================================== --- harmony/enhanced/classlib/trunk/modules/lang-management/src/test/impl/java/com/ibm/lang/management/ThreadMXBeanImplTest.java (added) +++ harmony/enhanced/classlib/trunk/modules/lang-management/src/test/impl/java/com/ibm/lang/management/ThreadMXBeanImplTest.java Fri Jan 12 21:08:48 2007 @@ -0,0 +1,546 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package com.ibm.lang.management; + +import java.lang.management.ManagementFactory; +import java.util.Hashtable; +import java.util.Iterator; + +import javax.management.Attribute; +import javax.management.AttributeList; +import javax.management.AttributeNotFoundException; +import javax.management.InvalidAttributeValueException; +import javax.management.MBeanAttributeInfo; +import javax.management.MBeanConstructorInfo; +import javax.management.MBeanException; +import javax.management.MBeanInfo; +import javax.management.MBeanNotificationInfo; +import javax.management.MBeanOperationInfo; +import javax.management.openmbean.CompositeData; + + +import com.ibm.lang.management.ThreadMXBeanImpl; + +public class ThreadMXBeanImplTest extends SingleInstanceDynamicMXBeanImplTest { + + protected void setUp() throws Exception { + super.setUp(); + mb = (ThreadMXBeanImpl) ManagementFactory.getThreadMXBean(); + } + + protected void tearDown() throws Exception { + super.tearDown(); + } + + // ----------------------------------------------------------------- + // DynamicMBean behaviour tests follow .... + // ----------------------------------------------------------------- + + public final void testGetAttribute() throws Exception { + // The good attributes... + assertNotNull(mb.getAttribute("AllThreadIds")); + assertTrue(mb.getAttribute("AllThreadIds") instanceof long[]); + + assertNotNull(mb.getAttribute("DaemonThreadCount")); + assertTrue(mb.getAttribute("DaemonThreadCount") instanceof Integer); + + assertNotNull(mb.getAttribute("PeakThreadCount")); + assertTrue(mb.getAttribute("PeakThreadCount") instanceof Integer); + + assertNotNull(mb.getAttribute("ThreadCount")); + assertTrue(mb.getAttribute("ThreadCount") instanceof Integer); + + assertNotNull(mb.getAttribute("TotalStartedThreadCount")); + assertTrue(mb.getAttribute("TotalStartedThreadCount") instanceof Long); + + assertNotNull(mb.getAttribute("CurrentThreadCpuTimeSupported")); + assertTrue(mb.getAttribute("CurrentThreadCpuTimeSupported") instanceof Boolean); + + if ((Boolean) mb.getAttribute("CurrentThreadCpuTimeSupported")) { + assertNotNull(mb.getAttribute("CurrentThreadCpuTime")); + assertTrue(mb.getAttribute("CurrentThreadCpuTime") instanceof Long); + + assertNotNull(mb.getAttribute("CurrentThreadUserTime")); + assertTrue(mb.getAttribute("CurrentThreadUserTime") instanceof Long); + } else { + try { + long t1 = (Long) mb.getAttribute("CurrentThreadCpuTime"); + fail("Should have thrown MBeanException"); + } catch (MBeanException ignore) { + } + + try { + long t2 = (Long) mb.getAttribute("CurrentThreadUserTime"); + fail("Should have thrown MBeanException"); + } catch (MBeanException ignore) { + } + } + + assertNotNull(mb.getAttribute("ThreadContentionMonitoringSupported")); + assertTrue((mb.getAttribute("ThreadContentionMonitoringSupported")) instanceof Boolean); + + if ((Boolean) mb.getAttribute("ThreadContentionMonitoringSupported")) { + assertNotNull(mb.getAttribute("ThreadContentionMonitoringEnabled")); + assertTrue(mb.getAttribute("ThreadContentionMonitoringEnabled") instanceof Boolean); + } else { + try { + boolean b = ((Boolean) (mb + .getAttribute("ThreadContentionMonitoringEnabled"))) + .booleanValue(); + fail("Should have thrown MBeanException"); + } catch (MBeanException ignore) { + } + } + + assertNotNull(mb.getAttribute("ThreadCpuTimeSupported")); + assertTrue((mb.getAttribute("ThreadCpuTimeSupported")) instanceof Boolean); + + if ((Boolean) mb.getAttribute("ThreadCpuTimeSupported")) { + assertNotNull(mb.getAttribute("ThreadCpuTimeEnabled")); + assertTrue((mb.getAttribute("ThreadCpuTimeEnabled")) instanceof Boolean); + } else { + try { + boolean b = ((Boolean) (mb.getAttribute("ThreadCpuTimeEnabled"))) + .booleanValue(); + fail("Should have thrown MBeanException"); + } catch (MBeanException ignore) { + } + } + + // A nonexistent attribute should throw an AttributeNotFoundException + try { + long rpm = ((Long) (mb.getAttribute("RPM"))); + fail("Should have thrown an AttributeNotFoundException."); + } catch (AttributeNotFoundException ignore) { + } + + // Type mismatch should result in a casting exception + try { + String bad = (String) (mb.getAttribute("CurrentThreadUserTime")); + fail("Should have thrown a ClassCastException"); + } catch (ClassCastException ignore) { + } + } + + public final void testSetAttribute() throws Exception { + // There are only two writable attributes in this type. + + Attribute attr = new Attribute("ThreadContentionMonitoringEnabled", + new Boolean(true)); + + if ((Boolean) mb.getAttribute("ThreadContentionMonitoringSupported")) { + mb.setAttribute(attr); + } else { + try { + mb.setAttribute(attr); + fail("Should have thrown MBeanException!"); + } catch (MBeanException ignore) { + } + } + + attr = new Attribute("ThreadCpuTimeEnabled", new Boolean(true)); + if ((Boolean) mb.getAttribute("ThreadCpuTimeSupported")) { + mb.setAttribute(attr); + } else { + try { + mb.setAttribute(attr); + fail("Should have thrown MBeanException!"); + } catch (MBeanException ignore) { + } + } + + // The rest of the attempted sets should fail + + attr = new Attribute("AllThreadIds", new long[] { 1L, 2L, 3L, 4L }); + try { + mb.setAttribute(attr); + fail("Should have thrown an AttributeNotFoundException."); + } catch (AttributeNotFoundException ignore) { + } + + attr = new Attribute("CurrentThreadCpuTime", 1415L); + try { + mb.setAttribute(attr); + fail("Should have thrown an AttributeNotFoundException."); + } catch (AttributeNotFoundException ignore) { + } + + attr = new Attribute("DaemonThreadCount", 1415); + try { + mb.setAttribute(attr); + fail("Should have thrown an AttributeNotFoundException."); + } catch (AttributeNotFoundException ignore) { + } + + attr = new Attribute("PeakThreadCount", 1415); + try { + mb.setAttribute(attr); + fail("Should have thrown an AttributeNotFoundException."); + } catch (AttributeNotFoundException ignore) { + } + + attr = new Attribute("ThreadCount", 1415); + try { + mb.setAttribute(attr); + fail("Should have thrown an AttributeNotFoundException."); + } catch (AttributeNotFoundException ignore) { + } + + attr = new Attribute("TotalStartedThreadCount", 1415L); + try { + mb.setAttribute(attr); + fail("Should have thrown an AttributeNotFoundException."); + } catch (AttributeNotFoundException ignore) { + } + + attr = new Attribute("CurrentThreadCpuTimeSupported", true); + try { + mb.setAttribute(attr); + fail("Should have thrown an AttributeNotFoundException."); + } catch (AttributeNotFoundException ignore) { + } + + attr = new Attribute("ThreadContentionMonitoringEnabled", true); + + if ((Boolean) mb.getAttribute("ThreadContentionMonitoringSupported")) { + mb.setAttribute(attr); + } else { + try { + mb.setAttribute(attr); + fail("Should have thrown an AttributeNotFoundException."); + } catch (AttributeNotFoundException ignore) { + } + } + + attr = new Attribute("ThreadContentionMonitoringSupported", true); + try { + mb.setAttribute(attr); + fail("Should have thrown an AttributeNotFoundException."); + } catch (AttributeNotFoundException ignore) { + } + + attr = new Attribute("ThreadCpuTimeEnabled", true); + if ((Boolean) mb.getAttribute("ThreadCpuTimeSupported")) { + mb.setAttribute(attr); + } else { + try { + mb.setAttribute(attr); + fail("Should have thrown an MBeanException."); + } catch (MBeanException ignore) { + } + } + + attr = new Attribute("ThreadCpuTimeSupported", true); + try { + mb.setAttribute(attr); + fail("Should have thrown an AttributeNotFoundException."); + } catch (AttributeNotFoundException ignore) { + } + + // Try and set an attribute with an incorrect type. + attr = new Attribute("ThreadContentionMonitoringEnabled", new Long(42)); + if ((Boolean) mb.getAttribute("ThreadContentionMonitoringSupported")) { + try { + mb.setAttribute(attr); + fail("Should have thrown an InvalidAttributeValueException"); + } catch (InvalidAttributeValueException ignore) { + } + } + } + + public final void testGetAttributes() { + AttributeList attributes = mb.getAttributes(attribs.keySet().toArray( + new String[] {})); + assertNotNull(attributes); + assertTrue(attribs.size() >= attributes.size()); + + // Check through the returned values + Iterator it = attributes.iterator(); + while (it.hasNext()) { + Attribute element = (Attribute) it.next(); + assertNotNull(element); + String name = element.getName(); + Object value = element.getValue(); + if (attribs.containsKey(name)) { + if (attribs.get(name).type.equals(Long.TYPE.getName())) { + // Values of -1 are permitted for this kind of bean. + // e.g. -1 can be returned from + // getCurrentThreadCpuTime() + // if CPU time measurement is currently disabled. + assertTrue(((Long) (value)) > -2); + }// end else a long expected + else if ((attribs.get(name).type) + .equals(Boolean.TYPE.getName())) { + boolean tmp = ((Boolean) value).booleanValue(); + }// end else a boolean expected + else if (attribs.get(name).type.equals(Integer.TYPE.getName())) { + // Values of -1 are permitted for this kind of bean. + // e.g. -1 can be returned from + // getCurrentThreadCpuTime() + // if CPU time measurement is currently disabled. + assertTrue(((Integer) (value)) > -2); + }// end else a long expected + else if (attribs.get(name).type.equals("[J")) { + long[] tmp = (long[]) value; + assertNotNull(tmp); + }// end else a String array expected + else { + fail("Unexpected attribute type returned! : " + name + + " , value = " + value); + } + }// end if a known attribute + else { + fail("Unexpected attribute name returned!"); + }// end else an unknown attribute + }// end while + } + + public final void testSetAttributes() { + // Ideal scenario... + AttributeList attList = new AttributeList(); + Attribute tcme = new Attribute("ThreadContentionMonitoringEnabled", + new Boolean(false)); + Attribute tcte = new Attribute("ThreadCpuTimeEnabled", + new Boolean(true)); + attList.add(tcme); + attList.add(tcte); + AttributeList setAttrs = mb.setAttributes(attList); + assertNotNull(setAttrs); + assertTrue(setAttrs.size() <= 2); + + // A failure scenario - a non-existent attribute... + AttributeList badList = new AttributeList(); + Attribute garbage = new Attribute("Auchenback", new Long(2888)); + badList.add(garbage); + setAttrs = mb.setAttributes(badList); + assertNotNull(setAttrs); + assertTrue(setAttrs.size() == 0); + + // Another failure scenario - a non-writable attribute... + badList = new AttributeList(); + garbage = new Attribute("ThreadCount", new Long(2888)); + badList.add(garbage); + setAttrs = mb.setAttributes(badList); + assertNotNull(setAttrs); + assertTrue(setAttrs.size() == 0); + + // Yet another failure scenario - a wrongly-typed attribute... + badList = new AttributeList(); + garbage = new Attribute("ThreadCpuTimeEnabled", new Long(2888)); + badList.add(garbage); + setAttrs = mb.setAttributes(badList); + assertNotNull(setAttrs); + assertTrue(setAttrs.size() == 0); + } + + public final void testInvoke() throws Exception { + // This type of bean has 8 different operations that can be invoked + // on it. + Object retVal = mb.invoke("findMonitorDeadlockedThreads", + new Object[] {}, null); + // Can get a null return if there are currently no deadlocked + // threads. + + // Good case. + retVal = mb.invoke("getThreadCpuTime", new Object[] { new Long(Thread + .currentThread().getId()) }, + new String[] { Long.TYPE.getName() }); + assertNotNull(retVal); + assertTrue(retVal instanceof Long); + + // Force exception by passing in a negative Thread id + try { + retVal = mb.invoke("getThreadCpuTime", + new Object[] { new Long(-757) }, new String[] { Long.TYPE + .getName() }); + fail("Should have thrown an IllegalArgumentException!"); + } catch (IllegalArgumentException ignore) { + } + + // Good case. long + retVal = mb.invoke("getThreadInfo", new Object[] { new Long(Thread + .currentThread().getId()) }, + new String[] { Long.TYPE.getName() }); + assertNotNull(retVal); + assertTrue(retVal instanceof CompositeData); + CompositeData cd = (CompositeData) retVal; + assertTrue(cd.containsKey("threadId")); + + // Force exception by passing in a negative Thread id. long + try { + retVal = mb.invoke("getThreadInfo", + new Object[] { new Long(-5353) }, new String[] { Long.TYPE + .getName() }); + fail("Should have thrown an IllegalArgumentException!"); + } catch (IllegalArgumentException ignore) { + } + + // Good case. long, int + retVal = mb.invoke("getThreadInfo", new Object[] { + new Long(Thread.currentThread().getId()), new Integer(0) }, + new String[] { Long.TYPE.getName(), Integer.TYPE.getName() }); + // TODO Can't test until we can get back ThreadInfo objects + // from the getThreadInfo(long) method. + assertNotNull(retVal); + assertTrue(retVal instanceof CompositeData); + cd = (CompositeData) retVal; + assertTrue(cd.containsKey("threadId")); + + // Force exception by passing in a negative Thread id. long, int + try { + retVal = mb.invoke("getThreadInfo", new Object[] { new Long(-8467), + new Integer(0) }, new String[] { Long.TYPE.getName(), + Integer.TYPE.getName() }); + fail("Should have thrown an IllegalArgumentException!"); + } catch (IllegalArgumentException ignore) { + } + + // Good case. long[], int + retVal = mb.invoke("getThreadInfo", + new Object[] { new long[] { Thread.currentThread().getId() }, + new Integer(0) }, new String[] { "[J", + Integer.TYPE.getName() }); + // TODO Can't test until we can get back ThreadInfo objects + // from the getThreadInfo(long) method. + assertNotNull(retVal); + assertTrue(retVal instanceof CompositeData[]); + CompositeData[] cdArray = (CompositeData[]) retVal; + assertTrue(cdArray[0].containsKey("threadId")); + + // Force exception by passing in a negative Thread id. long[], int + try { + retVal = mb.invoke("getThreadInfo", new Object[] { + new long[] { -54321L }, new Integer(0) }, new String[] { + "[J", Integer.TYPE.getName() }); + fail("Should have thrown an IllegalArgumentException!"); + } catch (IllegalArgumentException ignore) { + } + + // Good case. long[] + retVal = mb.invoke("getThreadInfo", new Object[] { new long[] { Thread + .currentThread().getId() } }, new String[] { "[J" }); + assertNotNull(retVal); + assertTrue(retVal instanceof CompositeData[]); + cdArray = (CompositeData[]) retVal; + assertTrue(cdArray[0].containsKey("threadId")); + + // Force exception by passing in a negative Thread id. long[] + try { + retVal = mb.invoke("getThreadInfo", + new Object[] { new long[] { -74747L } }, + new String[] { "[J" }); + fail("Should have thrown an IllegalArgumentException!"); + } catch (IllegalArgumentException ignore) { + } + + // Good case. + retVal = mb.invoke("getThreadUserTime", new Object[] { new Long(Thread + .currentThread().getId()) }, + new String[] { Long.TYPE.getName() }); + assertNotNull(retVal); + assertTrue(retVal instanceof Long); + + // Force exception by passing in a negative Thread id + try { + retVal = mb.invoke("getThreadUserTime", new Object[] { new Long( + -757) }, new String[] { Long.TYPE.getName() }); + fail("Should have thrown an IllegalArgumentException!"); + } catch (IllegalArgumentException ignore) { + } + + retVal = mb.invoke("resetPeakThreadCount", new Object[] {}, null); + assertNull(retVal); + // Verify that after this operation is invoked, the + // peak thread count equals the value of current thread count + // taken prior to this method call. + assertTrue(((Integer) mb.getAttribute("PeakThreadCount")) + .equals((Integer) mb.getAttribute("ThreadCount"))); + } + + public final void testGetMBeanInfo() { + MBeanInfo mbi = mb.getMBeanInfo(); + assertNotNull(mbi); + + // Now make sure that what we got back is what we expected. + + // Class name + assertTrue(mbi.getClassName().equals(mb.getClass().getName())); + + // No public constructors + MBeanConstructorInfo[] constructors = mbi.getConstructors(); + assertNotNull(constructors); + assertTrue(constructors.length == 0); + + // No public operations + MBeanOperationInfo[] operations = mbi.getOperations(); + assertNotNull(operations); + assertTrue(operations.length == 8); + + // No notifications + MBeanNotificationInfo[] notifications = mbi.getNotifications(); + assertNotNull(notifications); + assertTrue(notifications.length == 0); + + // Description is just the class name (until I hear it should be + // different) + assertTrue(mbi.getDescription().equals(mb.getClass().getName())); + + // Sixteen attributes - none writable. + MBeanAttributeInfo[] attributes = mbi.getAttributes(); + assertNotNull(attributes); + assertTrue(attributes.length == 12); + for (int i = 0; i < attributes.length; i++) { + MBeanAttributeInfo info = attributes[i]; + assertNotNull(info); + validateAttributeInfo(info); + }// end for + } + + @Override + protected void populateTestAttributes() { + attribs = new Hashtable(); + attribs + .put("AllThreadIds", + new AttributeData("[J", true, false, false)); + attribs.put("CurrentThreadCpuTime", new AttributeData(Long.TYPE + .getName(), true, false, false)); + attribs.put("CurrentThreadUserTime", new AttributeData(Long.TYPE + .getName(), true, false, false)); + attribs.put("DaemonThreadCount", new AttributeData(Integer.TYPE + .getName(), true, false, false)); + attribs.put("PeakThreadCount", new AttributeData( + Integer.TYPE.getName(), true, false, false)); + attribs.put("ThreadCount", new AttributeData(Integer.TYPE.getName(), + true, false, false)); + attribs.put("TotalStartedThreadCount", new AttributeData(Long.TYPE + .getName(), true, false, false)); + attribs.put("CurrentThreadCpuTimeSupported", new AttributeData( + Boolean.TYPE.getName(), true, false, true)); + attribs.put("ThreadContentionMonitoringEnabled", new AttributeData( + Boolean.TYPE.getName(), true, true, true)); + attribs.put("ThreadContentionMonitoringSupported", new AttributeData( + Boolean.TYPE.getName(), true, false, true)); + attribs.put("ThreadCpuTimeEnabled", new AttributeData(Boolean.TYPE + .getName(), true, true, true)); + attribs.put("ThreadCpuTimeSupported", new AttributeData(Boolean.TYPE + .getName(), true, false, true)); + } +} Propchange: harmony/enhanced/classlib/trunk/modules/lang-management/src/test/impl/java/com/ibm/lang/management/ThreadMXBeanImplTest.java ------------------------------------------------------------------------------ svn:eol-style = native