harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r785152 - in /harmony/enhanced/classlib/trunk/modules/lang-management/src: main/java/java/lang/management/MemoryUsage.java test/api/java/org/apache/harmony/lang/management/tests/java/lang/management/MemoryUsageTest.java
Date Tue, 16 Jun 2009 10:43:20 GMT
Author: tellison
Date: Tue Jun 16 10:43:18 2009
New Revision: 785152

URL: http://svn.apache.org/viewvc?rev=785152&view=rev
Log:
Apply patch for HARMONY-6218 ([classlib][lang-management] java.lang.management.MemoryUsage.from(CompositeData
cd) should throw IllegalArgumentException but not NullPointerException)

Modified:
    harmony/enhanced/classlib/trunk/modules/lang-management/src/main/java/java/lang/management/MemoryUsage.java
    harmony/enhanced/classlib/trunk/modules/lang-management/src/test/api/java/org/apache/harmony/lang/management/tests/java/lang/management/MemoryUsageTest.java

Modified: harmony/enhanced/classlib/trunk/modules/lang-management/src/main/java/java/lang/management/MemoryUsage.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/lang-management/src/main/java/java/lang/management/MemoryUsage.java?rev=785152&r1=785151&r2=785152&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/lang-management/src/main/java/java/lang/management/MemoryUsage.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/lang-management/src/main/java/java/lang/management/MemoryUsage.java
Tue Jun 16 10:43:18 2009
@@ -19,6 +19,8 @@
 
 import javax.management.openmbean.CompositeData;
 
