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.GarbageCollectorMXBean;
024: import java.lang.management.ManagementFactory;
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:
042: public class GarbageCollectorMXBeanImplTest extends
043: MultiInstanceDynamicMXBeanImplTestBase {
044:
045: protected void setUp() throws Exception {
046: super .setUp();
047: List<GarbageCollectorMXBean> allBeans = ManagementFactory
048: .getGarbageCollectorMXBeans();
049: mbList = new ArrayList<DynamicMXBeanImpl>();
050: for (GarbageCollectorMXBean bean : allBeans) {
051: mbList.add((GarbageCollectorMXBeanImpl) bean);
052: }// end for
053: }
054:
055: protected void tearDown() throws Exception {
056: super .tearDown();
057: }
058:
059: // -----------------------------------------------------------------
060: // DynamicMBean behaviour tests follow ....
061: // -----------------------------------------------------------------
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: } else if (name.equals("CollectionCount")) {
095: assertNotNull(value);
096: assertTrue(value instanceof Long);
097: // It is possible for this count to be -1 if undefined for
098: // this garbage collector
099: assertTrue((Long) value > -2);
100: } else if (name.equals("CollectionTime")) {
101: assertNotNull(value);
102: assertTrue(value instanceof Long);
103: // It is possible for this time to be -1 if undefined for
104: // this garbage collector
105: assertTrue((Long) value > -2);
106: }
107: }// end while
108: }// end for
109: }
110:
111: public final void testSetAttributes() {
112: // No writable attributes for this type
113: for (DynamicMXBeanImpl mb : mbList) {
114: AttributeList badList = new AttributeList();
115: Attribute garbage = new Attribute("Name", "Bez");
116: badList.add(garbage);
117: AttributeList setAttrs = mb.setAttributes(badList);
118: assertNotNull(setAttrs);
119: assertTrue(setAttrs.size() == 0);
120: }// end for
121: }
122:
123: public final void testGetMBeanInfo() {
124: for (DynamicMXBeanImpl mb : mbList) {
125: MBeanInfo mbi = mb.getMBeanInfo();
126: assertNotNull(mbi);
127:
128: // Now make sure that what we got back is what we expected.
129:
130: // Class name
131: assertTrue(mbi.getClassName().equals(
132: mb.getClass().getName()));
133:
134: // No public constructors
135: MBeanConstructorInfo[] constructors = mbi.getConstructors();
136: assertNotNull(constructors);
137: assertTrue(constructors.length == 0);
138:
139: // No public operations
140: MBeanOperationInfo[] operations = mbi.getOperations();
141: assertNotNull(operations);
142: assertTrue(operations.length == 0);
143:
144: // No notifications
145: MBeanNotificationInfo[] notifications = mbi
146: .getNotifications();
147: assertNotNull(notifications);
148: assertTrue(notifications.length == 0);
149:
150: // Description is just the class name
151: assertTrue(mbi.getDescription().equals(
152: mb.getClass().getName()));
153:
154: // 5 standard attributes - none is writable.
155: MBeanAttributeInfo[] attributes = mbi.getAttributes();
156: assertNotNull(attributes);
157: assertTrue(attributes.length == 5);
158: for (int i = 0; i < attributes.length; i++) {
159: MBeanAttributeInfo info = attributes[i];
160: assertNotNull(info);
161: validateAttributeInfo(info);
162: }// end for
163: }// end for
164: }
165:
166: public final void testGetAttribute() throws Exception {
167: for (DynamicMXBeanImpl mb : mbList) {
168: // The good attributes...
169: assertTrue(mb.getAttribute("MemoryPoolNames") instanceof String[]);
170: String[] arr = (String[]) mb
171: .getAttribute("MemoryPoolNames");
172: for (int i = 0; i < arr.length; i++) {
173: String element = arr[i];
174: assertNotNull(element);
175: assertTrue(element.length() > 0);
176: }// end for
177:
178: assertTrue(mb.getAttribute("Name") instanceof String);
179: assertTrue(((String) mb.getAttribute("Name")).length() > 0);
180:
181: // This could be true or false - just so long as we don't get an
182: // exception raised...
183: boolean validVal = ((Boolean) (mb.getAttribute("Valid")));
184:
185: assertTrue(mb.getAttribute("CollectionCount") instanceof Long);
186: // It is possible for this count to be -1 if undefined for
187: // this garbage collector
188: assertTrue(((Long) mb.getAttribute("CollectionCount")) > -2);
189:
190: assertTrue(mb.getAttribute("CollectionTime") instanceof Long);
191: // It is possible for this time to be -1 if undefined for
192: // this garbage collector
193: assertTrue(((Long) mb.getAttribute("CollectionTime")) > -2);
194:
195: // A nonexistent attribute should throw an
196: // AttributeNotFoundException
197: try {
198: long rpm = ((Long) (mb.getAttribute("RPM")));
199: fail("Should have thrown an AttributeNotFoundException.");
200: } catch (AttributeNotFoundException ignore) {
201: }
202:
203: // Type mismatch should result in a casting exception
204: try {
205: String bad = (String) (mb
206: .getAttribute("CollectionTime"));
207: fail("Should have thrown a ClassCastException");
208: } catch (ClassCastException ignore) {
209: }
210: }// end for
211: }
212:
213: public final void testSetAttribute() throws Exception {
214: for (DynamicMXBeanImpl mb : mbList) {
215:
216: // Let's try and set some non-writable attributes.
217: Attribute attr = new Attribute("Name", "Dando");
218: try {
219: mb.setAttribute(attr);
220: fail("Should have thrown an AttributeNotFoundException.");
221: } catch (AttributeNotFoundException ignore) {
222: }
223:
224: attr = new Attribute("Valid", new Boolean(true));
225: try {
226: mb.setAttribute(attr);
227: fail("Should have thrown an AttributeNotFoundException.");
228: } catch (AttributeNotFoundException ignore) {
229: }
230:
231: attr = new Attribute("MemoryPoolNames", new String[] { "X",
232: "Y", "Z" });
233: try {
234: mb.setAttribute(attr);
235: fail("Should have thrown an AttributeNotFoundException.");
236: } catch (AttributeNotFoundException ignore) {
237: }
238:
239: attr = new Attribute("CollectionCount", new Long(233));
240: try {
241: mb.setAttribute(attr);
242: fail("Should have thrown an AttributeNotFoundException.");
243: } catch (AttributeNotFoundException ignore) {
244: }
245:
246: attr = new Attribute("CollectionTime", new Long(233));
247: try {
248: mb.setAttribute(attr);
249: fail("Should have thrown an AttributeNotFoundException.");
250: } catch (AttributeNotFoundException ignore) {
251: }
252: }// end for
253: }
254:
255: @Override
256: protected void populateTestAttributes() {
257: attribs = new Hashtable<String, AttributeData>();
258: attribs.put("MemoryPoolNames", new AttributeData(
259: "[Ljava.lang.String;", true, false, false));
260: attribs.put("Name", new AttributeData(String.class.getName(),
261: true, false, false));
262: attribs.put("Valid", new AttributeData(Boolean.TYPE.getName(),
263: true, false, true));
264: attribs.put("CollectionCount", new AttributeData(Long.TYPE
265: .getName(), true, false, false));
266: attribs.put("CollectionTime", new AttributeData(Long.TYPE
267: .getName(), true, false, false));
268: }
269: }
|