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 [2/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...
Added: logging/log4j/trunk/tests/src/java/org/apache/log4j/helpers/DateLayoutTest.java
URL: http://svn.apache.org/viewcvs/logging/log4j/trunk/tests/src/java/org/apache/log4j/helpers/DateLayoutTest.java?rev=359667&view=auto
==============================================================================
--- logging/log4j/trunk/tests/src/java/org/apache/log4j/helpers/DateLayoutTest.java (added)
+++ logging/log4j/trunk/tests/src/java/org/apache/log4j/helpers/DateLayoutTest.java Wed Dec 28 15:57:41 2005
@@ -0,0 +1,268 @@
+/*
+ * 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.
+ * @deprecated since DateLayout is deprecated.
+ */
+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.
+ * @deprecated since DateLayout is deprecated.
+ */
+ public void testNullDateFormat() {
+ assertEquals("NULL", DateLayout.NULL_DATE_FORMAT);
+ }
+
+ /**
+ * Tests DateLayout.RELATIVE constant.
+ * @deprecated since DateLayout is deprecated.
+ */
+ 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.
+ * @deprecated since DateLayout is deprecated.
+ */
+ public void testSetDateFormat() {
+ DateLayout layout = (DateLayout) createLayout();
+ layout.setDateFormat("ABSOLUTE");
+ assertEquals("ABSOLUTE", layout.getDateFormat());
+ }
+
+ /**
+ * Tests setTimeZone.
+ * @deprecated since DateLayout is deprecated.
+ */
+ public void testSetTimeZone() {
+ DateLayout layout = (DateLayout) createLayout();
+ layout.setTimeZone("+05:00");
+ assertEquals("+05:00", layout.getTimeZone());
+ }
+
+ /**
+ * Tests 2 parameter setDateFormat with null.
+ * @deprecated since DateLayout is deprecated.
+ */
+ public void testSetDateFormatNull() {
+ DateLayout layout = (DateLayout) createLayout();
+ layout.setDateFormat((String) null, null);
+ }
+
+ /**
+ * Tests 2 parameter setDateFormat with "NULL".
+ * @deprecated since DateLayout is deprecated.
+ */
+ public void testSetDateFormatNullString() {
+ DateLayout layout = (DateLayout) createLayout();
+ layout.setDateFormat("NuLL", null);
+ }
+
+ /**
+ * Tests 2 parameter setDateFormat with "RELATIVE".
+ * @deprecated since DateLayout is deprecated.
+ */
+ public void testSetDateFormatRelative() {
+ DateLayout layout = (DateLayout) createLayout();
+ layout.setDateFormat("rElatIve", TimeZone.getDefault());
+ }
+
+ /**
+ * Tests 2 parameter setDateFormat with "ABSOLUTE".
+ * @deprecated since DateLayout is deprecated.
+ */
+ public void testSetDateFormatAbsolute() {
+ DateLayout layout = (DateLayout) createLayout();
+ layout.setDateFormat("aBsolUte", TimeZone.getDefault());
+ }
+
+ /**
+ * Tests 2 parameter setDateFormat with "DATETIME".
+ * @deprecated since DateLayout is deprecated.
+ */
+ public void testSetDateFormatDateTime() {
+ DateLayout layout = (DateLayout) createLayout();
+ layout.setDateFormat("dAte", TimeZone.getDefault());
+ }
+
+ /**
+ * Tests 2 parameter setDateFormat with "ISO8601".
+ * @deprecated since DateLayout is deprecated.
+ */
+ public void testSetDateFormatISO8601() {
+ DateLayout layout = (DateLayout) createLayout();
+ layout.setDateFormat("iSo8601", TimeZone.getDefault());
+ }
+
+ /**
+ * Tests 2 parameter setDateFormat with "HH:mm:ss".
+ * @deprecated since DateLayout is deprecated.
+ */
+ public void testSetDateFormatSimple() {
+ DateLayout layout = (DateLayout) createLayout();
+ layout.setDateFormat("HH:mm:ss", TimeZone.getDefault());
+ }
+
+ /**
+ * Tests activateOptions.
+ * @deprecated since DateLayout is deprecated.
+ */
+ public void testActivateOptions() {
+ DateLayout layout = (DateLayout) createLayout();
+ layout.setDateFormat("HH:mm:ss");
+ layout.setTimeZone("+05:00");
+ layout.activateOptions();
+ }
+
+ /**
+ * Tests setDateFormat(DateFormat, TimeZone).
+ * @deprecated since DateLayout is deprecated.
+ */
+ public void testSetDateFormatWithFormat() {
+ DateFormat format = new SimpleDateFormat("HH:mm");
+ DateLayout layout = (DateLayout) createLayout();
+ layout.setDateFormat(format, TimeZone.getDefault());
+ }
+
+ /**
+ * Concrete Layout class for tests.
+ * @deprecated
+ */
+ 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;
+ }
+ }
+}
Modified: logging/log4j/trunk/tests/src/java/org/apache/log4j/html/Loop.java
URL: http://svn.apache.org/viewcvs/logging/log4j/trunk/tests/src/java/org/apache/log4j/html/Loop.java?rev=359667&r1=359666&r2=359667&view=diff
==============================================================================
--- logging/log4j/trunk/tests/src/java/org/apache/log4j/html/Loop.java (original)
+++ logging/log4j/trunk/tests/src/java/org/apache/log4j/html/Loop.java Wed Dec 28 15:57:41 2005
@@ -19,6 +19,7 @@
import org.apache.log4j.FileAppender;
import org.apache.log4j.Logger;
+import org.apache.log4j.HTMLLayout;
import java.util.Random;
Added: logging/log4j/trunk/tests/src/java/org/apache/log4j/xml/XMLLayoutTest.java
URL: http://svn.apache.org/viewcvs/logging/log4j/trunk/tests/src/java/org/apache/log4j/xml/XMLLayoutTest.java?rev=359667&view=auto
==============================================================================
--- logging/log4j/trunk/tests/src/java/org/apache/log4j/xml/XMLLayoutTest.java (added)
+++ logging/log4j/trunk/tests/src/java/org/apache/log4j/xml/XMLLayoutTest.java Wed Dec 28 15:57:41 2005
@@ -0,0 +1,317 @@
+/*
+ * 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.MDC;
+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 {
+ MDC.clear();
+ NDC.clear();
+ 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 {
+ MDC.clear();
+ NDC.clear();
+ 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 {
+ MDC.clear();
+ NDC.clear();
+ 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