-
Notifications
You must be signed in to change notification settings - Fork 416
/
Copy pathLoggingTest.java
166 lines (149 loc) · 5.02 KB
/
LoggingTest.java
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
/**
* Copyright (C) 2006-2009 Dustin Sallings
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALING
* IN THE SOFTWARE.
*/
package net.spy.memcached.compat.log;
import junit.framework.TestCase;
// XXX: This really needs to get log4j configured first.
/**
* Make sure logging is enabled.
*/
public class LoggingTest extends TestCase {
private Logger logger = null;
/**
* Get an instance of LoggingTest.
*/
public LoggingTest(String name) {
super(name);
}
/**
* Set up logging.
*/
@Override
public void setUp() {
logger = LoggerFactory.getLogger(getClass());
}
/**
* Make sure logging is enabled.
*/
public void testDebugLogging() {
// assertTrue("Debug logging is not enabled", logger.isDebugEnabled());
logger.debug("debug message");
}
/**
* Make sure info is enabled, and test it.
*/
public void testInfoLogging() {
assertTrue(logger.isInfoEnabled());
logger.info("info message");
}
/**
* Test other log stuff.
*/
public void testOtherLogging() {
logger.warn("warn message");
logger.warn("test %s", "message");
logger.error("error message");
logger.error("test %s", "message");
logger.fatal("fatal message");
logger.fatal("test %s", "message");
logger.log(null, "test null", null);
assertEquals(getClass().getName(), logger.getName());
}
/**
* Make sure we're using log4j.
*/
public void testLog4j() {
// Logger l=LoggerFactory.getLogger(getClass());
// assertEquals("net.spy.memcached.compat.log.Log4JLogger", l.getClass().getName());
}
/**
* Test the sun logger.
*/
public void testSunLogger() {
Logger l = new SunLogger(getClass().getName());
assertFalse(l.isDebugEnabled());
l.debug("debug message");
assertTrue(l.isInfoEnabled());
l.info("info message");
l.warn("warn message");
l.error("error message");
l.fatal("fatal message");
l.fatal("fatal message with exception", new Exception());
l.log(null, "test null", null);
l.log(null, "null message with exception and no requestor",
new Exception());
}
/**
* Test the default logger.
*/
public void testMyLogger() {
Logger l = new DefaultLogger(getClass().getName());
assertFalse(l.isDebugEnabled());
l.debug("debug message");
assertTrue(l.isInfoEnabled());
l.info("info message");
l.warn("warn message");
l.error("error message");
l.fatal("fatal message");
l.fatal("fatal message with exception", new Exception());
l.log(null, "test null", null);
l.log(null, "null message with exception and no requestor",
new Exception());
try {
l = new DefaultLogger(null);
fail("Allowed me to create a logger with null name: " + l);
} catch (NullPointerException e) {
assertEquals("Logger name may not be null.", e.getMessage());
}
}
/**
* Test stringing levels.
*/
public void testLevelStrings() {
assertEquals("{LogLevel: DEBUG}", String.valueOf(Level.DEBUG));
assertEquals("{LogLevel: INFO}", String.valueOf(Level.INFO));
assertEquals("{LogLevel: WARN}", String.valueOf(Level.WARN));
assertEquals("{LogLevel: ERROR}", String.valueOf(Level.ERROR));
assertEquals("{LogLevel: FATAL}", String.valueOf(Level.FATAL));
assertEquals("DEBUG", Level.DEBUG.name());
assertEquals("INFO", Level.INFO.name());
assertEquals("WARN", Level.WARN.name());
assertEquals("ERROR", Level.ERROR.name());
assertEquals("FATAL", Level.FATAL.name());
}
/**
* Test picking up an exception argument.
*/
public void testExceptionArg() throws Exception {
Object[] args = new Object[] { "a", 42, new Exception("test") };
Throwable t = ((AbstractLogger) logger).getThrowable(args);
assertNotNull(t);
assertEquals("test", t.getMessage());
}
/**
* Test when the last argument is not an exception.
*/
public void testNoExceptionArg() throws Exception {
Object[] args = new Object[] { "a", 42, new Exception("test"), "x" };
Throwable t = ((AbstractLogger) logger).getThrowable(args);
assertNull(t);
}
}