forked from kward/log4sh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log4sh_test_pattern_layout.sh
executable file
·237 lines (201 loc) · 6.33 KB
/
log4sh_test_pattern_layout.sh
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
#! /bin/sh
# $Id$
# vim:et:ft=sh:sts=2:sw=2
#
# Copyright 2008 Kate Ward. All Rights Reserved.
# Released under the LGPL (GNU Lesser General Public License)
# Author: [email protected] (Kate Ward)
#
# log4sh unit test for pattern layouts in appender output.
# load test helpers
. ./log4sh_test_helpers
APP_NAME='stdout'
#------------------------------------------------------------------------------
# custom asserts
#
assertPattern()
{
msg=''
if [ $# -eq 3 ]; then
msg=$1
shift
fi
pattern=$1
expected=$2
appender_setPattern ${APP_NAME} "${pattern}"
appender_activateOptions ${APP_NAME}
actual=`logger_info 'dummy'`
${DEBUG} "pattern='${pattern}' expected='${expected}' actual='${actual}'"
msg=`eval "echo \"${msg}\""`
assertEquals "${msg}" "${expected}" "${actual}"
}
#------------------------------------------------------------------------------
# suite tests
#
testCategoryPattern()
{
pattern='%c'
expected='shell'
msg="category '%c' pattern failed: '\${expected}' != '\${actual}'"
assertPattern "${msg}" "${pattern}" "${expected}"
}
commonDatePatternAssert()
{
pattern=$1
regex=$2
appender_setPattern ${APP_NAME} "${pattern}"
appender_activateOptions ${APP_NAME}
result=`logger_info 'dummy'`
matched=`echo ${result} |sed "s/${regex}//"`
${DEBUG} "pattern='${pattern}' result='${result}' regex='${regex}' matched='${matched}'"
assertNotNull \
"date pattern '${pattern}' failed: empty result '${result}'" \
"${result}"
assertNull \
"date pattern '${pattern}' failed: result '${result}' did not match the regex '${regex}'" \
"${matched}"
}
testDatePattern()
{
# without conversion specifier (Unix date format '+%Y-%m-%d %H:%M:%S')
pattern='%d'
regex='^[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\} [0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\}'
commonDatePatternAssert "${pattern}" "${regex}"
# ISODATE conversion specifier
pattern='%d{ISODATE}'
regex='^[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\} [0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\}'
commonDatePatternAssert "${pattern}" "${regex}"
# custom conversion specifier
pattern='%d{HH:mm:ss,SSS}'
regex='^[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\} [0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\}'
commonDatePatternAssert "${pattern}" "${regex}"
}
testFileNamePattern()
{
pattern='%F'
expected="${TH_ARGV0}"
msg="file name '%F' pattern failed: '\${expected}' != '\${actual}'"
assertPattern "${msg}" "${pattern}" "${expected}"
}
testLineNumberPattern()
{
pattern='%L'
expected=''
msg="line number '%L' pattern failed: '\${expected}' != '\${actual}'"
assertPattern "${msg}" "${pattern}" "${expected}"
}
testLineSeparatorPattern()
{
pattern='%n'
expected=''
msg="line separator '%n' pattern failed: '\${expected}' != '\${actual}'"
assertPattern "${msg}" "${pattern}" "${expected}"
}
testMessagePattern()
{
pattern='%m'
expected='dummy'
msg="message '%m' pattern failed: '\${expected}' != '\${actual}'"
assertPattern "${msg}" "${pattern}" "${expected}"
}
# note: this function using the base log() function rather than the logger_*()
# functions as the functionality is effectively the same
testPriorityPattern()
{
currPriority=`appender_getLevel ${APP_NAME}`
pattern='%p'
appender_setPattern ${APP_NAME} "${pattern}"
for priority in TRACE DEBUG INFO WARN ERROR FATAL; do
appender_setLevel ${APP_NAME} ${priority}
appender_activateOptions ${APP_NAME}
result=`log ${priority} 'dummy'`
${DEBUG} "pattern='${pattern}' priority='${priority}' result='${result}'"
assertEquals \
"priority pattern '${pattern}' failed: the requested priority of '${priority}' does not match the returned priority of '${result}'" \
"${priority}" "${result}"
done
appender_setLevel ${APP_NAME} ${currPriority}
appender_activateOptions ${APP_NAME}
}
testRunningTimePattern()
{
pattern='%r'
regex='^[0-9]*(\.[0-9]*){0,1}$'
appender_setPattern ${APP_NAME} "${pattern}"
appender_activateOptions ${APP_NAME}
result=`logger_info 'dummy'`
matched=`echo "${result}" |egrep "${regex}"`
${DEBUG} "pattern='${pattern}' result='${result}' regex='${regex}' matched='${matched}'"
assertNotNull \
"running time pattern '${pattern}' failed: empty result '${result}'" \
"${result}"
assertNotNull \
"running time pattern '${pattern}' failed: result '${result}' did not match the regex '${regex}'" \
"${matched}"
}
testThreadNamePattern()
{
pattern='%t'
expected=${__LOG4SH_THREAD_DEFAULT}
msg="thread name '%t' pattern failed: '\${expected}' != '\${actual}'"
assertPattern "${msg}" "${pattern}" "${expected}"
}
# NDC (Nested Diagnostic Context)
testNDCPattern()
{
pattern='%x'
expected=''
msg="NDC '%x' pattern failed: '\${expected}' != '\${actual}'"
assertPattern "${msg}" "${pattern}" "${expected}"
}
# MDC (Mapped Diagnostic Context)
testMDCPattern()
{
msg="MDC '\${pattern}' pattern failed: '\${expected}' != '\${actual}'"
pattern='%X{USER}'
expected=${USER}
assertPattern "${msg}" "${pattern}" "${expected}"
# the %X pattern should not parse unless it has an environment variable name
# enclosed in {} chars following the 'X'
pattern='%X'
expected=${pattern}
assertPattern "${msg}" "${pattern}" "${expected}"
}
testPercentPattern()
{
pattern='%%'
expected='%'
msg="percent '\${pattern}' pattern failed: '\${expected}' != '\${actual}'"
assertPattern "${msg}" "${pattern}" "${expected}"
}
testDefaultPattern()
{
# the pattern should be '%d %p - %m%n'
pattern=${__LOG4SH_PATTERN_DEFAULT}
regex='^[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\} [0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\} INFO - dummy'
result=`logger_info 'dummy'`
matched=`echo ${result} |sed "s/${regex}//"`
${DEBUG} "pattern='${pattern}' result='${result}' regex='${regex}' matched='${matched}'"
assertNotNull \
"default pattern '${pattern}' failed: empty result '${result}'" \
"${result}"
assertNull \
"default pattern '${pattern}' failed: result '${result}' did not match the regex '${regex}'" \
"${matched}"
}
#------------------------------------------------------------------------------
# suite functions
#
oneTimeSetUp()
{
LOG4SH_CONFIGURATION='none'
th_oneTimeSetUp
logger_setLevel INFO
appender_setLayout ${APP_NAME} PatternLayout
}
# need working egrep. the one for Solaris is in /usr/xpg4/bin, so we will add
# that to the path
[ -d '/usr/xpg4/bin' ] && PATH="/usr/xpg4/bin:${PATH}"
# load and run shUnit2
[ -n "${ZSH_VERSION:-}" ] && SHUNIT_PARENT=$0
. ${TH_SHUNIT}