Author: ndbeyer Date: Sun May 3 04:58:49 2009 New Revision: 771022 URL: http://svn.apache.org/viewvc?rev=771022&view=rev Log: Apply second patch for HARMONY-6149 - [classlib][lang-management] java.lang.management.MemoryNotificationInfo.from(CompositeData cd) should throw IllegalArgumentException if cd doesn't represent a MemoryNotificationInfo object Removed: harmony/enhanced/classlib/trunk/modules/lang-management/src/test/api/java/org/apache/harmony/lang/management/tests/java/lang/management/HARMONY6149Test.java Modified: harmony/enhanced/classlib/trunk/modules/lang-management/src/main/java/java/lang/management/MemoryNotificationInfo.java harmony/enhanced/classlib/trunk/modules/lang-management/src/main/java/org/apache/harmony/lang/management/ManagementUtils.java harmony/enhanced/classlib/trunk/modules/lang-management/src/test/api/java/org/apache/harmony/lang/management/tests/java/lang/management/MemoryNotificationInfoTest.java harmony/enhanced/classlib/trunk/modules/lang-management/src/test/impl/java/org/apache/harmony/lang/management/ThreadInfoTest.java Modified: harmony/enhanced/classlib/trunk/modules/lang-management/src/main/java/java/lang/management/MemoryNotificationInfo.java URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/lang-management/src/main/java/java/lang/management/MemoryNotificationInfo.java?rev=771022&r1=771021&r2=771022&view=diff ============================================================================== --- harmony/enhanced/classlib/trunk/modules/lang-management/src/main/java/java/lang/management/MemoryNotificationInfo.java (original) +++ harmony/enhanced/classlib/trunk/modules/lang-management/src/main/java/java/lang/management/MemoryNotificationInfo.java Sun May 3 04:58:49 2009 @@ -19,6 +19,8 @@ import javax.management.openmbean.CompositeData; +import org.apache.harmony.lang.management.ManagementUtils; + /** *

