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.MemoryManagerMXBean;
025: import java.util.ArrayList;
026: import java.util.Hashtable;
027: import java.util.Iterator;
028: import java.util.List;
029:
030: import javax.management.Attribute;
031: import javax.management.AttributeList;
032: import javax.management.AttributeNotFoundException;
033: import javax.management.MBeanAttributeInfo;
034: import javax.management.MBeanConstructorInfo;
035: import javax.management.MBeanInfo;
036: import javax.management.MBeanNotificationInfo;
037: import javax.management.MBeanOperationInfo;
038:
039: import org.apache.harmony.lang.management.DynamicMXBeanImpl;
040: import org.apache.harmony.lang.management.GarbageCollectorMXBeanImpl;
041: import org.apache.harmony.lang.management.MemoryManagerMXBeanImpl;
042:
043: public class MemoryManagerImplTest extends
044: MultiInstanceDynamicMXBeanImplTestBase {
045:
046: protected void setUp() throws Exception {
047: super .setUp();
048: List<MemoryManagerMXBean> allBeans = ManagementFactory
049: .getMemoryManagerMXBeans();
050: mbList = new ArrayList<DynamicMXBeanImpl>();
051: for (MemoryManagerMXBean bean : allBeans) {
052: mbList.add((MemoryManagerMXBeanImpl) bean);
053: }// end for
054: }
055:
056: protected void tearDown() throws Exception {
057: super .tearDown();
058: }
059:
060: // -----------------------------------------------------------------
061: // DynamicMBean behaviour tests follow ....
062: // -----------------------------------------------------------------
063: public final void testGetAttributes() throws Exception {
064: for (DynamicMXBeanImpl mb : mbList) {
065: AttributeList attributes = mb.getAttributes(attribs
066: .keySet().toArray(new String[] {}));
067: assertNotNull(attributes);
068: assertTrue(attributes.size() == attribs.size());
069:
070: // Check through the returned values
071: Iterator<?> it = attributes.iterator();
072: while (it.hasNext()) {
073: Attribute element = (Attribute) it.next();
074: assertNotNull(element);
075: String name = element.getName();
076: Object value = element.getValue();
077: if (name.equals("Valid")) {
078: // This could be true or false - just so long as we don't
079: // get an exception raised...
080: boolean validVal = ((Boolean) value).booleanValue();
081: } else if (name.equals("Name")) {
082: assertNotNull(value);
083: assertTrue(value instanceof String);
084: assertTrue(((String) value).length() > 0);
085: } else if (name.equals("MemoryPoolNames")) {
086: assertNotNull(value);
087: assertTrue(value instanceof String[]);
088: String[] strVals = (String[]) value;
089: for (int i = 0; i < strVals.length; i++) {
090: String poolName = strVals[i];
091: assertNotNull(poolName);
092: assertTrue(poolName.length() > 0);
093: }// end for
094: }
095: }// end while
096: }// end for
097: }
098:
099: public final void testSetAttributes() {
100: AttributeList badList = new AttributeList();
101: Attribute garbage = new Attribute("Name", "Bez");
102: badList.add(garbage);
103: for (DynamicMXBeanImpl mb : mbList) {
104: // No writable attributes for this type
105: AttributeList setAttrs = mb.setAttributes(badList);
106: assertNotNull(setAttrs);
107: assertTrue(setAttrs.size() == 0);
108: }
109: }
110:
111: public final void testGetMBeanInfo() {
112: for (DynamicMXBeanImpl mb : mbList) {
113: MBeanInfo mbi = mb.getMBeanInfo();
114: assertNotNull(mbi);
115:
116: // Now make sure that what we got back is what we expected.
117:
118: // Class name
119: assertTrue(mbi.getClassName().equals(
120: mb.getClass().getName()));
121:
122: // No public constructors
123: MBeanConstructorInfo[] constructors = mbi.getConstructors();
124: assertNotNull(constructors);
125: assertTrue(constructors.length == 0);
126:
127: // No public operations
128: MBeanOperationInfo[] operations = mbi.getOperations();
129: assertNotNull(operations);
130: assertTrue(operations.length == 0);
131:
132: // No notifications
133: MBeanNotificationInfo[] notifications = mbi
134: .getNotifications();
135: assertNotNull(notifications);
136: assertTrue(notifications.length == 0);
137:
138: // Description is just the class name (until I hear it should be
139: // different)
140: assertTrue(mbi.getDescription().equals(
141: mb.getClass().getName()));
142:
143: // The number of attributes depends on what kind of
144: // MemoryManagerMXBean we have.
145: MBeanAttributeInfo[] attributes = mbi.getAttributes();
146: assertNotNull(attributes);
147: String runtimeType = mb.getClass().getName();
148: if (runtimeType.equals(MemoryManagerMXBeanImpl.class
149: .getName())) {
150: assertEquals(3, attributes.length);
151: validateMemoryManagerAttributes(attributes);
152: } else if (runtimeType
153: .equals(GarbageCollectorMXBeanImpl.class.getName())) {
154: assertEquals(5, attributes.length);
155: validateGCAttributes(attributes);
156: } else {
157: fail("Unexpected kind of memory manager MXBean : "
158: + runtimeType);
159: }
160: }// end for
161: }
162:
163: /**
164: * @param attributes
165: */
166: private void validateMemoryManagerAttributes(
167: MBeanAttributeInfo[] attributes) {
168: for (int i = 0; i < attributes.length; i++) {
169: MBeanAttributeInfo info = attributes[i];
170: assertNotNull(info);
171: validateAttributeInfo(info);
172: }// end for
173: }
174:
175: /**
176: * @param attributes
177: */
178: private void validateGCAttributes(MBeanAttributeInfo[] attributes) {
179: GarbageCollectorMXBeanImplTest gcTest = new GarbageCollectorMXBeanImplTest();
180: for (int i = 0; i < attributes.length; i++) {
181: MBeanAttributeInfo info = attributes[i];
182: assertNotNull(info);
183: gcTest.validateAttributeInfo(info);
184: }// end for
185: }
186:
187: public final void testGetAttribute() throws Exception {
188: for (DynamicMXBeanImpl mb : mbList) {
189: // The good attributes...
190: assertTrue(mb.getAttribute("MemoryPoolNames") instanceof String[]);
191: String[] arr = (String[]) mb
192: .getAttribute("MemoryPoolNames");
193: for (int i = 0; i < arr.length; i++) {
194: String element = arr[i];
195: assertNotNull(element);
196: assertTrue(element.length() > 0);
197: }// end for
198:
199: assertTrue(mb.getAttribute("Name") instanceof String);
200: assertTrue(((String) mb.getAttribute("Name")).length() > 0);
201:
202: // This could be true or false - just so long as we don't get an
203: // exception raised...
204: boolean validVal = ((Boolean) (mb.getAttribute("Valid")));
205:
206: // A nonexistent attribute should throw an
207: // AttributeNotFoundException
208: try {
209: long rpm = ((Long) (mb.getAttribute("RPM")));
210: fail("Should have thrown an AttributeNotFoundException.");
211: } catch (AttributeNotFoundException ignore) {
212: }
213:
214: // Type mismatch should result in a casting exception
215: try {
216: String bad = (String) (mb
217: .getAttribute("MemoryPoolNames"));
218: fail("Should have thrown a ClassCastException");
219: } catch (ClassCastException ignore) {
220: }
221: }// end for
222: }
223:
224: public final void testSetAttribute() throws Exception {
225: for (DynamicMXBeanImpl mb : mbList) {
226: // Let's try and set some non-writable attributes.
227: Attribute attr = new Attribute("Name", "Dando");
228: try {
229: mb.setAttribute(attr);
230: fail("Should have thrown an AttributeNotFoundException.");
231: } catch (AttributeNotFoundException ignore) {
232: }
233:
234: attr = new Attribute("Valid", new Boolean(true));
235: try {
236: mb.setAttribute(attr);
237: fail("Should have thrown an AttributeNotFoundException.");
238: } catch (AttributeNotFoundException ignore) {
239: }
240:
241: attr = new Attribute("MemoryPoolNames", new String[] { "X",
242: "Y", "Z" });
243: try {
244: mb.setAttribute(attr);
245: fail("Should have thrown an AttributeNotFoundException.");
246: } catch (AttributeNotFoundException ignore) {
247: }
248: }// end for
249: }
250:
251: @Override
252: protected void populateTestAttributes() {
253: attribs = new Hashtable<String, AttributeData>();
254: attribs.put("MemoryPoolNames", new AttributeData(
255: "[Ljava.lang.String;", true, false, false));
256: attribs.put("Name", new AttributeData(String.class.getName(),
257: true, false, false));
258: attribs.put("Valid", new AttributeData(Boolean.TYPE.getName(),
259: true, false, true));
260: }
261: }
|