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