You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by ca...@apache.org on 2005/12/29 00:57:51 UTC
svn commit: r359667 [1/2] - in /logging/log4j/trunk:
src/java/org/apache/log4j/ src/java/org/apache/log4j/helpers/
src/java/org/apache/log4j/html/ src/java/org/apache/log4j/net/
src/java/org/apache/log4j/xml/ tests/ tests/src/java/org/apache/log4j/ tes...
Author: carnold
Date: Wed Dec 28 15:57:41 2005
New Revision: 359667
URL: http://svn.apache.org/viewcvs?rev=359667&view=rev
Log:
Bug 38024: Layout not compatible between 1.2 and 1.3
Added:
logging/log4j/trunk/src/java/org/apache/log4j/HTMLLayout.java
- copied, changed from r358338, logging/log4j/trunk/src/java/org/apache/log4j/html/HTMLLayout.java
logging/log4j/trunk/src/java/org/apache/log4j/helpers/DateLayout.java
logging/log4j/trunk/tests/src/java/org/apache/log4j/HTMLLayoutTest.java
logging/log4j/trunk/tests/src/java/org/apache/log4j/LayoutTest.java
logging/log4j/trunk/tests/src/java/org/apache/log4j/PatternLayoutTestCase.java
logging/log4j/trunk/tests/src/java/org/apache/log4j/TTCCLayoutTest.java
logging/log4j/trunk/tests/src/java/org/apache/log4j/helpers/DateLayoutTest.java
logging/log4j/trunk/tests/src/java/org/apache/log4j/xml/XMLLayoutTest.java
Removed:
logging/log4j/trunk/src/java/org/apache/log4j/html/HTMLLayout.java
Modified:
logging/log4j/trunk/src/java/org/apache/log4j/Layout.java
logging/log4j/trunk/src/java/org/apache/log4j/PatternLayout.java
logging/log4j/trunk/src/java/org/apache/log4j/SimpleLayout.java
logging/log4j/trunk/src/java/org/apache/log4j/TTCCLayout.java
logging/log4j/trunk/src/java/org/apache/log4j/helpers/Transform.java
logging/log4j/trunk/src/java/org/apache/log4j/net/SyslogAppender.java
logging/log4j/trunk/src/java/org/apache/log4j/xml/XMLLayout.java
logging/log4j/trunk/tests/build.xml
logging/log4j/trunk/tests/src/java/org/apache/log4j/AbstractAppenderTest.java
logging/log4j/trunk/tests/src/java/org/apache/log4j/CoreTestSuite.java
logging/log4j/trunk/tests/src/java/org/apache/log4j/PatternLayoutTest.java
logging/log4j/trunk/tests/src/java/org/apache/log4j/html/Loop.java
Copied: logging/log4j/trunk/src/java/org/apache/log4j/HTMLLayout.java (from r358338, logging/log4j/trunk/src/java/org/apache/log4j/html/HTMLLayout.java)
URL: http://svn.apache.org/viewcvs/logging/log4j/trunk/src/java/org/apache/log4j/HTMLLayout.java?p2=logging/log4j/trunk/src/java/org/apache/log4j/HTMLLayout.java&p1=logging/log4j/trunk/src/java/org/apache/log4j/html/HTMLLayout.java&r1=358338&r2=359667&rev=359667&view=diff
==============================================================================
--- logging/log4j/trunk/src/java/org/apache/log4j/html/HTMLLayout.java (original)
+++ logging/log4j/trunk/src/java/org/apache/log4j/HTMLLayout.java Wed Dec 28 15:57:41 2005
@@ -14,15 +14,12 @@
* limitations under the License.
*/
-package org.apache.log4j.html;
+package org.apache.log4j;
-import org.apache.log4j.Layout;
import org.apache.log4j.helpers.Transform;
import org.apache.log4j.pattern.*;
import org.apache.log4j.spi.LoggingEvent;
-import java.io.IOException;
-import java.io.Writer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@@ -58,6 +55,31 @@
*/
private static final String PATTERN_RULE_REGISTRY = "PATTERN_RULE_REGISTRY";
+ /**
+ A string constant used in naming the option for setting the the
+ location information flag. Current value of this string
+ constant is <b>LocationInfo</b>.
+
+ <p>Note that all option keys are case sensitive.
+
+ @deprecated Options are now handled using the JavaBeans paradigm.
+ This constant is not longer needed and will be removed in the
+ <em>near</em> term.
+
+ */
+ public static final String LOCATION_INFO_OPTION = "LocationInfo";
+
+ /**
+ A string constant used in naming the option for setting the the
+ HTML document title. Current value of this string
+ constant is <b>Title</b>.
+ @deprecated Options are now handled using the JavaBeans paradigm.
+ This constant is not longer needed and will be removed in the
+ <em>near</em> term.
+ */
+ public static final String TITLE_OPTION = "Title";
+
+
private static final String TRACE_PREFIX = "<br> ";
protected final int BUF_SIZE = 256;
protected final int MAX_CAPACITY = 1024;
@@ -66,6 +88,7 @@
private FormattingInfo[] patternFields;
private String timezone;
private String title = "Log4J Log Messages";
+ private boolean locationInfo;
private boolean internalCSS = false;
private String url2ExternalCSS = "http://logging.apache.org/log4j/docs/css/eventTable-1.0.css";
@@ -94,6 +117,31 @@
activateOptions();
}
+ /**
+ The <b>LocationInfo</b> option takes a boolean value. By
+ default, it is set to false which means there will be no location
+ information output by this layout. If the the option is set to
+ true, then the file name and line number of the statement
+ at the origin of the log statement will be output.
+
+ <p>If you are embedding this layout within an {@link
+ org.apache.log4j.net.SMTPAppender} then make sure to set the
+ <b>LocationInfo</b> option of that appender as well.
+ */
+ public
+ void setLocationInfo(boolean flag) {
+ locationInfo = flag;
+ }
+
+ /**
+ Returns the current value of the <b>LocationInfo</b> option.
+ */
+ public
+ boolean getLocationInfo() {
+ return locationInfo;
+ }
+
+
/**
* Set the <b>ConversionPattern </b> option. This is the string which
* controls formatting and consists of a mix of literal content and
@@ -215,18 +263,18 @@
return "text/html";
}
- void appendThrowableAsHTML(String[] s, Writer sbuf) throws IOException {
+ void appendThrowableAsHTML(final String[] s, final StringBuffer sbuf) {
if (s != null) {
int len = s.length;
if (len == 0) {
return;
}
- Transform.escapeTags(s[0], sbuf);
- sbuf.write(Layout.LINE_SEP);
+ sbuf.append(Transform.escapeTags(s[0]));
+ sbuf.append(Layout.LINE_SEP);
for (int i = 1; i < len; i++) {
- sbuf.write(TRACE_PREFIX);
- Transform.escapeTags(s[i], sbuf);
- sbuf.write(Layout.LINE_SEP);
+ sbuf.append(TRACE_PREFIX);
+ sbuf.append(Transform.escapeTags(s[i]));
+ sbuf.append(Layout.LINE_SEP);
}
}
}
@@ -304,10 +352,9 @@
}
/**
- * @see org.apache.log4j.Layout#format(java.io.Writer, org.apache.log4j.spi.LoggingEvent)
+ * @{inheritDoc}
*/
- public void format(Writer output, LoggingEvent event)
- throws IOException {
+ public String format(LoggingEvent event) {
boolean odd = true;
if(((counter++) & 1) == 0) {
@@ -340,17 +387,18 @@
}
buf.append("</tr>");
buf.append(Layout.LINE_SEP);
- output.write(buf.toString());
// if the pattern chain handles throwables then no need to do it again here.
if(!chainHandlesThrowable) {
String[] s = event.getThrowableStrRep();
if (s != null) {
- output.write("<tr><td class=\"exception\" colspan=\"6\">");
- appendThrowableAsHTML(s, output);
- output.write("</td></tr>" + Layout.LINE_SEP);
+ buf.append("<tr><td class=\"exception\" colspan=\"6\">");
+ appendThrowableAsHTML(s, buf);
+ buf.append("</td></tr>");
+ buf.append(Layout.LINE_SEP);
}
}
+ return buf.toString();
}
/**
Modified: logging/log4j/trunk/src/java/org/apache/log4j/Layout.java
URL: http://svn.apache.org/viewcvs/logging/log4j/trunk/src/java/org/apache/log4j/Layout.java?rev=359667&r1=359666&r2=359667&view=diff
==============================================================================
--- logging/log4j/trunk/src/java/org/apache/log4j/Layout.java (original)
+++ logging/log4j/trunk/src/java/org/apache/log4j/Layout.java Wed Dec 28 15:57:41 2005
@@ -38,11 +38,6 @@
public static final String LINE_SEP = System.getProperty("line.separator");
public static final int LINE_SEP_LEN = LINE_SEP.length();
-
-
-
- public CharArrayWriter charArrayWriter = new CharArrayWriter(1024);
-
String header;
String footer;
@@ -51,18 +46,7 @@
/**
* Implement this method to create your own layout format.
* */
- public String format(LoggingEvent event) {
- charArrayWriter.reset();
- try {
- format(charArrayWriter, event);
- } catch(IOException ie) {
- // There cannot be an IoException while writing to a CharArrayWriter
- getLogger().error("Unexpected IOException while writing to CharArrayWriter", ie);
- }
- return charArrayWriter.toString();
- }
-
- public abstract void format(Writer output, LoggingEvent event) throws IOException;
+ public abstract String format(LoggingEvent event);
/**
Returns the content type output by this layout. The base class
Modified: logging/log4j/trunk/src/java/org/apache/log4j/PatternLayout.java
URL: http://svn.apache.org/viewcvs/logging/log4j/trunk/src/java/org/apache/log4j/PatternLayout.java?rev=359667&r1=359666&r2=359667&view=diff
==============================================================================
--- logging/log4j/trunk/src/java/org/apache/log4j/PatternLayout.java (original)
+++ logging/log4j/trunk/src/java/org/apache/log4j/PatternLayout.java Wed Dec 28 15:57:41 2005
@@ -422,6 +422,18 @@
public static final String TTCC_CONVERSION_PATTERN =
"%r [%t] %p %c %x - %m%n";
+ /**
+ * Initial size of internal buffer, no longer used.
+ * @deprecated since 1.3
+ */
+ protected final int BUF_SIZE = 256;
+
+ /**
+ * Maximum capacity of internal buffer, no longer used.
+ * @deprecated since 1.3
+ */
+ protected final int MAX_CAPACITY = 1024;
+
/**
* Customized pattern conversion rules are stored under this key in the
* {@link org.apache.log4j.spi.LoggerRepository LoggerRepository} object store.
@@ -538,12 +550,9 @@
/**
* Formats a logging event to a writer.
- * @param output writer to receive output.
* @param event logging event to be formatted.
- * @throws IOException if unable to write content.
*/
- public void format(Writer output, LoggingEvent event)
- throws IOException {
+ public String format(final LoggingEvent event) {
buf.setLength(0);
for (int i = 0; i < patternConverters.length; i++) {
@@ -552,8 +561,9 @@
patternFields[i].format(startField, buf);
}
- output.write(buf.toString());
+ String retval = buf.toString();
buf.setLength(0);
+ return retval;
}
/**
Modified: logging/log4j/trunk/src/java/org/apache/log4j/SimpleLayout.java
URL: http://svn.apache.org/viewcvs/logging/log4j/trunk/src/java/org/apache/log4j/SimpleLayout.java?rev=359667&r1=359666&r2=359667&view=diff
==============================================================================
--- logging/log4j/trunk/src/java/org/apache/log4j/SimpleLayout.java (original)
+++ logging/log4j/trunk/src/java/org/apache/log4j/SimpleLayout.java Wed Dec 28 15:57:41 2005
@@ -53,12 +53,12 @@
<p>The <code>category</code> parameter is ignored.
<p>
@param event The LoggingEvent to format and write
- @param output The java.io.Writer to write to
*/
- public void format(Writer output, LoggingEvent event) throws java.io.IOException {
- output.write(event.getLevel().toString());
- output.write(" - ");
- output.write(event.getRenderedMessage());
- output.write(LINE_SEP);
+ public String format(final LoggingEvent event) {
+ StringBuffer sbuf = new StringBuffer(event.getLevel().toString());
+ sbuf.append(" - ");
+ sbuf.append(event.getRenderedMessage());
+ sbuf.append(LINE_SEP);
+ return sbuf.toString();
}
}
Modified: logging/log4j/trunk/src/java/org/apache/log4j/TTCCLayout.java
URL: http://svn.apache.org/viewcvs/logging/log4j/trunk/src/java/org/apache/log4j/TTCCLayout.java?rev=359667&r1=359666&r2=359667&view=diff
==============================================================================
--- logging/log4j/trunk/src/java/org/apache/log4j/TTCCLayout.java (original)
+++ logging/log4j/trunk/src/java/org/apache/log4j/TTCCLayout.java Wed Dec 28 15:57:41 2005
@@ -21,11 +21,6 @@
import org.apache.log4j.helpers.Constants;
import org.apache.log4j.spi.LoggingEvent;
-import java.text.DateFormat;
-import java.text.FieldPosition;
-import java.text.SimpleDateFormat;
-
-import java.util.Date;
/**
@@ -71,15 +66,12 @@
@author <A HREF="mailto:heinz.richter@ecmwf.int">Heinz Richter</a>
@deprecated Please use {@link PatternLayout} instead.
*/
-public class TTCCLayout extends Layout {
+public class TTCCLayout extends org.apache.log4j.helpers.DateLayout {
// Internal representation of options
private boolean threadPrinting = true;
private boolean categoryPrefixing = true;
private boolean contextPrinting = true;
protected final StringBuffer buf = new StringBuffer(64);
- Date date = new Date();
- DateFormat formatter;
- protected FieldPosition pos = new FieldPosition(0);
/**
* Instantiate a TTCCLayout object with in the ISO8601 format as the date
@@ -149,30 +141,6 @@
return contextPrinting;
}
- public void setDateFormat(String dateFormatStr) {
- if (dateFormatStr == null) {
- this.formatter = null;
- return;
- }
- String dateFormatPattern = null;
-
- if (dateFormatStr.equalsIgnoreCase("NULL")) {
- dateFormatPattern = null;
- } else if (dateFormatStr.equalsIgnoreCase(Constants.ABSOLUTE_FORMAT)) {
- dateFormatPattern = Constants.ABSOLUTE_TIME_PATTERN;
- } else if (
- dateFormatStr.equalsIgnoreCase(Constants.DATE_AND_TIME_FORMAT)) {
- dateFormatPattern = Constants.DATE_AND_TIME_PATTERN;
- } else if (dateFormatStr.equalsIgnoreCase(Constants.ISO8601_FORMAT)) {
- dateFormatPattern = Constants.ISO8601_PATTERN;
- } else {
- dateFormatPattern = dateFormatStr;
- }
-
- if (dateFormatPattern != null) {
- formatter = new SimpleDateFormat(dateFormatPattern);
- }
- }
/**
In addition to the level of the statement and message, the
@@ -182,41 +150,36 @@
<p>Time, thread, category and diagnostic context are printed
depending on options.
*/
- public void format(java.io.Writer output, LoggingEvent event)
- throws java.io.IOException {
+ public String format(final LoggingEvent event) {
buf.setLength(0);
- if (formatter != null) {
- date.setTime(event.getTimeStamp());
- formatter.format(date, buf, this.pos);
- buf.append(' ');
- }
- output.write(buf.toString());
+ dateFormat(buf, event);
if (this.threadPrinting) {
- output.write('[');
- output.write(event.getThreadName());
- output.write("] ");
+ buf.append('[');
+ buf.append(event.getThreadName());
+ buf.append("] ");
}
- output.write(event.getLevel().toString());
- output.write(' ');
+ buf.append(event.getLevel().toString());
+ buf.append(' ');
if (this.categoryPrefixing) {
- output.write(event.getLoggerName());
- output.write(' ');
+ buf.append(event.getLoggerName());
+ buf.append(' ');
}
if (this.contextPrinting) {
String ndc = event.getNDC();
if (ndc != null) {
- output.write(ndc);
- output.write(' ');
+ buf.append(ndc);
+ buf.append(' ');
}
}
- output.write("- ");
- output.write(event.getRenderedMessage());
- output.write(LINE_SEP);
+ buf.append("- ");
+ buf.append(event.getRenderedMessage());
+ buf.append(LINE_SEP);
+ return buf.toString();
}
}
Added: logging/log4j/trunk/src/java/org/apache/log4j/helpers/DateLayout.java
URL: http://svn.apache.org/viewcvs/logging/log4j/trunk/src/java/org/apache/log4j/helpers/DateLayout.java?rev=359667&view=auto
==============================================================================
--- logging/log4j/trunk/src/java/org/apache/log4j/helpers/DateLayout.java (added)
+++ logging/log4j/trunk/src/java/org/apache/log4j/helpers/DateLayout.java Wed Dec 28 15:57:41 2005
@@ -0,0 +1,207 @@
+/*
+ * Copyright 1999,2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// Contributors: Christopher Williams
+// Mathias Bogaert
+package org.apache.log4j.helpers;
+
+import org.apache.log4j.Layout;
+import org.apache.log4j.spi.LoggingEvent;
+
+import java.text.DateFormat;
+import java.text.FieldPosition;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.TimeZone;
+
+
+/**
+ This abstract layout takes care of all the date related options and
+ formatting work.
+
+
+ @author Ceki Gülcü
+ @deprecated since 1.3
+ */
+abstract public class DateLayout extends Layout {
+
+ /**
+ String constant designating no time information. Current value of
+ this constant is <b>NULL</b>.
+
+ */
+ public final static String NULL_DATE_FORMAT = "NULL";
+
+ /**
+ String constant designating relative time. Current value of
+ this constant is <b>RELATIVE</b>.
+ */
+ public final static String RELATIVE_TIME_DATE_FORMAT = "RELATIVE";
+
+ protected FieldPosition pos = new FieldPosition(0);
+
+ /**
+ @deprecated Options are now handled using the JavaBeans paradigm.
+ This constant is not longer needed and will be removed in the
+ <em>near</em> term.
+ */
+ final static public String DATE_FORMAT_OPTION = "DateFormat";
+
+ /**
+ @deprecated Options are now handled using the JavaBeans paradigm.
+ This constant is not longer needed and will be removed in the
+ <em>near</em> term.
+ */
+ final static public String TIMEZONE_OPTION = "TimeZone";
+
+ private String timeZoneID;
+ private String dateFormatOption;
+
+ protected DateFormat dateFormat;
+ protected Date date = new Date();
+
+
+ /**
+ * Instantiate a DateLayout object with in the ISO8601 format as the date
+ * formatter.
+ * */
+ public DateLayout() {
+ }
+
+ /**
+ Instantiate a DateLayout object using the local time zone. The
+ DateFormat used will depend on the <code>dateFormatType</code>.
+
+ <p>This constructor just calls the {@link #setDateFormat} method.
+ */
+ public DateLayout(final String dateFormatType) {
+ this.setDateFormat(dateFormatType);
+ }
+
+
+ /**
+ @deprecated Use the setter method for the option directly instead
+ of the generic <code>setOption</code> method.
+ */
+ public
+ String[] getOptionStrings() {
+ return new String[] {DATE_FORMAT_OPTION, TIMEZONE_OPTION};
+ }
+
+ /**
+ @deprecated Use the setter method for the option directly instead
+ of the generic <code>setOption</code> method.
+ */
+ public
+ void setOption(final String option, final String value) {
+ if(option.equalsIgnoreCase(DATE_FORMAT_OPTION)) {
+ dateFormatOption = value.toUpperCase();
+ } else if(option.equalsIgnoreCase(TIMEZONE_OPTION)) {
+ timeZoneID = value;
+ }
+ }
+
+ /**
+ The value of the <b>DateFormat</b> option should be either an
+ argument to the constructor of {@link SimpleDateFormat} or one of
+ the srings "NULL", "RELATIVE", "ABSOLUTE", "DATE" or "ISO8601.
+ */
+ public
+ void setDateFormat(String dateFormat) {
+ if (dateFormat != null) {
+ dateFormatOption = dateFormat;
+ }
+ setDateFormat(dateFormatOption, TimeZone.getDefault());
+ }
+
+
+
+ /**
+ Returns value of the <b>DateFormat</b> option.
+ */
+ public
+ String getDateFormat() {
+ return dateFormatOption;
+ }
+
+ /**
+ The <b>TimeZoneID</b> option is a time zone ID string in the format
+ expected by the {@link TimeZone#getTimeZone} method.
+ */
+ public
+ void setTimeZone(String timeZone) {
+ this.timeZoneID = timeZone;
+ }
+
+ /**
+ Returns value of the <b>TimeZone</b> option.
+ */
+ public
+ String getTimeZone() {
+ return timeZoneID;
+ }
+
+
+ public void activateOptions() {
+ }
+
+ /**
+ Sets the {@link DateFormat} used to format time and date in the
+ zone determined by <code>timeZone</code>.
+ */
+ public
+ void setDateFormat(DateFormat dateFormat, TimeZone timeZone) {
+ this.dateFormat = dateFormat;
+ this.dateFormat.setTimeZone(timeZone);
+ }
+
+
+ public void setDateFormat(final String dateFormatStr, final TimeZone timeZone) {
+ if (dateFormatStr == null) {
+ this.dateFormat = null;
+ return;
+ }
+
+ if (!dateFormatStr.equalsIgnoreCase("NULL")) {
+ if (dateFormatStr.equalsIgnoreCase(RELATIVE_TIME_DATE_FORMAT)) {
+ this.dateFormat = new RelativeTimeDateFormat();
+ } else {
+ if (dateFormatStr.equalsIgnoreCase(Constants.ABSOLUTE_FORMAT)) {
+ dateFormat = new SimpleDateFormat(Constants.ABSOLUTE_TIME_PATTERN);
+ } else if (dateFormatStr.equalsIgnoreCase(Constants.DATE_AND_TIME_FORMAT)) {
+ dateFormat = new SimpleDateFormat(Constants.DATE_AND_TIME_PATTERN);
+ } else if (dateFormatStr.equalsIgnoreCase(Constants.ISO8601_FORMAT)) {
+ dateFormat = new SimpleDateFormat(Constants.ISO8601_PATTERN);
+ } else {
+ dateFormat = new SimpleDateFormat(dateFormatStr);
+ }
+ if (timeZone != null) {
+ dateFormat.setTimeZone(timeZone);
+ }
+ }
+ }
+ }
+
+
+ public
+ void dateFormat(StringBuffer buf, LoggingEvent event) {
+ if(dateFormat != null) {
+ date.setTime(event.timeStamp);
+ dateFormat.format(date, buf, this.pos);
+ buf.append(' ');
+ }
+ }
+}
Modified: logging/log4j/trunk/src/java/org/apache/log4j/helpers/Transform.java
URL: http://svn.apache.org/viewcvs/logging/log4j/trunk/src/java/org/apache/log4j/helpers/Transform.java?rev=359667&r1=359666&r2=359667&view=diff
==============================================================================
--- logging/log4j/trunk/src/java/org/apache/log4j/helpers/Transform.java (original)
+++ logging/log4j/trunk/src/java/org/apache/log4j/helpers/Transform.java Wed Dec 28 15:57:41 2005
@@ -40,30 +40,26 @@
* characters with respective predefined entity references.
*
* @param input The text to be converted.
- * @param output the writer to which to write the modified input
*/
- public static void escapeTags(String input, Writer output) throws IOException {
+ public static String escapeTags(final String input) {
//Check if the string is null or zero length -- if so, return
//what was sent in.
- if ((input == null) || (input.length() == 0)) {
- return;
+ if ((input == null)
+ || (input.length() == 0)
+ || (input.indexOf("<") == -1 && input.indexOf(">") == -1)) {
+ return input;
}
- char ch = ' ';
-
- int len = input.length();
-
- for (int i = 0; i < len; i++) {
- ch = input.charAt(i);
-
- if (ch == '<') {
- output.write("<");
- } else if (ch == '>') {
- output.write(">");
- } else {
- output.write(ch);
- }
+ StringBuffer buf = new StringBuffer(input);
+ for(int i = 0;i < buf.length(); i++) {
+ char ch = buf.charAt(i);
+ if (ch == '<') {
+ buf.replace(i, i + 1, "<");
+ } else if (ch == '>') {
+ buf.replace(i, i + 1, ">");
+ }
}
+ return buf.toString();
}
//public static void appendEscapingCDATA(StringBuffer buf, String str) {
@@ -80,8 +76,7 @@
*
* @param str The String that is inserted into an existing CDATA Section.
* */
- public static void appendEscapingCDATA(Writer output, String str)
- throws IOException {
+ public static void appendEscapingCDATA(StringBuffer output, String str) {
if (str == null) {
return;
}
@@ -89,7 +84,7 @@
int end = str.indexOf(CDATA_END);
if (end < 0) {
- output.write(str);
+ output.append(str);
return;
}
@@ -97,8 +92,8 @@
int start = 0;
while (end > -1) {
- output.write(str.substring(start, end));
- output.write(CDATA_EMBEDED_END);
+ output.append(str.substring(start, end));
+ output.append(CDATA_EMBEDED_END);
start = end + CDATA_END_LEN;
if (start < str.length()) {
@@ -108,6 +103,6 @@
}
}
- output.write(str.substring(start));
+ output.append(str.substring(start));
}
}
Modified: logging/log4j/trunk/src/java/org/apache/log4j/net/SyslogAppender.java
URL: http://svn.apache.org/viewcvs/logging/log4j/trunk/src/java/org/apache/log4j/net/SyslogAppender.java?rev=359667&r1=359666&r2=359667&view=diff
==============================================================================
--- logging/log4j/trunk/src/java/org/apache/log4j/net/SyslogAppender.java (original)
+++ logging/log4j/trunk/src/java/org/apache/log4j/net/SyslogAppender.java Wed Dec 28 15:57:41 2005
@@ -374,7 +374,8 @@
try {
writeInitialParts(event);
- layout.format(sw, event);
+ String msg = layout.format(event);
+ sw.write(msg);
sw.flush();
/*
Modified: logging/log4j/trunk/src/java/org/apache/log4j/xml/XMLLayout.java
URL: http://svn.apache.org/viewcvs/logging/log4j/trunk/src/java/org/apache/log4j/xml/XMLLayout.java?rev=359667&r1=359666&r2=359667&view=diff
==============================================================================
--- logging/log4j/trunk/src/java/org/apache/log4j/xml/XMLLayout.java (original)
+++ logging/log4j/trunk/src/java/org/apache/log4j/xml/XMLLayout.java Wed Dec 28 15:57:41 2005
@@ -109,33 +109,34 @@
/**
* Formats a {@link LoggingEvent}in conformance with the log4j.dtd.
*/
- public void format(Writer output, LoggingEvent event) throws IOException {
+ public String format(LoggingEvent event) {
+ StringBuffer buf = new StringBuffer();
// We yield to the \r\n heresy.
- output.write("<log4j:event logger=\"");
- output.write(event.getLoggerName());
- output.write("\" timestamp=\"");
- output.write(Long.toString(event.getTimeStamp()));
- output.write("\" sequenceNumber=\"");
- output.write(Long.toString(event.getSequenceNumber()));
- output.write("\" level=\"");
- output.write(event.getLevel().toString());
- output.write("\" thread=\"");
- output.write(event.getThreadName());
- output.write("\">\r\n");
+ buf.append("<log4j:event logger=\"");
+ buf.append(event.getLoggerName());
+ buf.append("\" timestamp=\"");
+ buf.append(Long.toString(event.getTimeStamp()));
+ buf.append("\" sequenceNumber=\"");
+ buf.append(Long.toString(event.getSequenceNumber()));
+ buf.append("\" level=\"");
+ buf.append(event.getLevel().toString());
+ buf.append("\" thread=\"");
+ buf.append(event.getThreadName());
+ buf.append("\">\r\n");
- output.write("<log4j:message><![CDATA[");
+ buf.append("<log4j:message><![CDATA[");
// Append the rendered message. Also make sure to escape any
// existing CDATA sections.
- Transform.appendEscapingCDATA(output, event.getRenderedMessage());
- output.write("]]></log4j:message>\r\n");
+ Transform.appendEscapingCDATA(buf, event.getRenderedMessage());
+ buf.append("]]></log4j:message>\r\n");
String ndc = event.getNDC();
if (ndc != null) {
- output.write("<log4j:NDC><![CDATA[");
- output.write(ndc);
- output.write("]]></log4j:NDC>\r\n");
+ buf.append("<log4j:NDC><![CDATA[");
+ buf.append(ndc);
+ buf.append("]]></log4j:NDC>\r\n");
}
// Set mdcKeySet = event.getMDCKeySet();
@@ -151,7 +152,7 @@
// List sortedList = new ArrayList(mdcKeySet);
// Collections.sort(sortedList);
//
- // output.write("<log4j:MDC>\r\n");
+ // buf.append("<log4j:MDC>\r\n");
//
// Iterator iter = sortedList.iterator();
//
@@ -171,49 +172,50 @@
String[] s = event.getThrowableStrRep();
if (s != null) {
- output.write("<log4j:throwable><![CDATA[");
+ buf.append("<log4j:throwable><![CDATA[");
for (int i = 0; i < s.length; i++) {
- output.write(s[i]);
- output.write("\r\n");
+ buf.append(s[i]);
+ buf.append("\r\n");
}
- output.write("]]></log4j:throwable>\r\n");
+ buf.append("]]></log4j:throwable>\r\n");
}
}
if (locationInfo) {
LocationInfo locationInfo = event.getLocationInformation();
- output.write("<log4j:locationInfo class=\"");
- Transform.escapeTags(locationInfo.getClassName(), output);
- output.write("\" method=\"");
- Transform.escapeTags(locationInfo.getMethodName(), output);
- output.write("\" file=\"");
- output.write(locationInfo.getFileName());
- output.write("\" line=\"");
- output.write(locationInfo.getLineNumber());
- output.write("\"/>\r\n");
+ buf.append("<log4j:locationInfo class=\"");
+ buf.append(Transform.escapeTags(locationInfo.getClassName()));
+ buf.append("\" method=\"");
+ buf.append(Transform.escapeTags(locationInfo.getMethodName()));
+ buf.append("\" file=\"");
+ buf.append(locationInfo.getFileName());
+ buf.append("\" line=\"");
+ buf.append(locationInfo.getLineNumber());
+ buf.append("\"/>\r\n");
}
Set propertySet = event.getPropertyKeySet();
if ((propertySet != null) && (propertySet.size() > 0)) {
- output.write("<log4j:properties>\r\n");
+ buf.append("<log4j:properties>\r\n");
Iterator propIter = propertySet.iterator();
while (propIter.hasNext()) {
String propName = propIter.next().toString();
- output.write(" <log4j:data name=\"" + propName);
+ buf.append(" <log4j:data name=\"" + propName);
String propValue = event.getProperty(propName).toString();
- output.write("\" value=\"" + propValue);
- output.write("\"/>\r\n");
+ buf.append("\" value=\"" + propValue);
+ buf.append("\"/>\r\n");
}
- output.write("</log4j:properties>\r\n");
+ buf.append("</log4j:properties>\r\n");
}
- output.write("</log4j:event>\r\n\r\n");
+ buf.append("</log4j:event>\r\n\r\n");
+ return buf.toString();
}
}
Modified: logging/log4j/trunk/tests/build.xml
URL: http://svn.apache.org/viewcvs/logging/log4j/trunk/tests/build.xml?rev=359667&r1=359666&r2=359667&view=diff
==============================================================================
--- logging/log4j/trunk/tests/build.xml (original)
+++ logging/log4j/trunk/tests/build.xml Wed Dec 28 15:57:41 2005
@@ -295,7 +295,7 @@
<junit printsummary="yes" fork="yes" haltonfailure="${haltonfailure}">
<classpath refid="tests.classpath"/>
<formatter type="plain" usefile="false"/>
- <test name="org.apache.log4j.PatternLayoutTest" />
+ <test name="org.apache.log4j.PatternLayoutTestCase" />
</junit>
</target>
Modified: logging/log4j/trunk/tests/src/java/org/apache/log4j/AbstractAppenderTest.java
URL: http://svn.apache.org/viewcvs/logging/log4j/trunk/tests/src/java/org/apache/log4j/AbstractAppenderTest.java?rev=359667&r1=359666&r2=359667&view=diff
==============================================================================
--- logging/log4j/trunk/tests/src/java/org/apache/log4j/AbstractAppenderTest.java (original)
+++ logging/log4j/trunk/tests/src/java/org/apache/log4j/AbstractAppenderTest.java Wed Dec 28 15:57:41 2005
@@ -40,7 +40,7 @@
public class DummyLayout extends Layout {
- public void format(Writer output, LoggingEvent event) {}
+ public String format(LoggingEvent event) { return ""; }
public void activateOptions() {}
}
Modified: logging/log4j/trunk/tests/src/java/org/apache/log4j/CoreTestSuite.java
URL: http://svn.apache.org/viewcvs/logging/log4j/trunk/tests/src/java/org/apache/log4j/CoreTestSuite.java?rev=359667&r1=359666&r2=359667&view=diff
==============================================================================
--- logging/log4j/trunk/tests/src/java/org/apache/log4j/CoreTestSuite.java (original)
+++ logging/log4j/trunk/tests/src/java/org/apache/log4j/CoreTestSuite.java Wed Dec 28 15:57:41 2005
@@ -30,6 +30,7 @@
/**
* Constructs test suite.
* @return test suite
+ * @deprecated since some tests in suite test deprecated classes.
*/
public static Test suite() {
TestSuite s = new TestSuite();
@@ -41,6 +42,12 @@
s.addTestSuite(org.apache.log4j.LevelTest.class);
s.addTestSuite(org.apache.log4j.LogManagerTest.class);
s.addTestSuite(org.apache.log4j.helpers.LogLogTest.class);
+ s.addTestSuite(org.apache.log4j.LayoutTest.class);
+ s.addTestSuite(org.apache.log4j.helpers.DateLayoutTest.class);
+ s.addTestSuite(org.apache.log4j.TTCCLayoutTest.class);
+ s.addTestSuite(org.apache.log4j.xml.XMLLayoutTest.class);
+ s.addTestSuite(org.apache.log4j.HTMLLayoutTest.class);
+ s.addTestSuite(org.apache.log4j.PatternLayoutTest.class);
return s;
}
}
Added: logging/log4j/trunk/tests/src/java/org/apache/log4j/HTMLLayoutTest.java
URL: http://svn.apache.org/viewcvs/logging/log4j/trunk/tests/src/java/org/apache/log4j/HTMLLayoutTest.java?rev=359667&view=auto
==============================================================================
--- logging/log4j/trunk/tests/src/java/org/apache/log4j/HTMLLayoutTest.java (added)
+++ logging/log4j/trunk/tests/src/java/org/apache/log4j/HTMLLayoutTest.java Wed Dec 28 15:57:41 2005
@@ -0,0 +1,179 @@
+/*
+ * Copyright 1999,2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.log4j;
+
+import org.apache.log4j.spi.LoggingEvent;
+
+import org.w3c.dom.Document;
+
+import org.xml.sax.InputSource;
+
+import java.io.Reader;
+import java.io.StringReader;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+
+
+/**
+ * Test for HTMLLayout.
+ *
+ * @author Curt Arnold
+ */
+public class HTMLLayoutTest extends LayoutTest {
+ /**
+ * Construct new instance of XMLLayoutTest.
+ *
+ * @param testName test name.
+ */
+ public HTMLLayoutTest(final String testName) {
+ super(testName, "text/html", false, null, null);
+ }
+
+ /**
+ * @{inheritDoc}
+ */
+ protected Layout createLayout() {
+ return new HTMLLayout();
+ }
+
+ /**
+ * Parses the string as the body of an XML document and returns the document element.
+ * @param source source string.
+ * @return document element.
+ * @throws Exception if parser can not be constructed or source is not a valid XML document.
+ */
+ private Document parse(final String source) throws Exception {
+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+ factory.setNamespaceAware(false);
+ factory.setCoalescing(true);
+
+ DocumentBuilder builder = factory.newDocumentBuilder();
+ Reader reader = new StringReader(source);
+
+ return builder.parse(new InputSource(reader));
+ }
+
+ /**
+ * Tests formatted results.
+ * @throws Exception if unable to create parser or output is not valid XML.
+ */
+ public void testFormat() throws Exception {
+ Logger logger = Logger.getLogger("org.apache.log4j.xml.HTMLLayoutTest");
+ NDC.push("NDC goes here");
+
+ LoggingEvent event =
+ new LoggingEvent(
+ "org.apache.log4j.Logger", logger, Level.INFO, "Hello, World", null);
+ HTMLLayout layout = (HTMLLayout) createLayout();
+ layout.setLocationInfo(true);
+
+ String result = layout.format(event);
+ NDC.pop();
+
+ String src =
+ "<!DOCTYPE body [ <!ENTITY nbsp ' '>]><body>" + result + "</body>";
+ Document doc = parse(src);
+ }
+
+ /**
+ * Tests getHeader.
+ */
+ public void testGetHeader() {
+ assertEquals("<!DOCTYPE", createLayout().getHeader().substring(0, 9));
+ }
+
+ /**
+ * Tests getHeader with locationInfo = true.
+ */
+ public void testGetHeaderWithLocation() {
+ HTMLLayout layout = (HTMLLayout) createLayout();
+ layout.setLocationInfo(true);
+ assertEquals("<!DOCTYPE", layout.getHeader().substring(0, 9));
+ }
+
+ /**
+ * Tests getFooter.
+ */
+ public void testGetFooter() {
+ assertEquals("</table>", createLayout().getFooter().substring(0, 8));
+ }
+
+ /**
+ * Tests getLocationInfo and setLocationInfo.
+ */
+ public void testGetSetLocationInfo() {
+ HTMLLayout layout = new HTMLLayout();
+ assertEquals(false, layout.getLocationInfo());
+ layout.setLocationInfo(true);
+ assertEquals(true, layout.getLocationInfo());
+ layout.setLocationInfo(false);
+ assertEquals(false, layout.getLocationInfo());
+ }
+
+ /**
+ * Tests activateOptions().
+ */
+ public void testActivateOptions() {
+ HTMLLayout layout = new HTMLLayout();
+ layout.activateOptions();
+ }
+
+ /**
+ * Tests getTitle and setTitle.
+ */
+ public void testGetSetTitle() {
+ HTMLLayout layout = new HTMLLayout();
+ assertEquals("Log4J Log Messages", layout.getTitle());
+ layout.setTitle(null);
+ assertNull(layout.getTitle());
+
+ String newTitle = "A treatise on messages of log persuasion";
+ layout.setTitle(newTitle);
+ assertEquals(newTitle, layout.getTitle());
+ }
+
+ /**
+ * Tests buffer downsizing and DEBUG and WARN colorization code paths.
+ */
+ public void testFormatResize() {
+ Logger logger = Logger.getLogger("org.apache.log4j.xml.HTMLLayoutTest");
+ NDC.clear();
+
+ char[] msg = new char[2000];
+
+ for (int i = 0; i < msg.length; i++) {
+ msg[i] = 'A';
+ }
+
+ LoggingEvent event1 =
+ new LoggingEvent(
+ "org.apache.log4j.Logger", logger, Level.DEBUG, new String(msg), null);
+ HTMLLayout layout = (HTMLLayout) createLayout();
+ layout.setLocationInfo(true);
+
+ String result = layout.format(event1);
+ Exception ex = new IllegalArgumentException("'foo' is not a valid value.");
+ LoggingEvent event2 =
+ new LoggingEvent(
+ "org.apache.log4j.Logger", logger, Level.WARN, "Hello, World", ex);
+ result = layout.format(event2);
+ assertEquals(
+ Layout.LINE_SEP + "<tr class=",
+ result.substring(0, Layout.LINE_SEP.length() + 10));
+ }
+}
Added: logging/log4j/trunk/tests/src/java/org/apache/log4j/LayoutTest.java
URL: http://svn.apache.org/viewcvs/logging/log4j/trunk/tests/src/java/org/apache/log4j/LayoutTest.java?rev=359667&view=auto
==============================================================================
--- logging/log4j/trunk/tests/src/java/org/apache/log4j/LayoutTest.java (added)
+++ logging/log4j/trunk/tests/src/java/org/apache/log4j/LayoutTest.java Wed Dec 28 15:57:41 2005
@@ -0,0 +1,167 @@
+/*
+ * Copyright 1999,2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.log4j;
+
+import junit.framework.TestCase;
+
+import org.apache.log4j.spi.LoggingEvent;
+
+
+/**
+ * Tests for Layout.
+ *
+ */
+public class LayoutTest extends TestCase {
+ /**
+ * Expected content type.
+ */
+ private final String contentType;
+
+ /**
+ * Expected value for ignoresThrowable.
+ */
+ private final boolean ignoresThrowable;
+
+ /**
+ * Expected value for header.
+ */
+ private final String header;
+
+ /**
+ * Expected value for footer.
+ */
+ private final String footer;
+
+ /**
+ * Construct a new instance of LayoutTest.
+ * @param testName test name.
+ */
+ public LayoutTest(final String testName) {
+ super(testName);
+ contentType = "text/plain";
+ ignoresThrowable = true;
+ header = null;
+ footer = null;
+ }
+
+ /**
+ * Constructor for use by derived tests.
+ * @param testName name of test.
+ * @param expectedContentType expected value for getContentType().
+ * @param expectedIgnoresThrowable expected value for ignoresThrowable().
+ * @param expectedHeader expected value for getHeader().
+ * @param expectedFooter expected value for getFooter().
+ */
+ protected LayoutTest(
+ final String testName, final String expectedContentType,
+ final boolean expectedIgnoresThrowable, final String expectedHeader,
+ final String expectedFooter) {
+ super(testName);
+ contentType = expectedContentType;
+ ignoresThrowable = expectedIgnoresThrowable;
+ header = expectedHeader;
+ footer = expectedFooter;
+ }
+
+ /**
+ * Tests Layout.LINE_SEP.
+ */
+ public void testLineSep() {
+ assertEquals(System.getProperty("line.separator"), Layout.LINE_SEP);
+ }
+
+ /**
+ * Tests Layout.LINE_SEP.
+ */
+ public void testLineSepLen() {
+ assertEquals(Layout.LINE_SEP.length(), Layout.LINE_SEP_LEN);
+ }
+
+ /**
+ * Creates layout for test.
+ * @return new instance of Layout.
+ */
+ protected Layout createLayout() {
+ return new MockLayout();
+ }
+
+ /**
+ * Tests getContentType.
+ */
+ public void testGetContentType() {
+ assertEquals(contentType, createLayout().getContentType());
+ }
+
+ /**
+ * Tests ignoresThrowable.
+ */
+ public void testIgnoresThrowable() {
+ assertEquals(ignoresThrowable, createLayout().ignoresThrowable());
+ }
+
+ /**
+ * Tests getHeader.
+ */
+ public void testGetHeader() {
+ assertEquals(header, createLayout().getHeader());
+ }
+
+ /**
+ * Tests getFooter.
+ */
+ public void testGetFooter() {
+ assertEquals(footer, createLayout().getFooter());
+ }
+
+ /**
+ * Tests format.
+ * @throws Exception derived tests, particular XMLLayoutTest, may throw exceptions.
+ */
+ public void testFormat() throws Exception {
+ Logger logger = Logger.getLogger("org.apache.log4j.LayoutTest");
+ LoggingEvent event =
+ new LoggingEvent(
+ "org.apache.log4j.Logger", logger, Level.INFO, "Hello, World", null);
+ String result = createLayout().format(event);
+ assertEquals("Mock", result);
+ }
+
+ /**
+ * Concrete Layout class for tests.
+ */
+ private static final class MockLayout extends Layout {
+ /**
+ * @{inheritDoc}
+ */
+ public String format(final LoggingEvent event) {
+ return "Mock";
+ }
+
+ /**
+ * @{inheritDoc}
+ */
+ public void activateOptions() {
+ }
+
+ /**
+ * @{inheritDoc}
+ */
+ public boolean ignoresThrowable() {
+ return true;
+ }
+ }
+}
Modified: logging/log4j/trunk/tests/src/java/org/apache/log4j/PatternLayoutTest.java
URL: http://svn.apache.org/viewcvs/logging/log4j/trunk/tests/src/java/org/apache/log4j/PatternLayoutTest.java?rev=359667&r1=359666&r2=359667&view=diff
==============================================================================
--- logging/log4j/trunk/tests/src/java/org/apache/log4j/PatternLayoutTest.java (original)
+++ logging/log4j/trunk/tests/src/java/org/apache/log4j/PatternLayoutTest.java Wed Dec 28 15:57:41 2005
@@ -1,12 +1,12 @@
/*
- * Copyright 1999,2004 The Apache Software Foundation.
- *
+ * Copyright 1999,2005 The Apache Software Foundation.
+ *
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -16,434 +16,128 @@
package org.apache.log4j;
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-
-import org.apache.log4j.util.AbsoluteDateAndTimeFilter;
-import org.apache.log4j.util.AbsoluteTimeFilter;
-import org.apache.log4j.util.Compare;
-import org.apache.log4j.util.ControlFilter;
-import org.apache.log4j.util.Filter;
-import org.apache.log4j.util.ISO8601Filter;
-import org.apache.log4j.util.JunitTestRunnerFilter;
-import org.apache.log4j.util.LineNumberFilter;
-import org.apache.log4j.util.RelativeTimeFilter;
-import org.apache.log4j.util.SunReflectFilter;
-import org.apache.log4j.util.Transformer;
-
-
-public class PatternLayoutTest extends TestCase {
- static String TEMP = "output/temp";
- static String FILTERED = "output/filtered";
- static String EXCEPTION1 = "java.lang.Exception: Just testing";
- static String EXCEPTION2 = "\\s*at .*\\(.*:\\d{1,4}\\)";
- static String EXCEPTION3 = "\\s*at .*\\(Native Method\\)";
- static String PAT0 =
- "\\[main]\\ (DEBUG|INFO|WARN|ERROR|FATAL) .* - Message \\d{1,2}";
- static String PAT1 = Filter.ISO8601_PAT + " " + PAT0;
- static String PAT2 = Filter.ABSOLUTE_DATE_AND_TIME_PAT + " " + PAT0;
- static String PAT3 = Filter.ABSOLUTE_TIME_PAT + " " + PAT0;
- static String PAT4 = Filter.RELATIVE_TIME_PAT + " " + PAT0;
- static String PAT5 =
- "\\[main]\\ (DEBUG|INFO|WARN|ERROR|FATAL) .* : Message \\d{1,2}";
- static String PAT6 =
- "\\[main]\\ (DEBUG|INFO |WARN |ERROR|FATAL) org.apache.log4j.PatternLayoutTest.common\\(PatternLayoutTest.java:\\d{1,4}\\): Message \\d{1,2}";
- static String PAT11a =
- "^(DEBUG|INFO |WARN |ERROR|FATAL) \\[main]\\ log4j.PatternLayoutTest: Message \\d{1,2}";
- static String PAT11b =
- "^(DEBUG|INFO |WARN |ERROR|FATAL) \\[main]\\ root: Message \\d{1,2}";
- static String PAT12 =
- "^\\[main]\\ (DEBUG|INFO |WARN |ERROR|FATAL) "
- + "org.apache.log4j.PatternLayoutTest.common\\(PatternLayoutTest.java:\\d{3}\\): "
- + "Message \\d{1,2}";
- static String PAT13 =
- "^\\[main]\\ (DEBUG|INFO |WARN |ERROR|FATAL) "
- + "apache.log4j.PatternLayoutTest.common\\(PatternLayoutTest.java:\\d{3}\\): "
- + "Message \\d{1,2}";
- static String PAT14 =
- "^(DEBUG| INFO| WARN|ERROR|FATAL)\\ \\d{1,2}\\ *- Message \\d{1,2}";
- static String PAT_MDC_1 = "";
- Logger root;
- Logger logger;
+import org.apache.log4j.spi.LoggingEvent;
- public PatternLayoutTest(String name) {
- super(name);
- }
-
- public void setUp() {
- root = Logger.getRootLogger();
- logger = Logger.getLogger(PatternLayoutTest.class);
- }
-
- public void tearDown() {
- root.getLoggerRepository().resetConfiguration();
- }
-
- public void test1() throws Exception {
- PropertyConfigurator.configure("input/pattern/patternLayout1.properties");
- common();
- Transformer.transform(
- TEMP, FILTERED,
- new Filter[] {
- new LineNumberFilter(), new SunReflectFilter(),
- new JunitTestRunnerFilter()
- });
- assertTrue(Compare.compare(FILTERED, "witness/pattern/patternLayout.1"));
- }
-
- public void test2() throws Exception {
- PropertyConfigurator.configure("input/pattern/patternLayout2.properties");
- common();
-
- ControlFilter cf1 =
- new ControlFilter(
- new String[] { PAT1, EXCEPTION1, EXCEPTION2, EXCEPTION3 });
- Transformer.transform(
- TEMP, FILTERED,
- new Filter[] {
- cf1, new LineNumberFilter(), new ISO8601Filter(),
- new SunReflectFilter(), new JunitTestRunnerFilter()
- });
- assertTrue(Compare.compare(FILTERED, "witness/pattern/patternLayout.2"));
- }
-
- public void test3() throws Exception {
- PropertyConfigurator.configure("input/pattern/patternLayout3.properties");
- common();
-
- ControlFilter cf1 =
- new ControlFilter(
- new String[] { PAT1, EXCEPTION1, EXCEPTION2, EXCEPTION3 });
- Transformer.transform(
- TEMP, FILTERED,
- new Filter[] {
- cf1, new LineNumberFilter(), new ISO8601Filter(),
- new SunReflectFilter(), new JunitTestRunnerFilter()
- });
- assertTrue(Compare.compare(FILTERED, "witness/pattern/patternLayout.3"));
- }
- // Output format:
- // 06 avr. 2002 18:30:58,937 [main] DEBUG atternLayoutTest - Message 0
- public void test4() throws Exception {
- PropertyConfigurator.configure("input/pattern/patternLayout4.properties");
- common();
-
- ControlFilter cf1 =
- new ControlFilter(
- new String[] { PAT2, EXCEPTION1, EXCEPTION2, EXCEPTION3 });
- Transformer.transform(
- TEMP, FILTERED,
- new Filter[] {
- cf1, new LineNumberFilter(), new AbsoluteDateAndTimeFilter(),
- new SunReflectFilter(), new JunitTestRunnerFilter()
- });
- assertTrue(Compare.compare(FILTERED, "witness/pattern/patternLayout.4"));
+/**
+ * Test for PatternLayout.
+ *
+ * @author Curt Arnold
+ */
+public class PatternLayoutTest extends LayoutTest {
+ /**
+ * Construct new instance of PatternLayoutTest.
+ *
+ * @param testName test name.
+ */
+ public PatternLayoutTest(final String testName) {
+ super(testName, "text/plain", true, null, null);
}
- public void test5() throws Exception {
- PropertyConfigurator.configure("input/pattern/patternLayout5.properties");
- common();
-
- ControlFilter cf1 =
- new ControlFilter(
- new String[] { PAT2, EXCEPTION1, EXCEPTION2, EXCEPTION3 });
- Transformer.transform(
- TEMP, FILTERED,
- new Filter[] {
- cf1, new LineNumberFilter(), new AbsoluteDateAndTimeFilter(),
- new SunReflectFilter(), new JunitTestRunnerFilter()
- });
- assertTrue(Compare.compare(FILTERED, "witness/pattern/patternLayout.5"));
+ /**
+ * @{inheritDoc}
+ */
+ protected Layout createLayout() {
+ return new PatternLayout("[%t] %p %c - %m%n");
}
- // 18:54:19,201 [main] DEBUG atternLayoutTest - Message 0
- public void test6() throws Exception {
- PropertyConfigurator.configure("input/pattern/patternLayout6.properties");
- common();
-
- ControlFilter cf1 =
- new ControlFilter(
- new String[] { PAT3, EXCEPTION1, EXCEPTION2, EXCEPTION3 });
- Transformer.transform(
- TEMP, FILTERED,
- new Filter[] {
- cf1, new LineNumberFilter(), new AbsoluteTimeFilter(),
- new SunReflectFilter(), new JunitTestRunnerFilter()
- });
- assertTrue(Compare.compare(FILTERED, "witness/pattern/patternLayout.6"));
+ /**
+ * Tests format.
+ */
+ public void testFormat() {
+ Logger logger = Logger.getLogger("org.apache.log4j.LayoutTest");
+ LoggingEvent event =
+ new LoggingEvent(
+ "org.apache.log4j.Logger", logger, Level.INFO, "Hello, World", null);
+ PatternLayout layout = (PatternLayout) createLayout();
+ String result = layout.format(event);
+ StringBuffer buf = new StringBuffer(100);
+ buf.append('[');
+ buf.append(event.getThreadName());
+ buf.append("] ");
+ buf.append(event.getLevel().toString());
+ buf.append(' ');
+ buf.append(event.getLoggerName());
+ buf.append(" - ");
+ buf.append(event.getMessage());
+ buf.append(System.getProperty("line.separator"));
+ assertEquals(buf.toString(), result);
}
- public void test7() throws Exception {
- PropertyConfigurator.configure("input/pattern/patternLayout7.properties");
- common();
-
- ControlFilter cf1 =
- new ControlFilter(
- new String[] { PAT3, EXCEPTION1, EXCEPTION2, EXCEPTION3 });
- Transformer.transform(
- TEMP, FILTERED,
- new Filter[] {
- cf1, new LineNumberFilter(), new AbsoluteTimeFilter(),
- new SunReflectFilter(), new JunitTestRunnerFilter()
- });
- assertTrue(Compare.compare(FILTERED, "witness/pattern/patternLayout.7"));
+ /**
+ * Tests getPatternFormat().
+ */
+ public void testGetPatternFormat() {
+ PatternLayout layout = (PatternLayout) createLayout();
+ assertEquals("[%t] %p %c - %m%n", layout.getConversionPattern());
}
- public void test8() throws Exception {
- PropertyConfigurator.configure("input/pattern/patternLayout8.properties");
- common();
-
- ControlFilter cf1 =
- new ControlFilter(
- new String[] { PAT4, EXCEPTION1, EXCEPTION2, EXCEPTION3 });
- Transformer.transform(
- TEMP, FILTERED,
- new Filter[] {
- cf1, new LineNumberFilter(), new RelativeTimeFilter(),
- new SunReflectFilter(), new JunitTestRunnerFilter()
- });
- assertTrue(Compare.compare(FILTERED, "witness/pattern/patternLayout.8"));
+ /**
+ * Tests DEFAULT_CONVERSION_PATTERN constant.
+ */
+ public void testDefaultConversionPattern() {
+ assertEquals("%m%n", PatternLayout.DEFAULT_CONVERSION_PATTERN);
}
- public void test9() throws Exception {
- PropertyConfigurator.configure("input/pattern/patternLayout9.properties");
- common();
-
- ControlFilter cf1 =
- new ControlFilter(
- new String[] { PAT5, EXCEPTION1, EXCEPTION2, EXCEPTION3 });
- Transformer.transform(
- TEMP, FILTERED,
- new Filter[] {
- cf1, new LineNumberFilter(), new SunReflectFilter(),
- new JunitTestRunnerFilter()
- });
- assertTrue(Compare.compare(FILTERED, "witness/pattern/patternLayout.9"));
+ /**
+ * Tests DEFAULT_CONVERSION_PATTERN constant.
+ */
+ public void testTTCCConversionPattern() {
+ assertEquals(
+ "%r [%t] %p %c %x - %m%n", PatternLayout.TTCC_CONVERSION_PATTERN);
}
- public void test10() throws Exception {
- PropertyConfigurator.configure("input/pattern/patternLayout10.properties");
- common();
-
- ControlFilter cf1 =
- new ControlFilter(
- new String[] { PAT6, EXCEPTION1, EXCEPTION2, EXCEPTION3 });
- Transformer.transform(
- TEMP, FILTERED,
- new Filter[] {
- cf1, new LineNumberFilter(), new SunReflectFilter(),
- new JunitTestRunnerFilter()
- });
- assertTrue(Compare.compare(FILTERED, "witness/pattern/patternLayout.10"));
- }
+ /**
+ * Tests buffer downsizing code path.
+ */
+ public void testFormatResize() {
+ Logger logger = Logger.getLogger("org.apache.log4j.xml.PatternLayoutTest");
+ NDC.clear();
- public void test11() throws Exception {
- PropertyConfigurator.configure("input/pattern/patternLayout11.properties");
- common();
-
- ControlFilter cf1 =
- new ControlFilter(
- new String[] { PAT11a, PAT11b, EXCEPTION1, EXCEPTION2, EXCEPTION3 });
- Transformer.transform(
- TEMP, FILTERED,
- new Filter[] {
- cf1, new LineNumberFilter(), new SunReflectFilter(),
- new JunitTestRunnerFilter()
- });
- assertTrue(Compare.compare(FILTERED, "witness/pattern/patternLayout.11"));
- }
+ char[] msg = new char[2000];
- public void test12() throws Exception {
- PropertyConfigurator.configure("input/pattern/patternLayout12.properties");
- common();
-
- ControlFilter cf1 =
- new ControlFilter(
- new String[] { PAT12, EXCEPTION1, EXCEPTION2, EXCEPTION3 });
- Transformer.transform(
- TEMP, FILTERED,
- new Filter[] {
- cf1, new LineNumberFilter(), new SunReflectFilter(),
- new JunitTestRunnerFilter()
- });
- assertTrue(Compare.compare(FILTERED, "witness/pattern/patternLayout.12"));
- }
+ for (int i = 0; i < msg.length; i++) {
+ msg[i] = 'A';
+ }
- public void test13() throws Exception {
- PropertyConfigurator.configure("input/pattern/patternLayout13.properties");
- common();
-
- ControlFilter cf1 =
- new ControlFilter(
- new String[] { PAT13, EXCEPTION1, EXCEPTION2, EXCEPTION3 });
- Transformer.transform(
- TEMP, FILTERED,
- new Filter[] {
- cf1, new LineNumberFilter(), new SunReflectFilter(),
- new JunitTestRunnerFilter()
- });
- assertTrue(Compare.compare(FILTERED, "witness/pattern/patternLayout.13"));
+ LoggingEvent event1 =
+ new LoggingEvent(
+ "org.apache.log4j.Logger", logger, Level.DEBUG, new String(msg), null);
+ PatternLayout layout = (PatternLayout) createLayout();
+ String result = layout.format(event1);
+ LoggingEvent event2 =
+ new LoggingEvent(
+ "org.apache.log4j.Logger", logger, Level.WARN, "Hello, World", null);
+ result = layout.format(event2);
+ assertEquals("[", result.substring(0, 1));
}
+ /**
+ * Class to ensure that protected members are still available.
+ */
+ private static final class DerivedPatternLayout extends PatternLayout {
/**
- * Test of class abbreviation.
- *
- * @throws Exception
+ * Constructs a new instance of DerivedPatternLayout.
*/
- public void test14() throws Exception {
- PropertyConfigurator.configure("input/pattern/patternLayout14.properties");
- common();
-
- Transformer.transform(
- TEMP, FILTERED,
- new Filter[] {
- new LineNumberFilter(), new SunReflectFilter(),
- new JunitTestRunnerFilter()
- });
- assertTrue(Compare.compare(FILTERED, "witness/pattern/patternLayout.14"));
+ public DerivedPatternLayout() {
}
+ /**
+ * Get BUF_SIZE.
+ * @return return initial buffer size in characters.
+ * @deprecated
+ */
+ public int getBufSize() {
+ return BUF_SIZE;
+ }
- public void testMDC1() throws Exception {
- PropertyConfigurator.configure("input/pattern/patternLayout.mdc.1.properties");
- MDC.put("key1", "va11");
- MDC.put("key2", "va12");
- logger.debug("Hello World");
- MDC.clear();
-
- Transformer.transform(
- TEMP, FILTERED,
- new Filter[] {
- new LineNumberFilter(), new SunReflectFilter(),
- new JunitTestRunnerFilter()
- });
- assertTrue(Compare.compare(FILTERED, "witness/pattern/patternLayout.mdc.1"));
- }
-
- void common() {
- int i = -1;
-
- logger.debug("Message " + ++i);
- root.debug("Message " + i);
-
- logger.info("Message " + ++i);
- root.info("Message " + i);
-
- logger.warn("Message " + ++i);
- root.warn("Message " + i);
-
- logger.error("Message " + ++i);
- root.error("Message " + i);
-
- logger.log(Level.FATAL, "Message " + ++i);
- root.log(Level.FATAL, "Message " + i);
-
- Exception e = new Exception("Just testing");
- logger.debug("Message " + ++i, e);
- logger.info("Message " + ++i, e);
- logger.warn("Message " + ++i, e);
- logger.error("Message " + ++i, e);
- logger.log(Level.FATAL, "Message " + ++i, e);
- }
-
- /**
- Test case for MDC conversion pattern. */
- public void testMDC2() throws Exception {
- String OUTPUT_FILE = "output/patternLayout.mdc.2";
- String WITNESS_FILE = "witness/pattern/patternLayout.mdc.2";
-
- String mdcMsgPattern1 = "%m : %X%n";
- String mdcMsgPattern2 = "%m : %X{key1}%n";
- String mdcMsgPattern3 = "%m : %X{key2}%n";
- String mdcMsgPattern4 = "%m : %X{key3}%n";
- String mdcMsgPattern5 = "%m : %X{key1},%X{key2},%X{key3}%n";
-
- // set up appender
- PatternLayout layout = new PatternLayout("%m%n");
- Appender appender = new FileAppender(layout, OUTPUT_FILE, false);
-
- // set appender on root and set level to debug
- root.addAppender(appender);
- root.setLevel(Level.DEBUG);
-
- // output starting message
- root.debug("starting mdc pattern test");
-
- layout.setConversionPattern(mdcMsgPattern1);
- layout.activateOptions();
- root.debug("empty mdc, no key specified in pattern");
-
- layout.setConversionPattern(mdcMsgPattern2);
- layout.activateOptions();
- root.debug("empty mdc, key1 in pattern");
-
- layout.setConversionPattern(mdcMsgPattern3);
- layout.activateOptions();
- root.debug("empty mdc, key2 in pattern");
-
- layout.setConversionPattern(mdcMsgPattern4);
- layout.activateOptions();
- root.debug("empty mdc, key3 in pattern");
-
- layout.setConversionPattern(mdcMsgPattern5);
- layout.activateOptions();
- root.debug("empty mdc, key1, key2, and key3 in pattern");
-
- MDC.put("key1", "value1");
- MDC.put("key2", "value2");
-
- layout.setConversionPattern(mdcMsgPattern1);
- layout.activateOptions();
- root.debug("filled mdc, no key specified in pattern");
-
- layout.setConversionPattern(mdcMsgPattern2);
- layout.activateOptions();
- root.debug("filled mdc, key1 in pattern");
-
- layout.setConversionPattern(mdcMsgPattern3);
- layout.activateOptions();
- root.debug("filled mdc, key2 in pattern");
-
- layout.setConversionPattern(mdcMsgPattern4);
- layout.activateOptions();
- root.debug("filled mdc, key3 in pattern");
-
- layout.setConversionPattern(mdcMsgPattern5);
- layout.activateOptions();
- root.debug("filled mdc, key1, key2, and key3 in pattern");
-
- MDC.remove("key1");
- MDC.remove("key2");
-
- layout.setConversionPattern("%m%n");
- layout.activateOptions();
- root.debug("finished mdc pattern test");
-
- assertTrue(Compare.compare(OUTPUT_FILE, WITNESS_FILE));
- }
-
- public static Test XXXsuite() {
- TestSuite suite = new TestSuite();
- suite.addTest(new PatternLayoutTest("test1"));
-
- suite.addTest(new PatternLayoutTest("test2"));
- suite.addTest(new PatternLayoutTest("test3"));
- suite.addTest(new PatternLayoutTest("test4"));
- suite.addTest(new PatternLayoutTest("test5"));
- suite.addTest(new PatternLayoutTest("test6"));
- suite.addTest(new PatternLayoutTest("test7"));
- suite.addTest(new PatternLayoutTest("test8"));
- suite.addTest(new PatternLayoutTest("test9"));
- suite.addTest(new PatternLayoutTest("test10"));
- suite.addTest(new PatternLayoutTest("test11"));
- suite.addTest(new PatternLayoutTest("test12"));
- suite.addTest(new PatternLayoutTest("test13"));
- suite.addTest(new PatternLayoutTest("test14"));
- suite.addTest(new PatternLayoutTest("testMDC1"));
- suite.addTest(new PatternLayoutTest("testMDC2"));
-
- return suite;
+ /**
+ * Get MAX_CAPACITY.
+ * @return maximum capacity in characters.
+ * @deprecated
+ */
+ public int getMaxCapacity() {
+ return MAX_CAPACITY;
+ }
}
}
Added: logging/log4j/trunk/tests/src/java/org/apache/log4j/PatternLayoutTestCase.java
URL: http://svn.apache.org/viewcvs/logging/log4j/trunk/tests/src/java/org/apache/log4j/PatternLayoutTestCase.java?rev=359667&view=auto
==============================================================================
--- logging/log4j/trunk/tests/src/java/org/apache/log4j/PatternLayoutTestCase.java (added)
+++ logging/log4j/trunk/tests/src/java/org/apache/log4j/PatternLayoutTestCase.java Wed Dec 28 15:57:41 2005
@@ -0,0 +1,449 @@
+/*
+ * Copyright 1999,2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.log4j;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.log4j.util.AbsoluteDateAndTimeFilter;
+import org.apache.log4j.util.AbsoluteTimeFilter;
+import org.apache.log4j.util.Compare;
+import org.apache.log4j.util.ControlFilter;
+import org.apache.log4j.util.Filter;
+import org.apache.log4j.util.ISO8601Filter;
+import org.apache.log4j.util.JunitTestRunnerFilter;
+import org.apache.log4j.util.LineNumberFilter;
+import org.apache.log4j.util.RelativeTimeFilter;
+import org.apache.log4j.util.SunReflectFilter;
+import org.apache.log4j.util.Transformer;
+
+
+public class PatternLayoutTestCase extends TestCase {
+ static String TEMP = "output/temp";
+ static String FILTERED = "output/filtered";
+ static String EXCEPTION1 = "java.lang.Exception: Just testing";
+ static String EXCEPTION2 = "\\s*at .*\\(.*:\\d{1,4}\\)";
+ static String EXCEPTION3 = "\\s*at .*\\(Native Method\\)";
+ static String PAT0 =
+ "\\[main]\\ (DEBUG|INFO|WARN|ERROR|FATAL) .* - Message \\d{1,2}";
+ static String PAT1 = Filter.ISO8601_PAT + " " + PAT0;
+ static String PAT2 = Filter.ABSOLUTE_DATE_AND_TIME_PAT + " " + PAT0;
+ static String PAT3 = Filter.ABSOLUTE_TIME_PAT + " " + PAT0;
+ static String PAT4 = Filter.RELATIVE_TIME_PAT + " " + PAT0;
+ static String PAT5 =
+ "\\[main]\\ (DEBUG|INFO|WARN|ERROR|FATAL) .* : Message \\d{1,2}";
+ static String PAT6 =
+ "\\[main]\\ (DEBUG|INFO |WARN |ERROR|FATAL) org.apache.log4j.PatternLayoutTest.common\\(PatternLayoutTest.java:\\d{1,4}\\): Message \\d{1,2}";
+ static String PAT11a =
+ "^(DEBUG|INFO |WARN |ERROR|FATAL) \\[main]\\ log4j.PatternLayoutTest: Message \\d{1,2}";
+ static String PAT11b =
+ "^(DEBUG|INFO |WARN |ERROR|FATAL) \\[main]\\ root: Message \\d{1,2}";
+ static String PAT12 =
+ "^\\[main]\\ (DEBUG|INFO |WARN |ERROR|FATAL) "
+ + "org.apache.log4j.PatternLayoutTest.common\\(PatternLayoutTest.java:\\d{3}\\): "
+ + "Message \\d{1,2}";
+ static String PAT13 =
+ "^\\[main]\\ (DEBUG|INFO |WARN |ERROR|FATAL) "
+ + "apache.log4j.PatternLayoutTest.common\\(PatternLayoutTest.java:\\d{3}\\): "
+ + "Message \\d{1,2}";
+ static String PAT14 =
+ "^(DEBUG| INFO| WARN|ERROR|FATAL)\\ \\d{1,2}\\ *- Message \\d{1,2}";
+ static String PAT_MDC_1 = "";
+ Logger root;
+ Logger logger;
+
+ public PatternLayoutTestCase(final String name) {
+ super(name);
+ }
+
+ public void setUp() {
+ root = Logger.getRootLogger();
+ logger = Logger.getLogger(PatternLayoutTest.class);
+ }
+
+ public void tearDown() {
+ root.getLoggerRepository().resetConfiguration();
+ }
+
+ public void test1() throws Exception {
+ PropertyConfigurator.configure("input/pattern/patternLayout1.properties");
+ common();
+ Transformer.transform(
+ TEMP, FILTERED,
+ new Filter[] {
+ new LineNumberFilter(), new SunReflectFilter(),
+ new JunitTestRunnerFilter()
+ });
+ assertTrue(Compare.compare(FILTERED, "witness/pattern/patternLayout.1"));
+ }
+
+ public void test2() throws Exception {
+ PropertyConfigurator.configure("input/pattern/patternLayout2.properties");
+ common();
+
+ ControlFilter cf1 =
+ new ControlFilter(
+ new String[] { PAT1, EXCEPTION1, EXCEPTION2, EXCEPTION3 });
+ Transformer.transform(
+ TEMP, FILTERED,
+ new Filter[] {
+ cf1, new LineNumberFilter(), new ISO8601Filter(),
+ new SunReflectFilter(), new JunitTestRunnerFilter()
+ });
+ assertTrue(Compare.compare(FILTERED, "witness/pattern/patternLayout.2"));
+ }
+
+ public void test3() throws Exception {
+ PropertyConfigurator.configure("input/pattern/patternLayout3.properties");
+ common();
+
+ ControlFilter cf1 =
+ new ControlFilter(
+ new String[] { PAT1, EXCEPTION1, EXCEPTION2, EXCEPTION3 });
+ Transformer.transform(
+ TEMP, FILTERED,
+ new Filter[] {
+ cf1, new LineNumberFilter(), new ISO8601Filter(),
+ new SunReflectFilter(), new JunitTestRunnerFilter()
+ });
+ assertTrue(Compare.compare(FILTERED, "witness/pattern/patternLayout.3"));
+ }
+
+ // Output format:
+ // 06 avr. 2002 18:30:58,937 [main] DEBUG atternLayoutTest - Message 0
+ public void test4() throws Exception {
+ PropertyConfigurator.configure("input/pattern/patternLayout4.properties");
+ common();
+
+ ControlFilter cf1 =
+ new ControlFilter(
+ new String[] { PAT2, EXCEPTION1, EXCEPTION2, EXCEPTION3 });
+ Transformer.transform(
+ TEMP, FILTERED,
+ new Filter[] {
+ cf1, new LineNumberFilter(), new AbsoluteDateAndTimeFilter(),
+ new SunReflectFilter(), new JunitTestRunnerFilter()
+ });
+ assertTrue(Compare.compare(FILTERED, "witness/pattern/patternLayout.4"));
+ }
+
+ public void test5() throws Exception {
+ PropertyConfigurator.configure("input/pattern/patternLayout5.properties");
+ common();
+
+ ControlFilter cf1 =
+ new ControlFilter(
+ new String[] { PAT2, EXCEPTION1, EXCEPTION2, EXCEPTION3 });
+ Transformer.transform(
+ TEMP, FILTERED,
+ new Filter[] {
+ cf1, new LineNumberFilter(), new AbsoluteDateAndTimeFilter(),
+ new SunReflectFilter(), new JunitTestRunnerFilter()
+ });
+ assertTrue(Compare.compare(FILTERED, "witness/pattern/patternLayout.5"));
+ }
+
+ // 18:54:19,201 [main] DEBUG atternLayoutTest - Message 0
+ public void test6() throws Exception {
+ PropertyConfigurator.configure("input/pattern/patternLayout6.properties");
+ common();
+
+ ControlFilter cf1 =
+ new ControlFilter(
+ new String[] { PAT3, EXCEPTION1, EXCEPTION2, EXCEPTION3 });
+ Transformer.transform(
+ TEMP, FILTERED,
+ new Filter[] {
+ cf1, new LineNumberFilter(), new AbsoluteTimeFilter(),
+ new SunReflectFilter(), new JunitTestRunnerFilter()
+ });
+ assertTrue(Compare.compare(FILTERED, "witness/pattern/patternLayout.6"));
+ }
+
+ public void test7() throws Exception {
+ PropertyConfigurator.configure("input/pattern/patternLayout7.properties");
+ common();
+
+ ControlFilter cf1 =
+ new ControlFilter(
+ new String[] { PAT3, EXCEPTION1, EXCEPTION2, EXCEPTION3 });
+ Transformer.transform(
+ TEMP, FILTERED,
+ new Filter[] {
+ cf1, new LineNumberFilter(), new AbsoluteTimeFilter(),
+ new SunReflectFilter(), new JunitTestRunnerFilter()
+ });
+ assertTrue(Compare.compare(FILTERED, "witness/pattern/patternLayout.7"));
+ }
+
+ public void test8() throws Exception {
+ PropertyConfigurator.configure("input/pattern/patternLayout8.properties");
+ common();
+
+ ControlFilter cf1 =
+ new ControlFilter(
+ new String[] { PAT4, EXCEPTION1, EXCEPTION2, EXCEPTION3 });
+ Transformer.transform(
+ TEMP, FILTERED,
+ new Filter[] {
+ cf1, new LineNumberFilter(), new RelativeTimeFilter(),
+ new SunReflectFilter(), new JunitTestRunnerFilter()
+ });
+ assertTrue(Compare.compare(FILTERED, "witness/pattern/patternLayout.8"));
+ }
+
+ public void test9() throws Exception {
+ PropertyConfigurator.configure("input/pattern/patternLayout9.properties");
+ common();
+
+ ControlFilter cf1 =
+ new ControlFilter(
+ new String[] { PAT5, EXCEPTION1, EXCEPTION2, EXCEPTION3 });
+ Transformer.transform(
+ TEMP, FILTERED,
+ new Filter[] {
+ cf1, new LineNumberFilter(), new SunReflectFilter(),
+ new JunitTestRunnerFilter()
+ });
+ assertTrue(Compare.compare(FILTERED, "witness/pattern/patternLayout.9"));
+ }
+
+ public void test10() throws Exception {
+ PropertyConfigurator.configure("input/pattern/patternLayout10.properties");
+ common();
+
+ ControlFilter cf1 =
+ new ControlFilter(
+ new String[] { PAT6, EXCEPTION1, EXCEPTION2, EXCEPTION3 });
+ Transformer.transform(
+ TEMP, FILTERED,
+ new Filter[] {
+ cf1, new LineNumberFilter(), new SunReflectFilter(),
+ new JunitTestRunnerFilter()
+ });
+ assertTrue(Compare.compare(FILTERED, "witness/pattern/patternLayout.10"));
+ }
+
+ public void test11() throws Exception {
+ PropertyConfigurator.configure("input/pattern/patternLayout11.properties");
+ common();
+
+ ControlFilter cf1 =
+ new ControlFilter(
+ new String[] { PAT11a, PAT11b, EXCEPTION1, EXCEPTION2, EXCEPTION3 });
+ Transformer.transform(
+ TEMP, FILTERED,
+ new Filter[] {
+ cf1, new LineNumberFilter(), new SunReflectFilter(),
+ new JunitTestRunnerFilter()
+ });
+ assertTrue(Compare.compare(FILTERED, "witness/pattern/patternLayout.11"));
+ }
+
+ public void test12() throws Exception {
+ PropertyConfigurator.configure("input/pattern/patternLayout12.properties");
+ common();
+
+ ControlFilter cf1 =
+ new ControlFilter(
+ new String[] { PAT12, EXCEPTION1, EXCEPTION2, EXCEPTION3 });
+ Transformer.transform(
+ TEMP, FILTERED,
+ new Filter[] {
+ cf1, new LineNumberFilter(), new SunReflectFilter(),
+ new JunitTestRunnerFilter()
+ });
+ assertTrue(Compare.compare(FILTERED, "witness/pattern/patternLayout.12"));
+ }
+
+ public void test13() throws Exception {
+ PropertyConfigurator.configure("input/pattern/patternLayout13.properties");
+ common();
+
+ ControlFilter cf1 =
+ new ControlFilter(
+ new String[] { PAT13, EXCEPTION1, EXCEPTION2, EXCEPTION3 });
+ Transformer.transform(
+ TEMP, FILTERED,
+ new Filter[] {
+ cf1, new LineNumberFilter(), new SunReflectFilter(),
+ new JunitTestRunnerFilter()
+ });
+ assertTrue(Compare.compare(FILTERED, "witness/pattern/patternLayout.13"));
+ }
+
+ /**
+ * Test of class abbreviation.
+ *
+ * @throws Exception
+ */
+ public void test14() throws Exception {
+ PropertyConfigurator.configure("input/pattern/patternLayout14.properties");
+ common();
+
+ Transformer.transform(
+ TEMP, FILTERED,
+ new Filter[] {
+ new LineNumberFilter(), new SunReflectFilter(),
+ new JunitTestRunnerFilter()
+ });
+ assertTrue(Compare.compare(FILTERED, "witness/pattern/patternLayout.14"));
+ }
+
+
+ public void testMDC1() throws Exception {
+ PropertyConfigurator.configure("input/pattern/patternLayout.mdc.1.properties");
+ MDC.put("key1", "va11");
+ MDC.put("key2", "va12");
+ logger.debug("Hello World");
+ MDC.clear();
+
+ Transformer.transform(
+ TEMP, FILTERED,
+ new Filter[] {
+ new LineNumberFilter(), new SunReflectFilter(),
+ new JunitTestRunnerFilter()
+ });
+ assertTrue(Compare.compare(FILTERED, "witness/pattern/patternLayout.mdc.1"));
+ }
+
+ void common() {
+ int i = -1;
+
+ logger.debug("Message " + ++i);
+ root.debug("Message " + i);
+
+ logger.info("Message " + ++i);
+ root.info("Message " + i);
+
+ logger.warn("Message " + ++i);
+ root.warn("Message " + i);
+
+ logger.error("Message " + ++i);
+ root.error("Message " + i);
+
+ logger.log(Level.FATAL, "Message " + ++i);
+ root.log(Level.FATAL, "Message " + i);
+
+ Exception e = new Exception("Just testing");
+ logger.debug("Message " + ++i, e);
+ logger.info("Message " + ++i, e);
+ logger.warn("Message " + ++i, e);
+ logger.error("Message " + ++i, e);
+ logger.log(Level.FATAL, "Message " + ++i, e);
+ }
+
+ /**
+ Test case for MDC conversion pattern. */
+ public void testMDC2() throws Exception {
+ String OUTPUT_FILE = "output/patternLayout.mdc.2";
+ String WITNESS_FILE = "witness/pattern/patternLayout.mdc.2";
+
+ String mdcMsgPattern1 = "%m : %X%n";
+ String mdcMsgPattern2 = "%m : %X{key1}%n";
+ String mdcMsgPattern3 = "%m : %X{key2}%n";
+ String mdcMsgPattern4 = "%m : %X{key3}%n";
+ String mdcMsgPattern5 = "%m : %X{key1},%X{key2},%X{key3}%n";
+
+ // set up appender
+ PatternLayout layout = new PatternLayout("%m%n");
+ Appender appender = new FileAppender(layout, OUTPUT_FILE, false);
+
+ // set appender on root and set level to debug
+ root.addAppender(appender);
+ root.setLevel(Level.DEBUG);
+
+ // output starting message
+ root.debug("starting mdc pattern test");
+
+ layout.setConversionPattern(mdcMsgPattern1);
+ layout.activateOptions();
+ root.debug("empty mdc, no key specified in pattern");
+
+ layout.setConversionPattern(mdcMsgPattern2);
+ layout.activateOptions();
+ root.debug("empty mdc, key1 in pattern");
+
+ layout.setConversionPattern(mdcMsgPattern3);
+ layout.activateOptions();
+ root.debug("empty mdc, key2 in pattern");
+
+ layout.setConversionPattern(mdcMsgPattern4);
+ layout.activateOptions();
+ root.debug("empty mdc, key3 in pattern");
+
+ layout.setConversionPattern(mdcMsgPattern5);
+ layout.activateOptions();
+ root.debug("empty mdc, key1, key2, and key3 in pattern");
+
+ MDC.put("key1", "value1");
+ MDC.put("key2", "value2");
+
+ layout.setConversionPattern(mdcMsgPattern1);
+ layout.activateOptions();
+ root.debug("filled mdc, no key specified in pattern");
+
+ layout.setConversionPattern(mdcMsgPattern2);
+ layout.activateOptions();
+ root.debug("filled mdc, key1 in pattern");
+
+ layout.setConversionPattern(mdcMsgPattern3);
+ layout.activateOptions();
+ root.debug("filled mdc, key2 in pattern");
+
+ layout.setConversionPattern(mdcMsgPattern4);
+ layout.activateOptions();
+ root.debug("filled mdc, key3 in pattern");
+
+ layout.setConversionPattern(mdcMsgPattern5);
+ layout.activateOptions();
+ root.debug("filled mdc, key1, key2, and key3 in pattern");
+
+ MDC.remove("key1");
+ MDC.remove("key2");
+
+ layout.setConversionPattern("%m%n");
+ layout.activateOptions();
+ root.debug("finished mdc pattern test");
+
+ assertTrue(Compare.compare(OUTPUT_FILE, WITNESS_FILE));
+ }
+
+ public static Test XXXsuite() {
+ TestSuite suite = new TestSuite();
+ suite.addTest(new PatternLayoutTest("test1"));
+
+ suite.addTest(new PatternLayoutTest("test2"));
+ suite.addTest(new PatternLayoutTest("test3"));
+ suite.addTest(new PatternLayoutTest("test4"));
+ suite.addTest(new PatternLayoutTest("test5"));
+ suite.addTest(new PatternLayoutTest("test6"));
+ suite.addTest(new PatternLayoutTest("test7"));
+ suite.addTest(new PatternLayoutTest("test8"));
+ suite.addTest(new PatternLayoutTest("test9"));
+ suite.addTest(new PatternLayoutTest("test10"));
+ suite.addTest(new PatternLayoutTest("test11"));
+ suite.addTest(new PatternLayoutTest("test12"));
+ suite.addTest(new PatternLayoutTest("test13"));
+ suite.addTest(new PatternLayoutTest("test14"));
+ suite.addTest(new PatternLayoutTest("testMDC1"));
+ suite.addTest(new PatternLayoutTest("testMDC2"));
+
+ return suite;
+ }
+}
Added: logging/log4j/trunk/tests/src/java/org/apache/log4j/TTCCLayoutTest.java
URL: http://svn.apache.org/viewcvs/logging/log4j/trunk/tests/src/java/org/apache/log4j/TTCCLayoutTest.java?rev=359667&view=auto
==============================================================================
--- logging/log4j/trunk/tests/src/java/org/apache/log4j/TTCCLayoutTest.java (added)
+++ logging/log4j/trunk/tests/src/java/org/apache/log4j/TTCCLayoutTest.java Wed Dec 28 15:57:41 2005
@@ -0,0 +1,116 @@
+/*
+ * Copyright 1999,2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.log4j;
+
+import org.apache.log4j.spi.LoggingEvent;
+
+
+/**
+ * Test for TTCCLayout.
+ * @deprecated Since TTCCLayoutTest is deprecated
+ *
+ * @author Curt Arnold
+ */
+public class TTCCLayoutTest extends org.apache.log4j.helpers.DateLayoutTest {
+ /**
+ * Construct new instance of TTCCLayoutTest.
+ *
+ * @param testName test name.
+ */
+ public TTCCLayoutTest(final String testName) {
+ super(testName, "text/plain", true, null, null);
+ }
+
+ /**
+ * @{inheritDoc}
+ * @deprecated since TTCCLayout is deprecated.
+ */
+ protected Layout createLayout() {
+ return new TTCCLayout();
+ }
+
+ /**
+ * Tests format.
+ * @deprecated since TTCCLayout is deprecated.
+ */
+ public void testFormat() {
+ NDC.clear();
+ NDC.push("NDC goes here");
+
+ Logger logger = Logger.getLogger("org.apache.log4j.LayoutTest");
+ LoggingEvent event =
+ new LoggingEvent(
+ "org.apache.log4j.Logger", logger, Level.INFO, "Hello, World", null);
+ TTCCLayout layout = (TTCCLayout) createLayout();
+ String result = layout.format(event);
+ NDC.pop();
+
+ StringBuffer buf = new StringBuffer(100);
+ layout.dateFormat(buf, event);
+ buf.append('[');
+ buf.append(event.getThreadName());
+ buf.append("] ");
+ buf.append(event.getLevel().toString());
+ buf.append(' ');
+ buf.append(event.getLoggerName());
+ buf.append(' ');
+ buf.append("NDC goes here");
+ buf.append(" - ");
+ buf.append(event.getMessage());
+ buf.append(System.getProperty("line.separator"));
+ assertEquals(buf.toString(), result);
+ }
+
+ /**
+ * Tests getThreadPrinting and setThreadPrinting.
+ * @deprecated since TTCCLayout is deprecated.
+ */
+ public void testGetSetThreadPrinting() {
+ TTCCLayout layout = new TTCCLayout();
+ assertEquals(true, layout.getThreadPrinting());
+ layout.setThreadPrinting(false);
+ assertEquals(false, layout.getThreadPrinting());
+ layout.setThreadPrinting(true);
+ assertEquals(true, layout.getThreadPrinting());
+ }
+
+ /**
+ * Tests getCategoryPrefixing and setCategoryPrefixing.
+ * @deprecated since TTCCLayout is deprecated.
+ */
+ public void testGetSetCategoryPrefixing() {
+ TTCCLayout layout = new TTCCLayout();
+ assertEquals(true, layout.getCategoryPrefixing());
+ layout.setCategoryPrefixing(false);
+ assertEquals(false, layout.getCategoryPrefixing());
+ layout.setCategoryPrefixing(true);
+ assertEquals(true, layout.getCategoryPrefixing());
+ }
+
+ /**
+ * Tests getContextPrinting and setContextPrinting.
+ * @deprecated since TTCCLayout is deprecated.
+ */
+ public void testGetSetContextPrinting() {
+ TTCCLayout layout = new TTCCLayout();
+ assertEquals(true, layout.getContextPrinting());
+ layout.setContextPrinting(false);
+ assertEquals(false, layout.getContextPrinting());
+ layout.setContextPrinting(true);
+ assertEquals(true, layout.getContextPrinting());
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org