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.util.Enumeration;
024: import java.util.Hashtable;
025: import java.util.logging.Level;
026: import java.util.logging.LogManager;
027: import java.util.logging.Logger;
028: import java.util.logging.LoggingMXBean;
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: import javax.management.ReflectionException;
039:
040: import org.apache.harmony.lang.management.LoggingMXBeanImpl;
041: import org.apache.harmony.lang.management.ManagementUtils;
042:
043: public class LoggingMXBeanImplTest extends
044: SingleInstanceDynamicMXBeanImplTestBase {
045:
046: private Enumeration<String> loggerNames;
047:
048: protected void setUp() throws Exception {
049: super .setUp();
050: mb = (LoggingMXBeanImpl) LogManager.getLoggingMXBean();
051: loggerNames = LogManager.getLogManager().getLoggerNames();
052: }
053:
054: protected void tearDown() throws Exception {
055: super .tearDown();
056: }
057:
058: public final void testIsSingleton() {
059: // Verify we always get the same instance
060: LoggingMXBean bean = ManagementUtils.getLoggingBean();
061: assertSame(mb, bean);
062: }
063:
064: // -----------------------------------------------------------------
065: // DynamicMBean behaviour tests follow ....
066: // -----------------------------------------------------------------
067:
068: public final void testGetAttribute() throws Exception {
069: // Only one readable attribute for this type.
070: assertNotNull(mb.getAttribute("LoggerNames"));
071: assertTrue(mb.getAttribute("LoggerNames") instanceof String[]);
072:
073: // A nonexistent attribute should throw an AttributeNotFoundException
074: try {
075: long rpm = ((Long) (mb.getAttribute("RPM")));
076: fail("Should have thrown an AttributeNotFoundException.");
077: } catch (AttributeNotFoundException ignore) {
078: }
079:
080: // Type mismatch should result in a casting exception
081: try {
082: String bad = (String) (mb.getAttribute("LoggerNames"));
083: fail("Should have thrown a ClassCastException");
084: } catch (ClassCastException ignore) {
085: }
086: }
087:
088: public final void testSetAttribute() throws Exception {
089: // Nothing is writable for this type
090: Attribute attr = new Attribute("LoggerLevel", "Boris");
091: try {
092: mb.setAttribute(attr);
093: fail("Should have thrown AttributeNotFoundException.");
094: } catch (AttributeNotFoundException ignore) {
095: }
096:
097: attr = new Attribute("LoggerNames", new String[] { "Strummer",
098: "Jones" });
099: try {
100: mb.setAttribute(attr);
101: fail("Should have thrown AttributeNotFoundException.");
102: } catch (AttributeNotFoundException ignore) {
103: }
104: }
105:
106: public final void testGetAttributes() {
107: AttributeList attributes = mb.getAttributes(attribs.keySet()
108: .toArray(new String[] {}));
109: assertNotNull(attributes);
110: assertTrue(attributes.size() == 1);
111:
112: // Check the returned value
113: Attribute element = (Attribute) attributes.get(0);
114: assertNotNull(element);
115: assertEquals("LoggerNames", element.getName());
116: Object value = element.getValue();
117: assertNotNull(value);
118: assertTrue(value instanceof String[]);
119: }
120:
121: public final void testSetAttributes() {
122: // No writable attributes for this type - should get a failure...
123: AttributeList badList = new AttributeList();
124: Attribute garbage = new Attribute("Name", "City Sickness");
125: Attribute trash = new Attribute("SpecVendor", "Marbles");
126: badList.add(garbage);
127: badList.add(trash);
128: AttributeList setAttrs = mb.setAttributes(badList);
129: assertNotNull(setAttrs);
130: assertTrue(setAttrs.size() == 0);
131: }
132:
133: public final void testInvoke() throws Exception {
134: // 3 different operations that can be invoked on this kind of bean.
135: String logName = null;
136: while (loggerNames.hasMoreElements()) {
137: logName = (String) loggerNames.nextElement();
138: // Store the logger's current log level.
139: Logger logger = LogManager.getLogManager().getLogger(
140: logName);
141: Level originalLevel = logger.getLevel();
142: Logger parentLogger = logger.getParent();
143:
144: // Operation #1 --- getLoggerLevel(String)
145: Object retVal = mb.invoke("getLoggerLevel",
146: new Object[] { logName },
147: new String[] { String.class.getName() });
148: assertNotNull(retVal);
149: assertTrue(retVal instanceof String);
150: if (originalLevel != null) {
151: assertEquals(originalLevel.getName(), (String) retVal);
152: } else {
153: assertEquals("", (String) retVal);
154: }
155:
156: // Operation #2 --- getParentLoggerName(String)
157: retVal = mb.invoke("getParentLoggerName",
158: new Object[] { logName },
159: new String[] { String.class.getName() });
160: assertNotNull(retVal);
161: assertTrue(retVal instanceof String);
162: if (parentLogger != null) {
163: assertEquals(parentLogger.getName(), (String) retVal);
164: } else {
165: assertEquals("", (String) retVal);
166: }
167:
168: // Call getParentLoggerName(String) again with a bad argument type.
169: try {
170: retVal = mb.invoke("getParentLoggerName",
171: new Object[] { new Long(311) },
172: new String[] { Long.TYPE.getName() });
173: fail("Should have thrown ReflectionException !!");
174: } catch (ReflectionException ignore) {
175: }
176:
177: // Operation #3 --- setLoggerLevel(String, String)
178: retVal = mb.invoke("setLoggerLevel", new Object[] {
179: logName, Level.SEVERE.getName() }, new String[] {
180: String.class.getName(), String.class.getName() });
181: // Verify the set worked
182: assertEquals(Level.SEVERE.getName(), logger.getLevel()
183: .getName());
184: }// end while
185:
186: // Try invoking a bogus operation ...
187: try {
188: Object retVal = mb.invoke("GetUpStandUp", new Object[] {
189: new Long(7446), new Long(54) }, new String[] {
190: "java.lang.Long", "java.lang.Long" });
191: fail("Should have thrown ReflectionException.");
192: } catch (ReflectionException ignore) {
193: }
194: }
195:
196: public final void testGetMBeanInfo() {
197: MBeanInfo mbi = mb.getMBeanInfo();
198: assertNotNull(mbi);
199:
200: // Now make sure that what we got back is what we expected.
201:
202: // Class name
203: assertTrue(mbi.getClassName().equals(mb.getClass().getName()));
204:
205: // No public constructors
206: MBeanConstructorInfo[] constructors = mbi.getConstructors();
207: assertNotNull(constructors);
208: assertTrue(constructors.length == 0);
209:
210: // Three public operations
211: MBeanOperationInfo[] operations = mbi.getOperations();
212: assertNotNull(operations);
213: assertTrue(operations.length == 3);
214:
215: // No notifications
216: MBeanNotificationInfo[] notifications = mbi.getNotifications();
217: assertNotNull(notifications);
218: assertTrue(notifications.length == 0);
219:
220: // Description is just the class name (until I hear it should be
221: // different)
222: assertTrue(mbi.getDescription().equals(mb.getClass().getName()));
223:
224: // One attribute - not writable.
225: MBeanAttributeInfo[] attributes = mbi.getAttributes();
226: assertNotNull(attributes);
227: assertTrue(attributes.length == 1);
228: MBeanAttributeInfo attribute = attributes[0];
229: assertTrue(attribute.isReadable());
230: assertFalse(attribute.isWritable());
231: assertEquals("LoggerNames", attribute.getName());
232: assertEquals(String[].class.getName(), attribute.getType());
233: }
234:
235: @Override
236: protected void populateTestAttributes() {
237: attribs = new Hashtable<String, AttributeData>();
238: attribs.put("LoggerNames", new AttributeData(
239: "[Ljava.lang.String;", true, false, false));
240: }
241: }
|