Index: pom.xml =================================================================== --- pom.xml (revision 1469923) +++ pom.xml (working copy) @@ -321,10 +321,18 @@ maven-checkstyle-plugin + org.apache.maven.plugins maven-compiler-plugin + 3.0 ${maven.compile.source} ${maven.compile.target} + true + true + + 10000 + + Index: taglib/pom.xml =================================================================== --- taglib/pom.xml (revision 1469923) +++ taglib/pom.xml (working copy) @@ -39,6 +39,7 @@ ${basedir}/.. Log4j Tag Library Documentation /taglib + 3.1.2.RELEASE @@ -46,10 +47,6 @@ log4j-api - org.apache.logging.log4j - log4j-core - - javax.servlet servlet-api 2.5 @@ -64,6 +61,11 @@ org.apache.logging.log4j log4j-core + test + + + org.apache.logging.log4j + log4j-core test-jar test @@ -72,6 +74,18 @@ junit test + + org.springframework + spring-core + ${spring.version} + test + + + org.springframework + spring-test + ${spring.version} + test + @@ -95,7 +109,6 @@ maven-checkstyle-plugin 2.7 - ${log4jParentDir}/checkstyle.xml ${log4jParentDir}/checkstyle-suppressions.xml false @@ -185,7 +198,7 @@ 2.2 - + Index: taglib/src/main/java/org/apache/logging/log4j/taglib/CatchingTag.java =================================================================== --- taglib/src/main/java/org/apache/logging/log4j/taglib/CatchingTag.java (revision 1469923) +++ taglib/src/main/java/org/apache/logging/log4j/taglib/CatchingTag.java (working copy) @@ -27,6 +27,8 @@ * @since 2.0 */ public class CatchingTag extends ExceptionAwareTagSupport { + private static final long serialVersionUID = 1L; + private static final String FQCN = CatchingTag.class.getName(); private Level level; Index: taglib/src/main/java/org/apache/logging/log4j/taglib/DebugTag.java =================================================================== --- taglib/src/main/java/org/apache/logging/log4j/taglib/DebugTag.java (revision 1469923) +++ taglib/src/main/java/org/apache/logging/log4j/taglib/DebugTag.java (working copy) @@ -24,6 +24,8 @@ * @since 2.0 */ public class DebugTag extends LoggingMessageTagSupport { + private static final long serialVersionUID = 1L; + @Override protected Level getLevel() { return Level.DEBUG; Index: taglib/src/main/java/org/apache/logging/log4j/taglib/DumpTag.java =================================================================== --- taglib/src/main/java/org/apache/logging/log4j/taglib/DumpTag.java (revision 1469923) +++ taglib/src/main/java/org/apache/logging/log4j/taglib/DumpTag.java (working copy) @@ -29,6 +29,8 @@ * @since 2.0 */ public class DumpTag extends TagSupport { + private static final long serialVersionUID = 1L; + private int scope; public DumpTag() { @@ -55,7 +57,7 @@ try { Enumeration names = this.pageContext.getAttributeNamesInScope(this.scope); this.pageContext.getOut().write("
"); - while (names.hasMoreElements()) { + while (names != null && names.hasMoreElements()) { String name = names.nextElement(); Object value = this.pageContext.getAttribute(name, this.scope); Index: taglib/src/main/java/org/apache/logging/log4j/taglib/EntryTag.java =================================================================== --- taglib/src/main/java/org/apache/logging/log4j/taglib/EntryTag.java (revision 1469923) +++ taglib/src/main/java/org/apache/logging/log4j/taglib/EntryTag.java (working copy) @@ -30,6 +30,8 @@ * @since 2.0 */ public class EntryTag extends LoggerAwareTagSupport implements DynamicAttributes { + private static final long serialVersionUID = 1L; + private static final String FQCN = EntryTag.class.getName(); private List attributes; Index: taglib/src/main/java/org/apache/logging/log4j/taglib/ErrorTag.java =================================================================== --- taglib/src/main/java/org/apache/logging/log4j/taglib/ErrorTag.java (revision 1469923) +++ taglib/src/main/java/org/apache/logging/log4j/taglib/ErrorTag.java (working copy) @@ -24,6 +24,8 @@ * @since 2.0 */ public class ErrorTag extends LoggingMessageTagSupport { + private static final long serialVersionUID = 1L; + @Override protected Level getLevel() { return Level.ERROR; Index: taglib/src/main/java/org/apache/logging/log4j/taglib/ExceptionAwareTagSupport.java =================================================================== --- taglib/src/main/java/org/apache/logging/log4j/taglib/ExceptionAwareTagSupport.java (revision 1469923) +++ taglib/src/main/java/org/apache/logging/log4j/taglib/ExceptionAwareTagSupport.java (working copy) @@ -22,6 +22,8 @@ * @since 2.0 */ abstract class ExceptionAwareTagSupport extends LoggerAwareTagSupport { + private static final long serialVersionUID = 1L; + private Throwable exception; @Override Index: taglib/src/main/java/org/apache/logging/log4j/taglib/ExitTag.java =================================================================== --- taglib/src/main/java/org/apache/logging/log4j/taglib/ExitTag.java (revision 1469923) +++ taglib/src/main/java/org/apache/logging/log4j/taglib/ExitTag.java (working copy) @@ -27,9 +27,11 @@ * @since 2.0 */ public class ExitTag extends LoggerAwareTagSupport { + private static final long serialVersionUID = 1L; + private static final String FQCN = ExitTag.class.getName(); - private Object result; + private transient Object result; @Override protected void init() { Index: taglib/src/main/java/org/apache/logging/log4j/taglib/FatalTag.java =================================================================== --- taglib/src/main/java/org/apache/logging/log4j/taglib/FatalTag.java (revision 1469923) +++ taglib/src/main/java/org/apache/logging/log4j/taglib/FatalTag.java (working copy) @@ -24,6 +24,8 @@ * @since 2.0 */ public class FatalTag extends LoggingMessageTagSupport { + private static final long serialVersionUID = 1L; + @Override protected Level getLevel() { return Level.FATAL; Index: taglib/src/main/java/org/apache/logging/log4j/taglib/IfEnabledTag.java =================================================================== --- taglib/src/main/java/org/apache/logging/log4j/taglib/IfEnabledTag.java (revision 1469923) +++ taglib/src/main/java/org/apache/logging/log4j/taglib/IfEnabledTag.java (working copy) @@ -28,8 +28,10 @@ * @since 2.0 */ public class IfEnabledTag extends LoggerAwareTagSupport { - private Object level; + private static final long serialVersionUID = 1L; + private transient Object level; + private Marker marker; @Override Index: taglib/src/main/java/org/apache/logging/log4j/taglib/InfoTag.java =================================================================== --- taglib/src/main/java/org/apache/logging/log4j/taglib/InfoTag.java (revision 1469923) +++ taglib/src/main/java/org/apache/logging/log4j/taglib/InfoTag.java (working copy) @@ -24,6 +24,8 @@ * @since 2.0 */ public class InfoTag extends LoggingMessageTagSupport { + private static final long serialVersionUID = 1L; + @Override protected Level getLevel() { return Level.INFO; Index: taglib/src/main/java/org/apache/logging/log4j/taglib/Log4jTaglibLoggerContext.java =================================================================== --- taglib/src/main/java/org/apache/logging/log4j/taglib/Log4jTaglibLoggerContext.java (revision 1469923) +++ taglib/src/main/java/org/apache/logging/log4j/taglib/Log4jTaglibLoggerContext.java (working copy) @@ -24,8 +24,7 @@ import org.apache.logging.log4j.spi.LoggerContext; import javax.servlet.ServletContext; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; +import java.util.WeakHashMap; /** * This bridge between the tag library and the Log4j API ensures that instances of {@link Log4jTaglibLogger} are @@ -34,11 +33,13 @@ * @since 2.0 */ final class Log4jTaglibLoggerContext implements LoggerContext { - private static final ConcurrentMap CONTEXTS = - new ConcurrentHashMap(); + // These were change to WeakHashMaps to avoid ClassLoader (memory) leak, something that's particularly + // important in Servlet containers. + private static final WeakHashMap CONTEXTS = + new WeakHashMap(); - private final ConcurrentMap loggers = - new ConcurrentHashMap(); + private final WeakHashMap loggers = + new WeakHashMap(); private final ServletContext servletContext; @@ -61,15 +62,22 @@ return logger; } - // wrap a logger from an underlying implementation - Logger original = factory == null ? LogManager.getLogger(name) : LogManager.getLogger(name, factory); - if (original instanceof AbstractLogger) { - logger = new Log4jTaglibLogger((AbstractLogger) original, name, original.getMessageFactory()); - Log4jTaglibLogger existing = this.loggers.putIfAbsent(name, logger); - return existing == null ? logger : existing; + synchronized (this.loggers) { + logger = this.loggers.get(name); + if (logger == null) { + Logger original = factory == null ? LogManager.getLogger(name) : LogManager.getLogger(name, factory); + if (!(original instanceof AbstractLogger)) { + throw new LoggingException( + "Log4j Tag Library requires base logging system to extend Log4j AbstractLogger." + ); + } + // wrap a logger from an underlying implementation + logger = new Log4jTaglibLogger((AbstractLogger) original, name, original.getMessageFactory()); + this.loggers.put(name, logger); + } } - throw new LoggingException("Log4j Tag Library requires base logging system to extend Log4j AbstractLogger."); + return logger; } public boolean hasLogger(String name) { @@ -77,12 +85,19 @@ } static synchronized Log4jTaglibLoggerContext getInstance(ServletContext servletContext) { - if (CONTEXTS.containsKey(servletContext)) { - return CONTEXTS.get(servletContext); + Log4jTaglibLoggerContext loggerContext = CONTEXTS.get(servletContext); + if (loggerContext != null) { + return loggerContext; } - Log4jTaglibLoggerContext context = new Log4jTaglibLoggerContext(servletContext); - Log4jTaglibLoggerContext existing = CONTEXTS.putIfAbsent(servletContext, context); - return existing == null ? context : existing; + synchronized (CONTEXTS) { + loggerContext = CONTEXTS.get(servletContext); + if (loggerContext == null) { + loggerContext = new Log4jTaglibLoggerContext(servletContext); + CONTEXTS.put(servletContext, loggerContext); + } + } + + return loggerContext; } } Index: taglib/src/main/java/org/apache/logging/log4j/taglib/LogTag.java =================================================================== --- taglib/src/main/java/org/apache/logging/log4j/taglib/LogTag.java (revision 1469923) +++ taglib/src/main/java/org/apache/logging/log4j/taglib/LogTag.java (working copy) @@ -24,6 +24,8 @@ * @since 2.0 */ public class LogTag extends LoggingMessageTagSupport { + private static final long serialVersionUID = 1L; + private Level level; @Override Index: taglib/src/main/java/org/apache/logging/log4j/taglib/LoggerAwareTagSupport.java =================================================================== --- taglib/src/main/java/org/apache/logging/log4j/taglib/LoggerAwareTagSupport.java (revision 1469923) +++ taglib/src/main/java/org/apache/logging/log4j/taglib/LoggerAwareTagSupport.java (working copy) @@ -26,9 +26,12 @@ * @since 2.0 */ abstract class LoggerAwareTagSupport extends BodyTagSupport { - private Log4jTaglibLoggerContext loggerContext; - private Object logger; + private static final long serialVersionUID = 1L; + private transient Log4jTaglibLoggerContext loggerContext; + + private transient Object logger; + protected LoggerAwareTagSupport() { this.init(); } @@ -44,7 +47,7 @@ } @Override - public void setPageContext(PageContext pageContext) { + public final void setPageContext(PageContext pageContext) { super.setPageContext(pageContext); this.loggerContext = Log4jTaglibLoggerContext.getInstance(pageContext.getServletContext()); } Index: taglib/src/main/java/org/apache/logging/log4j/taglib/LoggingMessageTagSupport.java =================================================================== --- taglib/src/main/java/org/apache/logging/log4j/taglib/LoggingMessageTagSupport.java (revision 1469923) +++ taglib/src/main/java/org/apache/logging/log4j/taglib/LoggingMessageTagSupport.java (working copy) @@ -33,9 +33,11 @@ * @since 2.0 */ abstract class LoggingMessageTagSupport extends ExceptionAwareTagSupport implements DynamicAttributes { + private static final long serialVersionUID = 1L; + private static final String FQCN = LoggingMessageTagSupport.class.getName(); - private Object message; + private transient Object message; private Marker marker; Index: taglib/src/main/java/org/apache/logging/log4j/taglib/SetLoggerTag.java =================================================================== --- taglib/src/main/java/org/apache/logging/log4j/taglib/SetLoggerTag.java (revision 1469923) +++ taglib/src/main/java/org/apache/logging/log4j/taglib/SetLoggerTag.java (working copy) @@ -29,12 +29,14 @@ * @since 2.0 */ public class SetLoggerTag extends BodyTagSupport { - private Log4jTaglibLoggerContext loggerContext; + private static final long serialVersionUID = 1L; - private Object logger; + private transient Log4jTaglibLoggerContext loggerContext; - private MessageFactory factory; + private transient Object logger; + private transient MessageFactory factory; + private String var; private int scope; Index: taglib/src/main/java/org/apache/logging/log4j/taglib/TraceTag.java =================================================================== --- taglib/src/main/java/org/apache/logging/log4j/taglib/TraceTag.java (revision 1469923) +++ taglib/src/main/java/org/apache/logging/log4j/taglib/TraceTag.java (working copy) @@ -24,6 +24,8 @@ * @since 2.0 */ public class TraceTag extends LoggingMessageTagSupport { + private static final long serialVersionUID = 1L; + @Override protected Level getLevel() { return Level.TRACE; Index: taglib/src/main/java/org/apache/logging/log4j/taglib/WarnTag.java =================================================================== --- taglib/src/main/java/org/apache/logging/log4j/taglib/WarnTag.java (revision 1469923) +++ taglib/src/main/java/org/apache/logging/log4j/taglib/WarnTag.java (working copy) @@ -24,6 +24,8 @@ * @since 2.0 */ public class WarnTag extends LoggingMessageTagSupport { + private static final long serialVersionUID = 1L; + @Override protected Level getLevel() { return Level.WARN; Index: taglib/src/main/java/org/apache/logging/log4j/taglib/package-info.java =================================================================== --- taglib/src/main/java/org/apache/logging/log4j/taglib/package-info.java (revision 1469923) +++ taglib/src/main/java/org/apache/logging/log4j/taglib/package-info.java (working copy) @@ -19,4 +19,4 @@ + + @since 2.0 */ -package org.apache.logging.log4j.taglib; \ No newline at end of file +package org.apache.logging.log4j.taglib; Index: taglib/src/test/java/org/apache/logging/log4j/taglib/CatchingTagTest.java =================================================================== --- taglib/src/test/java/org/apache/logging/log4j/taglib/CatchingTagTest.java (revision 0) +++ taglib/src/test/java/org/apache/logging/log4j/taglib/CatchingTagTest.java (working copy) @@ -0,0 +1,115 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.logging.log4j.taglib; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.core.Appender; +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.config.XMLConfigurationFactory; +import org.apache.logging.log4j.status.StatusLogger; +import org.apache.logging.log4j.test.appender.ListAppender; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.springframework.mock.web.MockPageContext; + +import javax.servlet.jsp.tagext.Tag; +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +/** + * + */ +public class CatchingTagTest { + private static final String CONFIG = "log4j-test1.xml"; + + @BeforeClass + public static void setUpClass() { + System.setProperty(XMLConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG); + final LoggerContext context = (LoggerContext) LogManager.getContext(false); + context.getConfiguration(); + } + + @AfterClass + public static void cleanUpClass() { + System.clearProperty(XMLConfigurationFactory.CONFIGURATION_FILE_PROPERTY); + final LoggerContext context = (LoggerContext) LogManager.getContext(false); + context.reconfigure(); + StatusLogger.getLogger().reset(); + } + + private Logger logger = LogManager.getLogger("LoggingMessageTagSupportTestLogger"); + private CatchingTag tag; + + @Before + public void setUp() { + this.tag = new CatchingTag(); + this.tag.setPageContext(new MockPageContext()); + this.tag.setLogger(this.logger); + } + + @Test + public void testDoEndTag() throws Exception { + this.tag.setException(new Exception("This is a test.")); + + assertEquals("The return value is not correct.", Tag.EVAL_PAGE, this.tag.doEndTag()); + verify("catching ERROR M-CATCHING[ EXCEPTION ] E java.lang.Exception: This is a test."); + } + + @Test + public void testDoEndTagLevelString() throws Exception { + this.tag.setLevel("info"); + this.tag.setException(new RuntimeException("This is another test.")); + + assertEquals("The return value is not correct.", Tag.EVAL_PAGE, this.tag.doEndTag()); + verify("catching INFO M-CATCHING[ EXCEPTION ] E java.lang.RuntimeException: This is another test."); + } + + @Test + public void testDoEndTagLevelObject() throws Exception { + this.tag.setLevel(Level.WARN); + this.tag.setException(new Error("This is the last test.")); + + assertEquals("The return value is not correct.", Tag.EVAL_PAGE, this.tag.doEndTag()); + verify("catching WARN M-CATCHING[ EXCEPTION ] E java.lang.Error: This is the last test."); + } + + private void verify(final String expected) { + final LoggerContext ctx = (LoggerContext) LogManager.getContext(false); + final Map> list = ctx.getConfiguration().getAppenders(); + final Appender listApp = list.get("List"); + assertNotNull("Missing Appender", listApp); + assertTrue("Not a ListAppender", listApp instanceof ListAppender); + final List events = ((ListAppender) listApp).getMessages(); + try + { + assertEquals("Incorrect number of messages.", 1, events.size()); + assertEquals("Incorrect message.", "o.a.l.l.t.CatchingTagTest " + expected, events.get(0)); + } + finally + { + ((ListAppender) listApp).clear(); + } + } +} Index: taglib/src/test/java/org/apache/logging/log4j/taglib/DebugTagTest.java =================================================================== --- taglib/src/test/java/org/apache/logging/log4j/taglib/DebugTagTest.java (revision 0) +++ taglib/src/test/java/org/apache/logging/log4j/taglib/DebugTagTest.java (working copy) @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.logging.log4j.taglib; + +import org.apache.logging.log4j.Level; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * + */ +public class DebugTagTest { + @Test + public void testGetLevel() { + assertEquals("The logging level is not correct.", Level.DEBUG, new DebugTag().getLevel()); + } +} Index: taglib/src/test/java/org/apache/logging/log4j/taglib/DumpTagTest.java =================================================================== --- taglib/src/test/java/org/apache/logging/log4j/taglib/DumpTagTest.java (revision 0) +++ taglib/src/test/java/org/apache/logging/log4j/taglib/DumpTagTest.java (working copy) @@ -0,0 +1,120 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.logging.log4j.taglib; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.mock.web.MockJspWriter; +import org.springframework.mock.web.MockPageContext; + +import javax.servlet.jsp.JspWriter; +import javax.servlet.jsp.PageContext; +import javax.servlet.jsp.tagext.Tag; +import java.io.ByteArrayOutputStream; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.nio.charset.Charset; + +import static org.junit.Assert.assertEquals; + +/** + * + */ +public class DumpTagTest { + private static final Charset UTF8 = Charset.forName("UTF-8"); + private Writer writer; + private ByteArrayOutputStream output; + private MockPageContext context; + private DumpTag tag; + + @Before + public void setUp() { + this.output = new ByteArrayOutputStream(); + this.writer = new OutputStreamWriter(this.output, UTF8); + + this.context = new MockPageContext() { + private MockJspWriter jspWriter = new MockJspWriter(writer); + @Override + public JspWriter getOut() { + return this.jspWriter; + } + }; + + this.tag = new DumpTag(); + this.tag.setPageContext(this.context); + } + + @Test + public void testDoEndTagDefaultPageScopeNoAttributes() throws Exception { + int returnValue = this.tag.doEndTag(); + assertEquals("The return value is not correct.", Tag.EVAL_PAGE, returnValue); + + this.writer.flush(); + String output = new String(this.output.toByteArray(), UTF8); + assertEquals("The output is not correct.", "
", output); + } + + @Test + public void testDoEndTagDefaultPageScope() throws Exception { + this.context.setAttribute("testAttribute01", "testValue01", PageContext.PAGE_SCOPE); + this.context.setAttribute("anotherAttribute02", "finalValue02", PageContext.PAGE_SCOPE); + this.context.setAttribute("badAttribute03", "skippedValue03", PageContext.SESSION_SCOPE); + + int returnValue = this.tag.doEndTag(); + assertEquals("The return value is not correct.", Tag.EVAL_PAGE, returnValue); + + this.writer.flush(); + String output = new String(this.output.toByteArray(), UTF8); + assertEquals("The output is not correct.", + "
" + + "
testAttribute01
testValue01
" + + "
anotherAttribute02
finalValue02
" + + "
", output); + } + + @Test + public void testDoEndTagSessionScopeNoAttributes() throws Exception { + this.context.setAttribute("badAttribute01", "skippedValue01", PageContext.PAGE_SCOPE); + + this.tag.setScope("session"); + int returnValue = this.tag.doEndTag(); + assertEquals("The return value is not correct.", Tag.EVAL_PAGE, returnValue); + + this.writer.flush(); + String output = new String(this.output.toByteArray(), UTF8); + assertEquals("The output is not correct.", "
", output); + } + + @Test + public void testDoEndTagSessionScope() throws Exception { + this.context.setAttribute("otherAttribute03", "lostValue03", PageContext.PAGE_SCOPE); + this.context.setAttribute("coolAttribute01", "weirdValue01", PageContext.SESSION_SCOPE); + this.context.setAttribute("testAttribute02", "testValue02", PageContext.SESSION_SCOPE); + + this.tag.setScope("session"); + int returnValue = this.tag.doEndTag(); + assertEquals("The return value is not correct.", Tag.EVAL_PAGE, returnValue); + + this.writer.flush(); + String output = new String(this.output.toByteArray(), UTF8); + assertEquals("The output is not correct.", + "
" + + "
coolAttribute01
weirdValue01
" + + "
testAttribute02
testValue02
" + + "
", output); + } +} Index: taglib/src/test/java/org/apache/logging/log4j/taglib/EntryTagTest.java =================================================================== --- taglib/src/test/java/org/apache/logging/log4j/taglib/EntryTagTest.java (revision 0) +++ taglib/src/test/java/org/apache/logging/log4j/taglib/EntryTagTest.java (working copy) @@ -0,0 +1,103 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.logging.log4j.taglib; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.core.Appender; +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.config.XMLConfigurationFactory; +import org.apache.logging.log4j.status.StatusLogger; +import org.apache.logging.log4j.test.appender.ListAppender; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.springframework.mock.web.MockPageContext; + +import javax.servlet.jsp.tagext.Tag; +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +/** + * + */ +public class EntryTagTest { + private static final String CONFIG = "log4j-test1.xml"; + + @BeforeClass + public static void setUpClass() { + System.setProperty(XMLConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG); + final LoggerContext context = (LoggerContext) LogManager.getContext(false); + context.getConfiguration(); + } + + @AfterClass + public static void cleanUpClass() { + System.clearProperty(XMLConfigurationFactory.CONFIGURATION_FILE_PROPERTY); + final LoggerContext context = (LoggerContext) LogManager.getContext(false); + context.reconfigure(); + StatusLogger.getLogger().reset(); + } + + private Logger logger = LogManager.getLogger("LoggingMessageTagSupportTestLogger"); + private EntryTag tag; + + @Before + public void setUp() { + this.tag = new EntryTag(); + this.tag.setPageContext(new MockPageContext()); + this.tag.setLogger(this.logger); + } + + @Test + public void testDoEndTag() throws Exception { + assertEquals("The return value is not correct.", Tag.EVAL_PAGE, this.tag.doEndTag()); + verify("entry TRACE M-ENTRY[ FLOW ] E"); + } + + @Test + public void testDoEndTagAttributes() throws Exception { + this.tag.setDynamicAttribute(null, null, CONFIG); + this.tag.setDynamicAttribute(null, null, 5792); + + assertEquals("The return value is not correct.", Tag.EVAL_PAGE, this.tag.doEndTag()); + verify("entry params(log4j-test1.xml, 5792) TRACE M-ENTRY[ FLOW ] E"); + } + + private void verify(final String expected) { + final LoggerContext ctx = (LoggerContext) LogManager.getContext(false); + final Map> list = ctx.getConfiguration().getAppenders(); + final Appender listApp = list.get("List"); + assertNotNull("Missing Appender", listApp); + assertTrue("Not a ListAppender", listApp instanceof ListAppender); + final List events = ((ListAppender) listApp).getMessages(); + try + { + assertEquals("Incorrect number of messages.", 1, events.size()); + assertEquals("Incorrect message.", "o.a.l.l.t.EntryTagTest " + expected, events.get(0)); + } + finally + { + ((ListAppender) listApp).clear(); + } + } +} Index: taglib/src/test/java/org/apache/logging/log4j/taglib/ErrorTagTest.java =================================================================== --- taglib/src/test/java/org/apache/logging/log4j/taglib/ErrorTagTest.java (revision 0) +++ taglib/src/test/java/org/apache/logging/log4j/taglib/ErrorTagTest.java (working copy) @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.logging.log4j.taglib; + +import org.apache.logging.log4j.Level; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * + */ +public class ErrorTagTest { + @Test + public void testGetLevel() { + assertEquals("The logging level is not correct.", Level.ERROR, new ErrorTag().getLevel()); + } +} Index: taglib/src/test/java/org/apache/logging/log4j/taglib/ExceptionAwareTagSupportTest.java =================================================================== --- taglib/src/test/java/org/apache/logging/log4j/taglib/ExceptionAwareTagSupportTest.java (revision 0) +++ taglib/src/test/java/org/apache/logging/log4j/taglib/ExceptionAwareTagSupportTest.java (working copy) @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.logging.log4j.taglib; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; + +/** + * + */ +public class ExceptionAwareTagSupportTest { + private ExceptionAwareTagSupport tag; + + @Before + public void setUp() { + this.tag = new ExceptionAwareTagSupport() { + private static final long serialVersionUID = 1L; + }; + } + + @Test + public void testException() { + assertNull("The exception should be null (1).", this.tag.getException()); + + Exception e = new Exception(); + this.tag.setException(e); + assertSame("The exception is not correct (1).", e, this.tag.getException()); + + this.tag.init(); + assertNull("The exception should be null (2).", this.tag.getException()); + + e = new RuntimeException(); + this.tag.setException(e); + assertSame("The exception is not correct (2).", e, this.tag.getException()); + } +} Index: taglib/src/test/java/org/apache/logging/log4j/taglib/ExitTagTest.java =================================================================== --- taglib/src/test/java/org/apache/logging/log4j/taglib/ExitTagTest.java (revision 0) +++ taglib/src/test/java/org/apache/logging/log4j/taglib/ExitTagTest.java (working copy) @@ -0,0 +1,110 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.logging.log4j.taglib; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.core.Appender; +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.config.XMLConfigurationFactory; +import org.apache.logging.log4j.status.StatusLogger; +import org.apache.logging.log4j.test.appender.ListAppender; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.springframework.mock.web.MockPageContext; + +import javax.servlet.jsp.tagext.Tag; +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +/** + * + */ +public class ExitTagTest { + private static final String CONFIG = "log4j-test1.xml"; + + @BeforeClass + public static void setUpClass() { + System.setProperty(XMLConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG); + final LoggerContext context = (LoggerContext) LogManager.getContext(false); + context.getConfiguration(); + } + + @AfterClass + public static void cleanUpClass() { + System.clearProperty(XMLConfigurationFactory.CONFIGURATION_FILE_PROPERTY); + final LoggerContext context = (LoggerContext) LogManager.getContext(false); + context.reconfigure(); + StatusLogger.getLogger().reset(); + } + + private Logger logger = LogManager.getLogger("LoggingMessageTagSupportTestLogger"); + private ExitTag tag; + + @Before + public void setUp() { + this.tag = new ExitTag(); + this.tag.setPageContext(new MockPageContext()); + this.tag.setLogger(this.logger); + } + + @Test + public void testDoEndTag() throws Exception { + assertEquals("The return value is not correct.", Tag.EVAL_PAGE, this.tag.doEndTag()); + verify("exit TRACE M-EXIT[ FLOW ] E"); + } + + @Test + public void testDoEndTagResult01() throws Exception { + this.tag.setResult(CONFIG); + + assertEquals("The return value is not correct.", Tag.EVAL_PAGE, this.tag.doEndTag()); + verify("exit with(log4j-test1.xml) TRACE M-EXIT[ FLOW ] E"); + } + + @Test + public void testDoEndTagResult02() throws Exception { + this.tag.setResult(5792); + + assertEquals("The return value is not correct.", Tag.EVAL_PAGE, this.tag.doEndTag()); + verify("exit with(5792) TRACE M-EXIT[ FLOW ] E"); + } + + private void verify(final String expected) { + final LoggerContext ctx = (LoggerContext) LogManager.getContext(false); + final Map> list = ctx.getConfiguration().getAppenders(); + final Appender listApp = list.get("List"); + assertNotNull("Missing Appender", listApp); + assertTrue("Not a ListAppender", listApp instanceof ListAppender); + final List events = ((ListAppender) listApp).getMessages(); + try + { + assertEquals("Incorrect number of messages.", 1, events.size()); + assertEquals("Incorrect message.", "o.a.l.l.t.ExitTagTest " + expected, events.get(0)); + } + finally + { + ((ListAppender) listApp).clear(); + } + } +} Index: taglib/src/test/java/org/apache/logging/log4j/taglib/FatalTagTest.java =================================================================== --- taglib/src/test/java/org/apache/logging/log4j/taglib/FatalTagTest.java (revision 0) +++ taglib/src/test/java/org/apache/logging/log4j/taglib/FatalTagTest.java (working copy) @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.logging.log4j.taglib; + +import org.apache.logging.log4j.Level; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * + */ +public class FatalTagTest { + @Test + public void testGetLevel() { + assertEquals("The logging level is not correct.", Level.FATAL, new FatalTag().getLevel()); + } +} Index: taglib/src/test/java/org/apache/logging/log4j/taglib/IfEnabledTagTest.java =================================================================== --- taglib/src/test/java/org/apache/logging/log4j/taglib/IfEnabledTagTest.java (revision 0) +++ taglib/src/test/java/org/apache/logging/log4j/taglib/IfEnabledTagTest.java (working copy) @@ -0,0 +1,126 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.logging.log4j.taglib; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.MarkerManager; +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.config.XMLConfigurationFactory; +import org.apache.logging.log4j.status.StatusLogger; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.springframework.mock.web.MockPageContext; + +import javax.servlet.jsp.tagext.Tag; + +import static org.junit.Assert.assertEquals; + +/** + * + */ +public class IfEnabledTagTest { + private static final String CONFIG = "log4j-test1.xml"; + + @BeforeClass + public static void setUpClass() { + System.setProperty(XMLConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG); + final LoggerContext context = (LoggerContext) LogManager.getContext(false); + context.getConfiguration(); + } + + @AfterClass + public static void cleanUpClass() { + System.clearProperty(XMLConfigurationFactory.CONFIGURATION_FILE_PROPERTY); + final LoggerContext context = (LoggerContext) LogManager.getContext(false); + context.reconfigure(); + StatusLogger.getLogger().reset(); + } + + private Logger logger = LogManager.getLogger("IfEnabledTagTest"); + private IfEnabledTag tag; + + @Before + public void setUp() { + this.tag = new IfEnabledTag(); + this.tag.setPageContext(new MockPageContext()); + this.tag.setLogger(this.logger); + } + + @Test + public void testDoStartTagEnabledString() throws Exception { + this.tag.setLevel("warn"); + + assertEquals("The return value is not correct.", Tag.EVAL_BODY_INCLUDE, this.tag.doStartTag()); + } + + @Test + public void testDoStartTagEnabledLevel() throws Exception { + this.tag.setLevel(Level.WARN); + + assertEquals("The return value is not correct.", Tag.EVAL_BODY_INCLUDE, this.tag.doStartTag()); + } + + @Test + public void testDoStartTagEnabledStringMarker() throws Exception { + this.tag.setMarker(MarkerManager.getMarker("E01")); + this.tag.setLevel("error"); + + assertEquals("The return value is not correct.", Tag.EVAL_BODY_INCLUDE, this.tag.doStartTag()); + } + + @Test + public void testDoStartTagEnabledLevelMarker() throws Exception { + this.tag.setMarker(MarkerManager.getMarker("F02")); + this.tag.setLevel(Level.ERROR); + + assertEquals("The return value is not correct.", Tag.EVAL_BODY_INCLUDE, this.tag.doStartTag()); + } + + @Test + public void testDoStartTagDisabledString() throws Exception { + this.tag.setLevel("info"); + + assertEquals("The return value is not correct.", Tag.SKIP_BODY, this.tag.doStartTag()); + } + + @Test + public void testDoStartTagDisabledLevel() throws Exception { + this.tag.setLevel(Level.INFO); + + assertEquals("The return value is not correct.", Tag.SKIP_BODY, this.tag.doStartTag()); + } + + @Test + public void testDoStartTagDisabledStringMarker() throws Exception { + this.tag.setMarker(MarkerManager.getMarker("E01")); + this.tag.setLevel("trace"); + + assertEquals("The return value is not correct.", Tag.SKIP_BODY, this.tag.doStartTag()); + } + + @Test + public void testDoStartTagDisabledLevelMarker() throws Exception { + this.tag.setMarker(MarkerManager.getMarker("F02")); + this.tag.setLevel(Level.TRACE); + + assertEquals("The return value is not correct.", Tag.SKIP_BODY, this.tag.doStartTag()); + } +} Index: taglib/src/test/java/org/apache/logging/log4j/taglib/InfoTagTest.java =================================================================== --- taglib/src/test/java/org/apache/logging/log4j/taglib/InfoTagTest.java (revision 0) +++ taglib/src/test/java/org/apache/logging/log4j/taglib/InfoTagTest.java (working copy) @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.logging.log4j.taglib; + +import org.apache.logging.log4j.Level; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * + */ +public class InfoTagTest { + @Test + public void testGetLevel() { + assertEquals("The logging level is not correct.", Level.INFO, new InfoTag().getLevel()); + } +} Index: taglib/src/test/java/org/apache/logging/log4j/taglib/LogTagTest.java =================================================================== --- taglib/src/test/java/org/apache/logging/log4j/taglib/LogTagTest.java (revision 0) +++ taglib/src/test/java/org/apache/logging/log4j/taglib/LogTagTest.java (working copy) @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.logging.log4j.taglib; + +import org.apache.logging.log4j.Level; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + + +/** + * + */ +public class LogTagTest { + private LogTag tag; + + @Before + public void setUp() { + this.tag = new LogTag(); + } + + @Test + public void testLevel() { + assertNull("The level should be null (1).", this.tag.getLevel()); + + this.tag.setLevel(Level.INFO); + assertEquals("The level is not correct (1).", Level.INFO, this.tag.getLevel()); + + this.tag.init(); + assertNull("The level should be null (2).", this.tag.getLevel()); + + this.tag.setLevel("WARN"); + assertEquals("The level is not correct (2).", Level.WARN, this.tag.getLevel()); + } +} Index: taglib/src/test/java/org/apache/logging/log4j/taglib/LoggerAwareTagSupportTest.java =================================================================== --- taglib/src/test/java/org/apache/logging/log4j/taglib/LoggerAwareTagSupportTest.java (revision 0) +++ taglib/src/test/java/org/apache/logging/log4j/taglib/LoggerAwareTagSupportTest.java (working copy) @@ -0,0 +1,150 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.logging.log4j.taglib; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.spi.AbstractLogger; +import org.junit.Test; +import org.springframework.mock.web.MockPageContext; + +import static org.junit.Assert.*; + +/** + * + */ +public class LoggerAwareTagSupportTest { + private MockPageContext context; + private LoggerAwareTagSupport tag; + + private void setUp(final Object page) { + this.context = new MockPageContext() { + @Override + public Object getPage() { + return page == null ? this : page; + } + }; + this.tag = new LoggerAwareTagSupport() { + private static final long serialVersionUID = 1L; + }; + this.tag.setPageContext(this.context); + } + + @Test + public void testGetLoggerWithGenericLogger() throws Exception { + this.setUp(null); + + Logger logger = LogManager.getLogger("testGetLoggerWithGenericLogger"); + + this.tag.setLogger(logger); + Log4jTaglibLogger returned = this.tag.getLogger(); + + assertNotNull("The first returned logger should not be null.", returned); + assertNotSame("The first returned logger should not be the same as the set.", logger, returned); + assertEquals("The name is not correct.", "testGetLoggerWithGenericLogger", returned.getName()); + + Log4jTaglibLogger returned2 = this.tag.getLogger(); + + assertNotNull("The second returned logger should not be null.", returned2); + assertSame("The second returned logger should be the same as the first.", returned, returned2); + + this.tag.release(); + + Log4jTaglibLogger returned3 = this.tag.getLogger(); + + assertNotNull("The third returned logger should not be null.", returned3); + assertNotSame("The third returned logger should not be the same as the first.", returned, returned3); + } + + @Test + public void testGetLoggerWithTaglibLogger() throws Exception { + this.setUp(null); + + AbstractLogger wrapped = (AbstractLogger)LogManager.getLogger("testGetLoggerWithTaglibLogger"); + Log4jTaglibLogger logger = new Log4jTaglibLogger(wrapped, wrapped.getName(), wrapped.getMessageFactory()); + + this.tag.setLogger(logger); + Log4jTaglibLogger returned = this.tag.getLogger(); + + assertNotNull("The first returned logger should not be null.", returned); + assertSame("The first returned logger should be the same as the set.", logger, returned); + assertEquals("The name is not correct.", "testGetLoggerWithTaglibLogger", returned.getName()); + + returned = this.tag.getLogger(); + + assertNotNull("The second returned logger should not be null.", returned); + assertSame("The second returned logger should be the same as the set.", logger, returned); + } + + @Test + public void testGetLoggerWithStringLogger() throws Exception { + this.setUp(null); + + this.tag.setLogger("testGetLoggerWithStringLogger"); + Log4jTaglibLogger returned = this.tag.getLogger(); + + assertNotNull("The first returned logger should not be null.", returned); + assertEquals("The name is not correct.", "testGetLoggerWithStringLogger", returned.getName()); + + Log4jTaglibLogger returned2 = this.tag.getLogger(); + + assertNotNull("The second returned logger should not be null.", returned2); + assertSame("The second returned logger should be the same as the first.", returned, returned2); + } + + @Test + public void testGetDefaultLogger01() throws Exception { + Object page = new Object() {}; + this.setUp(page); + + assertNull("The default logger should be null.", TagUtils.getDefaultLogger(this.context)); + + Log4jTaglibLogger returned = this.tag.getLogger(); + assertNotNull("The first returned logger should not be null.", returned); + assertEquals("The logger name is not correct.", page.getClass().getName(), returned.getName()); + + Log4jTaglibLogger defaultLogger = TagUtils.getDefaultLogger(this.context); + assertNotNull("The default logger should not be null anymore.", defaultLogger); + assertSame("The default logger should be the same as the returned logger.", returned, defaultLogger); + + Log4jTaglibLogger returned2 = this.tag.getLogger(); + + assertNotNull("The second returned logger should not be null.", returned2); + assertSame("The second returned logger should be the same as the first.", returned, returned2); + } + + @Test + public void testGetDefaultLogger02() throws Exception { + Object page = new Object() {}; + this.setUp(page); + + assertNull("The default logger should be null.", TagUtils.getDefaultLogger(this.context)); + + Log4jTaglibLogger returned = this.tag.getLogger(); + assertNotNull("The first returned logger should not be null.", returned); + assertEquals("The logger name is not correct.", page.getClass().getName(), returned.getName()); + + Log4jTaglibLogger defaultLogger = TagUtils.getDefaultLogger(this.context); + assertNotNull("The default logger should not be null anymore.", defaultLogger); + assertSame("The default logger should be the same as the returned logger.", returned, defaultLogger); + + Log4jTaglibLogger returned2 = this.tag.getLogger(); + + assertNotNull("The second returned logger should not be null.", returned2); + assertSame("The second returned logger should be the same as the first.", returned, returned2); + } +} Index: taglib/src/test/java/org/apache/logging/log4j/taglib/LoggingMessageTagSupportTest.java =================================================================== --- taglib/src/test/java/org/apache/logging/log4j/taglib/LoggingMessageTagSupportTest.java (revision 0) +++ taglib/src/test/java/org/apache/logging/log4j/taglib/LoggingMessageTagSupportTest.java (working copy) @@ -0,0 +1,326 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.logging.log4j.taglib; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.MarkerManager; +import org.apache.logging.log4j.core.Appender; +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.config.XMLConfigurationFactory; +import org.apache.logging.log4j.status.StatusLogger; +import org.apache.logging.log4j.test.appender.ListAppender; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.springframework.mock.web.MockBodyContent; +import org.springframework.mock.web.MockPageContext; + +import javax.servlet.jsp.tagext.BodyTag; +import java.io.Writer; +import java.util.List; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +import static org.junit.Assert.*; + +/** + * + */ +public class LoggingMessageTagSupportTest { + private static final String CONFIG = "log4j-test1.xml"; + + @BeforeClass + public static void setUpClass() { + System.setProperty(XMLConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG); + final LoggerContext context = (LoggerContext) LogManager.getContext(false); + context.getConfiguration(); + } + + @AfterClass + public static void cleanUpClass() { + System.clearProperty(XMLConfigurationFactory.CONFIGURATION_FILE_PROPERTY); + final LoggerContext context = (LoggerContext) LogManager.getContext(false); + context.reconfigure(); + StatusLogger.getLogger().reset(); + } + + private Logger logger = LogManager.getLogger("LoggingMessageTagSupportTestLogger"); + private LoggingMessageTagSupport tag; + + private void setUp(final Level level) { + this.tag = new LoggingMessageTagSupport() { + private static final long serialVersionUID = 1L; + + @Override + protected Level getLevel() { + return level; + } + }; + this.tag.setPageContext(new MockPageContext()); + this.tag.setLogger(this.logger); + } + + @Test + public void testDoStartTag() { + this.setUp(null); + + assertEquals("The return value is not correct.", BodyTag.EVAL_BODY_BUFFERED, this.tag.doStartTag()); + } + + @Test + public void testMessageString() throws Exception { + this.setUp(null); + + this.tag.setMessage("This is my message 01."); + + assertEquals("The message is not correct.", "This is my message 01.", this.tag.getMessage()); + } + + @Test + public void testMessageObject() throws Exception { + this.setUp(null); + + Object message = new Object(); + this.tag.setMessage(message); + + assertSame("The message is not correct.", message, this.tag.getMessage()); + } + + @Test + public void testMessageBody() throws Exception { + this.setUp(null); + + MockBodyContent content = new MockBodyContent("This is the body content 01.", (Writer)null); + this.tag.setBodyContent(content); + + assertEquals("The message is not correct.", "This is the body content 01.", this.tag.getMessage()); + } + + @Test + public void testMessageStringBodyIgnored() throws Exception { + this.setUp(null); + + MockBodyContent content = new MockBodyContent("This is more body content 02.", (Writer)null); + this.tag.setBodyContent(content); + this.tag.setMessage("This is another message 02."); + + assertEquals("The message is not correct.", "This is another message 02.", this.tag.getMessage()); + } + + @Test + public void testDoEndTagStringMessageNoMarkerNoException() throws Exception { + this.setUp(Level.WARN); + + this.tag.setMessage("Hello message for testDoEndTagStringMessageNoMarkerNoException"); + + assertEquals("The return value is not correct.", BodyTag.EVAL_PAGE, this.tag.doEndTag()); + verify("Hello message for testDoEndTagStringMessageNoMarkerNoException WARN M- E"); + } + + @Test + public void testDoEndTagStringMessageMarkerNoException() throws Exception { + this.setUp(Level.INFO); + + this.tag.setMarker(MarkerManager.getMarker("E01")); + this.tag.setMessage("Goodbye message for testDoEndTagStringMessageMarkerNoException"); + + assertEquals("The return value is not correct.", BodyTag.EVAL_PAGE, this.tag.doEndTag()); + verify("Goodbye message for testDoEndTagStringMessageMarkerNoException INFO M-E01 E"); + } + + @Test + public void testDoEndTagStringMessageNoMarkerException() throws Exception { + this.setUp(Level.ERROR); + + this.tag.setException(new Exception("This is a test")); + this.tag.setMessage("Another message for testDoEndTagStringMessageNoMarkerException"); + + assertEquals("The return value is not correct.", BodyTag.EVAL_PAGE, this.tag.doEndTag()); + verify("Another message for testDoEndTagStringMessageNoMarkerException ERROR M- E java.lang.Exception: This is a test"); + } + + @Test + public void testDoEndTagStringMessageMarkerException() throws Exception { + this.setUp(Level.TRACE); + + this.tag.setException(new RuntimeException("This is another test")); + this.tag.setMarker(MarkerManager.getMarker("F02")); + this.tag.setMessage("Final message for testDoEndTagStringMessageMarkerException"); + + assertEquals("The return value is not correct.", BodyTag.EVAL_PAGE, this.tag.doEndTag()); + verify("Final message for testDoEndTagStringMessageMarkerException TRACE M-F02 E java.lang.RuntimeException: This is another test"); + } + + @Test + public void testDoEndTagStringWithParameters() throws Exception { + this.setUp(Level.FATAL); + + this.tag.setDynamicAttribute(null, null, "A"); + this.tag.setDynamicAttribute(null, null, TimeUnit.HOURS); + this.tag.setMessage("Test message with [{}] parameter of [{}]"); + + assertEquals("The return value is not correct.", BodyTag.EVAL_PAGE, this.tag.doEndTag()); + verify("Test message with [A] parameter of [HOURS] FATAL M- E"); + + } + + @Test + public void testDoEndTagStringWithParametersMarkerAndException() throws Exception { + this.setUp(Level.DEBUG); + + this.tag.setException(new Error("This is the last test")); + this.tag.setMarker(MarkerManager.getMarker("N03")); + this.tag.setDynamicAttribute(null, null, "Z"); + this.tag.setDynamicAttribute(null, null, TimeUnit.SECONDS); + this.tag.setMessage("Final message with [{}] parameter of [{}]"); + + assertEquals("The return value is not correct.", BodyTag.EVAL_PAGE, this.tag.doEndTag()); + verify("Final message with [Z] parameter of [SECONDS] DEBUG M-N03 E java.lang.Error: This is the last test"); + + } + + @Test + public void testDoEndTagMessageNoMarkerNoException() throws Exception { + this.setUp(Level.INFO); + + this.tag.setMessage( + logger.getMessageFactory().newMessage("First message for testDoEndTagMessageNoMarkerNoException") + ); + + assertEquals("The return value is not correct.", BodyTag.EVAL_PAGE, this.tag.doEndTag()); + verify("First message for testDoEndTagMessageNoMarkerNoException INFO M- E"); + } + + @Test + public void testDoEndTagMessageMarkerNoException() throws Exception { + this.setUp(Level.WARN); + + this.tag.setMarker(MarkerManager.getMarker("E01")); + this.tag.setMessage( + logger.getMessageFactory().newMessage("Another message for testDoEndTagMessageMarkerNoException") + ); + + assertEquals("The return value is not correct.", BodyTag.EVAL_PAGE, this.tag.doEndTag()); + verify("Another message for testDoEndTagMessageMarkerNoException WARN M-E01 E"); + } + + @Test + public void testDoEndTagMessageNoMarkerException() throws Exception { + this.setUp(Level.TRACE); + + this.tag.setException(new Exception("This is a test")); + this.tag.setMessage( + logger.getMessageFactory().newMessage("Third message for testDoEndTagMessageNoMarkerException") + ); + + assertEquals("The return value is not correct.", BodyTag.EVAL_PAGE, this.tag.doEndTag()); + verify("Third message for testDoEndTagMessageNoMarkerException TRACE M- E java.lang.Exception: This is a test"); + } + + @Test + public void testDoEndTagMessageMarkerException() throws Exception { + this.setUp(Level.ERROR); + + this.tag.setException(new RuntimeException("This is another test")); + this.tag.setMarker(MarkerManager.getMarker("F02")); + this.tag.setMessage( + logger.getMessageFactory().newMessage("Final message for testDoEndTagMessageMarkerException") + ); + + assertEquals("The return value is not correct.", BodyTag.EVAL_PAGE, this.tag.doEndTag()); + verify("Final message for testDoEndTagMessageMarkerException ERROR M-F02 E java.lang.RuntimeException: " + + "This is another test"); + } + + @Test + public void testDoEndTagObjectNoMarkerNoException() throws Exception { + this.setUp(Level.INFO); + + this.tag.setMessage(new MyMessage("First message for testDoEndTagObjectNoMarkerNoException")); + + assertEquals("The return value is not correct.", BodyTag.EVAL_PAGE, this.tag.doEndTag()); + verify("First message for testDoEndTagObjectNoMarkerNoException INFO M- E"); + } + + @Test + public void testDoEndTagObjectMarkerNoException() throws Exception { + this.setUp(Level.WARN); + + this.tag.setMarker(MarkerManager.getMarker("E01")); + this.tag.setMessage(new MyMessage("Another message for testDoEndTagObjectMarkerNoException")); + + assertEquals("The return value is not correct.", BodyTag.EVAL_PAGE, this.tag.doEndTag()); + verify("Another message for testDoEndTagObjectMarkerNoException WARN M-E01 E"); + } + + @Test + public void testDoEndTagObjectNoMarkerException() throws Exception { + this.setUp(Level.TRACE); + + this.tag.setException(new Exception("This is a test")); + this.tag.setMessage(new MyMessage("Third message for testDoEndTagObjectNoMarkerException")); + + assertEquals("The return value is not correct.", BodyTag.EVAL_PAGE, this.tag.doEndTag()); + verify("Third message for testDoEndTagObjectNoMarkerException TRACE M- E java.lang.Exception: This is a test"); + } + + @Test + public void testDoEndTagObjectMarkerException() throws Exception { + this.setUp(Level.ERROR); + + this.tag.setException(new RuntimeException("This is another test")); + this.tag.setMarker(MarkerManager.getMarker("F02")); + this.tag.setMessage(new MyMessage("Final message for testDoEndTagObjectMarkerException")); + + assertEquals("The return value is not correct.", BodyTag.EVAL_PAGE, this.tag.doEndTag()); + verify("Final message for testDoEndTagObjectMarkerException ERROR M-F02 E java.lang.RuntimeException: " + + "This is another test"); + } + + private void verify(final String expected) { + final LoggerContext ctx = (LoggerContext) LogManager.getContext(false); + final Map> list = ctx.getConfiguration().getAppenders(); + final Appender listApp = list.get("List"); + assertNotNull("Missing Appender", listApp); + assertTrue("Not a ListAppender", listApp instanceof ListAppender); + final List events = ((ListAppender) listApp).getMessages(); + try + { + assertEquals("Incorrect number of messages.", 1, events.size()); + assertEquals("Incorrect message.", "o.a.l.l.t.LoggingMessageTagSupportTest " + expected, events.get(0)); + } + finally + { + ((ListAppender) listApp).clear(); + } + } + + private static class MyMessage { + private final String internal; + + public MyMessage(String internal) { + this.internal = internal; + } + + @Override + public String toString() { + return this.internal; + } + } +} Index: taglib/src/test/java/org/apache/logging/log4j/taglib/SetLoggerTagTest.java =================================================================== --- taglib/src/test/java/org/apache/logging/log4j/taglib/SetLoggerTagTest.java (revision 0) +++ taglib/src/test/java/org/apache/logging/log4j/taglib/SetLoggerTagTest.java (working copy) @@ -0,0 +1,205 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.logging.log4j.taglib; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.message.MessageFactory; +import org.apache.logging.log4j.message.StringFormatterMessageFactory; +import org.junit.Before; +import org.junit.Test; +import org.springframework.mock.web.MockPageContext; + +import javax.servlet.jsp.PageContext; +import javax.servlet.jsp.tagext.Tag; + +import static org.junit.Assert.*; + +/** + * + */ +public class SetLoggerTagTest { + private MockPageContext context; + private SetLoggerTag tag; + + @Before + public void setUp() { + this.context = new MockPageContext(); + this.tag = new SetLoggerTag(); + this.tag.setPageContext(this.context); + } + + @Test + public void testDoEndTagLoggerVarPageScope() throws Exception { + this.tag.setLogger(LogManager.getLogger("testDoEndTagLoggerVarPageScope")); + + this.tag.setVar("helloWorld"); + + assertNull("The default logger should be null.", TagUtils.getDefaultLogger(this.context)); + assertEquals("The return value is not correct.", Tag.EVAL_PAGE, this.tag.doEndTag()); + assertNull("The default logger should still be null.", TagUtils.getDefaultLogger(this.context)); + + Object attribute = this.context.getAttribute("helloWorld", PageContext.PAGE_SCOPE); + assertNotNull("The attribute should not be null.", attribute); + assertTrue("The attribute should be a Log4jTaglibLogger.", attribute instanceof Log4jTaglibLogger); + + Log4jTaglibLogger logger = (Log4jTaglibLogger)attribute; + assertEquals("The logger name is not correct.", "testDoEndTagLoggerVarPageScope", logger.getName()); + } + + @Test + public void testDoEndTagStringVarPageScope() throws Exception { + this.tag.setLogger("testDoEndTagStringVarPageScope"); + + this.tag.setVar("goodbyeCruelWorld"); + + assertNull("The default logger should be null.", TagUtils.getDefaultLogger(this.context)); + assertEquals("The return value is not correct.", Tag.EVAL_PAGE, this.tag.doEndTag()); + assertNull("The default logger should still be null.", TagUtils.getDefaultLogger(this.context)); + + Object attribute = this.context.getAttribute("goodbyeCruelWorld", PageContext.PAGE_SCOPE); + assertNotNull("The attribute should not be null.", attribute); + assertTrue("The attribute should be a Log4jTaglibLogger.", attribute instanceof Log4jTaglibLogger); + + Log4jTaglibLogger logger = (Log4jTaglibLogger)attribute; + assertEquals("The logger name is not correct.", "testDoEndTagStringVarPageScope", logger.getName()); + } + + @Test + public void testDoEndTagStringFactoryVarPageScope() throws Exception { + this.tag.setLogger("testDoEndTagStringFactoryVarPageScope"); + + MessageFactory factory = new StringFormatterMessageFactory(); + + this.tag.setFactory(factory); + this.tag.setVar("goodbyeCruelWorld"); + + assertNull("The default logger should be null.", TagUtils.getDefaultLogger(this.context)); + assertEquals("The return value is not correct.", Tag.EVAL_PAGE, this.tag.doEndTag()); + assertNull("The default logger should still be null.", TagUtils.getDefaultLogger(this.context)); + + Object attribute = this.context.getAttribute("goodbyeCruelWorld", PageContext.PAGE_SCOPE); + assertNotNull("The attribute should not be null.", attribute); + assertTrue("The attribute should be a Log4jTaglibLogger.", attribute instanceof Log4jTaglibLogger); + + Log4jTaglibLogger logger = (Log4jTaglibLogger)attribute; + assertEquals("The logger name is not correct.", "testDoEndTagStringFactoryVarPageScope", logger.getName()); + assertSame("The message factory is not correct.", factory, logger.getMessageFactory()); + } + + @Test + public void testDoEndTagLoggerVarSessionScope() throws Exception { + this.tag.setLogger(LogManager.getLogger("testDoEndTagLoggerVarSessionScope")); + + this.tag.setVar("helloWorld"); + this.tag.setScope("session"); + + assertNull("The default logger should be null.", TagUtils.getDefaultLogger(this.context)); + assertEquals("The return value is not correct.", Tag.EVAL_PAGE, this.tag.doEndTag()); + assertNull("The default logger should still be null.", TagUtils.getDefaultLogger(this.context)); + + Object attribute = this.context.getAttribute("helloWorld", PageContext.SESSION_SCOPE); + assertNotNull("The attribute should not be null.", attribute); + assertTrue("The attribute should be a Log4jTaglibLogger.", attribute instanceof Log4jTaglibLogger); + + Log4jTaglibLogger logger = (Log4jTaglibLogger)attribute; + assertEquals("The logger name is not correct.", "testDoEndTagLoggerVarSessionScope", logger.getName()); + } + + @Test + public void testDoEndTagStringVarRequestScope() throws Exception { + this.tag.setLogger("testDoEndTagStringVarRequestScope"); + + this.tag.setVar("goodbyeCruelWorld"); + this.tag.setScope("request"); + + assertNull("The default logger should be null.", TagUtils.getDefaultLogger(this.context)); + assertEquals("The return value is not correct.", Tag.EVAL_PAGE, this.tag.doEndTag()); + assertNull("The default logger should still be null.", TagUtils.getDefaultLogger(this.context)); + + Object attribute = this.context.getAttribute("goodbyeCruelWorld", PageContext.REQUEST_SCOPE); + assertNotNull("The attribute should not be null.", attribute); + assertTrue("The attribute should be a Log4jTaglibLogger.", attribute instanceof Log4jTaglibLogger); + + Log4jTaglibLogger logger = (Log4jTaglibLogger)attribute; + assertEquals("The logger name is not correct.", "testDoEndTagStringVarRequestScope", logger.getName()); + } + + @Test + public void testDoEndTagStringFactoryVarApplicationScope() throws Exception { + this.tag.setLogger("testDoEndTagStringFactoryVarApplicationScope"); + + MessageFactory factory = new StringFormatterMessageFactory(); + + this.tag.setFactory(factory); + this.tag.setVar("goodbyeCruelWorld"); + this.tag.setScope("application"); + + assertNull("The default logger should be null.", TagUtils.getDefaultLogger(this.context)); + assertEquals("The return value is not correct.", Tag.EVAL_PAGE, this.tag.doEndTag()); + assertNull("The default logger should still be null.", TagUtils.getDefaultLogger(this.context)); + + Object attribute = this.context.getAttribute("goodbyeCruelWorld", PageContext.APPLICATION_SCOPE); + assertNotNull("The attribute should not be null.", attribute); + assertTrue("The attribute should be a Log4jTaglibLogger.", attribute instanceof Log4jTaglibLogger); + + Log4jTaglibLogger logger = (Log4jTaglibLogger)attribute; + assertEquals("The logger name is not correct.", "testDoEndTagStringFactoryVarApplicationScope", + logger.getName()); + assertSame("The message factory is not correct.", factory, logger.getMessageFactory()); + } + + @Test + public void testDoEndTagLoggerDefault() throws Exception { + this.tag.setLogger(LogManager.getLogger("testDoEndTagLoggerDefault")); + + assertNull("The default logger should be null.", TagUtils.getDefaultLogger(this.context)); + assertEquals("The return value is not correct.", Tag.EVAL_PAGE, this.tag.doEndTag()); + + Log4jTaglibLogger logger = TagUtils.getDefaultLogger(this.context); + assertNotNull("The default logger should not be null anymore.", logger); + assertEquals("The logger name is not correct.", "testDoEndTagLoggerDefault", logger.getName()); + } + + @Test + public void testDoEndTagStringDefault() throws Exception { + this.tag.setLogger("testDoEndTagStringDefault"); + + assertNull("The default logger should be null.", TagUtils.getDefaultLogger(this.context)); + assertEquals("The return value is not correct.", Tag.EVAL_PAGE, this.tag.doEndTag()); + + Log4jTaglibLogger logger = TagUtils.getDefaultLogger(this.context); + assertNotNull("The default logger should not be null anymore.", logger); + assertEquals("The logger name is not correct.", "testDoEndTagStringDefault", logger.getName()); + } + + @Test + public void testDoEndTagStringFactoryDefault() throws Exception { + this.tag.setLogger("testDoEndTagStringFactoryDefault"); + + MessageFactory factory = new StringFormatterMessageFactory(); + + this.tag.setFactory(factory); + + assertNull("The default logger should be null.", TagUtils.getDefaultLogger(this.context)); + assertEquals("The return value is not correct.", Tag.EVAL_PAGE, this.tag.doEndTag()); + + Log4jTaglibLogger logger = TagUtils.getDefaultLogger(this.context); + assertNotNull("The default logger should not be null anymore.", logger); + assertEquals("The logger name is not correct.", "testDoEndTagStringFactoryDefault", logger.getName()); + assertSame("The message factory is not correct.", factory, logger.getMessageFactory()); + } +} Index: taglib/src/test/java/org/apache/logging/log4j/taglib/TagUtilsTest.java =================================================================== --- taglib/src/test/java/org/apache/logging/log4j/taglib/TagUtilsTest.java (revision 0) +++ taglib/src/test/java/org/apache/logging/log4j/taglib/TagUtilsTest.java (working copy) @@ -0,0 +1,114 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.logging.log4j.taglib; + +import org.apache.logging.log4j.Level; +import org.junit.Test; + +import javax.servlet.jsp.PageContext; + +import static org.junit.Assert.assertEquals; + +/** + * + */ +public class TagUtilsTest { + @Test + public void testGetScopeSession() { + assertEquals("The scope is not correct.", PageContext.SESSION_SCOPE, TagUtils.getScope("session")); + } + + @Test + public void testGetScopeRequest() { + assertEquals("The scope is not correct.", PageContext.REQUEST_SCOPE, TagUtils.getScope("request")); + } + + @Test + public void testGetScopeApplication() { + assertEquals("The scope is not correct.", PageContext.APPLICATION_SCOPE, TagUtils.getScope("application")); + } + + @Test + public void testGetScopePage() { + assertEquals("The scope is not correct.", PageContext.PAGE_SCOPE, TagUtils.getScope("page")); + } + + @Test + public void testGetScopeUnknown() { + assertEquals("The scope is not correct.", PageContext.PAGE_SCOPE, TagUtils.getScope("oeu1209")); + } + + @Test + public void testResolveLevelTrace01() { + assertEquals("The value is not correct.", Level.TRACE, TagUtils.resolveLevel("trace")); + } + + @Test + public void testResolveLevelTrace02() { + assertEquals("The value is not correct.", Level.TRACE, TagUtils.resolveLevel(Level.TRACE)); + } + + @Test + public void testResolveLevelDebug01() { + assertEquals("The value is not correct.", Level.DEBUG, TagUtils.resolveLevel("debug")); + } + + @Test + public void testResolveLevelDebug02() { + assertEquals("The value is not correct.", Level.DEBUG, TagUtils.resolveLevel(Level.DEBUG)); + } + + @Test + public void testResolveLevelInfo01() { + assertEquals("The value is not correct.", Level.INFO, TagUtils.resolveLevel("info")); + } + + @Test + public void testResolveLevelInfo02() { + assertEquals("The value is not correct.", Level.INFO, TagUtils.resolveLevel(Level.INFO)); + } + + @Test + public void testResolveLevelWarn01() { + assertEquals("The value is not correct.", Level.WARN, TagUtils.resolveLevel("warn")); + } + + @Test + public void testResolveLevelWarn02() { + assertEquals("The value is not correct.", Level.WARN, TagUtils.resolveLevel(Level.WARN)); + } + + @Test + public void testResolveLevelError01() { + assertEquals("The value is not correct.", Level.ERROR, TagUtils.resolveLevel("error")); + } + + @Test + public void testResolveLevelError02() { + assertEquals("The value is not correct.", Level.ERROR, TagUtils.resolveLevel(Level.ERROR)); + } + + @Test + public void testResolveLevelFatal01() { + assertEquals("The value is not correct.", Level.FATAL, TagUtils.resolveLevel("fatal")); + } + + @Test + public void testResolveLevelFatal02() { + assertEquals("The value is not correct.", Level.FATAL, TagUtils.resolveLevel(Level.FATAL)); + } +} Index: taglib/src/test/java/org/apache/logging/log4j/taglib/TraceTagTest.java =================================================================== --- taglib/src/test/java/org/apache/logging/log4j/taglib/TraceTagTest.java (revision 0) +++ taglib/src/test/java/org/apache/logging/log4j/taglib/TraceTagTest.java (working copy) @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.logging.log4j.taglib; + +import org.apache.logging.log4j.Level; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * + */ +public class TraceTagTest { + @Test + public void testGetLevel() { + assertEquals("The logging level is not correct.", Level.TRACE, new TraceTag().getLevel()); + } +} Index: taglib/src/test/java/org/apache/logging/log4j/taglib/WarnTagTest.java =================================================================== --- taglib/src/test/java/org/apache/logging/log4j/taglib/WarnTagTest.java (revision 0) +++ taglib/src/test/java/org/apache/logging/log4j/taglib/WarnTagTest.java (working copy) @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.logging.log4j.taglib; + +import org.apache.logging.log4j.Level; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * + */ +public class WarnTagTest { + @Test + public void testGetLevel() { + assertEquals("The logging level is not correct.", Level.WARN, new WarnTag().getLevel()); + } +} Index: taglib/src/test/resources/log4j-test1.xml =================================================================== --- taglib/src/test/resources/log4j-test1.xml (revision 0) +++ taglib/src/test/resources/log4j-test1.xml (working copy) @@ -0,0 +1,41 @@ + + + + target/test.log + + + + + + + + + + + + + %d %p %C{1.} [%t] %m%n + + + + + + + + + + + > + + + + > + + + + + + + + + \ No newline at end of file