harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zhouke...@apache.org
Subject svn commit: r932328 - in /harmony/enhanced/java/trunk/classlib/modules/lang-management/src: main/java/java/lang/management/ThreadInfo.java test/impl/java/org/apache/harmony/lang/management/ThreadInfoTest.java
Date Fri, 09 Apr 2010 09:56:50 GMT
Author: zhoukevin
Date: Fri Apr  9 09:56:50 2010
New Revision: 932328

URL: http://svn.apache.org/viewvc?rev=932328&view=rev
Log:
Fix 2 NPE problems in java.lang.management.ThreadInfo code, add many test case for coverage.

Modified:
    harmony/enhanced/java/trunk/classlib/modules/lang-management/src/main/java/java/lang/management/ThreadInfo.java
    harmony/enhanced/java/trunk/classlib/modules/lang-management/src/test/impl/java/org/apache/harmony/lang/management/ThreadInfoTest.java

Modified: harmony/enhanced/java/trunk/classlib/modules/lang-management/src/main/java/java/lang/management/ThreadInfo.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/lang-management/src/main/java/java/lang/management/ThreadInfo.java?rev=932328&r1=932327&r2=932328&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/lang-management/src/main/java/java/lang/management/ThreadInfo.java
(original)
+++ harmony/enhanced/java/trunk/classlib/modules/lang-management/src/main/java/java/lang/management/ThreadInfo.java
Fri Apr  9 09:56:50 2010
@@ -101,6 +101,10 @@ public class ThreadInfo {
             Object[] attributeVals = cd.getAll(attributeNames);
             long threadIdVal = ((Long) attributeVals[0]).longValue();
             String threadNameVal = (String) attributeVals[1];
+            if (threadNameVal == null) {
+                throw new IllegalArgumentException(
+                        "Invalid composite data: Attribute threadName has null value"); //$NON-NLS-1$
+            }
             String threadStateStringVal = (String) attributeVals[2];
 
             // Verify that threadStateStringVal contains a string that can be
@@ -126,8 +130,10 @@ public class ThreadInfo {
             String lockOwnerNameVal = attributeVals[11] != null ? (String) attributeVals[11]
                     : null;
             CompositeData[] stackTraceDataVal = (CompositeData[]) attributeVals[12];
+            if (stackTraceDataVal == null) {
+                throw new IllegalArgumentException("StackTraceElement[] is missing"); //$NON-NLS-1$
+            }
             StackTraceElement[] stackTraceVals = getStackTracesFromCompositeData(stackTraceDataVal);
-
             result = new ThreadInfo(threadIdVal, threadNameVal, threadStateVal,
                     suspendedVal, inNativeVal, blockedCountVal, blockedTimeVal,
                     waitedCountVal, waitedTimeVal, lockNameVal, lockOwnerIdVal,

Modified: harmony/enhanced/java/trunk/classlib/modules/lang-management/src/test/impl/java/org/apache/harmony/lang/management/ThreadInfoTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/lang-management/src/test/impl/java/org/apache/harmony/lang/management/ThreadInfoTest.java?rev=932328&r1=932327&r2=932328&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/lang-management/src/test/impl/java/org/apache/harmony/lang/management/ThreadInfoTest.java
(original)
+++ harmony/enhanced/java/trunk/classlib/modules/lang-management/src/test/impl/java/org/apache/harmony/lang/management/ThreadInfoTest.java
Fri Apr  9 09:56:50 2010
@@ -88,6 +88,8 @@ public class ThreadInfoTest extends Test
 
     private static final String GOOD_LOCK_NAME = "foo.Bar@1234567";
 
+    private static final String GOOD_THREADINFO_CLASSNAME = ThreadInfo.class.getName();
+
     private CompositeData tiCD;
 
     private ThreadInfo ti;
@@ -364,6 +366,393 @@ public class ThreadInfoTest extends Test
         }
     }
 
+    private static final Object stackTraceElementData = createGoodStackTraceCompositeData();
+
+    private static final CompositeType stackTraceElementType = createGoodStackTraceElementCompositeType();
+
+    private String[] initialNames = { "threadId", "threadName", "threadState",
+            "suspended", "inNative", "blockedCount", "blockedTime",
+            "waitedCount", "waitedTime", "lockName", "lockOwnerId",
+            "lockOwnerName", "stackTrace", "additionalName" };
+
+    private Object[] initialValues = { 1L, "threadName",
+            GOOD_THREAD_STATE.toString(), true, false, 1L, 500L, 1L, 1L,
+            "lock", 2L, "lockOwner", stackTraceElementData, "additionalValue" };
+
+    public void test_from_scenario1() throws Exception {
+        ArrayType stackTraceArray = new ArrayType(1, stackTraceElementType);
+        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, stackTraceArray, SimpleType.STRING };
+        CompositeType compositeType = getCompositeType(initialNames, types);
+        CompositeData data = new CompositeDataSupport(compositeType,
+                initialNames, initialValues);
+        ThreadInfo threadInfo = ThreadInfo.from(data);
+        assertEquals(initialValues[0], threadInfo.getThreadId());
+        assertEquals(initialValues[1], threadInfo.getThreadName());
+        assertEquals(GOOD_THREAD_STATE, threadInfo.getThreadState());
+        assertEquals(initialValues[3], threadInfo.isSuspended());
+        assertEquals(initialValues[4], threadInfo.isInNative());
+        assertEquals(initialValues[5], threadInfo.getBlockedCount());
+        assertEquals(initialValues[6], threadInfo.getBlockedTime());
+        assertEquals(initialValues[7], threadInfo.getWaitedCount());
+        assertEquals(initialValues[8], threadInfo.getWaitedTime());
+        assertEquals(initialValues[9], threadInfo.getLockName());
+        assertEquals(initialValues[10], threadInfo.getLockOwnerId());
+        assertEquals(initialValues[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());
+        }
+    }
+
+    public void test_from_scenario2() throws Exception {
+        initialValues[0] = "1";
+        ArrayType stackTraceArray = new ArrayType(1, stackTraceElementType);
+        OpenType[] types = { SimpleType.STRING, SimpleType.STRING,
+                SimpleType.STRING, SimpleType.BOOLEAN, SimpleType.BOOLEAN,
+                SimpleType.LONG, SimpleType.LONG, SimpleType.LONG,
+                SimpleType.LONG, SimpleType.STRING, SimpleType.LONG,
+                SimpleType.STRING, stackTraceArray, SimpleType.STRING };
+        CompositeType compositeType = getCompositeType(initialNames, types);
+        CompositeData data = new CompositeDataSupport(compositeType,
+                initialNames, initialValues);
+        try {
+            ThreadInfo.from(data);
+            fail("should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // Expected
+        }
+    }
+
+    public void test_from_scenario3() throws Exception {
+        int length = 10;
+        String[] names = new String[length];
+        for (int index = 0; index < length; index++) {
+            names[index] = initialNames[index];
+        }
+        Object[] values = new Object[length];
+        for (int index = 0; index < length; index++) {
+            values[index] = initialValues[index];
+        }
+        OpenType[] types = { SimpleType.LONG, SimpleType.STRING,
+                SimpleType.STRING, SimpleType.BOOLEAN, SimpleType.BOOLEAN,
+                SimpleType.LONG, SimpleType.LONG, SimpleType.LONG,
+                SimpleType.LONG, SimpleType.STRING };
+        CompositeType compositeType = getCompositeType(names, types);
+        CompositeData data = new CompositeDataSupport(compositeType, names,
+                values);
+        try {
+            ThreadInfo.from(data);
+            fail("should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // Expected
+        }
+
+        values[0] = null;
+        compositeType = getCompositeType(names, types);
+        data = new CompositeDataSupport(compositeType, names, values);
+        try {
+            ThreadInfo.from(data);
+            fail("should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // Expected
+        }
+    }
+
+    public void test_from_scenario4() throws Exception {
+        initialValues[0] = null;
+        ArrayType stackTraceArray = new ArrayType(1, stackTraceElementType);
+        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, stackTraceArray, SimpleType.STRING };
+        CompositeType compositeType = getCompositeType(initialNames, types);
+        CompositeData data = new CompositeDataSupport(compositeType,
+                initialNames, initialValues);
+        try {
+            ThreadInfo.from(data);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // Expected
+        }
+    }
+
+    public void test_from_scenario5() throws Exception {
+        initialValues[1] = null;
+        ArrayType stackTraceArray = new ArrayType(1, stackTraceElementType);
+        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, stackTraceArray, SimpleType.STRING };
+        CompositeType compositeType = getCompositeType(initialNames, types);
+        CompositeData data = new CompositeDataSupport(compositeType,
+                initialNames, initialValues);
+        try {
+            ThreadInfo.from(data);
+            fail("should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // Expected
+        }
+    }
+
+    public void test_from_scenario6() throws Exception {
+        initialValues[2] = null;
+        ArrayType stackTraceArray = new ArrayType(1, stackTraceElementType);
+        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, stackTraceArray, SimpleType.STRING };
+        CompositeType compositeType = getCompositeType(initialNames, types);
+        CompositeData data = new CompositeDataSupport(compositeType,
+                initialNames, initialValues);
+        try {
+            ThreadInfo.from(data);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // Expected
+        }
+    }
+
+    public void test_from_scenario7() throws Exception {
+        initialValues[3] = null;
+        ArrayType stackTraceArray = new ArrayType(1, stackTraceElementType);
+        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, stackTraceArray, SimpleType.STRING };
+        CompositeType compositeType = getCompositeType(initialNames, types);
+        CompositeData data = new CompositeDataSupport(compositeType,
+                initialNames, initialValues);
+        try {
+            ThreadInfo.from(data);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // Expected
+        }
+    }
+
+    public void test_from_scenario8() throws Exception {
+        initialValues[4] = null;
+        ArrayType stackTraceArray = new ArrayType(1, stackTraceElementType);
+        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, stackTraceArray, SimpleType.STRING };
+        CompositeType compositeType = getCompositeType(initialNames, types);
+        CompositeData data = new CompositeDataSupport(compositeType,
+                initialNames, initialValues);
+        try {
+            ThreadInfo.from(data);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // Expected
+        }
+    }
+
+    public void test_from_scenario9() throws Exception {
+        initialValues[5] = null;
+        ArrayType stackTraceArray = new ArrayType(1, stackTraceElementType);
+        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, stackTraceArray, SimpleType.STRING };
+        CompositeType compositeType = getCompositeType(initialNames, types);
+        CompositeData data = new CompositeDataSupport(compositeType,
+                initialNames, initialValues);
+        try {
+            ThreadInfo.from(data);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // Expected
+        }
+    }
+
+    public void test_from_scenario10() throws Exception {
+        initialValues[6] = null;
+        ArrayType stackTraceArray = new ArrayType(1, stackTraceElementType);
+        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, stackTraceArray, SimpleType.STRING };
+        CompositeType compositeType = getCompositeType(initialNames, types);
+        CompositeData data = new CompositeDataSupport(compositeType,
+                initialNames, initialValues);
+        try {
+            ThreadInfo.from(data);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // Expected
+        }
+    }
+
+    public void test_from_scenario11() throws Exception {
+        initialValues[7] = null;
+        ArrayType stackTraceArray = new ArrayType(1, stackTraceElementType);
+        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, stackTraceArray, SimpleType.STRING };
+        CompositeType compositeType = getCompositeType(initialNames, types);
+        CompositeData data = new CompositeDataSupport(compositeType,
+                initialNames, initialValues);
+        try {
+            ThreadInfo.from(data);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // Expected
+        }
+    }
+
+    public void test_from_scenario12() throws Exception {
+        initialValues[8] = null;
+        ArrayType stackTraceArray = new ArrayType(1, stackTraceElementType);
+        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, stackTraceArray, SimpleType.STRING };
+        CompositeType compositeType = getCompositeType(initialNames, types);
+        CompositeData data = new CompositeDataSupport(compositeType,
+                initialNames, initialValues);
+        try {
+            ThreadInfo.from(data);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // Expected
+        }
+    }
+
+    public void test_from_scenario13() throws Exception {
+        initialValues[9] = null;
+        ArrayType stackTraceArray = new ArrayType(1, stackTraceElementType);
+        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, stackTraceArray, SimpleType.STRING };
+        CompositeType compositeType = getCompositeType(initialNames, types);
+        CompositeData data = new CompositeDataSupport(compositeType,
+                initialNames, initialValues);
+        ThreadInfo threadInfo = ThreadInfo.from(data);
+        assertEquals(initialValues[0], threadInfo.getThreadId());
+        assertEquals(initialValues[1], threadInfo.getThreadName());
+        assertEquals(GOOD_THREAD_STATE, threadInfo.getThreadState());
+        assertEquals(initialValues[3], threadInfo.isSuspended());
+        assertEquals(initialValues[4], threadInfo.isInNative());
+        assertEquals(initialValues[5], threadInfo.getBlockedCount());
+        assertEquals(initialValues[6], threadInfo.getBlockedTime());
+        assertEquals(initialValues[7], threadInfo.getWaitedCount());
+        assertEquals(initialValues[8], threadInfo.getWaitedTime());
+        assertNull(threadInfo.getLockName());
+        assertEquals(initialValues[10], threadInfo.getLockOwnerId());
+        assertEquals(initialValues[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());
+        }
+    }
+
+    public void test_from_scenario14() throws Exception {
+        initialValues[10] = null;
+        ArrayType stackTraceArray = new ArrayType(1, stackTraceElementType);
+        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, stackTraceArray, SimpleType.STRING };
+        CompositeType compositeType = getCompositeType(initialNames, types);
+        CompositeData data = new CompositeDataSupport(compositeType,
+                initialNames, initialValues);
+        try {
+            ThreadInfo.from(data);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // Expected
+        }
+    }
+
+    public void test_from_scenario15() throws Exception {
+        initialValues[11] = null;
+        ArrayType stackTraceArray = new ArrayType(1, stackTraceElementType);
+        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, stackTraceArray, SimpleType.STRING };
+        CompositeType compositeType = getCompositeType(initialNames, types);
+        CompositeData data = new CompositeDataSupport(compositeType,
+                initialNames, initialValues);
+        ThreadInfo.from(data);
+        ThreadInfo threadInfo = ThreadInfo.from(data);
+        assertEquals(initialValues[0], threadInfo.getThreadId());
+        assertEquals(initialValues[1], threadInfo.getThreadName());
+        assertEquals(GOOD_THREAD_STATE, threadInfo.getThreadState());
+        assertEquals(initialValues[3], threadInfo.isSuspended());
+        assertEquals(initialValues[4], threadInfo.isInNative());
+        assertEquals(initialValues[5], threadInfo.getBlockedCount());
+        assertEquals(initialValues[6], threadInfo.getBlockedTime());
+        assertEquals(initialValues[7], threadInfo.getWaitedCount());
+        assertEquals(initialValues[8], threadInfo.getWaitedTime());
+        assertEquals(initialValues[9], threadInfo.getLockName());
+        assertEquals(initialValues[10], threadInfo.getLockOwnerId());
+        assertNull(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());
+        }
+    }
+
+    public void test_from_scenario16() throws Exception {
+        initialValues[12] = null;
+        ArrayType stackTraceArray = new ArrayType(1, stackTraceElementType);
+        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, stackTraceArray, SimpleType.STRING };
+        CompositeType compositeType = getCompositeType(initialNames, types);
+        CompositeData data = new CompositeDataSupport(compositeType,
+                initialNames, initialValues);
+        try {
+            ThreadInfo.from(data);
+            fail("should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // Expected
+        }
+    }
+
+    protected CompositeType getCompositeType(String[] typeNames,
+            OpenType[] typeTypes) throws Exception {
+        return new CompositeType(GOOD_THREADINFO_CLASSNAME,
+                GOOD_THREADINFO_CLASSNAME, typeNames, typeNames, typeTypes);
+    }
+
     String getGoodToStringVal() {
         StringBuilder result = new StringBuilder();
         result.append("Thread " + GOOD_THREAD_NAME + " (Id = " + GOOD_THREAD_ID



Mime
View raw message