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.util.Hashtable;
025: import java.util.Iterator;
026:
027: import javax.management.Attribute;
028: import javax.management.AttributeList;
029: import javax.management.AttributeNotFoundException;
030: import javax.management.InvalidAttributeValueException;
031: import javax.management.MBeanAttributeInfo;
032: import javax.management.MBeanConstructorInfo;
033: import javax.management.MBeanInfo;
034: import javax.management.MBeanNotificationInfo;
035: import javax.management.MBeanOperationInfo;
036:
037: import org.apache.harmony.lang.management.ClassLoadingMXBeanImpl;
038:
039: public class ClassLoadingMXBeanImplTest extends
040: SingleInstanceDynamicMXBeanImplTestBase {
041:
042: protected void setUp() throws Exception {
043: super .setUp();
044: mb = (ClassLoadingMXBeanImpl) ManagementFactory
045: .getClassLoadingMXBean();
046: }
047:
048: protected void tearDown() throws Exception {
049: super .tearDown();
050: }
051:
052: // -----------------------------------------------------------------
053: // DynamicMBean behaviour tests follow ....
054: // -----------------------------------------------------------------
055:
056: public final void testGetAttribute() throws Exception {
057: // The good attributes...
058: assertTrue(((Integer) (mb.getAttribute("LoadedClassCount"))) > -1);
059: assertTrue(((Long) (mb.getAttribute("TotalLoadedClassCount"))) > -1);
060: assertTrue(((Long) (mb.getAttribute("UnloadedClassCount"))) > -1);
061:
062: // This could be true or false - just so long as we don't get an
063: // exception raised...
064: boolean verboseVal = ((Boolean) (mb.getAttribute("Verbose")));
065:
066: // A nonexistent attribute should throw an AttributeNotFoundException
067: try {
068: long rpm = ((Long) (mb.getAttribute("RPM")));
069: fail("Should have thrown an AttributeNotFoundException.");
070: } catch (AttributeNotFoundException ignore) {
071: }
072:
073: // Type mismatch should result in a casting exception
074: try {
075: String bad = (String) (mb
076: .getAttribute("TotalLoadedClassCount"));
077: fail("Should have thrown a ClassCastException");
078: } catch (ClassCastException ignore) {
079: }
080: }
081:
082: public final void testSetAttribute() throws Exception {
083: Attribute attr = null;
084: // The one writable attribute of ClassLoadingMXBeanImpl...
085: Boolean before = (Boolean) mb.getAttribute("Verbose");
086: boolean newVal = !before;
087: attr = new Attribute("Verbose", new Boolean(newVal));
088: mb.setAttribute(attr);
089: Boolean after = (Boolean) mb.getAttribute("Verbose");
090: assert (newVal == after);
091:
092: // Let's try and set some non-writable attributes.
093: attr = new Attribute("LoadedClassCount", new Integer(25));
094: try {
095: mb.setAttribute(attr);
096: fail("Should have thrown an AttributeNotFoundException.");
097: } catch (AttributeNotFoundException ignore) {
098: }
099:
100: attr = new Attribute("TotalLoadedClassCount", new Long(3300));
101: try {
102: mb.setAttribute(attr);
103: fail("Should have thrown an AttributeNotFoundException.");
104: } catch (AttributeNotFoundException ignore) {
105: }
106:
107: attr = new Attribute("UnloadedClassCount", new Long(38));
108: try {
109: mb.setAttribute(attr);
110: fail("Should have thrown an AttributeNotFoundException.");
111: } catch (AttributeNotFoundException ignore) {
112: }
113:
114: // Try and set the Verbose attribute with an incorrect type.
115: attr = new Attribute("Verbose", new Long(42));
116: try {
117: mb.setAttribute(attr);
118: fail("Should have thrown an InvalidAttributeValueException.");
119: } catch (InvalidAttributeValueException ignore) {
120: }
121: }
122:
123: public final void testGetAttributes() throws Exception {
124: AttributeList attributes = mb.getAttributes(attribs.keySet()
125: .toArray(new String[] {}));
126: assertNotNull(attributes);
127: assertTrue(attributes.size() == attribs.size());
128:
129: // Check through the returned values
130: Iterator<?> it = attributes.iterator();
131: while (it.hasNext()) {
132: Attribute element = (Attribute) it.next();
133: assertNotNull(element);
134: String name = element.getName();
135: Object value = element.getValue();
136: if (name.equals("Verbose")) {
137: // This could be true or false - just so long as we don't
138: // get an exception raised...
139: boolean verboseVal = ((Boolean) value).booleanValue();
140: } else if (name.equals("LoadedClassCount")) {
141: assertTrue(((Integer) (value)) > -1);
142: } else if (name.equals("TotalLoadedClassCount")) {
143: assertTrue(((Long) (mb
144: .getAttribute("TotalLoadedClassCount"))) > -1);
145: } else if (name.equals("UnloadedClassCount")) {
146: assertTrue(((Long) (value)) > -1);
147: } else {
148: fail("Unexpected attribute name returned!");
149: }
150: }// end while
151: }
152:
153: public final void testSetAttributes() {
154: // Ideal scenario...
155: AttributeList attList = new AttributeList();
156: Attribute verbose = new Attribute("Verbose", new Boolean(false));
157: attList.add(verbose);
158: AttributeList setAttrs = mb.setAttributes(attList);
159: assertNotNull(setAttrs);
160: assertTrue(setAttrs.size() == 1);
161: assertTrue(((Attribute) (setAttrs.get(0))).getName().equals(
162: "Verbose"));
163:
164: // A failure scenario - a non-existent attribute...
165: AttributeList badList = new AttributeList();
166: Attribute garbage = new Attribute("Bantry", new Long(2888));
167: badList.add(garbage);
168: setAttrs = mb.setAttributes(badList);
169: assertNotNull(setAttrs);
170: assertTrue(setAttrs.size() == 0);
171:
172: // Another failure scenario - a non-writable attribute...
173: badList = new AttributeList();
174: garbage = new Attribute("TotalLoadedClassCount", new Long(2888));
175: badList.add(garbage);
176: setAttrs = mb.setAttributes(badList);
177: assertNotNull(setAttrs);
178: assertTrue(setAttrs.size() == 0);
179:
180: // Yet another failure scenario - a wrongly-typed attribute...
181: badList = new AttributeList();
182: garbage = new Attribute("Verbose", new Long(2888));
183: badList.add(garbage);
184: setAttrs = mb.setAttributes(badList);
185: assertNotNull(setAttrs);
186: assertTrue(setAttrs.size() == 0);
187: }
188:
189: public final void testGetMBeanInfo() {
190: MBeanInfo mbi = mb.getMBeanInfo();
191: assertNotNull(mbi);
192:
193: // Now make sure that what we got back is what we expected.
194:
195: // Class name
196: assertTrue(mbi.getClassName().equals(mb.getClass().getName()));
197:
198: // No public constructors
199: MBeanConstructorInfo[] constructors = mbi.getConstructors();
200: assertNotNull(constructors);
201: assertTrue(constructors.length == 0);
202:
203: // No public operations
204: MBeanOperationInfo[] operations = mbi.getOperations();
205: assertNotNull(operations);
206: assertTrue(operations.length == 0);
207:
208: // No notifications
209: MBeanNotificationInfo[] notifications = mbi.getNotifications();
210: assertNotNull(notifications);
211: assertTrue(notifications.length == 0);
212:
213: // Description is just the class name (until I hear it should be
214: // different)
215: assertTrue(mbi.getDescription().equals(mb.getClass().getName()));
216:
217: // Four attributes - only Verbose is writable.
218: MBeanAttributeInfo[] attributes = mbi.getAttributes();
219: assertNotNull(attributes);
220: assertTrue(attributes.length == 4);
221: for (int i = 0; i < attributes.length; i++) {
222: MBeanAttributeInfo info = attributes[i];
223: assertNotNull(info);
224: validateAttributeInfo(info);
225: }// end for
226: }
227:
228: @Override
229: protected void populateTestAttributes() {
230: attribs = new Hashtable<String, AttributeData>();
231: attribs.put("Verbose", new AttributeData(
232: Boolean.TYPE.getName(), true, true, true));
233: attribs.put("LoadedClassCount", new AttributeData(Integer.TYPE
234: .getName(), true, false, false));
235: attribs.put("TotalLoadedClassCount", new AttributeData(
236: Long.TYPE.getName(), true, false, false));
237: attribs.put("UnloadedClassCount", new AttributeData(Long.TYPE
238: .getName(), true, false, false));
239: }
240: }
|