LOG4J2-1296 added tests for ReusableSimpleMessage
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/defe7149 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/defe7149 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/defe7149 Branch: refs/heads/master Commit: defe7149b0d3e23b83458894920ed7409db44960 Parents: 7a7f35b Author: rpopma <[email protected]> Authored: Tue Mar 8 21:00:59 2016 +1100 Committer: rpopma <[email protected]> Committed: Tue Mar 8 21:00:59 2016 +1100 ---------------------------------------------------------------------- .../message/ReusableSimpleMessageTest.java | 122 +++++++++++++++++++ 1 file changed, 122 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/defe7149/log4j-api/src/test/java/org/apache/logging/log4j/message/ReusableSimpleMessageTest.java ---------------------------------------------------------------------- diff --git a/log4j-api/src/test/java/org/apache/logging/log4j/message/ReusableSimpleMessageTest.java b/log4j-api/src/test/java/org/apache/logging/log4j/message/ReusableSimpleMessageTest.java new file mode 100644 index 0000000..47e468a --- /dev/null +++ b/log4j-api/src/test/java/org/apache/logging/log4j/message/ReusableSimpleMessageTest.java @@ -0,0 +1,122 @@ +/* + * 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.message; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Tests ReusableSimpleMessage. + */ +public class ReusableSimpleMessageTest { + + @Test + public void testSet_InitializesFormattedMessage() throws Exception { + ReusableSimpleMessage msg = new ReusableSimpleMessage(); + msg.set("abc"); + assertEquals("abc", msg.getFormattedMessage()); + } + + @Test + public void testGetFormattedMessage_InitiallyEmpty() throws Exception { + assertNull(new ReusableSimpleMessage().getFormattedMessage()); + } + + @Test + public void testGetFormattedMessage_ReturnsLatestSetString() throws Exception { + ReusableSimpleMessage msg = new ReusableSimpleMessage(); + msg.set("abc"); + assertEquals("abc", msg.getFormattedMessage()); + msg.set("def"); + assertEquals("def", msg.getFormattedMessage()); + msg.set("xyz"); + assertEquals("xyz", msg.getFormattedMessage()); + } + + @Test + public void testGetFormat_InitiallyEmpty() throws Exception { + assertNull(new ReusableSimpleMessage().getFormat()); + } + + @Test + public void testGetFormat_ReturnsLatestSetString() throws Exception { + ReusableSimpleMessage msg = new ReusableSimpleMessage(); + msg.set("abc"); + assertEquals("abc", msg.getFormat()); + msg.set("def"); + assertEquals("def", msg.getFormat()); + msg.set("xyz"); + assertEquals("xyz", msg.getFormat()); + } + + @Test + public void testGetParameters_InitiallyReturnsEmptyArray() throws Exception { + assertArrayEquals(new Object[0], new ReusableSimpleMessage().getParameters()); + } + + @Test + public void testGetParameters_ReturnsEmptyArrayAfterMessageSet() throws Exception { + ReusableSimpleMessage msg = new ReusableSimpleMessage(); + msg.set("abc"); + assertArrayEquals(new Object[0], msg.getParameters()); + msg.set("def"); + assertArrayEquals(new Object[0], msg.getParameters()); + } + + @Test + public void testGetThrowable_InitiallyReturnsNull() throws Exception { + assertNull(new ReusableSimpleMessage().getThrowable()); + } + + @Test + public void testGetThrowable_ReturnsNullAfterMessageSet() throws Exception { + ReusableSimpleMessage msg = new ReusableSimpleMessage(); + msg.set("abc"); + assertNull(msg.getThrowable()); + msg.set("def"); + assertNull(msg.getThrowable()); + } + + @Test + public void testFormatTo_InitiallyWritesNull() throws Exception { + ReusableSimpleMessage msg = new ReusableSimpleMessage(); + StringBuilder sb = new StringBuilder(); + msg.formatTo(sb); + assertEquals("null", sb.toString()); + } + + @Test + public void testFormatTo_WritesLatestSetString() throws Exception { + ReusableSimpleMessage msg = new ReusableSimpleMessage(); + StringBuilder sb = new StringBuilder(); + msg.formatTo(sb); + assertEquals("null", sb.toString()); + sb.setLength(0); + msg.set("abc"); + msg.formatTo(sb); + assertEquals("abc", sb.toString()); + sb.setLength(0); + msg.set("def"); + msg.formatTo(sb); + assertEquals("def", sb.toString()); + sb.setLength(0); + msg.set("xyz"); + msg.formatTo(sb); + assertEquals("xyz", sb.toString()); + } +} \ No newline at end of file
