-
Notifications
You must be signed in to change notification settings - Fork 23
/
log4sh_test_file_appender.sh
executable file
·192 lines (159 loc) · 4.6 KB
/
log4sh_test_file_appender.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
#! /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 the FileAppender.
# load test helpers
. ./log4sh_test_helpers
APP_ACCESSORS='accessors'
APP_ACCESSORS_FILE=''
APP_ACCESSORS_LOG4SH="${TH_TESTDATA_DIR}/file_appender_accessors.log4sh"
APP_SIMPLE='mySimple'
APP_SIMPLE_FILE=''
APP_SIMPLE_LOG4SH="${TH_TESTDATA_DIR}/file_appender_simple.log4sh"
APP_STDERR='mySTDERR'
APP_STDERR_LOG4SH="${TH_TESTDATA_DIR}/file_appender_stderr.log4sh"
#------------------------------------------------------------------------------
# suite tests
#
commonSTDERR()
{
assert=$1
msg=$2
cmd=$3
${DEBUG} "sending a message to ${APP_STDERR}"
result=`eval ${cmd}`
${DEBUG} "assert='${assert}' cmd='${cmd}' result='${result}'"
eval ${assert} \"${msg}\" \"${result}\"
}
testSTDERR_runtime()
{
# runtime configure a STDERR FileAppender
logger_setLevel INFO
logger_addAppender ${APP_STDERR}
appender_setType ${APP_STDERR} FileAppender
appender_file_setFile ${APP_STDERR} STDERR
appender_activateOptions ${APP_STDERR}
commonSTDERR \
'assertNotNull' \
"${APP_STDERR} runtime FileAppender didn't go to STDERR" \
"logger_info \'dummy\' 2>&1 >/dev/null" \
|| return
commonSTDERR \
'assertNull' \
"${APP_STDERR} runtime FileAppender went to STDOUT" \
"logger_info \'dummy\' 2>/dev/null" \
|| return
}
testSTDERR_config()
{
# configure a STDERR FileAppender via config
log4sh_doConfigure "${APP_STDERR_LOG4SH}"
commonSTDERR \
'assertNotNull' \
"${APP_STDERR} config FileAppender didn't go to STDERR" \
"logger_info \'dummy\' 2>&1 1>/dev/null" \
|| return
commonSTDERR \
'assertNull' \
"${APP_STDERR} config FileAppender went to STDOUT" \
"logger_info \'dummy\' 2>/dev/null" \
|| return
}
commonSimple()
{
assert=$1
msg=$2
cmd=$3
${DEBUG} "sending a message to ${APP_SIMPLE}"
result=`eval ${cmd}`
${DEBUG} "assert='${assert}' cmd='${cmd}' result='${result}'"
eval ${assert} \"${msg}\" \"${result}\"
}
testSimple_runtime()
{
# runtime configure a simple ConsoleAppender
logger_setLevel INFO
logger_addAppender ${APP_SIMPLE}
appender_setLevel ${APP_SIMPLE} DEBUG
appender_setType ${APP_SIMPLE} FileAppender
appender_file_setFile ${APP_SIMPLE} "${APP_SIMPLE_FILE}"
appender_activateOptions ${APP_SIMPLE}
commonSimple \
'assertNull' \
"${APP_SIMPLE} config FileAppender went to STDOUT or STDERR" \
"logger_info \'dummy\' 2>&1" \
|| return
commonSimple \
'assertNotNull' \
"${APP_SIMPLE} config FileAppender didn't go to file" \
"logger_info \'dummy\' 2>&1; cat \"${APP_SIMPLE_FILE}\"" \
|| return
}
testSimple_config()
{
# configure a simple ConsoleAppender via config
log4sh_doConfigure "${APP_SIMPLE_LOG4SH}"
commonSimple \
'assertNull' \
"${APP_SIMPLE} runtime FileAppender went to STDOUT or STDERR" \
"logger_info \'dummy\' 2>&1" \
|| return
commonSimple \
'assertNotNull' \
"${APP_SIMPLE} runtime FileAppender didn't go to file" \
"logger_info \'dummy\'; cat ${APP_SIMPLE_FILE}" \
|| return
}
# test that the filename set in the configuration file is readable
# programatically
testAccessors_getFilename()
{
# configure log4sh via config
log4sh_doConfigure "${APP_ACCESSORS_LOG4SH}"
fileName=`appender_file_getFile ${APP_ACCESSORS}`
assertEquals \
"${APP_ACCESSORS} file '${fileName}' was not '${APP_ACCESSORS_FILE}'" \
"${fileName}" "${APP_ACCESSORS_FILE}"
}
# test that setting the filename of an appender actually changes it
testAccessors_setgetFilename()
{
# configure log4sh via config
log4sh_doConfigure "${APP_ACCESSORS_LOG4SH}"
newFileName='/var/tmp/accessors.log'
# the current filename should be what is stored in ${APP_ACCESSORS_FILE}.
# changing it to /var/tmp/accessors.log
appender_file_setFile ${APP_ACCESSORS} "${newFileName}"
# now, re-reading the file name to verify
fileName=`appender_file_getFile ${APP_ACCESSORS}`
assertEquals \
"${APP_ACCESSORS} file was not ${newFileName}" \
"${fileName}" "${newFileName}"
}
#------------------------------------------------------------------------------
# suite functions
#
oneTimeSetUp()
{
LOG4SH_CONFIGURATION='none'
th_oneTimeSetUp
APP_ACCESSORS_FILE="${TH_TMPDIR}/myAccessors.out"
APP_SIMPLE_FILE="${TH_TMPDIR}/mySimple.out"
}
setUp()
{
# reset log4sh
log4sh_resetConfiguration
}
tearDown()
{
rm -f "${APP_ACCESSORS_FILE}" "${APP_SIMPLE_FILE}"
}
# load and run shUnit2
[ -n "${ZSH_VERSION:-}" ] && SHUNIT_PARENT=$0
. ${TH_SHUNIT}