1 15 package org.apache.hivemind.management.log4j; 16 17 import java.util.ArrayList ; 18 import java.util.List ; 19 20 import javax.management.MBeanServer ; 21 import javax.management.ObjectInstance ; 22 import javax.management.ObjectName ; 23 24 import junit.framework.AssertionFailedError; 25 26 import org.apache.hivemind.Registry; 27 import org.apache.hivemind.management.ObjectNameBuilder; 28 import org.apache.hivemind.management.impl.ObjectNameBuilderImpl; 29 import org.apache.hivemind.xml.XmlTestCase; 30 import org.apache.log4j.Logger; 31 import org.easymock.ArgumentsMatcher; 32 import org.easymock.MockControl; 33 34 40 public class TestLogManagementMBean extends XmlTestCase 41 { 42 private boolean isMatcherSet = false; 43 44 48 public void testContribution() throws Exception 49 { 50 MockControl serverControl = MockControl.createControl(MBeanServer .class); 53 addControl(serverControl); 54 MBeanServer server = (MBeanServer ) serverControl.getMock(); 55 56 List contributions = new ArrayList (); 57 58 Logger.getLogger("package1.logger1"); 60 Logger.getLogger("package1.logger2"); 61 Logger.getLogger("package1.subpackage1.logger1"); 62 Logger.getLogger("package1.subpackage2.logger1"); 63 Logger.getLogger("package2"); 64 Logger.getLogger("package2.logger1"); 65 Logger.getLogger("package2.logger2"); 66 Logger.getLogger("package3.logger1"); 67 68 addContribution(contributions, "package1.logger1"); 69 addContribution(contributions, "package1.sub*"); 70 addContribution(contributions, "package2.*"); 71 72 ObjectNameBuilder objectNameBuilder = new ObjectNameBuilderImpl(); 73 74 addExpectedRegistration(serverControl, server, "package1.logger1"); 77 addExpectedRegistration(serverControl, server, "package1.subpackage1.logger1"); 78 addExpectedRegistration(serverControl, server, "package1.subpackage2.logger1"); 79 addExpectedRegistration(serverControl, server, "package2.logger1"); 80 addExpectedRegistration(serverControl, server, "package2.logger2"); 81 82 replayControls(); 83 84 LogManagementMBean mbean = new LogManagementMBean(objectNameBuilder, contributions); 85 mbean.preRegister(server, new ObjectName ("hivemind:test=test")); 86 mbean.postRegister(Boolean.TRUE); 87 88 verifyControls(); 89 } 90 91 94 private void addExpectedRegistration(MockControl serverControl, MBeanServer server, 95 String loggerName) throws Exception 96 { 97 server.registerMBean(loggerName, null); 100 101 if (!isMatcherSet) 102 { 103 isMatcherSet = true; 104 serverControl.setMatcher(new ArgumentsMatcher() 106 { 107 public boolean matches(Object [] expected, Object [] value) 108 { 109 String expectedLoggerName = getLoggerNameFromMBean(expected[0]); 111 String actualLoggerName = getLoggerNameFromMBean(value[0]); 112 return expectedLoggerName.equals(actualLoggerName); 113 } 114 115 private String getLoggerNameFromMBean(Object mbean) throws AssertionFailedError 116 { 117 String logName; 118 try 119 { 120 if (mbean instanceof LoggerMBean) 121 { 122 LoggerMBean loggerMBean = (LoggerMBean) mbean; 123 logName = (String ) loggerMBean.getAttribute("name"); 124 } 125 else 126 logName = (String ) mbean; 127 } 128 catch (Exception e) 129 { 130 throw new AssertionFailedError("Error in getLoggerNameFromMBean: " + e.getMessage()); 131 } 132 return logName; 133 } 134 135 public String toString(Object [] value) 136 { 137 return getLoggerNameFromMBean(value[0]); 138 } 139 140 }); 141 } 142 serverControl.setReturnValue(null); 143 } 144 145 private LoggerContribution addContribution(List contributions, String loggerPattern) 146 { 147 LoggerContribution contribution1 = new LoggerContribution(); 148 contribution1.setLoggerPattern(loggerPattern); 149 contributions.add(contribution1); 150 return contribution1; 151 } 152 153 public void testContributionToString() 154 { 155 LoggerContribution contribution1 = new LoggerContribution(); 156 contribution1.setLoggerPattern("package1.test1"); 157 assertNotNull(contribution1.toString()); 158 } 159 160 164 public void testIntegration() throws Exception 165 { 166 Logger logger = Logger.getLogger("package1.logger1"); 167 168 Registry registry = buildFrameworkRegistry("testLogManagementMBean.xml"); 169 170 registry.getService(LogManagement.class); 171 172 MBeanServer mbeanServer = (MBeanServer ) registry.getService(MBeanServer .class); 173 ObjectNameBuilder objectNameBuilder = (ObjectNameBuilder) registry 174 .getService(ObjectNameBuilder.class); 175 ObjectName objectName = objectNameBuilder.createObjectName(logger.getName(), "logger"); 176 177 ObjectInstance instance = mbeanServer.getObjectInstance(objectName); 178 assertNotNull(instance); 179 180 registry.shutdown(); 181 } 182 } | Popular Tags |