001: /* ====================================================================
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 1997-2003 The Apache Software Foundation. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution,
020: * if any, must include the following acknowledgment:
021: * "This product includes software developed by the
022: * Apache Software Foundation (http://www.apache.org/)."
023: * Alternately, this acknowledgment may appear in the software
024: * itself, if and wherever such third-party acknowledgments
025: * normally appear.
026: *
027: * 4. The names "Jakarta", "Avalon", and "Apache Software Foundation"
028: * must not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation. For more
052: * information on the Apache Software Foundation, please see
053: * <http://www.apache.org/>.
054: */
055: package org.apache.log.format.test;
056:
057: import junit.framework.TestCase;
058: import org.apache.log.ContextMap;
059: import org.apache.log.ContextStack;
060: import org.apache.log.LogEvent;
061: import org.apache.log.Priority;
062: import org.apache.log.format.Formatter;
063: import org.apache.log.format.PatternFormatter;
064: import org.apache.log.format.RawFormatter;
065: import org.apache.log.format.SyslogFormatter;
066: import org.apache.log.format.XMLFormatter;
067:
068: /**
069: * Test suite for the formatters.
070: * TODO: Incorporate testing for ContextStack and ContextMap
071: *
072: * @author <a href="mailto:peter@apache.org">Peter Donald</a>
073: */
074: public final class FormatterTestCase extends TestCase {
075: private static String EOL = System.getProperty("line.separator",
076: "\n");
077:
078: private static String M1 = "Message1";
079: private static String M2 = "Message2Message2";
080: private static String M3 = "Message3Message3Message3";
081:
082: private static String C1 = "Category1";
083: private static String C2 = "Category2Category2";
084: private static String C3 = "Category3Category3Category3";
085:
086: private static long T1 = 0;
087: private static long T2 = 1;
088: private static long T3 = 2;
089:
090: private static Priority P1 = Priority.FATAL_ERROR;
091: private static Priority P2 = Priority.ERROR;
092: private static Priority P3 = Priority.WARN;
093:
094: private static boolean mapsConfigured;
095: private static ContextMap CM1 = new ContextMap();
096: private static ContextMap CM2 = new ContextMap();
097:
098: private static LogEvent E1 = createEvent(C1, M1, null, T1, P1,
099: null, CM1);
100: private static LogEvent E2 = createEvent(C2, M2, null, T2, P2,
101: null, CM2);
102: private static LogEvent E3 = createEvent(C3, M3, null, T3, P3,
103: null, null);
104:
105: private static String E1_XML = "<log-entry>" + EOL + " <time>"
106: + T1 + "</time>" + EOL + " <priority>" + P1.getName()
107: + "</priority>" + EOL + " <category>" + C1 + "</category>"
108: + EOL + " <message><![CDATA[" + M1 + "]]></message>" + EOL
109: + "</log-entry>" + EOL;
110:
111: private static String E2_XML = "<log-entry>" + EOL + " <time>"
112: + T2 + "</time>" + EOL + " <priority>" + P2.getName()
113: + "</priority>" + EOL + " <category>" + C2 + "</category>"
114: + EOL + " <message><![CDATA[" + M2 + "]]></message>" + EOL
115: + "</log-entry>" + EOL;
116:
117: private static String E3_XML = "<log-entry>" + EOL + " <time>"
118: + T3 + "</time>" + EOL + " <priority>" + P3.getName()
119: + "</priority>" + EOL + " <category>" + C3 + "</category>"
120: + EOL + " <message><![CDATA[" + M3 + "]]></message>" + EOL
121: + "</log-entry>" + EOL;
122:
123: private static int FACILITY_ID = 9 << 3; //cron
124: private static String FACILITY_NAME = "cron"; //cron
125:
126: private static String E1_SYSLOG = "<" + (2 | FACILITY_ID) + "> "
127: + M1;
128: private static String E2_SYSLOG = "<" + (3 | FACILITY_ID) + "> "
129: + M2;
130: private static String E3_SYSLOG = "<" + (4 | FACILITY_ID) + "> "
131: + M3;
132:
133: private static String E1_SYSLOG_WB = "<" + (2 | FACILITY_ID) + "> "
134: + FACILITY_NAME + ": " + M1;
135: private static String E2_SYSLOG_WB = "<" + (3 | FACILITY_ID) + "> "
136: + FACILITY_NAME + ": " + M2;
137: private static String E3_SYSLOG_WB = "<" + (4 | FACILITY_ID) + "> "
138: + FACILITY_NAME + ": " + M3;
139:
140: private static String PATTERN1 = "[%8.8{category}]: %{message}"
141: + EOL;
142: private static String E1_PATTERN1 = "[Category]: " + M1 + EOL;
143: private static String E2_PATTERN1 = "[Category]: " + M2 + EOL;
144: private static String E3_PATTERN1 = "[Category]: " + M3 + EOL;
145:
146: private static String PATTERN2 = "[%10.{category}]: %{message}"
147: + EOL;
148: private static String E1_PATTERN2 = "[" + C1 + " ]: " + M1 + EOL;
149: private static String E2_PATTERN2 = "[" + C2 + "]: " + M2 + EOL;
150: private static String E3_PATTERN2 = "[" + C3 + "]: " + M3 + EOL;
151:
152: private static String PATTERN3 = "[%.10{category}]: %{message}"
153: + EOL;
154: private static String E1_PATTERN3 = "[" + C1 + "]: " + M1 + EOL;
155: private static String E2_PATTERN3 = "[Category2C]: " + M2 + EOL;
156: private static String E3_PATTERN3 = "[Category3C]: " + M3 + EOL;
157:
158: private static String PATTERN4 = "[%+10.{category}]: %{message}"
159: + EOL;
160: private static String E1_PATTERN4 = "[" + C1 + " ]: " + M1 + EOL;
161: private static String E2_PATTERN4 = "[" + C2 + "]: " + M2 + EOL;
162: private static String E3_PATTERN4 = "[" + C3 + "]: " + M3 + EOL;
163:
164: private static String PATTERN5 = "[%-10.{category}]: %{message}"
165: + EOL;
166: private static String E1_PATTERN5 = "[ " + C1 + "]: " + M1 + EOL;
167: private static String E2_PATTERN5 = "[" + C2 + "]: " + M2 + EOL;
168: private static String E3_PATTERN5 = "[" + C3 + "]: " + M3 + EOL;
169:
170: private static String PATTERN6 = "[%{context}]: %{message}" + EOL;
171: private static String E1_PATTERN6 = "[]: " + M1 + EOL;
172: private static String E2_PATTERN6 = "[]: " + M2 + EOL;
173: private static String E3_PATTERN6 = "[]: " + M3 + EOL;
174:
175: private static String PATTERN7 = "[%{context:stack}]: %{message}"
176: + EOL;
177: private static String E1_PATTERN7 = "[]: " + M1 + EOL;
178: private static String E2_PATTERN7 = "[]: " + M2 + EOL;
179: private static String E3_PATTERN7 = "[]: " + M3 + EOL;
180:
181: private static String PATTERN8 = "[%{context:method}]: %{message}"
182: + EOL;
183: private static String E1_PATTERN8 = "[com.biz.MyObject.myMethod(MyObject:53)]: "
184: + M1 + EOL;
185: private static String E2_PATTERN8 = "[]: " + M2 + EOL;
186: private static String E3_PATTERN8 = "[]: " + M3 + EOL;
187:
188: private static String CLASS_PREFIX = FormatterTestCase.class
189: .getName()
190: + ".";
191:
192: private static String PATTERN9 = "[%{method}]: %{message}" + EOL;
193: private static String E1_PATTERN9 = "[com.biz.MyObject.myMethod(MyObject:53)]: "
194: + M1 + EOL;
195: private static String E2_PATTERN9_START = "[" + CLASS_PREFIX
196: + "testPattern9Formatter(";
197: private static String E2_PATTERN9_END = ")]: " + M2 + EOL;
198: private static String E3_PATTERN9_START = "[" + CLASS_PREFIX
199: + "testPattern9Formatter(";
200: private static String E3_PATTERN9_END = ")]: " + M3 + EOL;
201:
202: private static String PATTERN10 = "[%{context:method}]: %{message}"
203: + EOL;
204: private static String E1_PATTERN10 = "[com.biz.MyObject.myMethod(MyObject:53)]: "
205: + M1 + EOL;
206: private static String E2_PATTERN10 = "[]: " + M2 + EOL;
207: private static String E3_PATTERN10 = "[]: " + M3 + EOL;
208:
209: private static String PATTERN11 = "[%{context:method}]: %{message}"
210: + EOL;
211: private static String E1_PATTERN11 = "[com.biz.MyObject.myMethod(MyObject:53)]: "
212: + M1 + EOL;
213: private static String E2_PATTERN11 = "[]: " + M2 + EOL;
214: private static String E3_PATTERN11 = "[]: " + M3 + EOL;
215:
216: private static LogEvent createEvent(final String category,
217: final String message, final Throwable throwable,
218: final long time, final Priority priority,
219: final ContextStack contextStack, final ContextMap contextMap) {
220: setupContext();
221:
222: final LogEvent event = new LogEvent();
223: event.setCategory(category);
224: event.setMessage(message);
225: event.setThrowable(throwable);
226: event.setTime(time);
227: event.setPriority(priority);
228: event.setContextStack(contextStack);
229: event.setContextMap(contextMap);
230: return event;
231: }
232:
233: private static void setupContext() {
234: if (!mapsConfigured) {
235: mapsConfigured = true;
236: CM1.set("method", "com.biz.MyObject.myMethod(MyObject:53)");
237: CM1.set("hostname", "helm.realityforge.org");
238: CM1.set("interface", "127.0.0.1");
239: CM1.set("user", "barney");
240: CM1.makeReadOnly();
241:
242: CM2.set("hostname", "helm.realityforge.org");
243: CM2.set("interface", "127.0.0.1");
244: CM2.set("user", "barney");
245: CM2.makeReadOnly();
246: }
247: }
248:
249: public FormatterTestCase(final String name) {
250: super (name);
251: }
252:
253: public void testRawFormatter() {
254: final Formatter formatter = new RawFormatter();
255:
256: final String result1 = formatter.format(E1);
257: final String result2 = formatter.format(E2);
258: final String result3 = formatter.format(E3);
259:
260: assertEquals("Raw formatting of E1", E1.getMessage(), result1);
261: assertEquals("Raw formatting of E2", E2.getMessage(), result2);
262: assertEquals("Raw formatting of E3", E3.getMessage(), result3);
263: }
264:
265: public void testXMLFormatter() {
266: final Formatter formatter = new XMLFormatter();
267:
268: final String result1 = formatter.format(E1);
269: final String result2 = formatter.format(E2);
270: final String result3 = formatter.format(E3);
271:
272: assertEquals("XML formatting of E1", E1_XML, result1);
273: assertEquals("XML formatting of E2", E2_XML, result2);
274: assertEquals("XML formatting of E3", E3_XML, result3);
275: }
276:
277: public void testSyslogFormatter() {
278: final Formatter formatter = new SyslogFormatter(FACILITY_ID,
279: false);
280:
281: final String result1 = formatter.format(E1);
282: final String result2 = formatter.format(E2);
283: final String result3 = formatter.format(E3);
284:
285: assertEquals("SYSLOG formatting of E1", E1_SYSLOG, result1);
286: assertEquals("SYSLOG formatting of E2", E2_SYSLOG, result2);
287: assertEquals("SYSLOG formatting of E3", E3_SYSLOG, result3);
288: }
289:
290: public void testSyslogWithBannerFormatter() {
291: final Formatter formatter = new SyslogFormatter(FACILITY_ID,
292: true);
293:
294: final String result1 = formatter.format(E1);
295: final String result2 = formatter.format(E2);
296: final String result3 = formatter.format(E3);
297:
298: assertEquals("SYSLOG with banner formatting of E1",
299: E1_SYSLOG_WB, result1);
300: assertEquals("SYSLOG with banner formatting of E2",
301: E2_SYSLOG_WB, result2);
302: assertEquals("SYSLOG with banner formatting of E3",
303: E3_SYSLOG_WB, result3);
304: }
305:
306: public void testPattern1Formatter() {
307: final Formatter formatter = new PatternFormatter(PATTERN1);
308:
309: final String result1 = formatter.format(E1);
310: final String result2 = formatter.format(E2);
311: final String result3 = formatter.format(E3);
312:
313: assertEquals("Pattern1 formatting of E1", E1_PATTERN1, result1);
314: assertEquals("Pattern1 formatting of E2", E2_PATTERN1, result2);
315: assertEquals("Pattern1 formatting of E3", E3_PATTERN1, result3);
316: }
317:
318: public void testPattern2Formatter() {
319: final Formatter formatter = new PatternFormatter(PATTERN2);
320:
321: final String result1 = formatter.format(E1);
322: final String result2 = formatter.format(E2);
323: final String result3 = formatter.format(E3);
324:
325: assertEquals("Pattern2 formatting of E1", E1_PATTERN2, result1);
326: assertEquals("Pattern2 formatting of E2", E2_PATTERN2, result2);
327: assertEquals("Pattern2 formatting of E3", E3_PATTERN2, result3);
328: }
329:
330: public void testPattern3Formatter() {
331: final Formatter formatter = new PatternFormatter(PATTERN3);
332:
333: final String result1 = formatter.format(E1);
334: final String result2 = formatter.format(E2);
335: final String result3 = formatter.format(E3);
336:
337: assertEquals("Pattern3 formatting of E1", E1_PATTERN3, result1);
338: assertEquals("Pattern3 formatting of E2", E2_PATTERN3, result2);
339: assertEquals("Pattern3 formatting of E3", E3_PATTERN3, result3);
340: }
341:
342: public void testPattern4Formatter() {
343: final Formatter formatter = new PatternFormatter(PATTERN4);
344:
345: final String result1 = formatter.format(E1);
346: final String result2 = formatter.format(E2);
347: final String result3 = formatter.format(E3);
348:
349: assertEquals("Pattern4 formatting of E1", E1_PATTERN4, result1);
350: assertEquals("Pattern4 formatting of E2", E2_PATTERN4, result2);
351: assertEquals("Pattern4 formatting of E3", E3_PATTERN4, result3);
352: }
353:
354: public void testPattern5Formatter() {
355: final Formatter formatter = new PatternFormatter(PATTERN5);
356:
357: final String result1 = formatter.format(E1);
358: final String result2 = formatter.format(E2);
359: final String result3 = formatter.format(E3);
360:
361: assertEquals("Pattern5 formatting of E1", E1_PATTERN5, result1);
362: assertEquals("Pattern5 formatting of E2", E2_PATTERN5, result2);
363: assertEquals("Pattern5 formatting of E3", E3_PATTERN5, result3);
364: }
365:
366: public void testPattern6Formatter() {
367: final Formatter formatter = new PatternFormatter(PATTERN6);
368:
369: final String result1 = formatter.format(E1);
370: final String result2 = formatter.format(E2);
371: final String result3 = formatter.format(E3);
372:
373: assertEquals("Pattern6 formatting of E1", E1_PATTERN6, result1);
374: assertEquals("Pattern6 formatting of E2", E2_PATTERN6, result2);
375: assertEquals("Pattern6 formatting of E3", E3_PATTERN6, result3);
376: }
377:
378: public void testPattern7Formatter() {
379: final Formatter formatter = new PatternFormatter(PATTERN7);
380:
381: final String result1 = formatter.format(E1);
382: final String result2 = formatter.format(E2);
383: final String result3 = formatter.format(E3);
384:
385: assertEquals("Pattern7 formatting of E1", E1_PATTERN7, result1);
386: assertEquals("Pattern7 formatting of E2", E2_PATTERN7, result2);
387: assertEquals("Pattern7 formatting of E3", E3_PATTERN7, result3);
388: }
389:
390: public void testPattern8Formatter() {
391: final Formatter formatter = new PatternFormatter(PATTERN8);
392:
393: final String result1 = formatter.format(E1);
394: final String result2 = formatter.format(E2);
395: final String result3 = formatter.format(E3);
396:
397: assertEquals("Pattern8 formatting of E1", E1_PATTERN8, result1);
398: assertEquals("Pattern8 formatting of E2", E2_PATTERN8, result2);
399: assertEquals("Pattern8 formatting of E3", E3_PATTERN8, result3);
400: }
401:
402: /*
403: public void testPattern9Formatter()
404: {
405: final Formatter formatter = new PatternFormatter( PATTERN9 );
406:
407: final String result1 = formatter.format( E1 );
408: final String result2 = formatter.format( E2 );
409: final String result3 = formatter.format( E3 );
410:
411: System.out.println( "results1: " + result1 );
412: System.out.println( "results2: " + result2 );
413: System.out.println( "results3: " + result3 );
414:
415: assertEquals( "Pattern9 formatting of E1", E1_PATTERN9, result1 );
416: assertTrue( "Pattern9 formatting of E2", result2.startsWith( E2_PATTERN9_START ) );
417: assertTrue( "Pattern9 end formatting of E2", result2.endsWith( E2_PATTERN9_END ) );
418: assertTrue( "Pattern9 formatting of E3", result3.startsWith( E3_PATTERN9_START ) );
419: assertTrue( "Pattern9 end formatting of E3", result3.endsWith( E3_PATTERN9_END ) );
420: }
421: */
422: public void testPattern10Formatter() {
423: final Formatter formatter = new PatternFormatter(PATTERN10);
424:
425: final String result1 = formatter.format(E1);
426: final String result2 = formatter.format(E2);
427: final String result3 = formatter.format(E3);
428:
429: assertEquals("Pattern10 formatting of E1", E1_PATTERN10,
430: result1);
431: assertEquals("Pattern10 formatting of E2", E2_PATTERN10,
432: result2);
433: assertEquals("Pattern10 formatting of E3", E3_PATTERN10,
434: result3);
435: }
436:
437: public void testPattern11Formatter() {
438: final Formatter formatter = new PatternFormatter(PATTERN11);
439:
440: final String result1 = formatter.format(E1);
441: final String result2 = formatter.format(E2);
442: final String result3 = formatter.format(E3);
443:
444: assertEquals("Pattern11 formatting of E1", E1_PATTERN11,
445: result1);
446: assertEquals("Pattern11 formatting of E2", E2_PATTERN11,
447: result2);
448: assertEquals("Pattern11 formatting of E3", E3_PATTERN11,
449: result3);
450: }
451: }
|