-
Notifications
You must be signed in to change notification settings - Fork 23
/
log4sh_test_custom_mdc_patterns.sh
executable file
·142 lines (116 loc) · 3.36 KB
/
log4sh_test_custom_mdc_patterns.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
#! /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 custom MDC patterns.
APP_NAME='stdout'
# load test helpers
. ./log4sh_test_helpers
#------------------------------------------------------------------------------
# suite tests
#
testCustomDateMDC()
{
mdcDateFmt='+%Y.%m.%d'
pattern='%X{mdcDate}'
regex='^[0-9]\{4\}\.[0-9]\{2\}\.[0-9]\{2\}'
# define custom logger_info function
my_logger_info()
{
mdcDate=`date ${mdcDateFmt}`
log INFO "$@"
}
# set the custom pattern
appender_setPattern ${APP_NAME} ${pattern}
appender_activateOptions ${APP_NAME}
${DEBUG} 'sending message using custom MDC pattern'
result=`my_logger_info 'dummy'`
matched=`echo ${result} |sed "s/${regex}//"`
${DEBUG} "dateFormat='${mdcDateFmt}' pattern='${pattern}' result='${result}' matched='${matched}'"
assertNotNull \
"custom pattern '${pattern}' failed with empty result" \
"${result}" || return
assertNull \
"custom pattern '${pattern}' output of '${matched}' did not match the regex '${regex}'" \
"${matched}" || return
}
testCustomTimeMDC()
{
mdcTimeFmt='+%H:%M:%S'
pattern='%X{mdcTime}'
regex='^[0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\}'
# define custom logger_info function
my_logger_info()
{
mdcTime=`date ${mdcTimeFmt}`
log INFO "$@"
}
# set the custom pattern
appender_setPattern ${APP_NAME} ${pattern}
appender_activateOptions ${APP_NAME}
${DEBUG} 'sending message using custom MDC pattern'
result=`my_logger_info 'dummy'`
matched=`echo ${result} |sed "s/${regex}//"`
${DEBUG} "timeFormat='${mdcTimeFmt}' pattern='${pattern}' result='${result}' matched='${matched}'"
assertNotNull \
"custom pattern '${pattern}' failed with empty result" \
"${result}" || return
assertNull \
"custom pattern '${pattern}' output of '${matched}' did not match the regex '${regex}'" \
"${matched}" || return
}
testCustomUserHostMDC()
{
pattern='%X{USER}@%X{HOSTNAME}'
regex='[A-Za-z0-9]*@[-.A-Za-z0-9]*'
# set variables (if needed)
if [ -z "${HOSTNAME:-}" ]; then
HOSTNAME=`hostname`
export HOSTNAME
fi
# set the custom pattern
appender_setPattern ${APP_NAME} ${pattern}
appender_activateOptions ${APP_NAME}
${DEBUG} 'sending message using custom MDC pattern'
result=`logger_info 'dummy'`
matched=`echo ${result} |sed "s/${regex}//"`
${DEBUG} "pattern='${pattern}' result='${result}' matched='${matched}'"
assertNotNull \
"custom pattern '${pattern}' failed with empty result" \
"${result}" || return
assertNull \
"custom pattern '${pattern}' output of '${matched}' did not match the regex '${regex}'" \
"${matched}" || return
}
#------------------------------------------------------------------------------
# suite functions
#
oneTimeSetUp()
{
LOG4SH_CONFIGURATION='none'
th_oneTimeSetUp
resultF="${TH_TMPDIR}/result"
}
setUp()
{
# reset log4sh
log4sh_resetConfiguration
# configure log4sh
logger_setLevel INFO
logger_addAppender ${APP_NAME}
appender_setLayout ${APP_NAME} PatternLayout
}
tearDown()
{
rm -f "${resultF}"
}
#------------------------------------------------------------------------------
# main
#
# load and run shUnit2
[ -n "${ZSH_VERSION:-}" ] && SHUNIT_PARENT=$0
. ${TH_SHUNIT}