Added: logging/log4j/log4j2/trunk/taglib/src/test/java/org/apache/logging/log4j/taglib/SetLoggerTagTest.java URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/taglib/src/test/java/org/apache/logging/log4j/taglib/SetLoggerTagTest.java?rev=1470034&view=auto ============================================================================== --- logging/log4j/log4j2/trunk/taglib/src/test/java/org/apache/logging/log4j/taglib/SetLoggerTagTest.java (added) +++ logging/log4j/log4j2/trunk/taglib/src/test/java/org/apache/logging/log4j/taglib/SetLoggerTagTest.java Fri Apr 19 20:46:14 2013 @@ -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()); + } +}
Added: logging/log4j/log4j2/trunk/taglib/src/test/java/org/apache/logging/log4j/taglib/TagUtilsTest.java URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/taglib/src/test/java/org/apache/logging/log4j/taglib/TagUtilsTest.java?rev=1470034&view=auto ============================================================================== --- logging/log4j/log4j2/trunk/taglib/src/test/java/org/apache/logging/log4j/taglib/TagUtilsTest.java (added) +++ logging/log4j/log4j2/trunk/taglib/src/test/java/org/apache/logging/log4j/taglib/TagUtilsTest.java Fri Apr 19 20:46:14 2013 @@ -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)); + } +} Added: logging/log4j/log4j2/trunk/taglib/src/test/java/org/apache/logging/log4j/taglib/TraceTagTest.java URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/taglib/src/test/java/org/apache/logging/log4j/taglib/TraceTagTest.java?rev=1470034&view=auto ============================================================================== --- logging/log4j/log4j2/trunk/taglib/src/test/java/org/apache/logging/log4j/taglib/TraceTagTest.java (added) +++ logging/log4j/log4j2/trunk/taglib/src/test/java/org/apache/logging/log4j/taglib/TraceTagTest.java Fri Apr 19 20:46:14 2013 @@ -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()); + } +} Added: logging/log4j/log4j2/trunk/taglib/src/test/java/org/apache/logging/log4j/taglib/WarnTagTest.java URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/taglib/src/test/java/org/apache/logging/log4j/taglib/WarnTagTest.java?rev=1470034&view=auto ============================================================================== --- logging/log4j/log4j2/trunk/taglib/src/test/java/org/apache/logging/log4j/taglib/WarnTagTest.java (added) +++ logging/log4j/log4j2/trunk/taglib/src/test/java/org/apache/logging/log4j/taglib/WarnTagTest.java Fri Apr 19 20:46:14 2013 @@ -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()); + } +} Added: logging/log4j/log4j2/trunk/taglib/src/test/resources/log4j-test1.xml URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/taglib/src/test/resources/log4j-test1.xml?rev=1470034&view=auto ============================================================================== --- logging/log4j/log4j2/trunk/taglib/src/test/resources/log4j-test1.xml (added) +++ logging/log4j/log4j2/trunk/taglib/src/test/resources/log4j-test1.xml Fri Apr 19 20:46:14 2013 @@ -0,0 +1,41 @@ +<?xml version="1.0" encoding="UTF-8"?> +<configuration packages="org.apache.logging.log4j.test" status="warn" name="TaglibLoggerTest"> + <properties> + <property name="filename">target/test.log</property> + </properties> + <ThresholdFilter level="trace"/> + + <appenders> + <List name="EventLogger"> + <PatternLayout pattern="%C{1.} %m%n"/> + </List> + <Console name="STDOUT"> + <PatternLayout pattern="%C{1.} %m MDC%X%n"/> + </Console> + <File name="File" fileName="${filename}"> + <PatternLayout> + <pattern>%d %p %C{1.} [%t] %m%n</pattern> + </PatternLayout> + </File> + <List name="List"> + <PatternLayout pattern="%C{1.} %m %level M-%marker E%ex{1}"/> + </List> + </appenders> + + <loggers> + <logger name="EventLogger" level="info" additivity="false"> + <appender-ref ref="EventLogger"/> + </logger>> + + <logger name="org.apache.logging.log4j.test2" level="debug" additivity="false"> + <appender-ref ref="File"/> + </logger>> + + <root level="trace"> + <appender-ref ref="List"/> + </root> + + <logger name="IfEnabledTagTest" level="warn" /> + </loggers> + +</configuration> \ No newline at end of file
