summaryrefslogtreecommitdiff
path: root/java/broker/src/test/java/org/apache/qpid/server/logging/log4j/LoggingManagementFacadeTest.java
blob: 72b34868ba2e79b41a84e631c914654852e43608 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
package org.apache.qpid.server.logging.log4j;

import java.io.File;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Level;
import org.apache.qpid.test.utils.TestFileUtils;
import org.apache.qpid.util.FileUtils;

import junit.framework.TestCase;

public class LoggingManagementFacadeTest extends TestCase
{
    private LoggingManagementFacade _loggingFacade;
    private String _log4jXmlFile;

    @Override
    protected void setUp() throws Exception
    {
        super.setUp();
        _log4jXmlFile = createTestLog4jXml();
        _loggingFacade = LoggingManagementFacade.configure(_log4jXmlFile);
    }

    public void testGetAvailableLoggerLevels() throws Exception
    {
        List<String> levels = _loggingFacade.getAvailableLoggerLevels();
        assertTrue(levels.contains("ALL"));
        assertTrue(levels.contains("TRACE"));
        assertTrue(levels.contains("DEBUG"));
        assertTrue(levels.contains("INFO"));
        assertTrue(levels.contains("WARN"));
        assertTrue(levels.contains("ERROR"));
        assertTrue(levels.contains("FATAL"));
        assertTrue(levels.contains("OFF"));
        assertEquals(8, levels.size());
    }

    public void testRetrieveConfigFileRootLoggerLevel() throws Exception
    {
        String level = _loggingFacade.retrieveConfigFileRootLoggerLevel();
        assertEquals(Level.WARN.toString(), level);
    }

    public void testSetConfigFileRootLoggerLevel() throws Exception
    {
        String oldLevel = _loggingFacade.retrieveConfigFileRootLoggerLevel();
        assertEquals("WARN", oldLevel);

        _loggingFacade.setConfigFileRootLoggerLevel("INFO");

        String level = _loggingFacade.retrieveConfigFileRootLoggerLevel();
        assertEquals("INFO", level);
    }

    public void testRetrieveConfigFileLoggerLevels() throws Exception
    {
        Map<String, String> levels = _loggingFacade.retrieveConfigFileLoggersLevels();
        assertEquals(3, levels.size());
        String abcLevel = levels.get("a.b.c");
        String abc1Level = levels.get("a.b.c.1");
        String abc2Level = levels.get("a.b.c.2");
        assertEquals("INFO", abcLevel);
        assertEquals("DEBUG", abc1Level);
        assertEquals("TRACE", abc2Level);
    }

    public void testSetConfigFileLoggerLevels() throws Exception
    {
        final String loggerName = "a.b.c";

        assertConfigFileLoggingLevel(loggerName, "INFO");

        _loggingFacade.setConfigFileLoggerLevel(loggerName, "WARN");

        Map<String, String> levels = _loggingFacade.retrieveConfigFileLoggersLevels();
        String abcLevel = levels.get(loggerName);
        assertEquals("WARN", abcLevel);
    }

    public void testSetConfigFileLoggerLevelsWhereLoggerDoesNotExist() throws Exception
    {
        try
        {
            _loggingFacade.setConfigFileLoggerLevel("does.not.exist", "WARN");
            fail("Exception not thrown");
        }
        catch (LoggingFacadeException lfe)
        {
            // PASS
            assertEquals("Can't find logger does.not.exist", lfe.getMessage());
        }
    }

    public void testRetrieveRuntimeRootLoggerLevel() throws Exception
    {
        String level = _loggingFacade.retrieveRuntimeRootLoggerLevel();
        assertEquals(Level.WARN.toString(), level);
    }

    public void testSetRuntimeRootLoggerLevel() throws Exception
    {
        String oldLevel = _loggingFacade.retrieveRuntimeRootLoggerLevel();
        assertEquals("WARN", oldLevel);

        _loggingFacade.setRuntimeRootLoggerLevel("INFO");

        String level = _loggingFacade.retrieveRuntimeRootLoggerLevel();
        assertEquals("INFO", level);
    }

