blob: 1ca4b718dc06eba7809ec2cbf7f8b976b2bef2ec [file] [log] [blame]
Felix Meschberger08282c32009-01-28 07:01:55 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.apache.felix.cm.impl;
20
21
22import java.io.ByteArrayOutputStream;
23import java.io.PrintStream;
24import java.lang.reflect.Field;
25
26import junit.framework.TestCase;
27
28import org.apache.felix.cm.MockBundleContext;
29import org.apache.felix.cm.MockLogService;
30import org.osgi.service.log.LogService;
31import org.osgi.util.tracker.ServiceTracker;
32
33
34public class ConfigurationManagerTest extends TestCase
35{
36
37 private PrintStream replacedStdErr;
38
39 private ByteArrayOutputStream output;
40
41
42 protected void setUp() throws Exception
43 {
44 super.setUp();
45
46 replacedStdErr = System.err;
47
48 output = new ByteArrayOutputStream();
49 System.setErr( new PrintStream( output ) );
50 }
51
52
53 protected void tearDown() throws Exception
54 {
55 System.setErr( replacedStdErr );
56
57 super.tearDown();
58 }
59
60
61 public void testLogNoLogService()
62 {
63 ConfigurationManager configMgr = createConfigurationManager( null );
64
65 setLogLevel( configMgr, LogService.LOG_WARNING );
66 assertNoLog( configMgr, LogService.LOG_DEBUG, "Debug Test Message", null );
67 assertNoLog( configMgr, LogService.LOG_INFO, "Info Test Message", null );
68 assertLog( configMgr, LogService.LOG_WARNING, "Warning Test Message", null );
69 assertLog( configMgr, LogService.LOG_ERROR, "Error Test Message", null );
70
71 setLogLevel( configMgr, LogService.LOG_ERROR );
72 assertNoLog( configMgr, LogService.LOG_DEBUG, "Debug Test Message", null );
73 assertNoLog( configMgr, LogService.LOG_INFO, "Info Test Message", null );
74 assertNoLog( configMgr, LogService.LOG_WARNING, "Warning Test Message", null );
75 assertLog( configMgr, LogService.LOG_ERROR, "Error Test Message", null );
76
77 // lower than error -- no output
78 setLogLevel( configMgr, LogService.LOG_ERROR - 1 );
79 assertNoLog( configMgr, LogService.LOG_DEBUG, "Debug Test Message", null );
80 assertNoLog( configMgr, LogService.LOG_INFO, "Info Test Message", null );
81 assertNoLog( configMgr, LogService.LOG_WARNING, "Warning Test Message", null );
82 assertNoLog( configMgr, LogService.LOG_ERROR, "Error Test Message", null );
83
84 // minimal log level -- no output
85 setLogLevel( configMgr, Integer.MIN_VALUE );
86 assertNoLog( configMgr, LogService.LOG_DEBUG, "Debug Test Message", null );
87 assertNoLog( configMgr, LogService.LOG_INFO, "Info Test Message", null );
88 assertNoLog( configMgr, LogService.LOG_WARNING, "Warning Test Message", null );
89 assertNoLog( configMgr, LogService.LOG_ERROR, "Error Test Message", null );
90
91 setLogLevel( configMgr, LogService.LOG_INFO );
92 assertNoLog( configMgr, LogService.LOG_DEBUG, "Debug Test Message", null );
93 assertLog( configMgr, LogService.LOG_INFO, "Info Test Message", null );
94 assertLog( configMgr, LogService.LOG_WARNING, "Warning Test Message", null );
95 assertLog( configMgr, LogService.LOG_ERROR, "Error Test Message", null );
96
97 setLogLevel( configMgr, LogService.LOG_DEBUG );
98 assertLog( configMgr, LogService.LOG_DEBUG, "Debug Test Message", null );
99 assertLog( configMgr, LogService.LOG_INFO, "Info Test Message", null );
100 assertLog( configMgr, LogService.LOG_WARNING, "Warning Test Message", null );
101 assertLog( configMgr, LogService.LOG_ERROR, "Error Test Message", null );
102
103 // maximal log level -- all output
104 setLogLevel( configMgr, Integer.MAX_VALUE );
105 assertLog( configMgr, LogService.LOG_DEBUG, "Debug Test Message", null );
106 assertLog( configMgr, LogService.LOG_INFO, "Info Test Message", null );
107 assertLog( configMgr, LogService.LOG_WARNING, "Warning Test Message", null );
108 assertLog( configMgr, LogService.LOG_ERROR, "Error Test Message", null );
109 }
110
111
112 // this test always expects output since when using a LogService, the log
113 // level property is ignored
114 public void testLogWithLogService()
115 {
116 LogService logService = new MockLogService();
117 ConfigurationManager configMgr = createConfigurationManager( logService );
118
119 setLogLevel( configMgr, LogService.LOG_WARNING );
120 assertLog( configMgr, LogService.LOG_DEBUG, "Debug Test Message", null );
121 assertLog( configMgr, LogService.LOG_INFO, "Info Test Message", null );
122 assertLog( configMgr, LogService.LOG_WARNING, "Warning Test Message", null );
123 assertLog( configMgr, LogService.LOG_ERROR, "Error Test Message", null );
124
125 setLogLevel( configMgr, LogService.LOG_ERROR );
126 assertLog( configMgr, LogService.LOG_DEBUG, "Debug Test Message", null );
127 assertLog( configMgr, LogService.LOG_INFO, "Info Test Message", null );
128 assertLog( configMgr, LogService.LOG_WARNING, "Warning Test Message", null );
129 assertLog( configMgr, LogService.LOG_ERROR, "Error Test Message", null );
130
131 setLogLevel( configMgr, LogService.LOG_ERROR - 1 );
132 assertLog( configMgr, LogService.LOG_DEBUG, "Debug Test Message", null );
133 assertLog( configMgr, LogService.LOG_INFO, "Info Test Message", null );
134 assertLog( configMgr, LogService.LOG_WARNING, "Warning Test Message", null );
135 assertLog( configMgr, LogService.LOG_ERROR, "Error Test Message", null );
136
137 setLogLevel( configMgr, Integer.MIN_VALUE );
138 assertLog( configMgr, LogService.LOG_DEBUG, "Debug Test Message", null );
139 assertLog( configMgr, LogService.LOG_INFO, "Info Test Message", null );
140 assertLog( configMgr, LogService.LOG_WARNING, "Warning Test Message", null );
141 assertLog( configMgr, LogService.LOG_ERROR, "Error Test Message", null );
142
143 setLogLevel( configMgr, LogService.LOG_INFO );
144 assertLog( configMgr, LogService.LOG_DEBUG, "Debug Test Message", null );
145 assertLog( configMgr, LogService.LOG_INFO, "Info Test Message", null );
146 assertLog( configMgr, LogService.LOG_WARNING, "Warning Test Message", null );
147 assertLog( configMgr, LogService.LOG_ERROR, "Error Test Message", null );
148
149 setLogLevel( configMgr, LogService.LOG_DEBUG );
150 assertLog( configMgr, LogService.LOG_DEBUG, "Debug Test Message", null );
151 assertLog( configMgr, LogService.LOG_INFO, "Info Test Message", null );
152 assertLog( configMgr, LogService.LOG_WARNING, "Warning Test Message", null );
153 assertLog( configMgr, LogService.LOG_ERROR, "Error Test Message", null );
154
155 setLogLevel( configMgr, Integer.MAX_VALUE );
156 assertLog( configMgr, LogService.LOG_DEBUG, "Debug Test Message", null );
157 assertLog( configMgr, LogService.LOG_INFO, "Info Test Message", null );
158 assertLog( configMgr, LogService.LOG_WARNING, "Warning Test Message", null );
159 assertLog( configMgr, LogService.LOG_ERROR, "Error Test Message", null );
160 }
161
162
Felix Meschberger1311eb32009-01-28 07:13:49 +0000163 public void testLogSetup()
164 {
165 final MockBundleContext bundleContext = new MockBundleContext();
166 ConfigurationManager configMgr = createConfigurationManager( null );
167
Felix Meschberger988d0412009-01-28 08:01:35 +0000168 // ensure the configuration data goes to target
169 bundleContext.setProperty( "felix.cm.dir", "target/config" );
Felix Meschberger91085712011-10-21 13:54:14 +0000170
Felix Meschberger1311eb32009-01-28 07:13:49 +0000171 // default value is 2
172 bundleContext.setProperty( "felix.cm.loglevel", null );
173 configMgr.start( bundleContext );
174 assertEquals( 2, getLogLevel( configMgr ) );
175 configMgr.stop( bundleContext );
176
177 // illegal number yields default value
178 bundleContext.setProperty( "felix.cm.loglevel", "not-a-number" );
179 configMgr.start( bundleContext );
180 assertEquals( 2, getLogLevel( configMgr ) );
181 configMgr.stop( bundleContext );
182
183 bundleContext.setProperty( "felix.cm.loglevel", "-100" );
184 configMgr.start( bundleContext );
185 assertEquals( -100, getLogLevel( configMgr ) );
186 configMgr.stop( bundleContext );
187
188 bundleContext.setProperty( "felix.cm.loglevel", "4" );
189 configMgr.start( bundleContext );
190 assertEquals( 4, getLogLevel( configMgr ) );
191 configMgr.stop( bundleContext );
192 }
193
194
Felix Meschberger08282c32009-01-28 07:01:55 +0000195 private void assertNoLog( ConfigurationManager configMgr, int level, String message, Throwable t )
196 {
197 try
198 {
199 configMgr.log( level, message, t );
200 assertTrue( "Expecting no log output", output.size() == 0 );
201 }
202 finally
203 {
204 // clear the output for future data
205 output.reset();
206 }
207 }
208
209
210 private void assertLog( ConfigurationManager configMgr, int level, String message, Throwable t )
211 {
212 try
213 {
214 configMgr.log( level, message, t );
215 assertTrue( "Expecting log output", output.size() > 0 );
216
217 final String expectedLog = MockLogService.toMessageLine( level, message );
218 final String actualLog = new String( output.toByteArray() );
219 assertEquals( "Log Message not correct", expectedLog, actualLog );
220
221 }
222 finally
223 {
224 // clear the output for future data
225 output.reset();
226 }
227 }
228
229
230 private static void setLogLevel( ConfigurationManager configMgr, int level )
231 {
232 final String fieldName = "logLevel";
233 try
234 {
235 Field field = configMgr.getClass().getDeclaredField( fieldName );
236 field.setAccessible( true );
237 field.setInt( configMgr, level );
238 }
239 catch ( Throwable ignore )
240 {
Felix Meschberger1311eb32009-01-28 07:13:49 +0000241 throw ( IllegalArgumentException ) new IllegalArgumentException( "Cannot set logLevel field value" )
242 .initCause( ignore );
243 }
244 }
245
246
247 private static int getLogLevel( ConfigurationManager configMgr )
248 {
249 final String fieldName = "logLevel";
250 try
251 {
252 Field field = configMgr.getClass().getDeclaredField( fieldName );
253 field.setAccessible( true );
254 return field.getInt( configMgr );
255 }
256 catch ( Throwable ignore )
257 {
258 throw ( IllegalArgumentException ) new IllegalArgumentException( "Cannot get logLevel field value" )
259 .initCause( ignore );
Felix Meschberger08282c32009-01-28 07:01:55 +0000260 }
261 }
262
263
264 private static ConfigurationManager createConfigurationManager( final LogService logService )
265 {
266 ConfigurationManager configMgr = new ConfigurationManager();
267
268 try
269 {
270 Field field = configMgr.getClass().getDeclaredField( "logTracker" );
271 field.setAccessible( true );
272 field.set( configMgr, new ServiceTracker( new MockBundleContext(), "", null )
273 {
274 public Object getService()
275 {
276 return logService;
277 }
278 } );
279 }
280 catch ( Throwable ignore )
281 {
Felix Meschberger1311eb32009-01-28 07:13:49 +0000282 throw ( IllegalArgumentException ) new IllegalArgumentException( "Cannot set logTracker field value" )
283 .initCause( ignore );
Felix Meschberger08282c32009-01-28 07:01:55 +0000284 }
285
286 return configMgr;
287 }
288}