1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.log4j.filter;
18
19 import junit.framework.TestCase;
20 import org.apache.log4j.Level;
21 import org.apache.log4j.Logger;
22 import org.apache.log4j.extras.DOMConfigurator;
23 import org.apache.log4j.spi.Filter;
24 import org.apache.log4j.spi.LoggingEvent;
25 import org.apache.log4j.util.Compare;
26 import org.apache.log4j.xml.Log4jEntityResolver;
27 import org.w3c.dom.Document;
28
29 import javax.xml.parsers.DocumentBuilder;
30 import javax.xml.parsers.DocumentBuilderFactory;
31 import java.io.FileNotFoundException;
32 import java.io.InputStream;
33 import java.util.Calendar;
34 import java.util.TimeZone;
35
36 /***
37 * Tests for TimeFilter.
38 *
39 */
40 public class TimeFilterTest extends TestCase {
41 /***
42 * Construct new instance.
43 * @param testName test name.
44 */
45 public TimeFilterTest(final String testName) {
46 super(testName);
47 }
48
49 /***
50 * Configure log4j from resource.
51 * @param resourceName resource name.
52 * @throws Exception if IO or other error.
53 */
54 private final void configure(final String resourceName) throws Exception {
55 Logger.getRootLogger().getLoggerRepository().resetConfiguration();
56 InputStream is = getClass().getResourceAsStream(resourceName);
57 if (is == null) {
58 throw new FileNotFoundException(
59 "Could not find resource " + resourceName);
60 }
61 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
62 DocumentBuilder builder = factory.newDocumentBuilder();
63 builder.setEntityResolver(new Log4jEntityResolver());
64 Document doc = builder.parse(is);
65 DOMConfigurator.configure(doc.getDocumentElement());
66 }
67
68
69 /***
70 * Test 2 AM events against a 2 AM - 3 AM filter.
71 * @param tz time zone, may be null.
72 * @param dayIncrement days in advance of current date.
73 */
74 private void common2AM(String tz, int dayIncrement) {
75 TimeFilter timeFilter = new TimeFilter();
76 timeFilter.setStart("02:00:00");
77 timeFilter.setEnd("03:00:00");
78 if (tz != null) {
79 timeFilter.setTimeZone(tz);
80 }
81 timeFilter.activateOptions();
82
83 Calendar cal;
84 if (tz == null) {
85 cal = Calendar.getInstance();
86 } else {
87 cal = Calendar.getInstance(TimeZone.getTimeZone(tz));
88 }
89 cal.set(Calendar.HOUR_OF_DAY, 2);
90 if (dayIncrement != 0) {
91 cal.set(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR) + dayIncrement);
92 }
93 LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
94 Logger.getLogger(TimeFilterTest.class),
95 cal.getTimeInMillis(), Level.INFO, "Hello, world.", null);
96 assertEquals(Filter.ACCEPT, timeFilter.decide(event));
97 timeFilter.setAcceptOnMatch(false);
98 assertEquals(Filter.DENY, timeFilter.decide(event));
99 }
100
101 /***
102 * Test 3 AM events against a 2 AM - 3 AM filter.
103 * @param tz time zone, may be null.
104 * @param dayIncrement days in advance of current date.
105 */
106 private void common3AM(String tz, int dayIncrement) {
107 TimeFilter timeFilter = new TimeFilter();
108 timeFilter.setStart("02:00:00");
109 timeFilter.setEnd("03:00:00");
110 if (tz != null) {
111 timeFilter.setTimeZone(tz);
112 }
113 timeFilter.activateOptions();
114
115 Calendar cal;
116 if (tz == null) {
117 cal = Calendar.getInstance();
118 } else {
119 cal = Calendar.getInstance(TimeZone.getTimeZone(tz));
120 }
121 cal.set(Calendar.HOUR_OF_DAY, 3);
122 if (dayIncrement != 0) {
123 cal.set(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR) + dayIncrement);
124 }
125 LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
126 Logger.getLogger(TimeFilterTest.class),
127 cal.getTimeInMillis(), Level.INFO, "Hello, world.", null);
128 assertEquals(Filter.NEUTRAL, timeFilter.decide(event));
129 timeFilter.setAcceptOnMatch(false);
130 assertEquals(Filter.NEUTRAL, timeFilter.decide(event));
131 timeFilter.setAcceptOnMatch(true);
132 }
133
134 /***
135 * Test 2 AM local today event against 2 AM - 3 AM local time filter.
136 */
137 public void test2AMLocal() {
138 common2AM(null, 0);
139 }
140
141 /***
142 * Test 2 AM local yesterday event against 2 AM - 3 AM local time filter.
143 */
144 public void test2AMLocalYesterday() {
145 common2AM(null, -1);
146 }
147
148 /***
149 * Test 2 AM local tomorrow event against 2 AM - 3 AM local time filter.
150 */
151 public void test2AMLocalTomorrow() {
152 common2AM(null, 1);
153 }
154
155 /***
156 * Test 2 AM local last week event against 2 AM - 3 AM local time filter.
157 */
158 public void test2AMLocalLastWeek() {
159 common2AM(null, -7);
160 }
161
162 /***
163 * Test 2 AM local next week event against 2 AM - 3 AM local time filter.
164 */
165 public void test2AMLocalNextWeek() {
166 common2AM(null, 7);
167 }
168
169 /***
170 * Test 3 AM local today event against 2 AM - 3 AM local time filter.
171 */
172 public void test3AMLocal() {
173 common3AM(null, 0);
174 }
175
176 /***
177 * Test 3 AM local yesterday event against 2 AM - 3 AM local time filter.
178 */
179 public void test3AMLocalYesterday() {
180 common3AM(null, -1);
181 }
182
183 /***
184 * Test 3 AM local tomorrow event against 2 AM - 3 AM local time filter.
185 */
186 public void test3AMLocalTomorrow() {
187 common3AM(null, 1);
188 }
189
190 /***
191 * Test 3 AM local last week event against 2 AM - 3 AM local time filter.
192 */
193 public void test3AMLocalLastWeek() {
194 common3AM(null, -7);
195 }
196
197 /***
198 * Test 3 AM local next week event against 2 AM - 3 AM local time filter.
199 */
200 public void test3AMLocalNextWeek() {
201 common3AM(null, 7);
202 }
203
204 /***
205 * Test 2 AM UTC today event against 2 AM - 3 AM GMT filter.
206 */
207 public void test2AMGMT() {
208 common2AM("GMT", 0);
209 }
210
211 /***
212 * Test 3 AM UTC today event against 2 AM - 3 AM GMT filter.
213 */
214 public void test3AMGMT() {
215 common3AM("GMT", 0);
216 }
217
218 /***
219 * Log events every 15 minutes from midnight to midnight in
220 * using specified calendar.
221 * @param cal calendar.
222 */
223 private void common(Calendar cal) {
224 Logger logger = Logger.getLogger(TimeFilterTest.class);
225 cal.set(Calendar.HOUR_OF_DAY, 0);
226 cal.set(Calendar.MINUTE, 0);
227 cal.set(Calendar.SECOND, 0);
228 cal.set(Calendar.MILLISECOND, 0);
229 for (int hour = 0; hour < 24; hour++) {
230 for (int minute = 0; minute < 60; minute += 15) {
231 cal.set(Calendar.HOUR_OF_DAY, hour);
232 cal.set(Calendar.MINUTE, minute);
233 LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
234 logger,
235 cal.getTimeInMillis(), Level.INFO, "Hello, world.", null);
236 logger.callAppenders(event);
237 }
238 }
239 }
240
241
242 /***
243 * Test 2 AM-3AM local time accept on match filter.
244 * @throws Exception if IO exception.
245 */
246 public void testConfig1() throws Exception {
247 configure("timeFilter1.xml");
248 common(Calendar.getInstance());
249
250 assertTrue(Compare.compare(TimeFilterTest.class,
251 "timeFilter.1", "timeFilter1.log"));
252 }
253
254 /***
255 * Test 2 AM-3AM UTC reject on match filter.
256 * @throws Exception if IO exception.
257 */
258 public void testConfig2() throws Exception {
259 configure("timeFilter2.xml");
260 common(Calendar.getInstance(TimeZone.getTimeZone("GMT")));
261
262 assertTrue(Compare.compare(TimeFilterTest.class,
263 "timeFilter.2", "timeFilter2.log"));
264 }
265
266 }