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