-
Notifications
You must be signed in to change notification settings - Fork 23
/
log4sh_base_test.sh
executable file
·229 lines (184 loc) · 6.14 KB
/
log4sh_base_test.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
#!/bin/sh
# vim:et:ft=sh:sts=2:sw=2
#
# Copyright 2008-2018 Kate Ward. All Rights Reserved.
# Released under the Apache License 2.0 license.
#
# log4sh unit test for the ConsoleAppender.
# https://github.com/kward/log4sh
#
# Author: [email protected] (Kate Ward)
# load test helpers
. ./log4sh_test_helpers
#------------------------------------------------------------------------------
# suite tests
#
test__log4sh_getsetValue()
{
# getValue
_log4sh_getValue >"${stdoutF}" 2>"${stderrF}"
assertEquals ${LOG4SH_ERROR} $?
assertError 'invalid argument count'
value=`_log4sh_getValue 'missing_key' 2>"${stderrF}"`
assertFalse 'a missing key should be missed' $?
# setValue
_log4sh_setValue >"${stdoutF}" 2>"${stderrF}"
assertEquals ${LOG4SH_ERROR} $?
assertError 'invalid argument count'
_log4sh_setValue 'abc' 123 >"${stdoutF}" 2>"${stderrF}"
assertSuccess $?
diff "${__log4sh_dictFile}" - >/dev/null <<EOF
abc 123
EOF
assertTrue 'invalid data written to dictionary' $? || cat "${__log4sh_dictFile}"
value=`_log4sh_getValue 'abc' 2>"${stderrF}"`
assertSuccess $?
assertEquals 'unable to get key value' 123 "${value}"
}
test_log4sh_addLogger()
{
log4sh_addLogger >"${stdoutF}" 2>"${stderrF}"
assertEquals ${LOG4SH_ERROR} $?
assertError 'invalid argument count'
log4sh_addLogger 'root' >"${stdoutF}" 2>"${stderrF}"
assertEquals ${LOG4SH_FALSE} $?
assertError 'logger already exists'
log4sh_addLogger 'newLogger' >"${stdoutF}" 2>"${stderrF}"
assertEquals ${LOG4SH_TRUE} $?
_logger_isValid 'newLogger'
assertTrue 'new logger should exist' $?
}
test__logger_isValid()
{
_logger_isValid ''
assertFalse 'empty logger should fail' $?
_logger_isValid 'root'
assertTrue 'root logger should exist' $?
_logger_isValid 'invalid'
assertFalse 'invalid logger should not exist' $?
}
test_logger_addAppender()
{
logger_addAppender >"${stdoutF}" 2>"${stderrF}"
assertEquals ${LOG4SH_ERROR} $?
assertError 'invalid argument count'
logger_addAppender 'newRootAppender' >"${stdoutF}" 2>"${stderrF}"
assertSuccess $?
_appender_isValid 'newRootAppender'
assertTrue 'newRootAppender should exist' $?
log4sh_addLogger 'newLogger'
logger_addAppender 'newLoggerAppender' 'newLogger' \
>"${stdoutF}" 2>"${stderrF}"
assertSuccess $?
_appender_isValid 'newLogger.newLoggerAppender'
assertTrue 'newLogger.newLoggerAppender should exist' $?
}
test_logger_getAppenders()
{
# root logger
appenders=`logger_getAppenders 2>"${stderrF}"`
assertTrue 'default mode to retrieve root logger should succeed' $?
assertNull "${appenders}"
appenders=`logger_getAppenders root 2>"${stderrF}"`
assertTrue 'getting appenders for root logger should succeed' $?
assertNull "${appenders}"
logger_addAppender 'newRootAppender'
appenders=`logger_getAppenders 2>"${stderrF}"`
assertTrue 'just added the newRootAppender. where is it?' $?
assertEquals 'newRootAppender' "${appenders}"
# invalid logger
appenders=`logger_getAppenders invalidLogger 2>"${stderrF}"`
assertFalse 'getting appenders for an invalid logger should not succeed' $?
assertNull "${appenders}"
# custom
log4sh_addLogger custom
appenders=`logger_getAppenders custom 2>"${stderrF}"`
assertTrue 'getting appenders for custom logger should succeed' $?
assertNull "${appenders}"
logger_addAppender newCustomAppender custom
appenders=`logger_getAppenders custom 2>"${stderrF}"`
assertTrue 'just added the newCustomAppender. where is it?' $?
assertEquals 'newCustomAppender' "${appenders}"
}
test__appender_isValid()
{
_appender_isValid ''
assertFalse 'empty appender should fail' $?
}
test__appender_isValidType()
{
_appender_isValidType ''
assertFalse 'empty type should fail' $?
_appender_isValidType 'DummyAppender'
assertFalse 'unregistered Dummy appender type should fail' $?
_log4sh_register_appender 'DummyAppender'
_appender_isValidType 'DummyAppender'
assertTrue 'registered Dummy appender type should succeed' $?
}
test_appender_getsetType()
{
# getType
appType=`appender_getType 2>"${stderrF}"`
assertEquals ${LOG4SH_ERROR} $?
assertError 'getType' 'invalid argument count'
appType=`appender_getType 'invalidAppender' 2>"${stderrF}"`
assertFalse $?
assertError 'getType' 'invalid appender'
# setType
appender_setType >"${stdoutF}" 2>"${stderrF}"
assertEquals ${LOG4SH_ERROR} $?
assertError 'setType' 'invalid argument count'
appender_setType 'invalidAppender' 'DummyAppender' \
>"${stdoutF}" 2>"${stderrF}"
assertFalse $?
assertError 'setType' 'invalid appender'
# default appender type is a ConsoleAppender
logger_addAppender 'myConsoleAppender'
appType=`appender_getType 'myConsoleAppender' 2>"${stderrF}"`
assertSuccess 'failed to get myConsoleAppender type' $?
assertEquals 'ConsoleAppender' "${appType}"
_log4sh_register_appender 'DummyAppender'
logger_addAppender 'myDummyAppender'
appender_setType 'myDummyAppender' 'DummyAppender' >"${stdoutF}" 2>"${stderrF}"
assertSuccess 'failed to set myDummyAppender type' $?
appType=`appender_getType 'myDummyAppender' 2>"${stderrF}"`
assertSuccess 'failed to get myDummyAppender type' $?
assertEquals 'DummyAppender' "${appType}"
}
testSplitLoggerAppenderMacro()
{
set -- 'appender'
${_LOG4SH_SPLIT_LOGGER_APPENDER_}
assertEquals 'root' "${log4sh_logger_}"
assertEquals 'appender' "${log4sh_appender_}"
assertEquals 'root.appender' "${log4sh_fqAppender_}"
set -- 'logger.appender'
${_LOG4SH_SPLIT_LOGGER_APPENDER_}
assertEquals 'logger' "${log4sh_logger_}"
assertEquals 'appender' "${log4sh_appender_}"
assertEquals 'logger.appender' "${log4sh_fqAppender_}"
}
#
# stub functions
#
_appender_register_ConsoleAppender() { :; }
_appender_new_ConsoleAppender() { return ${LOG4SH_TRUE}; }
_appender_register_DummyAppender() { :; }
_appender_new_DummyAppender() { return ${LOG4SH_TRUE}; }
#------------------------------------------------------------------------------
# suite functions
#
oneTimeSetUp()
{
th_oneTimeSetUp
# load libraries
. ./log4sh_base
_log4sh_register_appender ConsoleAppender
}
setUp()
{
log4sh_resetConfiguration
}
# load and run shUnit2
[ -n "${ZSH_VERSION:-}" ] && SHUNIT_PARENT=$0
. ${TH_SHUNIT}