001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *
019: */
020:
021: package org.apache.harmony.lang.management;
022:
023: import java.lang.management.ManagementFactory;
024: import java.lang.management.MemoryPoolMXBean;
025: import java.lang.management.MemoryUsage;
026: import java.util.ArrayList;
027: import java.util.Hashtable;
028: import java.util.Iterator;
029: import java.util.List;
030:
031: import javax.management.Attribute;
032: import javax.management.AttributeList;
033: import javax.management.AttributeNotFoundException;
034: import javax.management.InvalidAttributeValueException;
035: import javax.management.MBeanAttributeInfo;
036: import javax.management.MBeanConstructorInfo;
037: import javax.management.MBeanInfo;
038: import javax.management.MBeanNotificationInfo;
039: import javax.management.MBeanOperationInfo;
040: import javax.management.ReflectionException;
041: import javax.management.openmbean.CompositeData;
042:
043: import org.apache.harmony.lang.management.DynamicMXBeanImpl;
044: import org.apache.harmony.lang.management.MemoryPoolMXBeanImpl;
045:
046: public class MemoryPoolMXBeanImplTest extends
047: MultiInstanceDynamicMXBeanImplTestBase {
048:
049: protected void setUp() throws Exception {
050: super .setUp();
051: mbList = new ArrayList<DynamicMXBeanImpl>();
052: List<MemoryPoolMXBean> allBeans = ManagementFactory
053: .getMemoryPoolMXBeans();
054: for (MemoryPoolMXBean bean : allBeans) {
055: mbList.add((MemoryPoolMXBeanImpl) bean);
056: }
057: }
058:
059: protected void tearDown() throws Exception {
060: super .tearDown();
061: }
062:
063: // -----------------------------------------------------------------
064: // DynamicMBean behaviour tests follow ....
065: // -----------------------------------------------------------------
066:
067: public final void testGetAttributes() {
068: for (DynamicMXBeanImpl mb : mbList) {
069: AttributeList attributes = mb.getAttributes(attribs
070: .keySet().toArray(new String[] {}));
071: assertNotNull(attributes);
072: assertTrue(attributes.size() <= attribs.size());
073:
074: Iterator<?> it = attributes.iterator();
075: while (it.hasNext()) {
076: Attribute element = (Attribute) it.next();
077: assertNotNull(element);
078: String name = element.getName();
079: Object value = element.getValue();
080: if (name.equals("CollectionUsage")) {
081: // Could return a null value if VM does not support
082: // the method.
083: if (value != null) {
084: assertTrue(value instanceof CompositeData);
085: MemoryUsage mu = MemoryUsage
086: .from((CompositeData) value);
087: assertTrue(mu.getCommitted() != -1);
088: assertTrue(mu.getUsed() != -1);
089: }
090: } else if (name.equals("CollectionUsageThreshold")) {
091: assertTrue(value instanceof Long);
092: assertTrue(((Long) (value)) > -1);
093: } else if (name.equals("CollectionUsageThresholdCount")) {
094: assertTrue(value instanceof Long);
095: assertTrue(((Long) (value)) > -1);
096: } else if (name.equals("MemoryManagerNames")) {
097: assertTrue(value instanceof String[]);
098: String[] names = (String[]) value;
099: assertTrue(names.length > 0);
100: for (int i = 0; i < names.length; i++) {
101: String string = names[i];
102: assertNotNull(string);
103: assertTrue(string.length() > 0);
104: }// end for
105: } else if (name.equals("Name")) {
106: assertTrue(value instanceof String);
107: String str = (String) value;
108: assertNotNull(str);
109: } else if (name.equals("PeakUsage")) {
110: assertTrue(value instanceof CompositeData);
111: MemoryUsage mu = MemoryUsage
112: .from((CompositeData) value);
113: assertNotNull(mu);
114: assertTrue(mu.getCommitted() != -1);
115: assertTrue(mu.getUsed() != -1);
116: } else if (name.equals("Type")) {
117: assertTrue(value instanceof String);
118: String str = (String) value;
119: assertNotNull(str);
120: } else if (name.equals("Usage")) {
121: assertTrue(value instanceof CompositeData);
122: MemoryUsage mu = MemoryUsage
123: .from((CompositeData) value);
124: assertNotNull(mu);
125: assertTrue(mu.getCommitted() != -1);
126: assertTrue(mu.getUsed() != -1);
127: } else if (name.equals("UsageThreshold")) {
128: assertTrue(value instanceof Long);
129: assertTrue(((Long) (value)) > -1);
130: } else if (name.equals("UsageThresholdCount")) {
131: assertTrue(value instanceof Long);
132: assertTrue(((Long) (value)) > -1);
133: } else if (name
134: .equals("CollectionUsageThresholdExceeded")) {
135: assertTrue(value instanceof Boolean);
136: } else if (name
137: .equals("CollectionUsageThresholdSupported")) {
138: assertTrue(value instanceof Boolean);
139: } else if (name.equals("UsageThresholdExceeded")) {
140: assertTrue(value instanceof Boolean);
141: } else if (name.equals("UsageThresholdSupported")) {
142: assertTrue(value instanceof Boolean);
143: } else if (name.equals("Valid")) {
144: assertTrue(value instanceof Boolean);
145: } else {
146: fail("Unexpected attribute returned : " + name
147: + " !!");
148: }
149: }// end while
150: }// end for
151: }
152:
153: public final void testSetAttributes() {
154: for (DynamicMXBeanImpl mb : mbList) {
155: // Only two attributes can be set for this platform bean type
156: // - UsageThreshold and CollectionUsageThreshold
157: AttributeList attList = new AttributeList();
158: Attribute newUT = new Attribute("UsageThreshold", new Long(
159: 100 * 1024));
160: attList.add(newUT);
161: AttributeList setAttrs = mb.setAttributes(attList);
162: assertNotNull(setAttrs);
163: assertTrue(setAttrs.size() <= 1);
164:
165: if (setAttrs.size() == 1) {
166: assertTrue(((Attribute) (setAttrs.get(0))).getName()
167: .equals("UsageThreshold"));
168: assertTrue(((Attribute) (setAttrs.get(0))).getValue() instanceof Long);
169: long recoveredValue = (Long) ((Attribute) (setAttrs
170: .get(0))).getValue();
171: assertEquals(100 * 1024, recoveredValue);
172: }
173:
174: attList = new AttributeList();
175: Attribute newCUT = new Attribute(
176: "CollectionUsageThreshold", new Long(250 * 1024));
177: attList.add(newCUT);
178: setAttrs = mb.setAttributes(attList);
179: assertNotNull(setAttrs);
180: assertTrue(setAttrs.size() <= 1);
181:
182: if (setAttrs.size() == 1) {
183: assertTrue(((Attribute) (setAttrs.get(0))).getName()
184: .equals("CollectionUsageThreshold"));
185: assertTrue(((Attribute) (setAttrs.get(0))).getValue() instanceof Long);
186: long recoveredValue = (Long) ((Attribute) (setAttrs
187: .get(0))).getValue();
188: assertEquals(250 * 1024, recoveredValue);
189: }
190:
191: // A failure scenario - a non-existent attribute...
192: AttributeList badList = new AttributeList();
193: Attribute garbage = new Attribute("Bantry", new Long(2888));
194: badList.add(garbage);
195: setAttrs = mb.setAttributes(badList);
196: assertNotNull(setAttrs);
197: assertTrue(setAttrs.size() == 0);
198:
199: // Another failure scenario - a non-writable attribute...
200: badList = new AttributeList();
201: garbage = new Attribute("Name", new String("george"));
202: badList.add(garbage);
203: setAttrs = mb.setAttributes(badList);
204: assertNotNull(setAttrs);
205: assertTrue(setAttrs.size() == 0);
206:
207: // Yet another failure scenario - a wrongly-typed attribute...
208: badList = new AttributeList();
209: garbage = new Attribute("CollectionUsageThreshold",
210: new Boolean(true));
211: badList.add(garbage);
212: setAttrs = mb.setAttributes(badList);
213: assertNotNull(setAttrs);
214: assertTrue(setAttrs.size() == 0);
215: }// end for
216: }
217:
218: public final void testGetMBeanInfo() {
219: for (DynamicMXBeanImpl mb : mbList) {
220: MBeanInfo mbi = mb.getMBeanInfo();
221: assertNotNull(mbi);
222:
223: // Now make sure that what we got back is what we expected.
224:
225: // Class name
226: assertTrue(mbi.getClassName().equals(
227: mb.getClass().getName()));
228:
229: // No public constructors
230: MBeanConstructorInfo[] constructors = mbi.getConstructors();
231: assertNotNull(constructors);
232: assertTrue(constructors.length == 0);
233:
234: // One public operation - resetPeakUsage
235: MBeanOperationInfo[] operations = mbi.getOperations();
236: assertNotNull(operations);
237: assertTrue(operations.length == 1);
238: assertEquals("resetPeakUsage", operations[0].getName());
239:
240: // No notifications
241: MBeanNotificationInfo[] notifications = mbi
242: .getNotifications();
243: assertNotNull(notifications);
244: assertTrue(notifications.length == 0);
245:
246: // Description is just the class name (until I hear it should be
247: // different)
248: assertTrue(mbi.getDescription().equals(
249: mb.getClass().getName()));
250:
251: // Fifteen attributes - only two are writable.
252: MBeanAttributeInfo[] attributes = mbi.getAttributes();
253: assertNotNull(attributes);
254: assertEquals(15, attributes.length);
255: for (int i = 0; i < attributes.length; i++) {
256: MBeanAttributeInfo info = attributes[i];
257: assertNotNull(info);
258: validateAttributeInfo(info);
259: }// end for
260: }// end for
261: }
262:
263: public final void testGetAttribute() throws Exception {
264: for (DynamicMXBeanImpl mb : mbList) {
265: // The 14 good public attributes...
266: {
267: // If collection usage not supported then we can get a
268: // null return here.
269: CompositeData cd = (CompositeData) mb
270: .getAttribute("CollectionUsage");
271: if (cd != null) {
272: MemoryUsage mu = MemoryUsage.from(cd);
273: assertTrue(mu.getCommitted() != -1);
274: assertTrue(mu.getUsed() != -1);
275: }
276: }
277:
278: {
279: if (((MemoryPoolMXBean) mb)
280: .isCollectionUsageThresholdSupported()) {
281: Long l = (Long) mb
282: .getAttribute("CollectionUsageThreshold");
283: assertNotNull(l);
284: assertTrue(l > -1);
285: } else {
286: try {
287: Long l = (Long) mb
288: .getAttribute("CollectionUsageThreshold");
289: } catch (UnsupportedOperationException ignore) {
290: }
291: }// end else collection usage threshold is not supported
292: }
293:
294: {
295: if (((MemoryPoolMXBean) mb)
296: .isCollectionUsageThresholdSupported()) {
297: Long l = (Long) mb
298: .getAttribute("CollectionUsageThresholdCount");
299: assertNotNull(l);
300: assertTrue(l > -1);
301: } else {
302: try {
303: Long l = (Long) mb
304: .getAttribute("CollectionUsageThresholdCount");
305: fail("Should have thrown UnsupportedOperationException");
306: } catch (UnsupportedOperationException ignore) {
307: }
308: }// end else collection usage threshold is not supported
309: }
310:
311: {
312: String[] names = (String[]) mb
313: .getAttribute("MemoryManagerNames");
314: assertNotNull(names);
315: for (int i = 0; i < names.length; i++) {
316: String string = names[i];
317: assertNotNull(string);
318: assertTrue(string.length() > 0);
319: }// end for
320: }
321:
322: {
323: String name = (String) mb.getAttribute("Name");
324: assertNotNull(name);
325: assertTrue(name.length() > 0);
326: }
327:
328: {
329: CompositeData cd = (CompositeData) mb
330: .getAttribute("PeakUsage");
331:
332: if (((MemoryPoolMXBean) mb).isValid()) {
333: assertNotNull(cd);
334: MemoryUsage mu = MemoryUsage.from(cd);
335: assertTrue(mu.getCommitted() != -1);
336: assertTrue(mu.getUsed() != -1);
337: } else {
338: assertNull(cd);
339: }
340: }
341:
342: {
343: String name = (String) mb.getAttribute("Type");
344: assertNotNull(name);
345: assertTrue(name.length() > 0);
346: }
347:
348: {
349: CompositeData cd = (CompositeData) mb
350: .getAttribute("Usage");
351: if (((MemoryPoolMXBean) mb).isValid()) {
352: assertNotNull(cd);
353: MemoryUsage mu = MemoryUsage.from(cd);
354: assertTrue(mu.getCommitted() != -1);
355: assertTrue(mu.getUsed() != -1);
356: } else {
357: assertNull(cd);
358: }
359: }
360:
361: {
362: if (((MemoryPoolMXBean) mb).isUsageThresholdSupported()) {
363: Long l = (Long) mb.getAttribute("UsageThreshold");
364: assertNotNull(l);
365: assertTrue(l > -1);
366: } else {
367: try {
368: Long l = (Long) mb
369: .getAttribute("UsageThreshold");
370: fail("Should have thrown UnsupportedOperationException");
371: } catch (UnsupportedOperationException ignore) {
372: }
373: }// end else usage threshold not supported
374: }
375:
376: {
377: if (((MemoryPoolMXBean) mb).isUsageThresholdSupported()) {
378: Long l = (Long) mb
379: .getAttribute("UsageThresholdCount");
380: assertNotNull(l);
381: assertTrue(l > -1);
382: } else {
383: try {
384: Long l = (Long) mb
385: .getAttribute("UsageThresholdCount");
386: fail("Should have thrown UnsupportedOperationException");
387: } catch (UnsupportedOperationException ignore) {
388: }
389: }// end else usage threshold not supported
390: }
391:
392: {
393: if (((MemoryPoolMXBean) mb)
394: .isCollectionUsageThresholdSupported()) {
395: Boolean b = (Boolean) mb
396: .getAttribute("CollectionUsageThresholdExceeded");
397: assertNotNull(b);
398: } else {
399: try {
400: Boolean b = (Boolean) mb
401: .getAttribute("CollectionUsageThresholdExceeded");
402: fail("Should have thrown UnsupportedOperationException");
403: } catch (UnsupportedOperationException ignore) {
404: }
405: }// end else collection usage threshold not supported
406: }
407:
408: {
409: Boolean b = (Boolean) mb
410: .getAttribute("CollectionUsageThresholdSupported");
411: assertNotNull(b);
412: }
413:
414: {
415: if (((MemoryPoolMXBean) mb).isUsageThresholdSupported()) {
416: Boolean b = (Boolean) mb
417: .getAttribute("UsageThresholdExceeded");
418: assertNotNull(b);
419: } else {
420: try {
421: Boolean b = (Boolean) mb
422: .getAttribute("UsageThresholdExceeded");
423: fail("Should have thrown UnsupportedOperationException");
424: } catch (UnsupportedOperationException ignore) {
425: }
426: }// end else usage threshold not supported
427: }
428:
429: {
430: Boolean b = (Boolean) mb
431: .getAttribute("UsageThresholdSupported");
432: assertNotNull(b);
433: }
434:
435: {
436: Boolean b = (Boolean) mb.getAttribute("Valid");
437: assertNotNull(b);
438: }
439:
440: // A nonexistent attribute should throw an
441: // AttributeNotFoundException
442: try {
443: long rpm = ((Long) (mb.getAttribute("RPM")));
444: fail("Should have thrown an AttributeNotFoundException.");
445: } catch (AttributeNotFoundException ignore) {
446: }
447:
448: // Type mismatch should result in a casting exception
449: try {
450: Long bad = (Long) (mb.getAttribute("Name"));
451: fail("Should have thrown a ClassCastException");
452: } catch (ClassCastException ignore) {
453: }
454: }// end for
455: }
456:
457: public void testSetUsageThresholdAttribute() throws Exception {
458: for (DynamicMXBeanImpl mb : mbList) {
459: if (((MemoryPoolMXBean) mb).isUsageThresholdSupported()) {
460: long originalUT = (Long) mb
461: .getAttribute("UsageThreshold");
462: long newUT = originalUT + 1024;
463: Attribute newUTAttr = new Attribute("UsageThreshold",
464: new Long(newUT));
465: mb.setAttribute(newUTAttr);
466:
467: assertEquals(new Long(newUT), (Long) mb
468: .getAttribute("UsageThreshold"));
469: } else {
470: try {
471: Attribute newUTAttr = new Attribute(
472: "UsageThreshold", new Long(100 * 1024));
473: mb.setAttribute(newUTAttr);
474: fail("Should have thrown UnsupportedOperationException!");
475: } catch (UnsupportedOperationException ignore) {
476: }
477: }// end else usage threshold is not supported
478: }
479: }
480:
481: public void testSetCollectionUsageThresholdAttribute()
482: throws Exception {
483: for (DynamicMXBeanImpl mb : mbList) {
484: if (((MemoryPoolMXBean) mb)
485: .isCollectionUsageThresholdSupported()) {
486: long originalCUT = (Long) mb
487: .getAttribute("CollectionUsageThreshold");
488: long newCUT = originalCUT + 1024;
489: Attribute newCUTAttr = new Attribute(
490: "CollectionUsageThreshold", new Long(newCUT));
491: mb.setAttribute(newCUTAttr);
492:
493: assertEquals(new Long(newCUT), (Long) mb
494: .getAttribute("CollectionUsageThreshold"));
495: } else {
496: try {
497: Attribute newCUTAttr = new Attribute(
498: "CollectionUsageThreshold", new Long(
499: 100 * 1024));
500: mb.setAttribute(newCUTAttr);
501: fail("Should have thrown UnsupportedOperationException");
502: } catch (UnsupportedOperationException ignore) {
503: }
504: }// end else collection usage threshold is not supported
505: }// end for
506: }
507:
508: @Override
509: public void testSetAttribute() throws Exception {
510: for (DynamicMXBeanImpl mb : mbList) {
511: // Good case - set the UsageThreshold value
512: if (((Boolean) mb.getAttribute("UsageThresholdSupported"))) {
513: Attribute attr = new Attribute("UsageThreshold",
514: new Long(68 * 1024));
515: mb.setAttribute(attr);
516: } else {
517: try {
518: Long l = (Long) mb.getAttribute("UsageThreshold");
519: fail("Should have thrown UnsupportedOperationException");
520: } catch (UnsupportedOperationException ignore) {
521: }
522: }// end else usage threshold not supported
523:
524: // Good case - set the CollectionUsageThreshold value
525: if (((Boolean) mb
526: .getAttribute("CollectionUsageThresholdSupported"))) {
527: Attribute attr = new Attribute(
528: "CollectionUsageThreshold", new Long(99 * 1024));
529: mb.setAttribute(attr);
530: } else {
531: try {
532: Long l = (Long) mb.getAttribute("UsageThreshold");
533: fail("Should have thrown UnsupportedOperationException");
534: } catch (UnsupportedOperationException ignore) {
535: }
536: }// end else usage threshold not supported
537:
538: // Let's try and set some non-writable attributes.
539: Attribute attr = new Attribute("UsageThresholdCount",
540: new Long(25));
541: try {
542: mb.setAttribute(attr);
543: fail("Should have thrown an AttributeNotFoundException.");
544: } catch (AttributeNotFoundException e) {
545: }
546:
547: // Try and set the UsageThreshold attribute with an incorrect
548: // type.
549: attr = new Attribute("UsageThreshold", "rubbish");
550: try {
551: mb.setAttribute(attr);
552: fail("Should have thrown an InvalidAttributeValueException");
553: } catch (InvalidAttributeValueException ignore) {
554: }
555: }// end for
556: }
557:
558: public final void testInvoke() throws Exception {
559: for (DynamicMXBeanImpl mb : mbList) {
560: // We have one operation - resetPeakUsage() which should reset
561: // *peak* usage to the current memory usage.
562: Object retVal = mb.invoke("resetPeakUsage",
563: new Object[] {}, null);
564: assertNull(retVal);
565:
566: // Try and invoke a non-existent method...
567: try {
568: retVal = mb.invoke("madeupMethod",
569: new Object[] { "fibber" },
570: new String[] { String.class.getName() });
571: fail("Should have thrown a ReflectionException");
572: } catch (ReflectionException ignore) {
573: }
574:
575: MemoryUsage currentMU = MemoryUsage.from((CompositeData) mb
576: .getAttribute("Usage"));
577: MemoryUsage peakMU = MemoryUsage.from((CompositeData) mb
578: .getAttribute("PeakUsage"));
579: assertEquals(currentMU.getCommitted(), peakMU
580: .getCommitted());
581: assertEquals(currentMU.getInit(), peakMU.getInit());
582: assertEquals(currentMU.getUsed(), peakMU.getUsed());
583: assertEquals(currentMU.getMax(), peakMU.getMax());
584: }// end for
585: }
586:
587: @Override
588: protected void populateTestAttributes() {
589: attribs = new Hashtable<String, AttributeData>();
590: attribs.put("CollectionUsage", new AttributeData(
591: CompositeData.class.getName(), true, false, false));
592: attribs.put("CollectionUsageThreshold", new AttributeData(
593: Long.TYPE.getName(), true, true, false));
594: attribs.put("CollectionUsageThresholdCount", new AttributeData(
595: Long.TYPE.getName(), true, false, false));
596: attribs.put("MemoryManagerNames", new AttributeData(
597: "[Ljava.lang.String;", true, false, false));
598: attribs.put("Name", new AttributeData(String.class.getName(),
599: true, false, false));
600: attribs.put("PeakUsage", new AttributeData(CompositeData.class
601: .getName(), true, false, false));
602: attribs.put("Type", new AttributeData(String.class.getName(),
603: true, false, false));
604: attribs.put("Usage", new AttributeData(CompositeData.class
605: .getName(), true, false, false));
606: attribs.put("UsageThreshold", new AttributeData(Long.TYPE
607: .getName(), true, true, false));
608: attribs.put("UsageThresholdCount", new AttributeData(Long.TYPE
609: .getName(), true, false, false));
610: attribs.put("CollectionUsageThresholdExceeded",
611: new AttributeData(Boolean.TYPE.getName(), true, false,
612: true));
613: attribs.put("CollectionUsageThresholdSupported",
614: new AttributeData(Boolean.TYPE.getName(), true, false,
615: true));
616: attribs.put("UsageThresholdExceeded", new AttributeData(
617: Boolean.TYPE.getName(), true, false, true));
618: attribs.put("UsageThresholdSupported", new AttributeData(
619: Boolean.TYPE.getName(), true, false, true));
620: attribs.put("Valid", new AttributeData(Boolean.TYPE.getName(),
621: true, false, true));
622: }
623: }
|