* Memory notification information. @@ -27,6 +29,13 @@ * @since 1.5 */ public class MemoryNotificationInfo { + + private final String poolName; + + private final MemoryUsage usage; + + private final long count; + public static final String MEMORY_COLLECTION_THRESHOLD_EXCEEDED = "java.management.memory.collection.threshold.exceeded"; public static final String MEMORY_THRESHOLD_EXCEEDED = "java.management.memory.threshold.exceeded"; @@ -45,30 +54,15 @@ if (cd == null) { return null; } - - final Object poolName = cd.get("poolName"); - if (!(poolName instanceof String)) { - throw new IllegalArgumentException("'poolName' attribute is null or not a String"); - } - - final Object usageObj = cd.get("usage"); - if (!(usageObj instanceof CompositeData)) { - throw new IllegalArgumentException("'usage' attribute is null or not a CompositeData"); - } - final MemoryUsage usage = MemoryUsage.from((CompositeData) usageObj); - - final Object count = cd.get("count"); - if (!(count instanceof Long)) { - throw new IllegalArgumentException("'count' attribute is null"); - } - return new MemoryNotificationInfo((String)poolName, usage, ((Long)count).longValue()); - } - - private final String poolName; + ManagementUtils.verifyFieldNumber(cd, 3); + String[] attributeNames = { "poolName", "usage", "count" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + ManagementUtils.verifyFieldNames(cd, attributeNames); + String[] attributeTypes = { "java.lang.String", + "javax.management.openmbean.CompositeData", "java.lang.Long", }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + ManagementUtils.verifyFieldTypes(cd, attributeNames, attributeTypes); - private final MemoryUsage usage; - - private final long count; + return new MemoryNotificationInfo(cd); + } /** *

@@ -86,7 +80,6 @@ if (poolName == null) { throw new NullPointerException("pooName is null"); //$NON-NLS-1$ } - if (usage == null) { throw new NullPointerException("usage is null"); //$NON-NLS-1$ } @@ -96,6 +89,17 @@ this.count = count; } + private MemoryNotificationInfo(CompositeData cd) { + final Object poolName = cd.get("poolName"); //$NON-NLS-1$ + if (poolName == null) { + throw new IllegalArgumentException( + "Attribute poolName has null value"); //$NON-NLS-1$ + } + this.poolName = (String) poolName; + this.usage = MemoryUsage.from((CompositeData) cd.get("usage")); //$NON-NLS-1$ + this.count = ((Long) cd.get("count")).longValue(); //$NON-NLS-1$ + } + public String getPoolName() { return poolName; } Modified: harmony/enhanced/classlib/trunk/modules/lang-management/src/main/java/org/apache/harmony/lang/management/ManagementUtils.java URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/lang-management/src/main/java/org/apache/harmony/lang/management/ManagementUtils.java?rev=771022&r1=771021&r2=771022&view=diff ============================================================================== --- harmony/enhanced/classlib/trunk/modules/lang-management/src/main/java/org/apache/harmony/lang/management/ManagementUtils.java (original) +++ harmony/enhanced/classlib/trunk/modules/lang-management/src/main/java/org/apache/harmony/lang/management/ManagementUtils.java Sun May 3 04:58:49 2009 @@ -793,12 +793,11 @@ if (allVals[i] instanceof CompositeData) { continue; } - } else { - throw new IllegalArgumentException( - "CompositeData contains an attribute not of expected type. " - + "Expected " + expectedType + ", found " - + actualType); } + throw new IllegalArgumentException( + "CompositeData contains an attribute not of expected type. " + + "Expected " + expectedType + ", found " + + actualType); } }// end for } @@ -825,16 +824,16 @@ /** * Throws an {@link IllegalArgumentException}if the {@link CompositeData} - * argument cd does not have the number of attributes - * specified in i. + * argument cd has less number of attributes specified in + * minSize. * * @param cd * a CompositeData object * @param i * the number of expected attributes in cd */ - public static void verifyFieldNumber(CompositeData cd, int i) { - if (cd.values().size() != i) { + public static void verifyFieldNumber(CompositeData cd, int minSize) { + if (cd.values().size() < minSize) { throw new IllegalArgumentException( "CompositeData object does not have the expected number of attributes"); //$NON-NLS-1$ } Modified: harmony/enhanced/classlib/trunk/modules/lang-management/src/test/api/java/org/apache/harmony/lang/management/tests/java/lang/management/MemoryNotificationInfoTest.java URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/lang-management/src/test/api/java/org/apache/harmony/lang/management/tests/java/lang/management/MemoryNotificationInfoTest.java?rev=771022&r1=771021&r2=771022&view=diff ============================================================================== --- harmony/enhanced/classlib/trunk/modules/lang-management/src/test/api/java/org/apache/harmony/lang/management/tests/java/lang/management/MemoryNotificationInfoTest.java (original) +++ harmony/enhanced/classlib/trunk/modules/lang-management/src/test/api/java/org/apache/harmony/lang/management/tests/java/lang/management/MemoryNotificationInfoTest.java Sun May 3 04:58:49 2009 @@ -21,6 +21,10 @@ import java.lang.management.MemoryUsage; import javax.management.openmbean.CompositeData; +import javax.management.openmbean.CompositeDataSupport; +import javax.management.openmbean.CompositeType; +import javax.management.openmbean.OpenType; +import javax.management.openmbean.SimpleType; import junit.framework.TestCase; @@ -28,6 +32,14 @@ public class MemoryNotificationInfoTest extends TestCase { + private static final String CLASS_NAME = MemoryNotificationInfo.class + .getName(); + + private static final CompositeType memoryUsageCompositeType = ManagementUtils + .getMemoryUsageCompositeType(); + + private CompositeData memoryCompositeData; + public void test_Constructor_NullPoolName_NullUsage() { try { new MemoryNotificationInfo((String) null, (MemoryUsage) null, @@ -87,4 +99,239 @@ final MemoryNotificationInfo memoryNotifyInfo = new MemoryNotificationInfo("Lloyd", memoryUsage, 42); assertEquals(42, memoryNotifyInfo.getCount()); } + + public void test_from_scenario1() throws Exception { + String[] names = { "poolName", "usage", "count" }; + Object[] values = { null, null, null }; + OpenType[] types = { SimpleType.STRING, memoryUsageCompositeType, + SimpleType.LONG }; + CompositeType compositeType = getCompositeType(names, types); + CompositeData data = new CompositeDataSupport(compositeType, names, + values); + try { + MemoryNotificationInfo.from(data); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // Expected + } + } + + public void test_from_scenario2() throws Exception { + String[] names = { "poolName", "usage", "count" }; + Object[] values = { "TestPoolName", null, new Long(-42) }; + OpenType[] types = { SimpleType.STRING, memoryUsageCompositeType, + SimpleType.LONG }; + + CompositeType compositeType = getCompositeType(names, types); + CompositeData data = new CompositeDataSupport(compositeType, names, + values); + MemoryNotificationInfo info = MemoryNotificationInfo.from(data); + assertEquals(values[0], info.getPoolName()); + assertEquals(values[2], info.getCount()); + assertNull(info.getUsage()); + } + + public void test_from_scenario3() throws Exception { + String[] names = { "poolName", "usage", "count" }; + Object[] values = { "TestPoolName", null, null }; + OpenType[] types = { SimpleType.STRING, memoryUsageCompositeType, + SimpleType.LONG }; + + CompositeType compositeType = getCompositeType(names, types); + CompositeData data = new CompositeDataSupport(compositeType, names, + values); + try { + MemoryNotificationInfo.from(data); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // Expected + } + } + + public void test_from_scenario4() throws Exception { // add + String[] names = { "poolName", "usage", "count" }; + Object[] values = { "TestPoolName", memoryCompositeData, new Long(-42) }; + OpenType[] types = { SimpleType.STRING, memoryUsageCompositeType, + SimpleType.LONG }; + + CompositeType compositeType = getCompositeType(names, types); + CompositeData data = new CompositeDataSupport(compositeType, names, + values); + MemoryNotificationInfo info = MemoryNotificationInfo.from(data); + assertEquals(values[0], info.getPoolName()); + assertEquals(values[2], info.getCount()); + MemoryUsage usage = info.getUsage(); + assertEquals(1, usage.getInit()); + assertEquals(2, usage.getUsed()); + assertEquals(3, usage.getCommitted()); + assertEquals(4, usage.getMax()); + } + + public void test_from_scenario5() throws Exception { + String[] names = { "poolName", "usage", "count" }; + Object[] values = { new Long(1), memoryCompositeData, new Long(42) }; + OpenType[] types = { SimpleType.LONG, memoryUsageCompositeType, + SimpleType.LONG }; + + CompositeType compositeType = getCompositeType(names, types); + CompositeData data = new CompositeDataSupport(compositeType, names, + values); + try { + MemoryNotificationInfo.from(data); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // Expected + } + } + + public void test_from_scenario6() throws Exception { + String[] names = { "poolName", "usage", "count" }; + Object[] values = { "TestPoolName", new Long(1), new Long(42) }; + OpenType[] types = { SimpleType.STRING, SimpleType.LONG, + SimpleType.LONG }; + + CompositeType compositeType = getCompositeType(names, types); + CompositeData data = new CompositeDataSupport(compositeType, names, + values); + try { + MemoryNotificationInfo.from(data); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // Expected + } + } + + public void test_from_scenario7() throws Exception { + String[] names = { "poolName", "usage", "count" }; + Object[] values = { "TestPoolName", memoryCompositeData, "42" }; + OpenType[] types = { SimpleType.STRING, memoryUsageCompositeType, + SimpleType.STRING }; + + CompositeType compositeType = getCompositeType(names, types); + CompositeData data = new CompositeDataSupport(compositeType, names, + values); + try { + MemoryNotificationInfo.from(data); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // Expected + } + } + + public void test_from_scenario8() throws Exception { + String[] names = { "poolName" }; + Object[] values = { "TestPoolName" }; + OpenType[] types = { SimpleType.STRING }; + + CompositeType compositeType = getCompositeType(names, types); + CompositeData data = new CompositeDataSupport(compositeType, names, + values); + try { + MemoryNotificationInfo.from(data); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // Expected + } + } + + public void test_from_scenario9() throws Exception { + String[] names = { "usage" }; + Object[] values = { memoryCompositeData }; + OpenType[] types = { memoryUsageCompositeType }; + + CompositeType compositeType = getCompositeType(names, types); + CompositeData data = new CompositeDataSupport(compositeType, names, + values); + try { + MemoryNotificationInfo.from(data); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // Expected + } + } + + public void test_from_scenario10() throws Exception { + String[] names = { "count" }; + Object[] values = { new Long(42) }; + OpenType[] types = { SimpleType.LONG }; + + CompositeType compositeType = getCompositeType(names, types); + CompositeData data = new CompositeDataSupport(compositeType, names, + values); + try { + MemoryNotificationInfo.from(data); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // Expected + } + } + + public void test_from_scenario11() throws Exception { + String[] names = { "notPoolName", "usage", "count" }; + Object[] values = { "TestNotPoolName", memoryCompositeData, + new Long(42) }; + OpenType[] types = { SimpleType.STRING, memoryUsageCompositeType, + SimpleType.LONG }; + + CompositeType compositeType = getCompositeType(names, types); + CompositeData data = new CompositeDataSupport(compositeType, names, + values); + try { + MemoryNotificationInfo.from(data); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // Expected + } + } + + public void test_from_scenario12() throws Exception { + String[] names = { "poolName", "usage", "count", "extention" }; + Object[] values = { "TestPoolName", memoryCompositeData, new Long(42), + null }; + OpenType[] types = { SimpleType.STRING, memoryUsageCompositeType, + SimpleType.LONG, SimpleType.LONG }; + + CompositeType compositeType = getCompositeType(names, types); + CompositeData data = new CompositeDataSupport(compositeType, names, + values); + MemoryNotificationInfo info = MemoryNotificationInfo.from(data); + assertEquals(values[0], info.getPoolName()); + assertEquals(values[2], info.getCount()); + MemoryUsage usage = info.getUsage(); + assertEquals(1, usage.getInit()); + assertEquals(2, usage.getUsed()); + assertEquals(3, usage.getCommitted()); + assertEquals(4, usage.getMax()); + } + + public void test_from_scenario13() throws Exception { + String[] names = { "poolName", "usage", "count", "extention" }; + Object[] values = { "TestPoolName", memoryCompositeData, new Long(42), + "Extention" }; + OpenType[] types = { SimpleType.STRING, memoryUsageCompositeType, + SimpleType.LONG, SimpleType.STRING }; + + CompositeType compositeType = getCompositeType(names, types); + CompositeData data = new CompositeDataSupport(compositeType, names, + values); + MemoryNotificationInfo info = MemoryNotificationInfo.from(data); + assertEquals(values[0], info.getPoolName()); + assertEquals(values[2], info.getCount()); + MemoryUsage usage = info.getUsage(); + assertEquals(1, usage.getInit()); + assertEquals(2, usage.getUsed()); + assertEquals(3, usage.getCommitted()); + assertEquals(4, usage.getMax()); + } + + protected void setUp() { + memoryCompositeData = ManagementUtils + .toMemoryUsageCompositeData(new MemoryUsage(1, 2, 3, 4)); + } + + protected CompositeType getCompositeType(String[] typeNames, + OpenType[] typeTypes) throws Exception { + return new CompositeType(CLASS_NAME, CLASS_NAME, typeNames, typeNames, + typeTypes); + } } Modified: harmony/enhanced/classlib/trunk/modules/lang-management/src/test/impl/java/org/apache/harmony/lang/management/ThreadInfoTest.java URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/lang-management/src/test/impl/java/org/apache/harmony/lang/management/ThreadInfoTest.java?rev=771022&r1=771021&r2=771022&view=diff ============================================================================== --- harmony/enhanced/classlib/trunk/modules/lang-management/src/test/impl/java/org/apache/harmony/lang/management/ThreadInfoTest.java (original) +++ harmony/enhanced/classlib/trunk/modules/lang-management/src/test/impl/java/org/apache/harmony/lang/management/ThreadInfoTest.java Sun May 3 04:58:49 2009 @@ -316,6 +316,54 @@ assertEquals(getGoodToStringVal(), ti.toString()); } + /* + * Test method for 'java.lang.management.ThreadInfo.from(CompositeData)' + * with more than 13 essential fields + */ + public void test_from_fields() throws Exception { + Object stackTraceElementData = createGoodStackTraceCompositeData(); + CompositeType stackTraceElementType = createGoodStackTraceElementCompositeType(); + String[] names = { "threadId", "threadName", "threadState", + "suspended", "inNative", "blockedCount", "blockedTime", + "waitedCount", "waitedTime", "lockName", "lockOwnerId", + "lockOwnerName", "stackTrace", "additionalName" }; + Object[] values = { 1L, "threadName", GOOD_THREAD_STATE.toString(), + true, false, 1L, 500L, 1L, 1L, "lock", 2L, "lockOwner", + stackTraceElementData, "additionalValue" }; + OpenType[] types = { 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, stackTraceElementType), + SimpleType.STRING }; + CompositeType compositeType = new CompositeType(ThreadInfo.class + .getName(), ThreadInfo.class.getName(), names, names, types); + CompositeData data = new CompositeDataSupport(compositeType, names, + values); + ThreadInfo threadInfo = ThreadInfo.from(data); + assertEquals(values[0], threadInfo.getThreadId()); + assertEquals(values[1], threadInfo.getThreadName()); + assertEquals(GOOD_THREAD_STATE, threadInfo.getThreadState()); + assertEquals(values[3], threadInfo.isSuspended()); + assertEquals(values[4], threadInfo.isInNative()); + assertEquals(values[5], threadInfo.getBlockedCount()); + assertEquals(values[6], threadInfo.getBlockedTime()); + assertEquals(values[7], threadInfo.getWaitedCount()); + assertEquals(values[8], threadInfo.getWaitedTime()); + assertEquals(values[9], threadInfo.getLockName()); + assertEquals(values[10], threadInfo.getLockOwnerId()); + assertEquals(values[11], threadInfo.getLockOwnerName()); + StackTraceElement[] stackElements = threadInfo.getStackTrace(); + assertEquals(GOOD_STACK_SIZE, stackElements.length); + for (StackTraceElement element : stackElements) { + 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()); + } + } + String getGoodToStringVal() { StringBuilder result = new StringBuilder(); result.append("Thread " + GOOD_THREAD_NAME + " (Id = " + GOOD_THREAD_ID