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/23 08:19:12 UTC
svn commit: r358758 - in
/logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j:
CoreTestSuite.java HTMLLayoutTest.java LayoutTest.java
PatternLayoutTest.java TTCCLayoutTest.java helpers/DateLayoutTest.java
xml/XMLLayoutTest.java
Author: carnold
Date: Thu Dec 22 23:19:08 2005
New Revision: 358758
URL: http://svn.apache.org/viewcvs?rev=358758&view=rev
Log:
Bug 38024: Additional tests for Layout classes
Added:
logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/HTMLLayoutTest.java
logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/LayoutTest.java
logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/PatternLayoutTest.java
logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/TTCCLayoutTest.java
logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/helpers/DateLayoutTest.java
logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/xml/XMLLayoutTest.java
Modified:
logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/CoreTestSuite.java
Modified: logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/CoreTestSuite.java
URL: http://svn.apache.org/viewcvs/logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/CoreTestSuite.java?rev=358758&r1=358757&r2=358758&view=diff
==============================================================================
--- logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/CoreTestSuite.java (original)
+++ logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/CoreTestSuite.java Thu Dec 22 23:19:08 2005
@@ -37,6 +37,12 @@
s.addTestSuite(org.apache.log4j.FileAppenderTest.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/branches/v1_2-branch/tests/src/java/org/apache/log4j/HTMLLayoutTest.java
URL: http://svn.apache.org/viewcvs/logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/HTMLLayoutTest.java?rev=358758&view=auto
==============================================================================
--- logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/HTMLLayoutTest.java (added)
+++ logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/HTMLLayoutTest.java Thu Dec 22 23:19:08 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>",
+ result.substring(0, Layout.LINE_SEP.length() + 4));
+ }
+}
Added: logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/LayoutTest.java
URL: http://svn.apache.org/viewcvs/logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/LayoutTest.java?rev=358758&view=auto
==============================================================================
--- logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/LayoutTest.java (added)
+++ logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/LayoutTest.java Thu Dec 22 23:19:08 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;
+ }
+ }
+}
Added: logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/PatternLayoutTest.java
URL: http://svn.apache.org/viewcvs/logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/PatternLayoutTest.java?rev=358758&view=auto
==============================================================================
--- logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/PatternLayoutTest.java (added)
+++ logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/PatternLayoutTest.java Thu Dec 22 23:19:08 2005
@@ -0,0 +1,141 @@
+/*
+ * 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 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);
+ }
+
+ /**
+ * @{inheritDoc}
+ */
+ protected Layout createLayout() {
+ return new PatternLayout("[%t] %p %c - %m%n");
+ }
+
+ /**
+ * 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);
+ }
+
+ /**
+ * Tests getPatternFormat().
+ */
+ public void testGetPatternFormat() {
+ PatternLayout layout = (PatternLayout) createLayout();
+ assertEquals("[%t] %p %c - %m%n", layout.getConversionPattern());
+ }
+
+ /**
+ * Tests DEFAULT_CONVERSION_PATTERN constant.
+ */
+ public void testDefaultConversionPattern() {
+ assertEquals("%m%n", PatternLayout.DEFAULT_CONVERSION_PATTERN);
+ }
+
+ /**
+ * Tests DEFAULT_CONVERSION_PATTERN constant.
+ */
+ public void testTTCCConversionPattern() {
+ assertEquals(
+ "%r [%t] %p %c %x - %m%n", PatternLayout.TTCC_CONVERSION_PATTERN);
+ }
+
+ /**
+ * Tests buffer downsizing code path.
+ */
+ public void testFormatResize() {
+ Logger logger = Logger.getLogger("org.apache.log4j.xml.PatternLayoutTest");
+ 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);
+ 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 {
+ /**
+ * Constructs a new instance of DerivedPatternLayout.
+ */
+ public DerivedPatternLayout() {
+ }
+
+ /**
+ * Get BUF_SIZE.
+ * @return return initial buffer size in characters.
+ */
+ public int getBufSize() {
+ return BUF_SIZE;
+ }
+
+ /**
+ * Get MAX_CAPACITY.
+ * @return maximum capacity in characters.
+ */
+ public int getMaxCapacity() {
+ return MAX_CAPACITY;
+ }
+ }
+}
Added: logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/TTCCLayoutTest.java
URL: http://svn.apache.org/viewcvs/logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/TTCCLayoutTest.java?rev=358758&view=auto
==============================================================================
--- logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/TTCCLayoutTest.java (added)
+++ logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/TTCCLayoutTest.java Thu Dec 22 23:19:08 2005
@@ -0,0 +1,111 @@
+/*
+ * 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.helpers.DateLayoutTest;
+import org.apache.log4j.spi.LoggingEvent;
+
+
+/**
+ * Test for TTCCLayout.
+ *
+ * @author Curt Arnold
+ */
+public class TTCCLayoutTest extends DateLayoutTest {
+ /**
+ * Construct new instance of TTCCLayoutTest.
+ *
+ * @param testName test name.
+ */
+ public TTCCLayoutTest(final String testName) {
+ super(testName, "text/plain", true, null, null);
+ }
+
+ /**
+ * @{inheritDoc}
+ */
+ protected Layout createLayout() {
+ return new TTCCLayout();
+ }
+
+ /**
+ * Tests format.
+ */
+ 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.
+ */
+ 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.
+ */
+ 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.
+ */
+ 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());
+ }
+}
Added: logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/helpers/DateLayoutTest.java
URL: http://svn.apache.org/viewcvs/logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/helpers/DateLayoutTest.java?rev=358758&view=auto
==============================================================================
--- logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/helpers/DateLayoutTest.java (added)
+++ logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/helpers/DateLayoutTest.java Thu Dec 22 23:19:08 2005
@@ -0,0 +1,254 @@
+/*
+ * 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.helpers;
+
+import org.apache.log4j.Layout;
+import org.apache.log4j.LayoutTest;
+import org.apache.log4j.spi.LoggingEvent;
+
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+
+import java.util.TimeZone;
+
+
+/**
+ * Tests for DateLayout.
+ *
+ */
+public class DateLayoutTest extends LayoutTest {
+ /**
+ * Construct a new instance of LayoutTest.
+ * @param testName test name.
+ */
+ public DateLayoutTest(final String testName) {
+ super(testName);
+ }
+
+ /**
+ * 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 DateLayoutTest(
+ final String testName, final String expectedContentType,
+ final boolean expectedIgnoresThrowable, final String expectedHeader,
+ final String expectedFooter) {
+ super(
+ testName, expectedContentType, expectedIgnoresThrowable, expectedHeader,
+ expectedFooter);
+ }
+
+ /**
+ * @{inheritDoc}
+ */
+ protected Layout createLayout() {
+ return new MockLayout();
+ }
+
+ /**
+ * Tests DateLayout.NULL_DATE_FORMAT constant.
+ */
+ public void testNullDateFormat() {
+ assertEquals("NULL", DateLayout.NULL_DATE_FORMAT);
+ }
+
+ /**
+ * Tests DateLayout.RELATIVE constant.
+ */
+ public void testRelativeTimeDateFormat() {
+ assertEquals("RELATIVE", DateLayout.RELATIVE_TIME_DATE_FORMAT);
+ }
+
+ /**
+ * Tests DateLayout.DATE_FORMAT_OPTION constant.
+ * @deprecated since constant is deprecated
+ */
+ public void testDateFormatOption() {
+ assertEquals("DateFormat", DateLayout.DATE_FORMAT_OPTION);
+ }
+
+ /**
+ * Tests DateLayout.TIMEZONE_OPTION constant.
+ * @deprecated since constant is deprecated
+ */
+ public void testTimeZoneOption() {
+ assertEquals("TimeZone", DateLayout.TIMEZONE_OPTION);
+ }
+
+ /**
+ * Tests getOptionStrings().
+ * @deprecated since getOptionStrings is deprecated.
+ *
+ */
+ public void testGetOptionStrings() {
+ String[] options = ((DateLayout) createLayout()).getOptionStrings();
+ assertEquals(2, options.length);
+ }
+
+ /**
+ * Tests setting DateFormat through setOption method.
+ * @deprecated since setOption is deprecated.
+ */
+ public void testSetOptionDateFormat() {
+ DateLayout layout = (DateLayout) createLayout();
+ layout.setOption("dAtefOrmat", "foobar");
+ assertEquals("FOOBAR", layout.getDateFormat());
+ }
+
+ /**
+ * Tests setting TimeZone through setOption method.
+ * @deprecated since setOption is deprecated.
+ */
+ public void testSetOptionTimeZone() {
+ DateLayout layout = (DateLayout) createLayout();
+ layout.setOption("tImezOne", "+05:00");
+ assertEquals("+05:00", layout.getTimeZone());
+ }
+
+ /**
+ * Tests setDateFormat.
+ */
+ public void testSetDateFormat() {
+ DateLayout layout = (DateLayout) createLayout();
+ layout.setDateFormat("ABSOLUTE");
+ assertEquals("ABSOLUTE", layout.getDateFormat());
+ }
+
+ /**
+ * Tests setTimeZone.
+ */
+ public void testSetTimeZone() {
+ DateLayout layout = (DateLayout) createLayout();
+ layout.setTimeZone("+05:00");
+ assertEquals("+05:00", layout.getTimeZone());
+ }
+
+ /**
+ * Tests 2 parameter setDateFormat with null.
+ */
+ public void testSetDateFormatNull() {
+ DateLayout layout = (DateLayout) createLayout();
+ layout.setDateFormat((String) null, null);
+ }
+
+ /**
+ * Tests 2 parameter setDateFormat with "NULL".
+ */
+ public void testSetDateFormatNullString() {
+ DateLayout layout = (DateLayout) createLayout();
+ layout.setDateFormat("NuLL", null);
+ }
+
+ /**
+ * Tests 2 parameter setDateFormat with "RELATIVE".
+ */
+ public void testSetDateFormatRelative() {
+ DateLayout layout = (DateLayout) createLayout();
+ layout.setDateFormat("rElatIve", TimeZone.getDefault());
+ }
+
+ /**
+ * Tests 2 parameter setDateFormat with "ABSOLUTE".
+ */
+ public void testSetDateFormatAbsolute() {
+ DateLayout layout = (DateLayout) createLayout();
+ layout.setDateFormat("aBsolUte", TimeZone.getDefault());
+ }
+
+ /**
+ * Tests 2 parameter setDateFormat with "DATETIME".
+ */
+ public void testSetDateFormatDateTime() {
+ DateLayout layout = (DateLayout) createLayout();
+ layout.setDateFormat("dAte", TimeZone.getDefault());
+ }
+
+ /**
+ * Tests 2 parameter setDateFormat with "ISO8601".
+ */
+ public void testSetDateFormatISO8601() {
+ DateLayout layout = (DateLayout) createLayout();
+ layout.setDateFormat("iSo8601", TimeZone.getDefault());
+ }
+
+ /**
+ * Tests 2 parameter setDateFormat with "HH:mm:ss".
+ */
+ public void testSetDateFormatSimple() {
+ DateLayout layout = (DateLayout) createLayout();
+ layout.setDateFormat("HH:mm:ss", TimeZone.getDefault());
+ }
+
+ /**
+ * Tests activateOptions.
+ */
+ public void testActivateOptions() {
+ DateLayout layout = (DateLayout) createLayout();
+ layout.setDateFormat("HH:mm:ss");
+ layout.setTimeZone("+05:00");
+ layout.activateOptions();
+ }
+
+ /**
+ * Tests setDateFormat(DateFormat, TimeZone).
+ */
+ public void testSetDateFormatWithFormat() {
+ DateFormat format = new SimpleDateFormat("HH:mm");
+ DateLayout layout = (DateLayout) createLayout();
+ layout.setDateFormat(format, TimeZone.getDefault());
+ }
+
+ /**
+ * Concrete Layout class for tests.
+ */
+ private static final class MockLayout extends DateLayout {
+ /**
+ * Create new instance of MockLayout.
+ */
+ public MockLayout() {
+ //
+ // checks that protected fields are properly initialized
+ assertNotNull(pos);
+ assertNotNull(date);
+ assertNull(dateFormat);
+ }
+
+ /**
+ * @{inheritDoc}
+ */
+ public String format(final LoggingEvent event) {
+ return "Mock";
+ }
+
+ /**
+ * @{inheritDoc}
+ */
+ public void activateOptions() {
+ }
+
+ /**
+ * @{inheritDoc}
+ */
+ public boolean ignoresThrowable() {
+ return true;
+ }
+ }
+}
Added: logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/xml/XMLLayoutTest.java
URL: http://svn.apache.org/viewcvs/logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/xml/XMLLayoutTest.java?rev=358758&view=auto
==============================================================================
--- logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/xml/XMLLayoutTest.java (added)
+++ logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/xml/XMLLayoutTest.java Thu Dec 22 23:19:08 2005
@@ -0,0 +1,310 @@
+/*
+ * 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.xml;
+
+import org.apache.log4j.Layout;
+import org.apache.log4j.LayoutTest;
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.apache.log4j.NDC;
+import org.apache.log4j.spi.LoggingEvent;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+
+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 XMLLayout.
+ *
+ * @author Curt Arnold
+ */
+public class XMLLayoutTest extends LayoutTest {
+ /**
+ * Construct new instance of XMLLayoutTest.
+ *
+ * @param testName test name.
+ */
+ public XMLLayoutTest(final String testName) {
+ super(testName, "text/plain", false, null, null);
+ }
+
+ /**
+ * @{inheritDoc}
+ */
+ protected Layout createLayout() {
+ return new XMLLayout();
+ }
+
+ /**
+ * 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 Element 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);
+ Document doc = builder.parse(new InputSource(reader));
+
+ return doc.getDocumentElement();
+ }
+
+ /**
+ * Checks a log4j:event element against expectations.
+ * @param element element, may not be null.
+ * @param event event, may not be null.
+ */
+ private void checkEventElement(
+ final Element element, final LoggingEvent event) {
+ assertEquals("log4j:event", element.getTagName());
+ assertEquals(
+ "org.apache.log4j.xml.XMLLayoutTest", element.getAttribute("logger"));
+ assertEquals(
+ Long.toString(event.timeStamp), element.getAttribute("timestamp"));
+ assertEquals("INFO", element.getAttribute("level"));
+ assertEquals(event.getThreadName(), element.getAttribute("thread"));
+ }
+
+ /**
+ * Checks a log4j:message element against expectations.
+ * @param element element, may not be null.
+ * @param message expected message.
+ */
+ private void checkMessageElement(
+ final Element element, final String message) {
+ assertEquals("log4j:message", element.getTagName());
+
+ Node messageNode = element.getFirstChild();
+ assertNotNull(messageNode);
+ assertEquals(Node.TEXT_NODE, messageNode.getNodeType());
+ assertEquals(message, messageNode.getNodeValue());
+ assertNull(messageNode.getNextSibling());
+ }
+
+ /**
+ * Checks a log4j:message element against expectations.
+ * @param element element, may not be null.
+ * @param message expected message.
+ */
+ private void checkNDCElement(final Element element, final String message) {
+ assertEquals("log4j:NDC", element.getTagName());
+
+ Node messageNode = element.getFirstChild();
+ assertNotNull(messageNode);
+ assertEquals(Node.TEXT_NODE, messageNode.getNodeType());
+ assertEquals(message, messageNode.getNodeValue());
+ assertNull(messageNode.getNextSibling());
+ }
+
+ /**
+ * Checks a log4j:throwable element against expectations.
+ * @param element element, may not be null.
+ * @param ex exception, may not be null.
+ */
+ private void checkThrowableElement(
+ final Element element, final Exception ex) {
+ assertEquals("log4j:throwable", element.getTagName());
+
+ Node messageNode = element.getFirstChild();
+ assertNotNull(messageNode);
+ assertEquals(Node.TEXT_NODE, messageNode.getNodeType());
+
+ String msg = ex.toString();
+ assertEquals(msg, messageNode.getNodeValue().substring(0, msg.length()));
+ assertNull(messageNode.getNextSibling());
+ }
+
+ /**
+ * Tests formatted results.
+ * @throws Exception if parser can not be constructed or source is not a valid XML document.
+ */
+ public void testFormat() throws Exception {
+ Logger logger = Logger.getLogger("org.apache.log4j.xml.XMLLayoutTest");
+ LoggingEvent event =
+ new LoggingEvent(
+ "org.apache.log4j.Logger", logger, Level.INFO, "Hello, World", null);
+ XMLLayout layout = (XMLLayout) createLayout();
+ String result = layout.format(event);
+ Element parsedResult = parse(result);
+ checkEventElement(parsedResult, event);
+
+ int childElementCount = 0;
+
+ for (
+ Node node = parsedResult.getFirstChild(); node != null;
+ node = node.getNextSibling()) {
+ switch (node.getNodeType()) {
+ case Node.ELEMENT_NODE:
+ childElementCount++;
+ checkMessageElement((Element) node, "Hello, World");
+
+ break;
+
+ case Node.COMMENT_NODE:
+ break;
+
+ case Node.TEXT_NODE:
+
+ // should only be whitespace
+ break;
+
+ default:
+ fail("Unexpected node type");
+
+ break;
+ }
+ }
+
+ assertEquals(1, childElementCount);
+ }
+
+ /**
+ * Tests formatted results with an exception.
+ * @throws Exception if parser can not be constructed or source is not a valid XML document.
+ */
+ public void testFormatWithException() throws Exception {
+ Logger logger = Logger.getLogger("org.apache.log4j.xml.XMLLayoutTest");
+ Exception ex = new IllegalArgumentException("'foo' is not a valid name");
+ LoggingEvent event =
+ new LoggingEvent(
+ "org.apache.log4j.Logger", logger, Level.INFO, "Hello, World", ex);
+ XMLLayout layout = (XMLLayout) createLayout();
+ String result = layout.format(event);
+ Element parsedResult = parse(result);
+ checkEventElement(parsedResult, event);
+
+ int childElementCount = 0;
+
+ for (
+ Node node = parsedResult.getFirstChild(); node != null;
+ node = node.getNextSibling()) {
+ switch (node.getNodeType()) {
+ case Node.ELEMENT_NODE:
+ childElementCount++;
+
+ if (childElementCount == 1) {
+ checkMessageElement((Element) node, "Hello, World");
+ } else {
+ checkThrowableElement((Element) node, ex);
+ }
+
+ break;
+
+ case Node.COMMENT_NODE:
+ break;
+
+ case Node.TEXT_NODE:
+
+ // should only be whitespace
+ break;
+
+ default:
+ fail("Unexpected node type");
+
+ break;
+ }
+ }
+
+ assertEquals(2, childElementCount);
+ }
+
+ /**
+ * Tests formatted results with an exception.
+ * @throws Exception if parser can not be constructed or source is not a valid XML document.
+ */
+ public void testFormatWithNDC() throws Exception {
+ Logger logger = Logger.getLogger("org.apache.log4j.xml.XMLLayoutTest");
+ NDC.push("NDC goes here");
+
+ LoggingEvent event =
+ new LoggingEvent(
+ "org.apache.log4j.Logger", logger, Level.INFO, "Hello, World", null);
+ XMLLayout layout = (XMLLayout) createLayout();
+ String result = layout.format(event);
+ NDC.pop();
+
+ Element parsedResult = parse(result);
+ checkEventElement(parsedResult, event);
+
+ int childElementCount = 0;
+
+ for (
+ Node node = parsedResult.getFirstChild(); node != null;
+ node = node.getNextSibling()) {
+ switch (node.getNodeType()) {
+ case Node.ELEMENT_NODE:
+ childElementCount++;
+
+ if (childElementCount == 1) {
+ checkMessageElement((Element) node, "Hello, World");
+ } else {
+ checkNDCElement((Element) node, "NDC goes here");
+ }
+
+ break;
+
+ case Node.COMMENT_NODE:
+ break;
+
+ case Node.TEXT_NODE:
+
+ // should only be whitespace
+ break;
+
+ default:
+ fail("Unexpected node type");
+
+ break;
+ }
+ }
+
+ assertEquals(2, childElementCount);
+ }
+
+ /**
+ * Tests getLocationInfo and setLocationInfo.
+ */
+ public void testGetSetLocationInfo() {
+ XMLLayout layout = new XMLLayout();
+ assertEquals(false, layout.getLocationInfo());
+ layout.setLocationInfo(true);
+ assertEquals(true, layout.getLocationInfo());
+ layout.setLocationInfo(false);
+ assertEquals(false, layout.getLocationInfo());
+ }
+
+ /**
+ * Tests activateOptions().
+ */
+ public void testActivateOptions() {
+ XMLLayout layout = new XMLLayout();
+ layout.activateOptions();
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org