KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > management > log4j > TestLogManagementMBean


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.management.log4j;
16
17 import java.util.ArrayList JavaDoc;
18 import java.util.List JavaDoc;
19
20 import javax.management.MBeanServer JavaDoc;
21 import javax.management.ObjectInstance JavaDoc;
22 import javax.management.ObjectName JavaDoc;
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 /**
35  * Tests {@link org.apache.hivemind.management.log4j.LogManagementMBean}
36  *
37  * @author Achim Huegen
38  * @since 1.1
39  */

40 public class TestLogManagementMBean extends XmlTestCase
41 {
42     private boolean isMatcherSet = false;
43
44     /**
45      * Checks that loggers defined in the contribution are correctly registered This is verified by
46      * mocking the Mbeanserver
47      */

48     public void testContribution() throws Exception JavaDoc
49     {
50         // Use unordered Mockcontrol, since ordered has a bug with
51
// matchers and multiple method calls of a method with return value
52
MockControl serverControl = MockControl.createControl(MBeanServer JavaDoc.class);
53         addControl(serverControl);
54         MBeanServer JavaDoc server = (MBeanServer JavaDoc) serverControl.getMock();
55
56         List JavaDoc contributions = new ArrayList JavaDoc();
57
58         // Add some loggers
59
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         // Training
75
// These are the loggers that are expected to be registered as mbean
76
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 JavaDoc("hivemind:test=test"));
86         mbean.postRegister(Boolean.TRUE);
87
88         verifyControls();
89     }
90
91     /**
92      * Adds an expected call of registerMBean to the server mock object
93      */

94     private void addExpectedRegistration(MockControl serverControl, MBeanServer JavaDoc server,
95             String JavaDoc loggerName) throws Exception JavaDoc
96     {
97         // Provide the logger name as first parameter, thats not type compatible
98
// but the matcher can handle it
99
server.registerMBean(loggerName, null);
100
101         if (!isMatcherSet)
102         {
103             isMatcherSet = true;
104             // Set a matcher that compares the name of the logger
105
serverControl.setMatcher(new ArgumentsMatcher()
106             {
107                 public boolean matches(Object JavaDoc[] expected, Object JavaDoc[] value)
108                 {
109                     // Compare name of the logger only
110
String JavaDoc expectedLoggerName = getLoggerNameFromMBean(expected[0]);
111                     String JavaDoc actualLoggerName = getLoggerNameFromMBean(value[0]);
112                     return expectedLoggerName.equals(actualLoggerName);
113                 }
114
115                 private String JavaDoc getLoggerNameFromMBean(Object JavaDoc mbean) throws AssertionFailedError
116                 {
117                     String JavaDoc logName;
118                     try
119                     {
120                         if (mbean instanceof LoggerMBean)
121                         {
122                             LoggerMBean loggerMBean = (LoggerMBean) mbean;
123                             logName = (String JavaDoc) loggerMBean.getAttribute("name");
124                         }
125                         else
126                             logName = (String JavaDoc) mbean;
127                     }
128                     catch (Exception JavaDoc e)
129                     {
130                         throw new AssertionFailedError("Error in getLoggerNameFromMBean: " + e.getMessage());
131                     }
132                     return logName;
133                 }
134
135                 public String JavaDoc toString(Object JavaDoc[] value)
136                 {
137                     return getLoggerNameFromMBean(value[0]);
138                 }
139
140             });
141         }
142         serverControl.setReturnValue(null);
143     }
144
145     private LoggerContribution addContribution(List JavaDoc contributions, String JavaDoc 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     /**
161      * Tests the LogManagementBean via the hivemind registry Configures one logger mbean and checks
162      * for its presence in the mbean server
163      */

164     public void testIntegration() throws Exception JavaDoc
165     {
166         Logger logger = Logger.getLogger("package1.logger1");
167
168         Registry registry = buildFrameworkRegistry("testLogManagementMBean.xml");
169
170         registry.getService(LogManagement.class);
171
172         MBeanServer JavaDoc mbeanServer = (MBeanServer JavaDoc) registry.getService(MBeanServer JavaDoc.class);
173         ObjectNameBuilder objectNameBuilder = (ObjectNameBuilder) registry
174                 .getService(ObjectNameBuilder.class);
175         ObjectName JavaDoc objectName = objectNameBuilder.createObjectName(logger.getName(), "logger");
176
177         ObjectInstance JavaDoc instance = mbeanServer.getObjectInstance(objectName);
178         assertNotNull(instance);
179
180         registry.shutdown();
181     }
182 }
Popular Tags