    public void testRetrieveRuntimeLoggersLevels() throws Exception
    {
        Map<String, String> levels = _loggingFacade.retrieveRuntimeLoggersLevels();
        // Don't assert size as implementation itself uses logging and we'd count its loggers too
        String abcLevel = levels.get("a.b.c");
        String abc1Level = levels.get("a.b.c.1");
        String abc2Level = levels.get("a.b.c.2");
        assertEquals("INFO", abcLevel);
        assertEquals("DEBUG", abc1Level);
        assertEquals("TRACE", abc2Level);
    }

    public void testSetRuntimeLoggerLevel() throws Exception
    {
        final String loggerName = "a.b.c";

        assertRuntimeLoggingLevel(loggerName, "INFO");

        _loggingFacade.setRuntimeLoggerLevel(loggerName, "WARN");

        assertRuntimeLoggingLevel(loggerName, "WARN");
    }

    public void testSetRuntimeLoggerToInheritFromParent() throws Exception
    {
        final String parentLoggerName = "a.b.c";
        final String childLoggerName = "a.b.c.1";

        assertRuntimeLoggingLevel(parentLoggerName, "INFO");
        assertRuntimeLoggingLevel(childLoggerName, "DEBUG");

        _loggingFacade.setRuntimeLoggerLevel(childLoggerName, null);

        assertRuntimeLoggingLevel(parentLoggerName, "INFO");
        assertRuntimeLoggingLevel(childLoggerName, "INFO");
    }

    public void testSetRuntimeLoggerLevelsWhereLoggerDoesNotExist() throws Exception
    {
        final String loggerName = "does.not.exist2";

        Map<String, String> oldLevels = _loggingFacade.retrieveRuntimeLoggersLevels();
        assertFalse(oldLevels.containsKey(loggerName));

        try
        {
            _loggingFacade.setRuntimeLoggerLevel(loggerName, "WARN");
            fail("Exception not thrown");
        }
        catch (LoggingFacadeException lfe)
        {
            // PASS
            assertEquals("Can't find logger " + loggerName, lfe.getMessage());
        }

        Map<String, String> levels = _loggingFacade.retrieveRuntimeLoggersLevels();
        assertFalse(levels.containsKey(loggerName));
   }

    public void testReloadOfChangedLog4JFileUpdatesRuntimeLogLevel() throws Exception
    {
        final String loggerName = "a.b.c";

        assertRuntimeLoggingLevel(loggerName, "INFO");
        assertConfigFileLoggingLevel(loggerName, "INFO");

        _loggingFacade.setConfigFileLoggerLevel(loggerName, "WARN");

        assertRuntimeLoggingLevel(loggerName, "INFO");

        _loggingFacade.reload();

        assertRuntimeLoggingLevel(loggerName, "WARN");
    }


    public void testReloadOfLog4JFileRevertsRuntimeChanges() throws Exception
    {
        final String loggerName = "a.b.c";

        assertRuntimeLoggingLevel(loggerName, "INFO");
        assertConfigFileLoggingLevel(loggerName, "INFO");

        _loggingFacade.setRuntimeLoggerLevel(loggerName, "WARN");

        assertRuntimeLoggingLevel(loggerName, "WARN");

        _loggingFacade.reload();

        assertRuntimeLoggingLevel(loggerName, "INFO");
    }

    private void assertConfigFileLoggingLevel(final String loggerName, String expectedLevel) throws Exception
    {
        Map<String, String> levels = _loggingFacade.retrieveConfigFileLoggersLevels();
        String actualLevel = levels.get(loggerName);
        assertEquals(expectedLevel, actualLevel);
    }

    private void assertRuntimeLoggingLevel(final String loggerName, String expectedLevel) throws Exception
    {
        Map<String, String> levels = _loggingFacade.retrieveRuntimeLoggersLevels();
        String actualLevel = levels.get(loggerName);
        assertEquals(expectedLevel, actualLevel);
    }

    private String createTestLog4jXml() throws Exception
    {
        return TestFileUtils.createTempFileFromResource(this, "LoggingFacadeTest.log4j.xml").getAbsolutePath();
    }
}