summaryrefslogtreecommitdiffstats
path: root/tags/java-M1-20060522/java/sca/common/src/test/java/org/apache/tuscany/common/monitor/impl/JavaLoggingTestCase.java
blob: 985f7ef9b62390c4162d766704d4d5c002f9c623 (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
/**
 *
 *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
 *
 *  Licensed 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.tuscany.common.monitor.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

import junit.framework.TestCase;

import org.apache.tuscany.common.monitor.MonitorFactory;
import org.apache.tuscany.common.monitor.LogLevel;

/**
 * Test case for the JavaLoggingMonitorFactory.
 * 
 * @version $Rev$ $Date$
 */
public class JavaLoggingTestCase extends TestCase {
    private static final Logger logger = Logger.getLogger(Monitor.class.getName());
    private static final MockHandler handler = new MockHandler();

    private MonitorFactory factory;

    /**
     * Smoke test to ensure the logger is working.
     */
    public void testLogger() {
        logger.info("test");
        assertEquals(1, handler.logs.size());
    }

    /**
     * Test that no record is logged.
     */
    public void testUnloggedEvent() {
        Monitor mon = factory.getMonitor(Monitor.class);
        mon.eventNotToLog();
        assertEquals(0, handler.logs.size());
    }

    /**
     * Test the correct record is written for an event with no arguments.
     */
    public void testEventWithNoArgs() {
        Monitor mon = factory.getMonitor(Monitor.class);
        mon.eventWithNoArgs();
        assertEquals(1, handler.logs.size());
        LogRecord record = handler.logs.get(0);
        assertEquals(Level.INFO, record.getLevel());
        assertEquals(logger.getName(), record.getLoggerName());
        assertEquals(Monitor.class.getName() + "#eventWithNoArgs", record.getMessage());
    }

    /**
     * Test the correct record is written for an event defined by annotation.
     */
    public void testEventWithAnnotation() {
        Monitor mon = factory.getMonitor(Monitor.class);
        mon.eventWithAnnotation();
        assertEquals(1, handler.logs.size());
        LogRecord record = handler.logs.get(0);
        assertEquals(Level.INFO, record.getLevel());
        assertEquals(logger.getName(), record.getLoggerName());
        assertEquals(Monitor.class.getName() + "#eventWithAnnotation", record.getMessage());
    }

    /**
     * Test a Throwable is logged when passed to an event.
     */
    public void testEventWithThrowable() {
        Exception e = new Exception();
        Monitor mon = factory.getMonitor(Monitor.class);
        mon.eventWithThrowable(e);
        assertEquals(1, handler.logs.size());
        LogRecord record = handler.logs.get(0);
        assertEquals(Level.WARNING, record.getLevel());
        assertEquals(logger.getName(), record.getLoggerName());
        assertEquals(Monitor.class.getName() + "#eventWithThrowable", record.getMessage());
        assertSame(e, record.getThrown());
    }

    /**
     * Test the argument is logged.
     */
    public void testEventWithOneArg() {
        Monitor mon = factory.getMonitor(Monitor.class);
        mon.eventWithOneArg("ARG");
        assertEquals(1, handler.logs.size());
        LogRecord record = handler.logs.get(0);
        assertEquals(Monitor.class.getName() + "#eventWithOneArg", record.getMessage());
    }

    protected void setUp() throws Exception {
        super.setUp();
        logger.setUseParentHandlers(false);
        logger.addHandler(handler);
        handler.flush();

        String sourceClass = Monitor.class.getName();
        Properties levels = new Properties();
        levels.setProperty(sourceClass + "#eventWithNoArgs", "INFO");
        levels.setProperty(sourceClass + "#eventWithOneArg", "INFO");
        levels.setProperty(sourceClass + "#eventWithThrowable", "WARNING");
        factory = new JavaLoggingMonitorFactory(levels, Level.FINE, "TestMessages");
    }

    protected void tearDown() throws Exception {
        logger.removeHandler(handler);
        handler.flush();
        super.tearDown();
    }

    /**
     * Mock log handler to capture records.
     */
    public static class MockHandler extends Handler {
        List<LogRecord> logs = new ArrayList<LogRecord>();

        public void publish(LogRecord record) {
            logs.add(record);
        }

        public void flush() {
            logs.clear();
        }

        public void close() throws SecurityException {
        }
    }

    @SuppressWarnings({"JavaDoc"})
    public static interface Monitor {
        void eventNotToLog();

        void eventWithNoArgs();

        void eventWithOneArg(String msg);

        void eventWithThrowable(Exception e);

        @LogLevel("INFO")
        void eventWithAnnotation();
    }
}