+import org.apache.harmony.lang.management.ManagementUtils;
+
 /**
  * <p>
  * A memory usage snapshot.
@@ -28,6 +30,14 @@
  */
 public class MemoryUsage {
 
+    private final long init;
+
+    private final long used;
+
+    private final long committed;
+
+    private final long max;
+
     /**
      * <p>
      * Constructs a MemoryUsage object from the CompositeData passed.
@@ -42,26 +52,21 @@
         if (cd == null) {
             return null;
         }
-        try {
-            long init = ((Long) cd.get("init")).longValue();
-            long used = ((Long) cd.get("used")).longValue();
-            long committed = ((Long) cd.get("committed")).longValue();
-            long max = ((Long) cd.get("max")).longValue();
-            return new MemoryUsage(init, used, committed, max);
-        } catch (ClassCastException e) {
-            // if any cast fails, then a type was incorrect
-            throw new IllegalArgumentException(e);
-        }
+        ManagementUtils.verifyFieldNumber(cd, 4);
+        String[] attributeNames = { "init", "used", "committed", "max" }; //$NON-NLS-1$ //$NON-NLS-2$
//$NON-NLS-3$ //$NON-NLS-4$
+        ManagementUtils.verifyFieldNames(cd, attributeNames);
+        String longClassName = "java.lang.Long"; //$NON-NLS-1$
+        String[] attributeTypes = { longClassName, longClassName,
+                longClassName, longClassName };
+        ManagementUtils.verifyFieldTypes(cd, attributeNames, attributeTypes);
+
+        long init = ((Long) cd.get("init")).longValue();
+        long used = ((Long) cd.get("used")).longValue();
+        long committed = ((Long) cd.get("committed")).longValue();
+        long max = ((Long) cd.get("max")).longValue();
+        return new MemoryUsage(init, used, committed, max);
     }
 
-    private final long init;
-
-    private final long used;
-
-    private final long committed;
-
-    private final long max;
-
     /**
      * <p>
      * Constructs a new MemoryUsage instance.

Modified: harmony/enhanced/classlib/trunk/modules/lang-management/src/test/api/java/org/apache/harmony/lang/management/tests/java/lang/management/MemoryUsageTest.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/MemoryUsageTest.java?rev=785152&r1=785151&r2=785152&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/lang-management/src/test/api/java/org/apache/harmony/lang/management/tests/java/lang/management/MemoryUsageTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/lang-management/src/test/api/java/org/apache/harmony/lang/management/tests/java/lang/management/MemoryUsageTest.java
Tue Jun 16 10:43:18 2009
@@ -17,6 +17,7 @@
 
 package org.apache.harmony.lang.management.tests.java.lang.management;
 
+import java.lang.management.MemoryNotificationInfo;
 import java.lang.management.MemoryUsage;
 
 import javax.management.openmbean.CompositeData;
@@ -29,6 +30,9 @@
 
 public class MemoryUsageTest extends TestCase {
 
+    private static final String CLASS_NAME = MemoryNotificationInfo.class
+            .getName();
+
     public void testFrom() throws Exception {
         String[] itemNames = { "init", "used", "committed", "max" };
         Object[] items = { Long.valueOf(1024), Long.valueOf(2048), Long.valueOf(4096),
@@ -94,6 +98,138 @@
         assertNull(MemoryUsage.from(null));
     }
 
+    public void test_from_scenario1() throws Exception {
+        String[] names = { "init", "used", "committed", "max", "additionName" };
+        Object[] values = { 1024L, 2048L, 4096L, 8128L, "additionalValue" };
+        OpenType[] types = { SimpleType.LONG, SimpleType.LONG, SimpleType.LONG,
+                SimpleType.LONG, SimpleType.STRING };
+        CompositeType compositeType = getCompositeType(names, types);
+        CompositeData data = new CompositeDataSupport(compositeType, names,
+                values);
+        MemoryUsage usage = MemoryUsage.from(data);
+        assertEquals(values[0], usage.getInit());
+        assertEquals(values[1], usage.getUsed());
+        assertEquals(values[2], usage.getCommitted());
+        assertEquals(values[3], usage.getMax());
+    }
+
+    public void test_from_scenario2() throws Exception {
+        String[] names = { "init", "used", "committed" };
+        Object[] values = { null, null, null };
+        OpenType[] types = { SimpleType.LONG, SimpleType.LONG, SimpleType.LONG };
+        CompositeType compositeType = getCompositeType(names, types);
+        CompositeData data = new CompositeDataSupport(compositeType, names,
+                values);
+        try {
+            MemoryUsage.from(data);
+            fail("should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // Expected
+        }
+    }
+
+    public void test_from_scenario3() throws Exception {
+        String[] names = { "init", "used", "committed", "max" };
+        Object[] values = { null, 2048L, 4096L, 8128L };
+        OpenType[] types = { SimpleType.LONG, SimpleType.LONG, SimpleType.LONG,
+                SimpleType.LONG };
+        CompositeType compositeType = getCompositeType(names, types);
+        CompositeData data = new CompositeDataSupport(compositeType, names,
+                values);
+        try {
+            MemoryUsage.from(data);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // Expected
+        }
+    }
+
+    public void test_from_scenario4() throws Exception {
+        String[] names = { "init", "used", "committed", "max" };
+        Object[] values = { 1024L, null, 4096L, 8128L };
+        OpenType[] types = { SimpleType.LONG, SimpleType.LONG, SimpleType.LONG,
+                SimpleType.LONG };
+        CompositeType compositeType = getCompositeType(names, types);
+        CompositeData data = new CompositeDataSupport(compositeType, names,
+                values);
+        try {
+            MemoryUsage.from(data);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // Expected
+        }
+    }
+
+    public void test_from_scenario5() throws Exception {
+        String[] names = { "init", "used", "committed", "max" };
+        Object[] values = { 1024L, 2048L, null, 8128L };
+        OpenType[] types = { SimpleType.LONG, SimpleType.LONG, SimpleType.LONG,
+                SimpleType.LONG };
+        CompositeType compositeType = getCompositeType(names, types);
+        CompositeData data = new CompositeDataSupport(compositeType, names,
+                values);
+        try {
+            MemoryUsage.from(data);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // Expected
+        }
+    }
+
+    public void test_from_scenario6() throws Exception {
+        String[] names = { "init", "used", "committed", "max" };
+        Object[] values = { 1024L, 2048L, 4096L, null };
+        OpenType[] types = { SimpleType.LONG, SimpleType.LONG, SimpleType.LONG,
+                SimpleType.LONG };
+        CompositeType compositeType = getCompositeType(names, types);
+        CompositeData data = new CompositeDataSupport(compositeType, names,
+                values);
+        try {
+            MemoryUsage.from(data);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // Expected
+        }
+    }
+
+    public void test_from_scenario7() throws Exception {
+        String[] names = { "noninit", "used", "committed", "max" };
+        Object[] values = { 1024L, 2048L, 4096L, 8128L };
+        OpenType[] types = { SimpleType.LONG, SimpleType.LONG, SimpleType.LONG,
+                SimpleType.LONG };
+        CompositeType compositeType = getCompositeType(names, types);
+        CompositeData data = new CompositeDataSupport(compositeType, names,
+                values);
+        try {
+            MemoryUsage.from(data);
+            fail("should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // Expected
+        }
+    }
+
+    public void test_from_scenario8() throws Exception {
+        String[] names = { "init", "used", "committed", "max" };
+        Object[] values = { "initValue", 2048L, 4096L, 8128L };
+        OpenType[] types = { SimpleType.STRING, SimpleType.LONG,
+                SimpleType.LONG, SimpleType.LONG };
+        CompositeType compositeType = getCompositeType(names, types);
+        CompositeData data = new CompositeDataSupport(compositeType, names,
+                values);
+        try {
+            MemoryUsage.from(data);
+            fail("should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // Expected
+        }
+    }
+
+    protected CompositeType getCompositeType(String[] typeNames,
+            OpenType[] typeTypes) throws Exception {
+        return new CompositeType(CLASS_NAME, CLASS_NAME, typeNames, typeNames,
+                typeTypes);
+    }
+
     public void testConstructor() {
         try {
             new MemoryUsage(-2, 2048, 4096, 8128);



Mime
View raw message